def test_path_typed_list(self): config = self.config ctxt1 = Context(config=config) ctxt1.push_source(mozpath.join(config.topsrcdir, "foo", "moz.build")) ctxt2 = Context(config=config) ctxt2.push_source(mozpath.join(config.topsrcdir, "bar", "moz.build")) paths = [ "!../bar/qux", "!/qux/qux", "!qux", "../bar/qux", "/qux/qux", "qux", ] MyList = ContextDerivedTypedList(Path) l = MyList(ctxt1) l += paths for p_str, p_path in zip(paths, l): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) self.assertEqual(p_path.join("foo"), Path(ctxt1, mozpath.join(p_str, "foo"))) l2 = MyList(ctxt2) l2 += paths for p_str, p_path in zip(paths, l2): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt2, p_str)) # Assigning with Paths from another context doesn't rebase them l2 = MyList(ctxt2) l2 += l for p_str, p_path in zip(paths, l2): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) MyListWithFlags = ContextDerivedTypedListWithItems( Path, StrictOrderingOnAppendListWithFlagsFactory({ "foo": bool, }), ) l = MyListWithFlags(ctxt1) l += paths for p in paths: l[p].foo = True for p_str, p_path in zip(paths, l): self.assertEqual(p_str, p_path) self.assertEqual(p_path, Path(ctxt1, p_str)) self.assertEqual(l[p_str].foo, True) self.assertEqual(l[p_path].foo, True)
def test_context_derived_typed_list(self): ns = Context(allowed_variables=VARIABLES) # Setting to a type that's rejected by coercion should not work. with self.assertRaises(ValueError): ns['HOGERA'] = [False] ns['HOGERA'] += ['a', 'b', 'c'] self.assertIsInstance( ns['HOGERA'], ContextDerivedTypedList(Piyo, StrictOrderingOnAppendList)) for n in range(0, 3): self.assertIsInstance(ns['HOGERA'][n], Piyo) self.assertEqual(ns['HOGERA'][n].value, ['a', 'b', 'c'][n]) self.assertEqual(ns['HOGERA'][n].context, ns) with self.assertRaises(UnsortedError): ns['HOGERA'] += ['f', 'e', 'd']
def __str__(self): return self.value def __cmp__(self, other): return cmp(self.value, str(other)) def __hash__(self): return hash(self.value) VARIABLES = { 'HOGE': (unicode, unicode, None), 'FUGA': (Fuga, unicode, None), 'PIYO': (Piyo, unicode, None), 'HOGERA': (ContextDerivedTypedList(Piyo, StrictOrderingOnAppendList), list, None), 'HOGEHOGE': (ContextDerivedTypedListWithItems( Piyo, StrictOrderingOnAppendListWithFlagsFactory({ 'foo': bool, })), list, None), } class TestContext(unittest.TestCase): def test_key_rejection(self): # Lowercase keys should be rejected during normal operation. ns = Context(allowed_variables=VARIABLES) with self.assertRaises(KeyError) as ke: ns['foo'] = True