]> mj.ucw.cz Git - moe.git/blobdiff - t/moe/config_test.py
Added function config_escape
[moe.git] / t / moe / config_test.py
index 2a0325b8b57e923b9ce76ccad47683e72eb1451e..d6eb51ba47ab465079bf46b435ae9d28abb833da 100644 (file)
@@ -12,11 +12,7 @@ class TestConfig(unittest.TestCase):
     s.t = cf.ConfigTree()    
 
   def parse(s, string, level=0, fname='test'):
-    cp = ConfigParser(string, s.t, fname, level)
-    ops = cp.parse()
-    cp.p_WS()
-    assert cp.eof()
-    return ops
+    return s.t.parse(string, source=fname, level=level)
 
   def var(s, varname, create=True):
     return s.t.lookup(varname, create=create)
@@ -83,22 +79,37 @@ class TestParser(TestConfig):
     s.assertRaises(ConfigSyntaxError, s.parse, "if 'a'<>'b' {}")
 
   def test_parse_remove(s):
-    s.parse('a="000"')
+    def raise_UserWarning():
+      with s.parse("b='F'", level=99):
+       assert s.val('b') == 'F'
+       raise UserWarning 
+    d0 = s.parse('a="000"')
     d1 = s.parse("a='A'; b='B'; c='C'", level=10)
     d2 = s.parse('c="{a}{a}"; b="XX" ', level=20)
     d3 = s.parse('b+=c ', level=30)
     assert s.val('b') == "XXAA"
     s.t.remove(d2)
     assert s.val('b') == "BC"
-    s.assertRaises(ValueError, s.t.remove, [('d', d1[1][0])])
-    s.assertRaises(ValueError, s.t.remove, [('b', d1[1][0])])
+    s.assertRaises(ValueError, s.t.remove, [('d', d1[0][1])])
+    s.assertRaises(ValueError, s.t.remove, [('b', d1[0][1])])
+    # Try exception in "with parse():" 
+    s.assertRaises(UserWarning, raise_UserWarning)
+    assert s.val('b') == "BC"
     # partially remove d1
     s.t.remove([('c', d1[2][1])])
     # try to remove rest - 'a' and 'b' should get removed
     s.assertRaises(ValueError, s.t.remove, d1)
     assert s.val('a') == "000"
+    # cleanup
     s.t.remove(d3)
-
+    s.t.remove(d0)
+    for v in 'abcd':
+      assert len(s.var(v).operations) == 0
+
+  def test_escape(s):
+    for tst in ['{B}"#\n\\"', '', '\\\n\\\\"\\{\\}', '"#"', s.s1, s.s2]:
+      s.parse('A="%s"; A+="0"'%config_escape(tst))
+      assert s.val('A') == tst+'0'
 
 class TestConfigEval(TestConfig):