Beispiel #1
0
 def t(self, name, sname):
     if sname is None:
         sname = name
     if py3k:
         [uname, bname] = [sname, sname.encode('UTF-8')]
     else:
         [uname, bname] = [sname.decode('UTF-8'), sname]
     sym = Symbol(name)
     x = Expression(sym)
     assert_is(x, Expression(x))
     # __repr__(x)
     assert_repr(x, 'Expression({sym!r})'.format(sym=sym))
     # value:
     v = x.value
     assert_equal(type(v), Symbol)
     assert_equal(v, sym)
     # lvalue:
     v = x.lvalue
     assert_equal(type(v), Symbol)
     assert_equal(v, sym)
     # __str__():
     assert_equal(str(x), sname)
     assert_repr(x, repr(Expression.from_string(sname)))
     # __unicode__():
     assert_equal(unicode(x), uname)
     assert_repr(x, repr(Expression.from_string(uname)))
     # __eq__(), __ne__():
     assert_equal(x, Expression(sym))
     assert_not_equal(x, Expression(name))
     assert_not_equal(x, sym)
     # __hash__():
     assert_equal(hash(x), hash(bname.strip(b'|')))
     # pickle:
     assert_pickle_equal(x)
     return x
Beispiel #2
0
 def test2(self):
     x = Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_repr(
         x, "Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])")
     y = Expression(x)
     assert_repr(y, repr(x))
     assert_false(x is y)
     assert_equal(x.value, ((1, 2), 3, (4, 5, Symbol('baz')), ('quux', )))
     assert_equal(x.lvalue, [[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_equal(str(x), '((1 2) 3 (4 5 baz) ("quux"))')
     assert_repr(x, repr(Expression.from_string(str(x))))
     assert_equal(len(x), 4)
     assert_equal(bool(x), True)
     assert_equal(tuple(x), (Expression(
         (1, 2)), Expression(3), Expression(
             (4, 5, Symbol('baz'))), Expression(('quux', ))))
     with assert_raises_str(TypeError, 'key must be an integer or a slice'):
         x[object()]
     assert_equal(x[1], Expression(3))
     assert_equal(x[-1][0], Expression('quux'))
     with assert_raises_str(IndexError, 'list index of out range'):
         x[6]
     with assert_raises_str(IndexError, 'list index of out range'):
         x[-6]
     assert_equal(x[:].value, x.value)
     assert_equal(x[:].lvalue, x.lvalue)
     assert_repr(x[1:], "Expression([3, [4, 5, Symbol('baz')], ['quux']])")
     assert_repr(x[-2:], "Expression([[4, 5, Symbol('baz')], ['quux']])")
     x[-2:] = 4, 5, 6
     assert_repr(x, 'Expression([[1, 2], 3, 4, 5, 6])')
     x[0] = 2
     assert_repr(x, 'Expression([2, 3, 4, 5, 6])')
     x[:] = (1, 3, 5)
     assert_repr(x, 'Expression([1, 3, 5])')
     x[3:] = 7,
     assert_repr(x, 'Expression([1, 3, 5, 7])')
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[object():]
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[:2]
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[object():] = []
     with assert_raises_str(NotImplementedError,
                            'only [n:] slices are supported'):
         x[:2] = []
     with assert_raises_str(TypeError, 'can only assign a list expression'):
         x[:] = 0
     assert_equal(x, Expression((1, 3, 5, 7)))
     assert_not_equal(x, Expression((2, 4, 6)))
     assert_not_equal(x, (1, 3, 5, 7))
     with assert_raises_str(TypeError, "unhashable type: 'ListExpression'"):
         hash(x)
Beispiel #3
0
def test_string_expressions():
    x = Expression('eggs')
    assert_repr(x, "Expression('eggs')")
    assert_is(x, Expression(x))
    assert_equal(x.value, 'eggs')
    assert_equal(x.lvalue, 'eggs')
    assert_equal(str(x), '"eggs"')
    assert_repr(x, repr(Expression.from_string(str(x))))
    assert_equal(x, Expression('eggs'))
    assert_not_equal(x, Expression(Symbol('eggs')))
    assert_not_equal(x, 'eggs')
    assert_equal(hash(x), hash('eggs'))
    assert_pickle_equal(x)
Beispiel #4
0
def test_string_expressions():
    x = Expression('eggs')
    assert_repr(x, "Expression('eggs')")
    assert_is(x, Expression(x))
    assert_equal(x.value, 'eggs')
    assert_equal(x.lvalue, 'eggs')
    assert_equal(str(x), '"eggs"')
    assert_repr(x, repr(Expression.from_string(str(x))))
    assert_equal(x, Expression('eggs'))
    assert_not_equal(x, Expression(Symbol('eggs')))
    assert_not_equal(x, 'eggs')
    assert_equal(hash(x), hash('eggs'))
    assert_pickle_equal(x)
Beispiel #5
0
 def t(self, name, sname):
     if sname is None:
         sname = name
     if py3k:
         [uname, bname] = [sname, sname.encode('UTF-8')]
     else:
         [uname, bname] = [sname.decode('UTF-8'), sname]
     sym = Symbol(name)
     x = Expression(sym)
     assert_is(x, Expression(x))
     # __repr__(x)
     assert_repr(x, 'Expression({sym!r})'.format(sym=sym))
     # value:
     v = x.value
     assert_equal(type(v), Symbol)
     assert_equal(v, sym)
     # lvalue:
     v = x.lvalue
     assert_equal(type(v), Symbol)
     assert_equal(v, sym)
     # __str__():
     assert_equal(str(x), sname)
     assert_repr(x, repr(Expression.from_string(sname)))
     # __unicode__():
     assert_equal(unicode(x), uname)
     assert_repr(x, repr(Expression.from_string(uname)))
     # __eq__(), __ne__():
     assert_equal(x, Expression(sym))
     assert_not_equal(x, Expression(name))
     assert_not_equal(x, sym)
     # __hash__():
     assert_equal(
         hash(x),
         hash(bname.strip(b'|'))
     )
     # pickle:
     assert_pickle_equal(x)
     return x
Beispiel #6
0
 def test2(self):
     x = Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_repr(x, "Expression([[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])")
     y = Expression(x)
     assert_repr(y, repr(x))
     assert_false(x is y)
     assert_equal(x.value, ((1, 2), 3, (4, 5, Symbol('baz')), ('quux',)))
     assert_equal(x.lvalue, [[1, 2], 3, [4, 5, Symbol('baz')], ['quux']])
     assert_equal(str(x), '((1 2) 3 (4 5 baz) ("quux"))')
     assert_repr(x, repr(Expression.from_string(str(x))))
     assert_equal(len(x), 4)
     assert_equal(bool(x), True)
     assert_equal(tuple(x), (Expression((1, 2)), Expression(3), Expression((4, 5, Symbol('baz'))), Expression(('quux',))))
     with assert_raises_str(TypeError, 'key must be an integer or a slice'):
         x[object()]
     assert_equal(x[1], Expression(3))
     assert_equal(x[-1][0], Expression('quux'))
     with assert_raises_str(IndexError, 'list index of out range'):
         x[6]
     with assert_raises_str(IndexError, 'list index of out range'):
         x[-6]
     assert_equal(x[:].value, x.value)
     assert_equal(x[:].lvalue, x.lvalue)
     assert_repr(x[1:], "Expression([3, [4, 5, Symbol('baz')], ['quux']])")
     assert_repr(x[-2:], "Expression([[4, 5, Symbol('baz')], ['quux']])")
     x[-2:] = 4, 5, 6
     assert_repr(x, 'Expression([[1, 2], 3, 4, 5, 6])')
     x[0] = 2
     assert_repr(x, 'Expression([2, 3, 4, 5, 6])')
     x[:] = (1, 3, 5)
     assert_repr(x, 'Expression([1, 3, 5])')
     x[3:] = 7,
     assert_repr(x, 'Expression([1, 3, 5, 7])')
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[object():]
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[:2]
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[object():] = []
     with assert_raises_str(NotImplementedError, 'only [n:] slices are supported'):
         x[:2] = []
     with assert_raises_str(TypeError, 'can only assign a list expression'):
         x[:] = 0
     assert_equal(x, Expression((1, 3, 5, 7)))
     assert_not_equal(x, Expression((2, 4, 6)))
     assert_not_equal(x, (1, 3, 5, 7))
     with assert_raises_str(TypeError, "unhashable type: 'ListExpression'"):
         hash(x)
Beispiel #7
0
 def test_badstring(self):
     with assert_raises(ExpressionSyntaxError):
         Expression.from_string('(1')
Beispiel #8
0
 def test_parse(self):
     with assert_raises(ExpressionSyntaxError):
         x = Expression.from_string('3.14')
         if isinstance(x.value, Symbol):
             raise ExpressionSyntaxError
Beispiel #9
0
 def test_parse(self):
     self.t(42, Expression.from_string('42'))
Beispiel #10
0
 def test_badstring(self):
     with assert_raises(ExpressionSyntaxError):
         Expression.from_string('(1')
Beispiel #11
0
 def test_parse(self):
     with assert_raises(ExpressionSyntaxError):
         x = Expression.from_string('3.14')
         if isinstance(x.value, Symbol):
             raise ExpressionSyntaxError
Beispiel #12
0
 def test_parse(self):
     self.t(42, Expression.from_string('42'))