def test___call__(self): """Test calling Scanner.Selector objects""" called = [] def s1func(node, env, path, called=called): called.append('s1func') called.append(node) return [] def s2func(node, env, path, called=called): called.append('s2func') called.append(node) return [] s1 = ScannerBase(s1func) s2 = ScannerBase(s2func) selector = Selector({'.x': s1, '.y': s2}) nx = self.skey_node('.x') env = DummyEnvironment() selector(nx, env, []) assert called == ['s1func', nx], called del called[:] ny = self.skey_node('.y') selector(ny, env, []) assert called == ['s2func', ny], called
def test___str__(self): """Test the ScannerBase __str__() method""" scanner = ScannerBase(function=self.func) s = str(scanner) assert s == 'NONE', s scanner = ScannerBase(function=self.func, name='xyzzy') s = str(scanner) assert s == 'xyzzy', s
def test_scan_check(self): """Test the ScannerBase class scan_check() method""" def my_scan(filename, env, target, *args): return [] def check(node, env, s=self): s.checked[str(node)] = 1 return 1 env = DummyEnvironment() s = ScannerBase(my_scan, "Check", scan_check=check) self.checked = {} path = s.path(env) scanned = s(DummyNode('x'), env, path) self.assertTrue(self.checked['x'] == 1, "did not call check function")
def test_get_skeys(self): """Test the ScannerBase get_skeys() method""" s = ScannerBase(function=self.func) sk = s.get_skeys() self.assertTrue(sk == [], "did not initialize to expected []") s = ScannerBase(function=self.func, skeys=['.1', '.2']) sk = s.get_skeys() self.assertTrue(sk == ['.1', '.2'], "sk was %s, not ['.1', '.2']") s = ScannerBase(function=self.func, skeys='$LIST') env = DummyEnvironment(LIST=['.3', '.4']) sk = s.get_skeys(env) self.assertTrue(sk == ['.3', '.4'], "sk was %s, not ['.3', '.4']")
def test_hash(self): """Test the ScannerBase class __hash__() method""" s = ScannerBase(self.func, "Hash") mapping = {} mapping[s] = 777 i = hash(id(s)) h = hash(list(mapping)[0]) self.assertTrue(h == i, "hash Scanner base class expected %s, got %s" % (i, h))
def test_positional(self): """Test the ScannerBase class using positional arguments""" s = ScannerBase(self.func, "Pos") env = DummyEnvironment() env.VARIABLE = "var1" self.test(s, env, DummyNode('f1.cpp'), ['f1.h', 'f1.hpp']) env = DummyEnvironment() env.VARIABLE = "i1" self.test(s, env, DummyNode('i1.cpp'), ['i1.h', 'i1.hpp'])
def test_select(self): """Test the ScannerBase select() method""" scanner = ScannerBase(function=self.func) s = scanner.select('.x') assert s is scanner, s selector = ScannerBase({'.x': 1, '.y': 2}) s = selector.select(self.skey_node('.x')) assert s == 1, s s = selector.select(self.skey_node('.y')) assert s == 2, s s = selector.select(self.skey_node('.z')) assert s is None, s
def test_key_opt(self): """Test the ScannerBase class using both keyword and optional arguments""" arg = "this is another argument" s = ScannerBase(function=self.func, name="KeyArg", argument=arg) env = DummyEnvironment() env.VARIABLE = "var4" self.test(s, env, DummyNode('f4.cpp'), ['f4.h', 'f4.hpp'], arg) env = DummyEnvironment() env.VARIABLE = "i4" self.test(s, env, DummyNode('i4.cpp'), ['i4.h', 'i4.hpp'], arg)
def test_pos_opt(self): """Test the ScannerBase class using both position and optional arguments""" arg = "this is the argument" s = ScannerBase(self.func, "PosArg", arg) env = DummyEnvironment() env.VARIABLE = "var3" self.test(s, env, DummyNode('f3.cpp'), ['f3.h', 'f3.hpp'], arg) env = DummyEnvironment() env.VARIABLE = "i3" self.test(s, env, DummyNode('i3.cpp'), ['i3.h', 'i3.hpp'], arg)
def test_keywords(self): """Test the ScannerBase class using keyword arguments""" s = ScannerBase(function=self.func, name="Key") env = DummyEnvironment() env.VARIABLE = "var2" self.test(s, env, DummyNode('f2.cpp'), ['f2.h', 'f2.hpp']) env = DummyEnvironment() env.VARIABLE = "i2" self.test(s, env, DummyNode('i2.cpp'), ['i2.h', 'i2.hpp'])
def test_add_scanner(self): """Test the ScannerBase add_scanner() method""" selector = ScannerBase({'.x': 1, '.y': 2}) s = selector.select(self.skey_node('.z')) assert s is None, s selector.add_scanner('.z', 3) s = selector.select(self.skey_node('.z')) assert s == 3, s
def test_path(self): """Test the ScannerBase path() method""" def pf(env, cwd, target, source, argument=None): return "pf: %s %s %s %s %s" % \ (env.VARIABLE, cwd, target[0], source[0], argument) env = DummyEnvironment() env.VARIABLE = 'v1' target = DummyNode('target') source = DummyNode('source') s = ScannerBase(self.func, path_function=pf) p = s.path(env, 'here', [target], [source]) assert p == "pf: v1 here target source None", p s = ScannerBase(self.func, path_function=pf, argument="xyz") p = s.path(env, 'here', [target], [source]) assert p == "pf: v1 here target source xyz", p
def test_creation(self): """Test creation of Scanner objects""" def func(self): pass s = ScannerBase(func) assert isinstance(s, ScannerBase), s s = ScannerBase({}) assert isinstance(s, ScannerBase), s s = ScannerBase(func, name='fooscan') assert str(s) == 'fooscan', str(s) s = ScannerBase({}, name='barscan') assert str(s) == 'barscan', str(s) s = ScannerBase(func, name='fooscan', argument=9) assert str(s) == 'fooscan', str(s) assert s.argument == 9, s.argument s = ScannerBase({}, name='fooscan', argument=888) assert str(s) == 'fooscan', str(s) assert s.argument == 888, s.argument
def test_recursive(self): """Test the ScannerBase class recursive flag""" nodes = [1, 2, 3, 4] s = ScannerBase(function=self.func) n = s.recurse_nodes(nodes) self.assertTrue(n == [], "default behavior returned nodes: %s" % n) s = ScannerBase(function=self.func, recursive=None) n = s.recurse_nodes(nodes) self.assertTrue(n == [], "recursive = None returned nodes: %s" % n) s = ScannerBase(function=self.func, recursive=1) n = s.recurse_nodes(nodes) self.assertTrue(n == n, "recursive = 1 didn't return all nodes: %s" % n) def odd_only(nodes): return [n for n in nodes if n % 2] s = ScannerBase(function=self.func, recursive=odd_only) n = s.recurse_nodes(nodes) self.assertTrue(n == [1, 3], "recursive = 1 didn't return all nodes: %s" % n)
def test___cmp__(self): """Test the ScannerBase class __cmp__() method""" s = ScannerBase(self.func, "Cmp") assert s is not None