Пример #1
0
def test_hash():
    map = {
        Integer(1): 'foo',
        String("bar"): 'baz',
        Float('{{herp}}'): 'derp',
        Boolean('true'): 'slerp'
    }
    assert Integer(1) in map
    assert String("bar") in map
    assert Float('{{herp}}') in map
    assert Float('{{derp}}') not in map
    assert Integer(2) not in map
    assert String("baz") not in map
    assert Boolean('false') not in map
    assert Boolean('true') in map
Пример #2
0
def test_environment_find():
  oe1 = Environment(a = { 'b': 1 })
  oe2 = Environment(a = { 'b': { 'c': List(Integer)([1,2,3]) } } )
  oe = Environment(oe1, oe2)
  assert oe.find(ref('a.b')) == '1'
  assert oe.find(ref('a.b.c[0]')) == Integer(1)
  assert oe.find(ref('a.b.c[1]')) == Integer(2)
  assert oe.find(ref('a.b.c[2]')) == Integer(3)

  missing_refs = [ref('b'), ref('b.c'), ref('a.c'), ref('a.b.c[3]')]
  for r in missing_refs:
    with pytest.raises(Namable.NotFound):
      oe.find(r)

  oe = Environment(a = { 'b': { 'c': 5 } } )
  assert oe.find(ref('a.b.c')) == '5'
Пример #3
0
def test_cmp():
    assert not Float(1) == Integer(1)
    assert Float(1) != Integer(1)
    assert not String(1) == Integer(1)
    assert Integer(1) < Integer(2)
    assert Integer(2) > Integer(1)
    assert Integer(1) == Integer(1)
    assert String("a") < String("b")
    assert String("a") == String("a")
    assert String("b") > String("a")
    assert Float(1) < Float(2)
    assert Float(2) > Float(1)
    assert Float(1) == Float(1)
    assert Float(1.1) > Float(1)

    # all types <
    for typ1 in (Float, String, Integer):
        for typ2 in (Float, String, Integer):
            if typ1 != typ2:
                assert typ1(1) < typ2(1)
                assert typ1(1) <= typ2(1)
                assert not typ1(1) > typ2(1)
                assert not typ1(1) >= typ2(1)
Пример #4
0
def test_bad_inputs():
    for typ in Float, Integer, String, Boolean:
        with pytest.raises(TypeError):
            typ()
        with pytest.raises(TypeError):
            typ("1", "2")
        with pytest.raises(TypeError):
            typ(foo='123')

        bad_inputs = [{
            1: 2
        }, None, type, Float, Integer, String, Boolean,
                      Float(1),
                      Integer(1),
                      String(1),
                      Boolean(1)]
        for inp in bad_inputs:
            with pytest.raises(SimpleObject.CoercionError):
                '%s' % typ(inp)
Пример #5
0
def test_integer_constructors():
    bad_inputs = ['', 'a b c', unicodey('a b c'), unicodey(''), '1e5']
    good_inputs = [
        unicodey('{{foo}}'), '1 ', ' 1', ' {{herp}}.{{derp}} ', 0, 0.0, 1e5
    ]

    for input in bad_inputs:
        with pytest.raises(SimpleObject.CoercionError):
            str(Integer(input))
        with pytest.raises(SimpleObject.CoercionError):
            repr(Integer(input))

    for input in good_inputs:
        str(Integer(input))
        repr(Integer(input))

    assert Integer(123).check().ok()
    assert Integer('500').check().ok()
    assert not Integer('{{foo}}').check().ok()