blob: 7999955436b3967c4eed99292555d4c768cfe2a6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
""""""
import functools
import grc.converter.cheetah_converter as parser
def test_basic():
c = parser.Converter(names={'abc'})
for convert in (c.convert_simple, c.convert_hard, c.to_python):
assert 'abc' == convert('$abc')
assert 'abc' == convert('$abc()')
assert 'abc' == convert('$(abc)')
assert 'abc' == convert('$(abc())')
assert 'abc' == convert('${abc}')
assert 'abc' == convert('${abc()}')
assert c.stats['simple'] == 2 * 6
assert c.stats['hard'] == 1 * 6
def test_simple():
convert = parser.Converter(names={'abc': {'def'}})
assert 'abc' == convert.convert_simple('$abc')
assert 'abc.def' == convert.convert_simple('$abc.def')
assert 'abc.def' == convert.convert_simple('$(abc.def)')
assert 'abc.def' == convert.convert_simple('${abc.def}')
try:
convert.convert_simple('$abc.not_a_sub_key')
except NameError:
assert True
else:
assert False
def test_conditional():
convert = parser.Converter(names={'abc'})
assert '(asb_asd_ if abc > 0 else __not__)' == convert.convert_inline_conditional(
'#if $abc > 0 then asb_$asd_ else __not__')
def test_simple_format_string():
convert = functools.partial(parser.Converter(names={'abc'}).convert_simple, spec=parser.FormatString)
assert '{abc}' == convert('$abc')
assert '{abc:eval}' == convert('$abc()')
assert '{abc}' == convert('$(abc)')
assert '{abc:eval}' == convert('$(abc())')
assert '{abc}' == convert('${abc}')
assert '{abc:eval}' == convert('${abc()}')
def test_hard_format_string():
names = {'abc': {'ff'}, 'param1': {}, 'param2': {}}
convert = functools.partial(parser.Converter(names).convert_hard, spec=parser.FormatString)
assert 'make_a_cool_block_{abc.ff}({param1}, {param2})' == \
convert('make_a_cool_block_${abc.ff}($param1, $param2)')
converter = parser.Converter(names={'abc'})
c2p = converter.to_python
def test_opts():
assert 'abc abc abc' == c2p('$abc $(abc) ${abc}')
assert 'abc abc.abc abc' == c2p('$abc $abc.abc ${abc}')
assert 'abc abc[''].abc abc' == c2p('$abc $abc[''].abc() ${abc}')
def test_nested():
assert 'abc(abc) abc + abc abc[abc]' == c2p('$abc($abc) $(abc + $abc) ${abc[$abc]}')
assert '(abc_abc_)' == c2p('(abc_$(abc)_)')
def test_nested2():
class Other(parser.Python):
nested_start = '{'
nested_end = '}'
assert 'abc({abc})' == converter.convert('$abc($abc)', spec=Other)
def test_nested3():
class Other(parser.Python):
start = '{'
end = '}'
assert '{abc(abc)}' == converter.convert('$abc($abc)', spec=Other)
def test_with_string():
assert 'abc "$(abc)" abc' == c2p('$abc "$(abc)" ${abc}')
assert 'abc \'$(abc)\' abc' == c2p('$abc \'$(abc)\' ${abc}')
assert 'abc "\'\'$(abc)" abc' == c2p('$abc "\'\'$(abc)" ${abc}')
def test_if():
result = converter.to_mako("""
#if $abc > 0
test
#else if $abc < 0
test
#else
bla
#end if
""")
expected = """
% if abc > 0:
test
% elif abc < 0:
test
% else:
bla
% endif
"""
assert result == expected
def test_hash_end():
result = converter.to_mako('$abc#slurp')
assert result == '${abc}\\'
def test_slurp_if():
result = converter.to_mako("""
$abc#slurp
#if $abc
""")
expected = """
${abc}
% if abc:
"""
assert result == expected
|