Skip to content

Commit

Permalink
Fix filters args + Add tests for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
yves-chevallier committed Nov 8, 2022
1 parent f85f855 commit e453275
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions baygon/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing
from collections import namedtuple
from pathlib import Path

from .error import InvalidExecutableError

Outputs = namedtuple('Outputs', ['exit_status', 'stdout', 'stderr'])
Expand Down
15 changes: 10 additions & 5 deletions baygon/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
from abc import ABC, abstractmethod
from collections.abc import Sequence

from .error import InvalidFilterError


Expand All @@ -15,11 +16,11 @@ def filter(self, value: str) -> str:
""" Return True if the value matches the filter. """
return value

def __init__(self, name: str = None):
self.name = name
def __init__(self, *args, **kwargs):
...

def __repr__(self):
return f'{self.__class__.__name__}<{self.name}>'
return f'{self.__class__.__name__}'

def __call__(self, value: str) -> str:
return self.filter(value)
Expand Down Expand Up @@ -133,8 +134,12 @@ def _parse_filter(self, filters):
if isinstance(filters, Filters):
return filters._filters
if isinstance(filters, dict):
return [filter_map[name](args)
for name, args in filters.items()]
instances = []
for name, args in filters.items():
if not isinstance(args, list):
args = [args]
instances.append(filter_map[name](*args))
return instances

raise InvalidFilterError(f'Invalid type for filters: {type(filters)}')

Expand Down
3 changes: 2 additions & 1 deletion baygon/suite.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
""" Test suite. """
import json
from pathlib import Path
from .error import InvalidExecutableError, ConfigError

import yaml

from . import error
from .error import ConfigError, InvalidExecutableError
from .executable import Executable
from .filters import Filters
from .id import Id
Expand Down
2 changes: 1 addition & 1 deletion tests/executable/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from unittest import TestCase

from baygon import Executable
from baygon.str import GreppableString
from baygon.error import InvalidExecutableError
from baygon.str import GreppableString

dir_path = Path(__file__).resolve(strict=True).parent

Expand Down
51 changes: 51 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" Test filters. """
from unittest import TestCase

from baygon.filters import (
FilterIgnoreSpaces,
FilterLowercase,
FilterNone,
FilterRegex,
FilterReplace,
Filters,
FilterTrim,
FilterUppercase,
)


class TestFilters(TestCase):
def test_filter_uppercase(self):
f = FilterUppercase()
self.assertEqual(f('hello'), 'HELLO')

def test_filter_lowercase(self):
f = FilterLowercase()
self.assertEqual(f('HELLO'), 'hello')

def test_filter_trim(self):
f = FilterTrim()
self.assertEqual(f(' hello '), 'hello')

def test_filter_none(self):
f = FilterNone()
self.assertEqual(f(' hello '), ' hello ')

def test_filter_replace(self):
f = FilterReplace('hello', 'bye')
self.assertEqual(f('hello world'), 'bye world')

def test_filter_regex(self):
f = FilterRegex(r'h[el]+o', 'bye')
self.assertEqual(f('hello world'), 'bye world')

def test_filter_ignore_spaces(self):
f = FilterIgnoreSpaces()
self.assertEqual(f('hello world'), 'helloworld')

def test_filter_multiple(self):
f = Filters({
'ignorespaces': True,
'uppercase': True,
'replace': ['L', 'Z']
})
self.assertEqual(f('hello world'), 'HEZZOWORZD')

0 comments on commit e453275

Please sign in to comment.