def testDelimiter1(self): """ Correct delimiter should be supplied """ _opt = {"delimiter": ";"} _p = BlockParser(_opt) _p.parse("block {a = 1\nb = 2\n}")
def testVarTransformation1(self): """ Check for correct var transformation """ def _f(a): raise XYZValueError(a) _opt = {"var_transform": _f} _p = BlockParser(_opt) _p.parse("block { a = VALUE }")
def testMacroValueValidator1(self): """ Check if value_validator raises exception on macro """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) return val # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) _p.parse("block { &m = INCORRECT_VALUE\n test = &m }")
def testDelimiter2(self): """ Correct delimiter should be supplied """ _opt = {"delimiter": ";"} _p = BlockParser(_opt) assert len(_p.parse("block {a = 1;b = 2} block2 {x=y;y=x}"))
def testAssignChar2(self): """ Test for assign character """ _opt = {"assignchar": ":"} _p = BlockParser(_opt) assert len(_p.parse("block {a: 1}"))
def testVarTransformation2(self): """ Check for correct var transformation """ _opt = {"var_transform": lambda x: x * 3} _p = BlockParser(_opt) _data = _p.parse("block { X = 1 }") assert "XXX" in _data["block"]
def testMacroValueValidator2(self): """ Check for value_validator correct value on macro """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) # Returning modified value return 1 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) len(_p.parse("block { &m = CORRECT_VALUE\n a = &m }"))
class TestBlock(object): def setUp(self): self.p = BlockParser() # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(XYZValueError) def testOptType(self): """ Raise error on wrong opt type """ BlockParser(1) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testQuote(self): """ Parsing should raise ParseError on unterminated quotes """ ParseError(self.p.parse('block {a = "string\n}')) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testXQuote(self): """ Test extended quotes """ assert len(self.p.parse("block { x = ''' ssaf \t \n ; \" ' '' ; & }''' }")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testEmptyQuote(self): """ Test parsing empty string value: "" """ assert len(self.p.parse('block { x = "" }')) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testEmptyXQuote(self): """ Test parsing empty x-quote """ assert len(self.p.parse("block { x = '''''' }")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testComments(self): """ Test proper commenting """ assert len(self.p.parse("# \tComment 1\n#Comment 2 ''' = }{\n block {}")) == 0 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testInit(self): """ In STATE_INIT state only keyword is acceptable """ self.p.parse("anything") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testUnknownVar(self): """ Variable not in valid list is not allowed """ _opt = {"validvars": ("a",)} BlockParser(_opt).parse("block {b = 1}") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testValidVar(self): """ Invalid variable name should raise exception """ self.p.parse("block {a+-; = 1}") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testAssignChar1(self): """ Test for assign character """ _opt = {"assignchar": ":"} _p = BlockParser(_opt) assert len(_p.parse("block {a: 1}")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testAssignChar2(self): """ Test for assign character """ _opt = {"assignchar": ":"} _p = BlockParser(_opt) assert len(_p.parse("block {a: 1}")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testDelimiter1(self): """ Correct delimiter should be supplied """ _opt = {"delimiter": ";"} _p = BlockParser(_opt) _p.parse("block {a = 1\nb = 2\n}") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testDelimiter2(self): """ Correct delimiter should be supplied """ _opt = {"delimiter": ";"} _p = BlockParser(_opt) assert len(_p.parse("block {a = 1;b = 2} block2 {x=y;y=x}")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testCompleteQuote(self): """ Check for unclosed quote upon EOF """ self.p.parse('block {a = "string') # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testCompleteBlock(self): """ Check for unclosed block upon EOF """ self.p.parse("block {a = value\n") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testValueValidator1(self): """ Check if value_validator raises exception1 """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) return val # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) _p.parse("block { a = INCORRECT_VALUE }") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testMacroValueValidator1(self): """ Check if value_validator raises exception on macro """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) return val # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) _p.parse("block { &m = INCORRECT_VALUE\n test = &m }") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testVarTransformation1(self): """ Check for correct var transformation """ def _f(a): raise XYZValueError(a) _opt = {"var_transform": _f} _p = BlockParser(_opt) _p.parse("block { a = VALUE }") # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testValueValidator2(self): """ Check for value_validator correct value """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) # Returning modified value return 1 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) assert len(_p.parse("block { a = CORRECT_VALUE }")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testMacroValueValidator2(self): """ Check for value_validator correct value on macro """ def _f(block, var, val): if val != "CORRECT_VALUE": raise XYZValueError("Incorrect value %s!" % val) # Returning modified value return 1 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ _opt = {"value_validator": _f} _p = BlockParser(_opt) len(_p.parse("block { &m = CORRECT_VALUE\n a = &m }")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testVarTransformation2(self): """ Check for correct var transformation """ _opt = {"var_transform": lambda x: x * 3} _p = BlockParser(_opt) _data = _p.parse("block { X = 1 }") assert "XXX" in _data["block"] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testEscaping(self): """ Check for proper escaping """ assert len(self.p.parse("block { var = a\\ b\\ c }")) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def testListValue(self): """ Check for proper list values parsing """ _src = "block { var = l, i, s ,t }" _r = self.p.parse(_src) assert isinstance(_r["block"]["var"], tuple) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @raises(ParseError) def testUndefinedMacro(self): """ Check for undefined macro """ self.p.parse("block { a = &undef }")