From 07cf6ac097e977189844e1812ab14302c93e7ff9 Mon Sep 17 00:00:00 2001 From: Eddy Pronk Date: Mon, 20 Oct 2008 12:22:00 +1100 Subject: [PATCH] Added fit.ActionFixture and fixed the loader. Added ListAdapter. --- pyfit/ColumnFixture.py | 2 +- pyfit/engines.py | 7 +++++ pyfit/fit/ActionFixture.py | 3 ++ pyfit/fit/__init__.py | 0 pyfit/test_engines.py | 8 ++++- pyfit/test_table.py | 4 ++- pyfit/test_util.py | 48 +++++++++++------------------- pyfit/util.py | 74 ++++++++++++++++++++++++++++------------------ 8 files changed, 84 insertions(+), 62 deletions(-) create mode 100644 pyfit/fit/ActionFixture.py create mode 100644 pyfit/fit/__init__.py diff --git a/pyfit/ColumnFixture.py b/pyfit/ColumnFixture.py index e22b20b..d569400 100644 --- a/pyfit/ColumnFixture.py +++ b/pyfit/ColumnFixture.py @@ -10,4 +10,4 @@ class ColumnFixture(object): for row in table.rows[2:]: for (d, cell) in zip(desc, row): - d.apply(self, cell) + d.apply(self, cell, self.adapters) diff --git a/pyfit/engines.py b/pyfit/engines.py index a282601..1ad4dcc 100644 --- a/pyfit/engines.py +++ b/pyfit/engines.py @@ -8,6 +8,9 @@ class DefaultLoader(object): except ImportError, inst: a = traceback.format_exc() print '{\n%s}' % inst.value + names = name.split('.')[1:] + for name in names: + module = getattr(module, name) class_ = getattr(module, name) return class_() @@ -30,6 +33,7 @@ class Engine(object): self.loader = DefaultLoader() self.fixture = None self.print_traceback = False + self.adapters = DefaultAdapters() def load_fixture(self, name): if self.fixture is None: @@ -44,6 +48,8 @@ class Engine(object): if self.fixture is None: raise Exception("fixture '%s' not found." % name) + self.fixture.adapters = self.adapters + def do_process(self, table): name = table.name() self.load_fixture(name) @@ -59,6 +65,7 @@ class Engine(object): self.do_process(table) except Exception, inst: '''Fixme: Should the rest of the table become grey?''' + table.cell(0,0).error(inst) if self.print_traceback: diff --git a/pyfit/fit/ActionFixture.py b/pyfit/fit/ActionFixture.py new file mode 100644 index 0000000..c7b14ba --- /dev/null +++ b/pyfit/fit/ActionFixture.py @@ -0,0 +1,3 @@ +class ActionFixture(object): + def process(self, table): + pass diff --git a/pyfit/fit/__init__.py b/pyfit/fit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyfit/test_engines.py b/pyfit/test_engines.py index 579a5ed..14a498a 100644 --- a/pyfit/test_engines.py +++ b/pyfit/test_engines.py @@ -4,7 +4,7 @@ from plaintypes import * class TestEngines(unittest.TestCase): - def test_action_fixture(self): + def test_loading_non_existing_fixture(self): engine = Engine() engine.loader = StringLoader('import doesntexist\n') try: @@ -14,6 +14,12 @@ class TestEngines(unittest.TestCase): self.assertEqual(str(inst), 'No module named doesntexist') + def test_action_fixture(self): + engine = Engine() + engine.load_fixture('RowFixture') + engine.load_fixture('ColumnFixture') + engine.load_fixture('fit.ActionFixture') + def test_input_table(self): engine = Engine() diff --git a/pyfit/test_table.py b/pyfit/test_table.py index 6fc61eb..d9499a7 100644 --- a/pyfit/test_table.py +++ b/pyfit/test_table.py @@ -141,7 +141,9 @@ class TestTable(unittest.TestCase): name = table.rows[0][0] def CreateFixture(name): - return globals()[name]() + fixture = globals()[name]() + fixture.adapters = DefaultAdapters() + return fixture fixture = CreateFixture(str(name)) fixture.process(table) diff --git a/pyfit/test_util.py b/pyfit/test_util.py index bd39527..358a547 100644 --- a/pyfit/test_util.py +++ b/pyfit/test_util.py @@ -3,6 +3,9 @@ from util import * from plaintypes import * class TestUtil(unittest.TestCase): + def setUp(self): + self.adapters = DefaultAdapters() + def test_if_apply_throws_when_function_returns_None(self): class FakeFixture(object): @@ -13,12 +16,12 @@ class TestUtil(unittest.TestCase): fixture = FakeFixture() cell = Cell('dummy') try: - operation.apply(fixture, cell) + operation.apply(fixture, cell, self.adapters) except Exception, inst: pass self.assertEqual(str(inst), 'returned None') - def test_2(self): + def test_bool_adapter_1(self): class FakeFixture(object): def func(self): @@ -27,43 +30,26 @@ class TestUtil(unittest.TestCase): operation = MethodCall('func') fixture = FakeFixture() cell = Cell('false') - operation.apply(fixture, cell) - self.assertEqual(cell.has_passed, True) - - def test_3(self): - - def parse_bool(s): - return s is 'true' - - self.assertEqual(True, parse_bool('true')) - self.assertEqual(False, parse_bool('false')) - def bool_to_string(s): - return str(s).lower() - - self.assertEqual('true', bool_to_string(True)) - self.assertEqual('false', bool_to_string(False)) + operation.apply(fixture, cell, self.adapters) + self.assertEqual(cell.has_passed, True) - def test_4(self): + def test_bool_adapter_2(self): class FakeFixture(object): reliable = False operation = SetAttribute('reliable') fixture = FakeFixture() cell = Cell('false') - operation.apply(fixture, cell) + operation.apply(fixture, cell, self.adapters) self.assertEqual(fixture.reliable, False) - class Bool(object): - def parse(self, s): - if s == 'true': - value = True - else: - if s == 'false': - value = False - else: - raise Exception("Can't convert `%s`" % s) - return value + def test_list_adapter(self): + class FakeFixture(object): + phones = [] - converter = Bool() - converter.parse('false') + operation = SetAttribute('phones') + fixture = FakeFixture() + cell = Cell('(209)373 7453, (209)373 7454') + operation.apply(fixture, cell, self.adapters) + self.assertEqual(fixture.phones, ['(209)373 7453', '(209)373 7454']) diff --git a/pyfit/util.py b/pyfit/util.py index 65c6187..bf65e9e 100644 --- a/pyfit/util.py +++ b/pyfit/util.py @@ -3,29 +3,43 @@ import sys import traceback import string +class BoolAdapter(object): + def parse(self, s): + if s == 'true': + value = True + else: + if s == 'false': + value = False + else: + raise Exception("Can't convert `%s`" % s) + return value + + def convert(self, s): + return s == 'true' + +class ListAdapter(object): + def parse(self, s): + return [x.lstrip() for x in s.split(',')] + class MethodCall(object): def __init__(self, name): self.name = name - def apply(self, fixture, cell): + + def apply(self, fixture, cell, adapters): f = getattr(fixture, self.name) actual = f() if actual is None: raise Exception('returned None') value = str(cell) + target_type = type(actual) + if adapters.has_key(target_type): + adapter = adapters[target_type] + expected = adapter.convert(value) + else: + expected = type(actual)(str(cell)) - def parse_bool(s): - return s == 'true' - - if type(actual) is bool: - if parse_bool(value) == actual: - cell.passed() - return - else: - cell.failed(actual) - return - - if type(actual)(str(cell)) == actual: + if expected == actual: cell.passed() else: cell.failed(actual) @@ -33,25 +47,20 @@ class MethodCall(object): class SetAttribute(object): def __init__(self, name): self.name = name - def apply(self, fixture, cell): + def apply(self, fixture, cell, adapters): - def string_to_bool(s): - if s == 'true': - value = True - else: - if s == 'false': - value = False - return value try: old = getattr(fixture, self.name) - new = str(cell) - if type(old) is bool: - setattr(fixture, self.name, string_to_bool(new)) + target_type = type(old) + cell_value = str(cell) + if adapters.has_key(target_type): + adapter = adapters[target_type] + new = adapter.parse(cell_value) else: - setattr(fixture, self.name, type(old)(str(cell))) + new = type(old)(str(cell)) + setattr(fixture, self.name, new) except AttributeError, inst: - #print e cell.error(str(inst)) def parse_action(action_desc): @@ -125,7 +134,8 @@ class ImportError(Exception): class Importer(object): def do_import_module(self, module_name): - __import__(module_name) + pass + #__import__(module_name) def import_module(self, name): try: @@ -145,9 +155,17 @@ class CreateFixture(object): type = self.globals[name] except KeyError, inst: raise Exception("Could not create fixture '%s'" % name) - return type() + fixture = type() + fixture.adapters = {} + return fixture def add_to_python_path(path): paths = path.split(':')[:-3] # remove classes:fitnesse.jar:fitlibrary.jar' sys.path.extend(paths) +def DefaultAdapters(): + adapters = {} + adapters[bool] = BoolAdapter() + adapters[list] = ListAdapter() + return adapters + -- 2.11.4.GIT