]> mj.ucw.cz Git - moe.git/blobdiff - t/moe/config_test.py
Added unfix(), corrected fix()
[moe.git] / t / moe / config_test.py
index 573934f11c6482f243f2e467d679fe3b67d8e669..f1c836d33c204021ce1dfd32dff3ff38f80e9979 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)
@@ -82,6 +78,34 @@ class TestParser(TestConfig):
     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}")
     s.assertRaises(ConfigSyntaxError, s.parse, "if 'a'<>'b' {}")
 
+  def test_parse_remove(s):
+    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[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
+
 
 class TestConfigEval(TestConfig):
 
@@ -188,6 +212,13 @@ class TestConfigEval(TestConfig):
     assert s.val('E') == '41'
     s.var('D').remove_operation(l[0][1])
     assert s.val('D') == '42'
+    # Fixing via ConfigTree.fix
+    s.t.fix('D')
+    s.t.fix(['E','A'])
+    s.parse('D=""; E=""; A=""; ')
+    s.assertRaises(cf.VariableFixedError, s.val, "D")
+    s.assertRaises(cf.VariableFixedError, s.val, "E")
+    s.assertRaises(cf.VariableFixedError, s.val, "A")
 
   def test_unicode(s):
     # Ascii (1b) and unicode (2b)
@@ -217,7 +248,11 @@ class TestConfigEval(TestConfig):
     s.var('a').add_operation(cf.Operation('APPEND', None, cf.ConfigExpression(["5"]), level=5))
     assert s.val('a')=='23a3b45'
 
-
+  def test_priority_in_level(s):
+    s.parse('a="A"; c=""; b="B"; c+="C"; d="D"', level=0)
+    s.parse('a=b; b=c; c=d; d="ZZZ"', level=10)
+    s.parse('c="XXX"; c=""; d="S"; c+="YYY"', level=20)
+    assert s.val('a') == "YYY"
 
 # TODO: Fail on 1st April
 # TODO (OPT): Somehow add log.debug('Maximum encountered depth: %d', cf.debug_maxdepth)