]> mj.ucw.cz Git - eval.git/blob - t/moe/conftest.py
Add unicode and file parsing test
[eval.git] / t / moe / conftest.py
1 # -*- coding: utf-8 -*-
2
3 import moe.conf as conf
4 from moe.confparser import *
5 import logging as log
6 import unittest
7 import tempfile
8
9 class TestConfig(unittest.TestCase):
10
11   def setUp(s):
12     s.t = conf.ConfigTree()    
13
14   def parse(s, string, level=0, fname='test'):
15     c=ConfigParser(string, s.t, fname, level)
16     ops = c.parse()
17     c.p_WS()
18     assert c.eof()
19     return ops
20
21   def var(s, varname, create=True):
22     return s.t.lookup(varname, create=create)
23
24   def val(s, varname):
25     return s.var(varname, create=False).value()
26
27   def eqparse(s, string, *args, **kwargs):
28     return [(i[0], i[1].operation) for i in s.parse(string, *args, **kwargs)]
29
30
31 class TestParser(TestConfig):
32   s1 = r"""a="1";z{b='2';w{S.Tr_an-g.e='""'}};c.d='\n';e="{a}{b}";e+='{c.d}';a+="\"\n\{\}";f+='Z{a.b}'"""
33   s2 = '\t\n \n ' + s1.replace('=', '= \n ').replace(';', '\t \n\t \n ').replace('+=',' \n += ') + '\n\n '
34
35   def test_noWS(s):
36     assert len(s.parse(s.s1)) == 8
37
38   def test_noWS_COMMENT(s):
39     assert s.eqparse(s.s1+'#COMMENT') == s.eqparse(s.s1+'#') == s.eqparse(s.s1+'#\n') == s.eqparse(s.s1+'\n#')
40
41   def test_manyWS(s):
42     assert s.eqparse(s.s2) == s.eqparse(s.s1)
43
44   def test_manyWS_COMMENT(s):
45     assert s.eqparse(s.s2.replace('\n',' #COMMENT \n')) == s.eqparse(s.s2.replace('\n','#\n')) == s.eqparse(s.s1)
46
47   def test_empty(s):
48     assert s.eqparse('') == s.eqparse('\n') == s.eqparse('') == s.eqparse('a{}') == \
49       s.eqparse('a.b.c{if ""==\'\' {d.e{\n\n#Nothing\n}} }') == []
50
51   def test_syntax_errors(s):
52     s.assertRaises(ConfigSyntaxError, s.parse, "a=#")
53     s.assertRaises(ConfigSyntaxError, s.parse, "a='\"")
54     s.assertRaises(ConfigSyntaxError, s.parse, 'a="{a@b}"')
55     s.assertRaises(ConfigSyntaxError, s.parse, 'a="A{A"')
56
57   def test_error_location(s):
58     try: s.parse('\t \n  \n  { \n \n ')
59     except ConfigSyntaxError, e:
60       assert e.line == 3 and e.column in range(2,4)
61
62   def test_quoting(s):
63     s.parse(' a="\\"\\{a$b\\}\'\n\n\'{z}" ')
64     assert s.var('z', create=False)
65     # No escaping in '-string 
66     s.assertRaises(ConfigSyntaxError, s.parse, " a='\"\\'\n\n' ")
67     # Variable should not be created
68     s.parse(" a='{z2}' ")
69     s.assertRaises(conf.ConfigError, s.var, 'z2', create=False)
70
71   def test_conditions(s):
72     s.assertRaises(ConfigSyntaxError, s.parse, "if '{a}'=='{b}' and ''!='' {}")
73     s.parse('if ((#C\n (\n (not not not""!="")\n#C\n)\t ) ) {}')
74     s.parse('if (""=="" and not (not not ""!="" or ""=="")){}')
75     s.parse('if(""==""){a{if(""==""){if(""==""){b{if(""==""){if(""==""){}}}}}}}')
76     s.assertRaises(ConfigSyntaxError, s.parse, "if notnot'{a}'=='{b}' {}")
77     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' not and ''!='') {}")
78     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}")
79
80
81 class TestConfigEval(TestConfig):
82
83   def test_ops(s):
84     s.parse('c+="-C_APP"', level=20)
85     s.parse('a="A"; b="{a}-B"; c="C1-{b}-C2"; a+="FOO"; a="AA"')
86     assert s.val('c') == 'C1-AA-B-C2-C_APP'
87     s.parse('b+="-A:\{{a}\}";a+="A"', level=10)
88     assert s.val('c') == 'C1-AAA-B-A:{AAA}-C2-C_APP'
89
90   def test_nested(s):
91     s.parse('a="0"; b{a="1"; b{a="2"; b{a="3"; b{a="4"; b{a="5"}}}}}')
92     assert s.val('b.b.b.a') == '3'
93     s.parse('b.b{b.b{b.a="5MOD"}}')
94     assert s.val('b.b.b.b.b.a') == '5MOD'
95
96   def test_escape_chars(s):
97     s.parse(r"""a='{a}\\\\#\n'; b="{a}'\"\{\}"; c='\'; c+="\{{b}\}";""")
98     assert s.val('c') == r"""\{{a}\\\\#\n'"{}}"""
99   ts = 'a="A:"; if "{c1}"=="1" {a+="C1"; b="B"; if ("{c2a}"=="1" or not "{c2b}"=="1") { a+="C2"; '\
100     'if ("{c3a}"=="1" and "{c3b}"=="1") { a+="C3" }}}'
101
102   def test_cond_chain(s):
103     s.parse(s.ts)
104     s.parse('c1="1"; c2="0"')
105     # b should have determined value, a should not (since c3a is undefined)
106     s.assertRaises(conf.UndefinedError, s.val, 'a')
107     assert s.val('b') == 'B'
108     s.parse('c1="0"')
109     # now b should be undefined
110     s.assertRaises(conf.UndefinedError, s.val, 'b')
111     # Normal evaluation
112     s.parse('c1="1"; c2a="1"; c2b="0"; c3a="0"')
113     assert s.val('a') == 'A:C1C2'
114     s.parse('c3a="1"; c3b="1"; c2b="1"')
115     assert s.val('a') == 'A:C1C2C3'
116     # tests condition invalidating 
117     s.parse('c2a+="0"')
118     assert s.val('a') == 'A:C1'
119
120   def test_cond_eager(s):
121     s.parse(s.ts)
122     # undefined c2b and c3a should not be evaluated 
123     s.parse('c1="1"; c2a="1"; c3a="0"')
124     assert s.val('a') == 'A:C1C2'
125     # but now c3b should be evaluated 
126     s.parse('c1="1"; c2a="1"; c3a="1"')
127     s.assertRaises(conf.UndefinedError, s.val, 'a')
128     s.parse('c1="1"; c2a="1"; c3b="1"')
129     assert s.val('a') == 'A:C1C2C3'
130
131   def test_undef(s):
132     s.assertRaises(conf.UndefinedError, s.val, 'a')
133     s.parse('a="{b}"')
134     s.assertRaises(conf.UndefinedError, s.val, 'a')
135     s.parse('b+="1"')
136     s.assertRaises(conf.UndefinedError, s.val, 'b')
137
138   def test_loopy_def(s):
139     s.parse('a="A"; a+="{a}"')
140     s.assertRaises(conf.CyclicConfigError, s.val, 'a')
141     s.parse('b="{c}"; c="{b}"')
142     s.assertRaises(conf.CyclicConfigError, s.val, 'b')
143
144   def test_varname(s):
145     s.assertRaises(conf.VariableNameError, s.val, 'b/c')
146     s.assertRaises(conf.VariableNameError, s.val, '.b.c')
147     s.assertRaises(conf.VariableNameError, s.val, 'b.c.')
148     s.assertRaises(conf.VariableNameError, s.val, 'b..c')
149
150   def test_remove(s):
151     l = s.parse('a="A1"; b="B1"; if "{cond}"=="1" {a+="A2"; b+="B2"}; a+="A3"; b+="B3"; cond="1"')
152     assert s.val('a') == 'A1A2A3'
153     assert s.val('b') == 'B1B2B3'
154     # remove b+="B2"
155     s.var('b').remove_operation(l[3][1])
156     assert s.val('a') == 'A1A2A3'
157     assert s.val('b') == 'B1B3'
158     # are the dependencies still handled properly? 
159     s.parse('cond+="-invalidated"')
160     assert s.val('a') == 'A1A3'
161     s.parse('cond="1"')
162     assert s.val('a') == 'A1A2A3'
163
164   def test_fix(s):
165     s.parse(""" A='4'; B="{A}"; C="2"; B+="{C}"; D="{B}"; E="{D}" """)
166     s.var('D').fix()
167     # Break by C 
168     l = s.parse('C="3"')
169     s.assertRaises(conf.VariableFixedError, s.val, "E")
170     s.assertRaises(conf.VariableFixedError, s.val, "D")
171     s.var('C').remove_operation(l[0][1])
172     # Break directly by D 
173     l = s.parse('D="41"')
174     s.assertRaises(conf.VariableFixedError, s.val, "D")
175     s.assertRaises(conf.VariableFixedError, s.val, "E")
176     # Unfix
177     s.var('D').unfix()
178     assert s.val('E') == '41'
179     s.var('D').remove_operation(l[0][1])
180     assert s.val('D') == '42'
181
182   def test_unicode(s):
183     # Ascii (1b) and unicode (2b)
184     s.parse(u'A="Ú"; C="ě"')
185     # String
186     s.parse('B=\'chyln\'')
187     # By escapes  
188     s.parse(u'D=\'\u0159e\u017eav\xe1\'')
189     s.parse(u'E="ŽluŤ"')
190     # Via utf8 file
191     f = tempfile.TemporaryFile(mode='w+t')
192     f.write(u'S1="\xdachyln\u011b \u0159e\u017eav\xe1 \u017dlu\u0164" ; S2="{A}{B}{C} {D} {E}"'.encode('utf8'))
193     f.seek(0)
194     s.parse(f)
195     # Test
196     s.parse(u'if "{S1}"=="{S2}" { ANS="jó!" } ')
197     assert s.val('ANS') == u"jó!"
198     assert s.val('S1') == s.val('S2') == u'\xdachyln\u011b \u0159e\u017eav\xe1 \u017dlu\u0164'
199
200
201
202
203 # TODO: Test conditions
204 # TODO: Fail on 1st April
205 # TODO: Somehow add log.debug('Maximum encountered depth: %d', conf.debug_maxdepth)
206
207 # Coverage via command "nosetests conftest --with-coverage --cover-html-dir=cover --cover-html"
208