def test_hashing(): map = { List(Integer)([1,2,3]): 'foo', Map(String,Integer)({'a': 1, 'b': 2}): 'bar' } assert List(Integer)([1,2,3]) in map assert Map(String,Integer)({'a': 1, 'b': 2}) in map assert List(Integer)([3,2,1]) not in map assert Map(String,Integer)({'a': 2, 'b': 1}) not in map
def test_iteration(): li = List(Integer)([1,2,3]) liter = iter(li) assert next(liter) == Integer(1) assert next(liter) == Integer(2) assert next(liter) == Integer(3) with pytest.raises(StopIteration): next(liter) li = List(Integer)([]) with pytest.raises(StopIteration): next(iter(li))
def test_indexing(): li = List(Integer)([1,2,3]) for bad in ['a', None, type, Integer]: with pytest.raises(TypeError): li[bad] # Indexing should also support slices li = List(Integer)(range(10)) assert li[0] == Integer(0) assert li[-1] == Integer(9) assert li[::2] == (Integer(0), Integer(2), Integer(4), Integer(6), Integer(8)) assert li[8:] == (Integer(8), Integer(9)) assert li[2:0:-1] == (Integer(2), Integer(1)) with pytest.raises(IndexError): li[10]
def test_basic_scoping(): i = Integer('{{intvalue}}') lst = List(Integer)([i.bind(intvalue = 1), i.bind(intvalue = 2), i]) lsti, _ = lst.bind(intvalue = 3).interpolate() assert lsti == List(Integer)([Integer(1), Integer(2), Integer(3)]) lsti, _ = lst.in_scope(intvalue = 3).interpolate() assert lsti == List(Integer)([Integer(1), Integer(2), Integer(3)]) one = ref('[0]') two = ref('[1]') three = ref('[2]') assert lst.find(one) == Integer(1) assert lst.find(two) == Integer(2) assert lst.find(three) == Integer('{{intvalue}}') assert lst.in_scope(intvalue = 3).find(one) == Integer(1) assert lst.in_scope(intvalue = 3).find(two) == Integer(2) assert lst.in_scope(intvalue = 3).find(three) == Integer(3)
def test_basic_lists(): assert List(Integer)([]).check().ok() assert List(Integer)([1]).check().ok() assert List(Integer)((1,)).check().ok() assert List(Integer)(["1",]).check().ok() assert not List(Integer)([1, "{{two}}"]).check().ok() assert (List(Integer)([1, "{{two}}"]) % {'two': 2}).check().ok() with pytest.raises(ValueError): List(Integer)({'not': 'a', 'list': 'type'}) repr(List(Integer)([1, '{{two}}']))
def test_list_find(): ls = List(String)(['a', 'b', 'c']) assert ls.find(ref('[0]')) == String('a') with pytest.raises(Namable.NamingError): ls.find(ref('.a')) with pytest.raises(Namable.NamingError): ls.find(ref('[a]')) with pytest.raises(Namable.NotFound): ls.find(ref('[4]')) with pytest.raises(Namable.Unnamable): ls.find(ref('[1].foo'))
def test_list_scoping(): assert List(Integer)([1, "{{wut}}"]).interpolate() == ( List(Integer)([Integer(1), Integer('{{wut}}')]), [ref('wut')]) assert List(Integer)([1, "{{wut}}"]).bind(wut = 23).interpolate() == ( List(Integer)([Integer(1), Integer(23)]), []) assert List(Integer)([1, Integer("{{wut}}").bind(wut = 24)]).bind(wut = 23).interpolate() == ( List(Integer)([Integer(1), Integer(24)]), [])
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'
def test_nested_mustache_resolution(): # straight oe = Environment(foo='{{bar}}', bar='{{baz}}', baz='hello') for pattern in ('{{foo}}', '{{bar}}', '{{baz}}', 'hello'): resolved, unbound = MustacheParser.resolve('%s world' % pattern, oe) assert resolved == 'hello world' assert unbound == [] # in structs class Process(Struct): name = Required(String) cmdline = String class Task(Struct): name = Default(String, '{{processes[0].name}}') processes = List(Process) task = Task(processes=[Process(name="hello"), Process(name="world")]) assert task.name().get() == 'hello' # iterably resolve_string = '{{foo[{{bar}}]}}' resolve_list = List(String)(["hello", "world"]) resolved, unbound = MustacheParser.resolve( resolve_string, Environment(foo=resolve_list, bar=0)) assert resolved == 'hello' assert unbound == [] resolved, unbound = MustacheParser.resolve( resolve_string, Environment(foo=resolve_list, bar="0")) assert resolved == 'hello' assert unbound == [] resolved, _ = MustacheParser.resolve(resolve_string, Environment(foo=resolve_list, bar=1)) assert resolved == 'world' resolved, unbound = MustacheParser.resolve( resolve_string, Environment(foo=resolve_list, bar=2)) assert resolved == '{{foo[2]}}' assert unbound == [ref('foo[2]')]
def test_equals(): assert List(Integer)([1, "{{wut}}"]).bind(wut=23) == List(Integer)([1, 23])
class Resources(Struct): cpu = Required(Float) ram = Integer disks = List(String)
class Task(Struct): name = Default(String, 'task-{{processes[0].name}}') processes = Required(List(Process))
class Task(Struct): name = Default(String, '{{processes[0].name}}') processes = List(Process)
def test_mustache_resolve_cycles(): with pytest.raises(MustacheParser.Uninterpolatable): MustacheParser.resolve( '{{foo[{{bar}}]}} {{baz}}', Environment(foo=List(String)(["{{foo[{{bar}}]}}", "world"])), Environment(bar=0))