def test_arglist_serialize(self): ls = [ ArgList(), ArgList(Arg('x'), Arg('y')), ArgList(Arg(name='x', type=str), ArgExtra(TupleOf(int, int))), ArgList(Arg(name='foo'), x=Arg(default=3.1), y=Arg(type=ListOf())), ArgList(Arg(type=ListOf(bool), default=[False, True])), ] for arglist in ls: nod = arglist.to_node() arglist2 = ArgList.from_node(nod) self.assert_arglists_identical(arglist, arglist2)
def test_wrapped_serialize(self): typ = Wrapped(int) val = type_to_node(typ) typ2 = node_to_type(val) self.assertTrue(isinstance(typ2, Wrapped)) self.assertEqual(typ2.type, int) typ = Wrapped(ListOf(int, bool)) val = type_to_node(typ) typ2 = node_to_type(val) self.assertTrue(isinstance(typ2, Wrapped)) self.assertTrue(isinstance(typ2.type, ListOf)) self.assertEqual(typ2.type.types, (int, bool))
def test_value_to_node(self): ls = [ (int, 5, '5'), (int, 5, '5'), (float, 5.1, '5.1'), (bool, False, 'false'), (bool, True, 'true'), (bool, 0, 'false'), (bool, 3, 'true'), (str, 'foo', 'foo'), (str, 'foo space', '"foo space"'), (str, 'foo', 'foo'), (str, 'foo', 'foo'), (str, 'unic\u0153de', 'unic\u0153de'), (list, [], '()'), (tuple, (), '()'), (list, ['foo', 'bar', ('x', 'y')], '(foo bar (x y))'), (tuple, ['foo', 'bar', ('x', 'y')], '(foo bar (x y))'), (ListOf(bool), [1, 0], '(true false)'), (TupleOf(int, bool, repeat=1), [1, 1, 0], '(1 true false)'), (None, 'foo', 'foo'), (None, 12, '12'), (None, [], '()'), (None, (), '()'), (None, ['foo', 'bar', ['x']], '(foo bar (x))'), (int, None, '(no=value)'), (str, None, '(no=value)'), (list, None, '(no=value)'), (ListOf(), None, '(no=value)'), (None, None, '(no=value)'), # XXX fails with no loader? # (agent.Agent, builtin.NullAgent(), '/boodle.builtin.NullAgent'), # (Wrapped(agent.Agent), builtin.NullAgent, '/boodle.builtin.NullAgent'), ] for (typ, val, res) in ls: nod = value_to_node(typ, val) st = nod.serialize() self.assertEqual(st, res)
def test_arg_serialize(self): ls = [ Arg(), Arg(name='foo'), Arg(name='foo', index=5, default=3.1, description='A thing.'), Arg(type=bool), Arg(type=ListOf(int, str, bool, repeat=2)), Arg(optional=True), Arg(default=[]), Arg(name='bar', optional=False), Arg(default='foo', optional=False), Arg(default=True, optional=True), ] for arg in ls: nod = arg.to_node() arg2 = Arg.from_node(nod) self.assert_args_identical(arg, arg2)
def test_arglist_extra(self): arglist = ArgList(Arg('x'), ArgExtra(ListOf(int)), Arg('y')) self.assertEqual(arglist.min_accepted(), 2) self.assertEqual(arglist.max_accepted(), None) arg = arglist.get_index(1) self.assertEqual(arg.name, 'x') arg = arglist.get_index(2) self.assertEqual(arg.name, 'y') arglist2 = arglist.clone() self.assertEqual(arglist2.min_accepted(), 2) self.assertEqual(arglist2.max_accepted(), None) arg = arglist2.get_index(1) self.assertEqual(arg.name, 'x') arg = arglist2.get_index(2) self.assertEqual(arg.name, 'y') self.assertTrue(isinstance(arglist2.listtype, ListOf)) self.assertEqual(arglist2.listtype.types, (int, ))
def test_seqtype_serialize(self): ls = [ ListOf(), ListOf(int), ListOf(int, str, bool, float), ListOf(int, str, min=1), ListOf(int, str, max=5), ListOf(int, str, bool, repeat=2), ListOf(int, str, bool, min=1, max=5, repeat=2), TupleOf(), TupleOf(int), TupleOf(int, str, bool, float), TupleOf(int, str, min=1), TupleOf(int, str, max=5), TupleOf(int, str, bool, repeat=2), TupleOf(int, str, bool, min=1, max=5, repeat=2), ] for typ in ls: val = typ.to_node() typ2 = node_to_type(val) self.assert_types_identical(typ, typ2) typ = ListOf(TupleOf(int, str, bool), TupleOf(int, str, min=1, max=5, repeat=1), ListOf()) val = typ.to_node() typ2 = node_to_type(val) self.assertEqual(repr(typ), repr(typ2))
def test_resolve(self): arglist = ArgList(Arg('x'), Arg('y')) goodls = [ ('(A xx yy)', ['xx', 'yy'], {}), ('(A xx ())', ['xx', []], {}), ('(A ((xx)) (yy))', [[['xx']], ['yy']], {}), ] badls = [ 'A', '()', '(A)', '(A xx)', '(A xx yy z=1)', '(A xx yy zz)', '(A xx x=xxx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(name='x', type=int), Arg(name='y', type=str)) goodls = [ ('(A 1 yy)', [1, 'yy'], {}), ('(A -5 "1 2 3")', [-5, '1 2 3'], {}), ('(A y=yy x=1)', [1, 'yy'], {}), ('(A 1 y=yy)', [1, 'yy'], {}), ] badls = [ '(A)', '(A 1)', '(A xx yy)', '(A 1 yy zz)', '(A 1 ())', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(name='x', type=str), ArgExtra(ListOf(int))) goodls = [ ('(A xx)', ['xx'], {}), ('(A xx 1 2 3)', ['xx', 1, 2, 3], {}), ('(A 0 1 2 3)', ['0', 1, 2, 3], {}), ] badls = [ '(A xx 1 2 z)', '(A xx z 2 3)', '(A xx 1 2 3 z=0)', '(A 1 2 3 x=xx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(name='x', type=str), ArgExtra(TupleOf(int, int))) goodls = [ ('(A xx 1 2)', ['xx', 1, 2], {}), ] badls = [ '(A xx)', '(A xx 1 2 3)', '(A xx 1 2 z)', '(A xx z 2 3)', '(A xx 1 2 3 z=0)', '(A 1 2 3 x=xx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(name='x', type=str), ArgExtra(ListOf(int, str, repeat=1, min=1, max=3))) goodls = [ ('(A xx 1)', ['xx', 1], {}), ('(A xx 1 2)', ['xx', 1, '2'], {}), ('(A xx 1 2 3)', ['xx', 1, '2', '3'], {}), ] badls = [ '(A xx)', '(A xx 1 2 3 4)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(type=int), Arg(type=str)) goodls = [ ('(A 1 yy)', [1, 'yy'], {}), ] badls = [ '(A)', '(A 1)', '(A 1 ())', '(A 1 yy zz)', '(A xx yy)', '(A 1 yy z=zz)', '(A x=1 y=yy)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg(type=ListOf(int)), Arg(type=list)) goodls = [ ('(A () ())', [[], []], {}), ('(A (1) (yy))', [[1], ['yy']], {}), ('(A (1 2 3) (yy () 33))', [[1, 2, 3], ['yy', [], '33']], {}), ] badls = [ '(A)', '(A 1)', '(A 1 ())', '(A (x) ())', '(A (()) ())', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(x=Arg(), y=Arg()) goodls = [ ('(A x=xx y=yy)', [], { 'x': 'xx', 'y': 'yy' }), ] badls = [ '(A)', '(A xx)', '(A xx yy)', '(A xx yy zz)', '(A x=xx)', '(A y=yy)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg('x', default='xd'), Arg('y', default='yd')) goodls = [ ('(A xx yy)', ['xx', 'yy'], {}), ('(A xx y=yy)', ['xx', 'yy'], {}), ('(A x=xx y=yy)', ['xx', 'yy'], {}), ('(A xx)', ['xx', 'yd'], {}), ('(A x=xx)', ['xx', 'yd'], {}), ('(A y=yy)', ['xd', 'yy'], {}), ('(A)', ['xd', 'yd'], {}), ] badls = [ '(A xx yy z=1)', '(A xx yy zz)', '(A xx x=xxx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(x=Arg(default='xd'), y=Arg(default='yd')) goodls = [ ('(A x=xx)', [], { 'x': 'xx' }), ('(A y=yy)', [], { 'y': 'yy' }), ('(A x=xx y=yy)', [], { 'x': 'xx', 'y': 'yy' }), ('(A)', [], {}), ] badls = [ '(A xx)', '(A xx yy)', '(A xx y=yy)', '(A xx yy z=1)', '(A xx yy zz)', '(A xx x=xxx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(Arg('x', default='xd', optional=False), Arg('y', default='yd')) goodls = [ ('(A xx yy)', ['xx', 'yy'], {}), ('(A xx y=yy)', ['xx', 'yy'], {}), ('(A x=xx y=yy)', ['xx', 'yy'], {}), ('(A xx)', ['xx', 'yd'], {}), ('(A x=xx)', ['xx', 'yd'], {}), ('(A y=yy)', ['xd', 'yy'], {}), ('(A)', ['xd', 'yd'], {}), ] badls = [ '(A xx yy z=1)', '(A xx yy zz)', '(A xx x=xxx)', ] self.one_test_resolve(arglist, goodls, badls) arglist = ArgList(x=Arg(default='xd', optional=False), y=Arg(default='yd')) goodls = [ ('(A x=xx)', [], { 'x': 'xx' }), ('(A y=yy)', [], { 'x': 'xd', 'y': 'yy' }), ('(A x=xx y=yy)', [], { 'x': 'xx', 'y': 'yy' }), ('(A)', [], { 'x': 'xd' }), ] badls = [ '(A xx)', '(A xx yy)', '(A xx y=yy)', '(A xx yy z=1)', '(A xx yy zz)', '(A xx x=xxx)', ] self.one_test_resolve(arglist, goodls, badls)
def test_sequence_node_to_val(self): typ = ListOf() self.assertEqual(typ.min, 0) self.assertEqual(typ.max, None) self.assertEqual(typ.repeat, 1) typ = TupleOf() self.assertEqual(typ.min, 0) self.assertEqual(typ.max, None) self.assertEqual(typ.repeat, 1) typ = ListOf(int, str, bool) self.assertEqual(typ.min, 0) self.assertEqual(typ.max, None) self.assertEqual(typ.repeat, 3) typ = TupleOf(int, str, bool) self.assertEqual(typ.min, 3) self.assertEqual(typ.max, 3) self.assertEqual(typ.repeat, 3) typ = ListOf(int, str, bool, min=1, max=4, repeat=2) self.assertEqual(typ.min, 1) self.assertEqual(typ.max, 4) self.assertEqual(typ.repeat, 2) typ = TupleOf(int, str, bool, min=1, max=4, repeat=2) self.assertEqual(typ.min, 1) self.assertEqual(typ.max, 4) self.assertEqual(typ.repeat, 2) typ = TupleOf(int, str, bool, max=4) self.assertEqual(typ.min, 0) self.assertEqual(typ.max, 4) self.assertEqual(typ.repeat, 3) typ = ListOf(int, str, bool, min=1) self.assertEqual(typ.min, 1) self.assertEqual(typ.max, None) self.assertEqual(typ.repeat, 3) goodls = [ (TupleOf(str, str), '(x y)', ('x', 'y')), (TupleOf(str, int, max=4), '(1 2 3 4)', ('1', 2, '3', 4)), (TupleOf(str, min=1, max=3), '(x)', ('x', )), (TupleOf(str, min=1, max=3), '(x y)', ('x', 'y')), (TupleOf(str, min=1, max=3), '(x y z)', ('x', 'y', 'z')), (TupleOf(str, int, repeat=1), '(1)', ('1', )), (TupleOf(str, int, repeat=1), '(1 2)', ('1', 2)), (TupleOf(str, int, repeat=1), '(1 2 3)', ('1', 2, 3)), (TupleOf(str, int, bool, repeat=2), '(1 2 3 4)', ('1', 2, True, 4)), ] badls = [ (TupleOf(str, str), '(x)'), (TupleOf(str, str), '(x y z)'), (TupleOf(str, min=1, max=3), '()'), (TupleOf(str, min=1, max=3), '(x y z w)'), ] for (typ, st, res) in goodls: nod = sparse.parse(st) val = node_to_value(typ, nod) val = resolve_value(val) self.assertEqual(val, res) self.assertEqual(type(val), type(res)) if isinstance(val, list): for (sub1, sub2) in zip(val, res): self.assertEqual(type(sub1), type(sub2)) for (typ, st) in badls: nod = sparse.parse(st) self.assertRaises(ValueError, node_to_value, typ, nod)
def test_simple_node_to_val(self): goodls = [ (int, '5', 5), (int, '005', 5), (int, '"5"', 5), (int, '5', 5), (float, '5', 5.0), (str, '5', '5'), (str, 'foo', 'foo'), (str, 'foo', 'foo'), (str, 'foo', 'foo'), (bool, '""', False), (bool, '0', False), (bool, 'no', False), (bool, 'FALSE', False), (bool, '1', True), (bool, 'YES', True), (bool, 'true', True), (list, '(() foo)', [[], 'foo']), (ListOf(), '(())', [[]]), (ListOf(), '(() foo)', [[], 'foo']), (ListOf(str), '(foo bar)', ['foo', 'bar']), (ListOf(int), '(1 3 2)', [1, 3, 2]), (ListOf(bool), '(0 1 false true NO YES)', [False, True, False, True, False, True]), (ListOf(int, str), '(1 2 3 4)', [1, '2', 3, '4']), (ListOf(str, None), '(foo (bar) baz (()))', ['foo', ['bar'], 'baz', [[]]]), (ListOf(TupleOf(int, str)), '((1 2) (3 4))', [(1, '2'), (3, '4')]), (tuple, '(() foo)', ([], 'foo')), (TupleOf(), '(() 1 2)', ([], '1', '2')), (TupleOf(int, str), '(1 2)', (1, '2')), (None, 'foo', 'foo'), (None, '()', []), (None, '(foo (1) ())', ['foo', ['1'], []]), (int, '(no=value)', None), (str, '(no=value)', None), (list, '(no=value)', None), (ListOf(), '(no=value)', None), (None, '(no=value)', None), ] badls = [ (int, '()'), (int, '(1)'), (int, 'foo'), (int, '5.0'), (int, '5x'), (str, '()'), (float, '()'), (float, 'foo'), (bool, '()'), (list, 'foo'), (list, '(foo x=1)'), (ListOf(), 'foo'), (ListOf(str), 'foo'), (ListOf(str), '(())'), (ListOf(int), '(foo)'), (ListOf(str, int), '(foo bar)'), (ListOf(int, str), '(1 foo bar)'), (TupleOf(str, str), '(baz)'), (TupleOf(str, str), '(baz foo bar)'), (int, '(none=one)'), (int, '(foo none=none)'), ] for (typ, st, res) in goodls: nod = sparse.parse(st) val = node_to_value(typ, nod) val = resolve_value(val) self.assertEqual(val, res) self.assertEqual(type(val), type(res)) if isinstance(val, list): for (sub1, sub2) in zip(val, res): self.assertEqual(type(sub1), type(sub2)) for (typ, st) in badls: nod = sparse.parse(st) self.assertRaises(ValueError, node_to_value, typ, nod)