예제 #1
0
    def test_wrapping_node_to_val(self):
        nod = sparse.parse('foo')
        val = node_to_value(str, nod)
        self.assertEqual(type(val), str)
        self.assertEqual(val, 'foo')

        nod = sparse.parse('(foo bar)')
        val = node_to_value(list, nod)
        self.assert_(isinstance(val, ArgListWrapper))
        self.assertEqual(val.ls, ['foo', 'bar'])

        nod = sparse.parse('(foo bar)')
        val = node_to_value(tuple, nod)
        self.assertEqual(val, ('foo', 'bar'))

        nod = sparse.parse('(foo ())')
        val = node_to_value(tuple, nod)
        self.assert_(isinstance(val, ArgTupleWrapper))
예제 #2
0
    def test_wrapping_node_to_val(self):
        nod = sparse.parse('foo')
        val = node_to_value(str, nod)
        self.assertEqual(type(val), str)
        self.assertEqual(val, 'foo')

        nod = sparse.parse('(foo bar)')
        val = node_to_value(list, nod)
        self.assert_(isinstance(val, ArgListWrapper))
        self.assertEqual(val.ls, ['foo', 'bar'])

        nod = sparse.parse('(foo bar)')
        val = node_to_value(tuple, nod)
        self.assertEqual(val, ('foo', 'bar'))

        nod = sparse.parse('(foo ())')
        val = node_to_value(tuple, nod)
        self.assert_(isinstance(val, ArgTupleWrapper))
예제 #3
0
    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 (type(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)
예제 #4
0
    def test_simple_node_to_val(self):

        goodls = [
            (int, '5', 5),
            (int, '005', 5),
            (int, '"5"', 5),
            (long, '5', 5),
            (float, '5', 5.0),
            (str, '5', '5'),
            (str, 'foo', 'foo'),
            (unicode, 'foo', 'foo'),
            (str, u'foo', u'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 (type(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)
예제 #5
0
    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 (type(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)
예제 #6
0
    def test_simple_node_to_val(self):

        goodls = [
            (int, '5', 5),
            (int, '005', 5),
            (int, '"5"', 5),
            (long, '5', 5),
            (float, '5', 5.0),
            (str, '5', '5'),
            (str, 'foo', 'foo'),
            (unicode, 'foo', 'foo'),
            (str, u'foo', u'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 (type(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)