From: Tomas Gavenciak Date: Sat, 29 May 2010 17:34:06 +0000 (-0400) Subject: Several new tests, updated, all passed X-Git-Tag: python-dummy-working~48 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=6831f0b92032686ed53b910da6fb9fa89bbe4889;p=moe.git Several new tests, updated, all passed Also covers all important code --- diff --git a/t/moe/conf.test.py b/t/moe/conf.test.py deleted file mode 100644 index d26b4fe..0000000 --- a/t/moe/conf.test.py +++ /dev/null @@ -1,85 +0,0 @@ -import conf -from confparser import * -import logging as log -import unittest - -class TestConfig(unittest.TestCase): - def setUp(s): - s.t = conf.ConfigTree() - def parse(s, string, level=0, fname='test'): - c=ConfigParser(string, s.t, fname, level) - ops = c.parse() - c.p_WS() - assert c.eof() - return ops - def val(s, varname): - return s.t.lookup(varname, create=False).value() - def eqparse(s, string, *args, **kwargs): - return [(i[0], i[1].operation) for i in s.parse(string, *args, **kwargs)] - -class TestParser(TestConfig): - 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}'""" - s2 = '\t\n \n ' + s1.replace('=', '= \n ').replace(';', '\t \n\t \n ').replace('+=',' \n += ') + '\n\n ' - def test_noWS(s): - assert len(s.parse(s.s1)) == 8 - def test_noWS_COMMENT(s): - assert s.eqparse(s.s1+'#COMMENT') == s.eqparse(s.s1+'#') == s.eqparse(s.s1+'#\n') == s.eqparse(s.s1+'\n#') - def test_manyWS(s): - assert s.eqparse(s.s2) == s.eqparse(s.s1) - def test_manyWS_COMMENT(s): - assert s.eqparse(s.s2.replace('\n',' #COMMENT \n')) == s.eqparse(s.s2.replace('\n','#\n')) == s.eqparse(s.s1) - def test_empty(s): - assert s.eqparse('') == s.eqparse('\n') == s.eqparse('') == s.eqparse('a{}') == \ - s.eqparse('a.b.c{if ""==\'\' {d.e{\n\n#Nothing\n}} }') == [] - def test_syntax_errors(s): - s.assertRaises(ConfigSyntaxError, s.parse, "a=#") - s.assertRaises(ConfigSyntaxError, s.parse, "a='\"") - s.assertRaises(ConfigSyntaxError, s.parse, 'a="{a@b}"') - s.assertRaises(ConfigSyntaxError, s.parse, 'a="A{A"') - def test_error_location(s): - try: s.parse('\t \n \n { \n \n ') - except ConfigSyntaxError, e: - assert e.line == 3 and e.column in range(2,4) - def test_quoting(s): - s.parse(' a="\\"\\{a$b\\}\'\n\n\'{z}" ') - assert s.t.lookup('z', create=False) - # No escaping in '-string - s.assertRaises(ConfigSyntaxError, s.parse, " a='\"\\'\n\n' ") - # Variable should not be created - s.parse(" a='{z2}' ") - s.assertRaises(conf.ConfigError, s.t.lookup, 'z2', create=False) - def test_conditions(s): - s.assertRaises(ConfigSyntaxError, s.parse, "if '{a}'=='{b}' and ''!='' {}") - s.parse('if ((#C\n (\n (not not not""!="")\n#C\n)\t ) ) {}') - s.parse('if (""=="" and not (not not ""!="" or ""=="")){}') - s.parse('if(""==""){a{if(""==""){if(""==""){b{if(""==""){if(""==""){}}}}}}}') - s.assertRaises(ConfigSyntaxError, s.parse, "if notnot'{a}'=='{b}' {}") - s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' not and ''!='') {}") - s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}") - -class TestConfigEval(TestConfig): - def test_ops(s): - s.parse('c+="-C_APP"', level=20) - s.parse('a="A"; b="{a}-B"; c="C1-{b}-C2"; a+="FOO"; a="AA"') - assert s.val('c') == 'C1-AA-B-C2-C_APP' - s.parse('b+="-A:\{{a}\}";a+="A"', level=10) - assert s.val('c') == 'C1-AAA-B-A:{AAA}-C2-C_APP' - def test_nested(s): - s.parse('a="0"; b{a="1"; b{a="2"; b{a="3"; b{a="4"; b{a="5"}}}}}') - assert s.val('b.b.b.a') == '3' - s.parse('b.b{b.b{b.a="5MOD"}}') - assert s.val('b.b.b.b.b.a') == '5MOD' - def test_escape_chars(s): - s.parse(r"""a='{a}\\\\#\n'; b="{a}'\"\{\}"; c='\'; c+="\{{b}\}";""") - assert s.val('c') == r"""\{{a}\\\\#\n'"{}}""" - -# TODO: conditions, chaining conditions, undefined (incl +=), loops, removal, fixing, fail on 1st April -# TODO: coverage - -if __name__ == '__main__': - log.getLogger().setLevel(log.WARN) -# log.getLogger().setLevel(log.DEBUG) - unittest.main() - -# TODO: log.info('maxdepth: %d', conf.debug_maxdepth) - diff --git a/t/moe/conftest.py b/t/moe/conftest.py new file mode 100644 index 0000000..beac064 --- /dev/null +++ b/t/moe/conftest.py @@ -0,0 +1,143 @@ +import moe.conf +from moe.confparser import * +import logging as log +import unittest + +class TestConfig(unittest.TestCase): + def setUp(s): + s.t = conf.ConfigTree() + def parse(s, string, level=0, fname='test'): + c=ConfigParser(string, s.t, fname, level) + ops = c.parse() + c.p_WS() + assert c.eof() + return ops + def val(s, varname): + return s.t.lookup(varname, create=False).value() + def eqparse(s, string, *args, **kwargs): + return [(i[0], i[1].operation) for i in s.parse(string, *args, **kwargs)] + +class TestParser(TestConfig): + 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}'""" + s2 = '\t\n \n ' + s1.replace('=', '= \n ').replace(';', '\t \n\t \n ').replace('+=',' \n += ') + '\n\n ' + def test_noWS(s): + assert len(s.parse(s.s1)) == 8 + def test_noWS_COMMENT(s): + assert s.eqparse(s.s1+'#COMMENT') == s.eqparse(s.s1+'#') == s.eqparse(s.s1+'#\n') == s.eqparse(s.s1+'\n#') + def test_manyWS(s): + assert s.eqparse(s.s2) == s.eqparse(s.s1) + def test_manyWS_COMMENT(s): + assert s.eqparse(s.s2.replace('\n',' #COMMENT \n')) == s.eqparse(s.s2.replace('\n','#\n')) == s.eqparse(s.s1) + def test_empty(s): + assert s.eqparse('') == s.eqparse('\n') == s.eqparse('') == s.eqparse('a{}') == \ + s.eqparse('a.b.c{if ""==\'\' {d.e{\n\n#Nothing\n}} }') == [] + def test_syntax_errors(s): + s.assertRaises(ConfigSyntaxError, s.parse, "a=#") + s.assertRaises(ConfigSyntaxError, s.parse, "a='\"") + s.assertRaises(ConfigSyntaxError, s.parse, 'a="{a@b}"') + s.assertRaises(ConfigSyntaxError, s.parse, 'a="A{A"') + def test_error_location(s): + try: s.parse('\t \n \n { \n \n ') + except ConfigSyntaxError, e: + assert e.line == 3 and e.column in range(2,4) + def test_quoting(s): + s.parse(' a="\\"\\{a$b\\}\'\n\n\'{z}" ') + assert s.t.lookup('z', create=False) + # No escaping in '-string + s.assertRaises(ConfigSyntaxError, s.parse, " a='\"\\'\n\n' ") + # Variable should not be created + s.parse(" a='{z2}' ") + s.assertRaises(conf.ConfigError, s.t.lookup, 'z2', create=False) + def test_conditions(s): + s.assertRaises(ConfigSyntaxError, s.parse, "if '{a}'=='{b}' and ''!='' {}") + s.parse('if ((#C\n (\n (not not not""!="")\n#C\n)\t ) ) {}') + s.parse('if (""=="" and not (not not ""!="" or ""=="")){}') + s.parse('if(""==""){a{if(""==""){if(""==""){b{if(""==""){if(""==""){}}}}}}}') + s.assertRaises(ConfigSyntaxError, s.parse, "if notnot'{a}'=='{b}' {}") + s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' not and ''!='') {}") + s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}") + +class TestConfigEval(TestConfig): + def test_ops(s): + s.parse('c+="-C_APP"', level=20) + s.parse('a="A"; b="{a}-B"; c="C1-{b}-C2"; a+="FOO"; a="AA"') + assert s.val('c') == 'C1-AA-B-C2-C_APP' + s.parse('b+="-A:\{{a}\}";a+="A"', level=10) + assert s.val('c') == 'C1-AAA-B-A:{AAA}-C2-C_APP' + def test_nested(s): + s.parse('a="0"; b{a="1"; b{a="2"; b{a="3"; b{a="4"; b{a="5"}}}}}') + assert s.val('b.b.b.a') == '3' + s.parse('b.b{b.b{b.a="5MOD"}}') + assert s.val('b.b.b.b.b.a') == '5MOD' + def test_escape_chars(s): + s.parse(r"""a='{a}\\\\#\n'; b="{a}'\"\{\}"; c='\'; c+="\{{b}\}";""") + assert s.val('c') == r"""\{{a}\\\\#\n'"{}}""" + ts = 'a="A:"; if "{c1}"=="1" {a+="C1"; b="B"; if ("{c2a}"=="1" or not "{c2b}"=="1") { a+="C2"; '\ + 'if ("{c3a}"=="1" and "{c3b}"=="1") { a+="C3" }}}' + def test_cond_chain(s): + s.parse(s.ts) + s.parse('c1="1"; c2="0"') + # b should have determined value, a should not (since c3a is undefined) + s.assertRaises(conf.UndefinedError, s.val, 'a') + assert s.val('b') == 'B' + s.parse('c1="0"') + # now b should be undefined + s.assertRaises(conf.UndefinedError, s.val, 'b') + # Normal evaluation + s.parse('c1="1"; c2a="1"; c2b="0"; c3a="0"') + assert s.val('a') == 'A:C1C2' + s.parse('c3a="1"; c3b="1"; c2b="1"') + assert s.val('a') == 'A:C1C2C3' + # tests condition invalidating + s.parse('c2a+="0"') + assert s.val('a') == 'A:C1' + def test_cond_eager(s): + s.parse(s.ts) + # undefined c2b and c3a should not be evaluated + s.parse('c1="1"; c2a="1"; c3a="0"') + assert s.val('a') == 'A:C1C2' + # but now c3b should be evaluated + s.parse('c1="1"; c2a="1"; c3a="1"') + s.assertRaises(conf.UndefinedError, s.val, 'a') + s.parse('c1="1"; c2a="1"; c3b="1"') + assert s.val('a') == 'A:C1C2C3' + def test_undef(s): + s.assertRaises(conf.UndefinedError, s.val, 'a') + s.parse('a="{b}"') + s.assertRaises(conf.UndefinedError, s.val, 'a') + s.parse('b+="1"') + s.assertRaises(conf.UndefinedError, s.val, 'b') + def test_loopy_def(s): + s.parse('a="A"; a+="{a}"') + s.assertRaises(conf.CyclicConfigError, s.val, 'a') + s.parse('b="{c}"; c="{b}"') + s.assertRaises(conf.CyclicConfigError, s.val, 'b') + def test_varname(s): + s.assertRaises(conf.VariableNameError, s.val, 'b/c') + s.assertRaises(conf.VariableNameError, s.val, '.b.c') + s.assertRaises(conf.VariableNameError, s.val, 'b.c.') + s.assertRaises(conf.VariableNameError, s.val, 'b..c') + def test_remove(s): + l = s.parse('a="A1"; b="B1"; if "{cond}"=="1" {a+="A2"; b+="B2"}; a+="A3"; b+="B3"; cond="1"') + assert s.val('a') == 'A1A2A3' + assert s.val('b') == 'B1B2B3' + # remove b+="B2" + s.t.lookup('b').remove_operation(l[3][1]) + assert s.val('a') == 'A1A2A3' + assert s.val('b') == 'B1B3' + # are the dependencies still handled properly? + s.parse('cond+="-invalidated"') + assert s.val('a') == 'A1A3' + s.parse('cond="1"') + assert s.val('a') == 'A1A2A3' + +# TODO: fixing, fail on 1st April +# TODO: coverage + +if __name__ == '__main__': + log.getLogger().setLevel(log.WARN) + #log.getLogger().setLevel(log.DEBUG) + unittest.main() + +# TODO: log.info('maxdepth: %d', conf.debug_maxdepth) +