def test_getitem_1(): template = '''{{ a['b']['c'][1]['d'][x] }}''' ast = parse(template).find(nodes.Getitem) config = Config() config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list' rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config) expected_struct = Dictionary({ 'a': Dictionary( { 'b': Dictionary( { 'c': List(Dictionary( { 'd': List(Scalar(linenos=[1]), label='d', linenos=[1]) }, linenos=[1]), label='c', linenos=[1]), }, label='b', linenos=[1]), }, label='a', linenos=[1]), 'x': Scalar(label='x', linenos=[1]), }) assert struct == expected_struct
def test_slice(): template = '''{{ xs[a:2:b] }}''' ast = parse(template).find(nodes.Getitem) rtype, struct = visit_getitem(ast, get_scalar_context(ast)) assert struct == Dictionary({ 'xs': List(Scalar(linenos=[1]), label='xs', linenos=[1]), 'a': Number(label='a', linenos=[1]), 'b': Number(label='b', linenos=[1]), })
def test_getitem_2(): template = '''{{ a[z] }}''' ast = parse(template).find(nodes.Getitem) config = Config() config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'dictionary' rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config) expected_struct = Dictionary({ 'a': Dictionary(label='a', linenos=[1]), 'z': Scalar(label='z', linenos=[1]), }) assert struct == expected_struct
def test_getitem_3(): template = '''{{ a[3] }}''' ast = parse(template).find(nodes.Getitem) config = Config() config.TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE = 'tuple' rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config) expected_struct = Dictionary({ 'a': Tuple([ Unknown(), Unknown(), Unknown(), Scalar(linenos=[1]), ], label='a', linenos=[1]), }) assert struct == expected_struct
def test_getitem_1(): template = '''{{ a['b']['c'][1]['d'][x] }}''' ast = parse(template).find(nodes.Getitem) config = Config() config.TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE = 'list' rtype, struct = visit_getitem(ast, get_scalar_context(ast), {}, config) expected_struct = Dictionary({ 'a': Dictionary({ 'b': Dictionary({ 'c': List(Dictionary({ 'd': List(Scalar(linenos=[1]), label='d', linenos=[1]) }, linenos=[1]), label='c', linenos=[1]), }, label='b', linenos=[1]), }, label='a', linenos=[1]), 'x': Scalar(label='x', linenos=[1]), }) assert struct == expected_struct