def test_case2(self): str1= StringIO( 'value1 = 5\n' 'value2 = 1.23 \n' 'section [first = "1",\n' ' second= "long string"] { \n' ' value3= on \n' ' # A comment line\n' ' value4= 1 \n' ' value4 = 2 # With comment \n' ' value4 = 42\n' ' list = 1,2, 3, 4 , 5\n' '}\n') result= str( '<?xml version="1.0" encoding="utf-8"?>\n' '<root>\n' ' <value1>5</value1>\n' ' <value2>1.23</value2>\n' ' <section first="1" second="long string">\n' ' <value3>on</value3>\n' ' <!-- A comment line-->\n' ' <value4>1</value4>\n' ' <value4>2</value4>\n' ' <!-- With comment -->\n' ' <value4>42</value4>\n' ' <list>1,2,3,4,5</list>\n' ' </section>\n' '</root>\n' ) reader= DefaultReader(self.handler1) reader.parse(str1) #print( self.stdout.getvalue() ) self.assertEqual(result, self.stdout.getvalue() )
def test_case2(self): str1= StringIO( 'value1 = 5\r\n' 'value2 = 1.23 \n' 'section [first = "1",\n' ' second= "long string"] {\n' ' value3= "on"\n' ' value4= 1 # with comment \n' ' # A comment line \n' '\n' ' value4= 2 \n' ' value4= 42 \n' ' value5= \'A very long quoted string\' \n' ' value6 {}\n' ' func= sin(arg1=1, arg2=2)\n' ' f= sin(arg1="xxx with space", \n' ' arg2= 55.123) \n' '} \n' ) expected= str( "root {\n" " value1= 5\n" " value2= 1.23\n" " section[first=\"1\", second=\"long string\"] {\n" " value3= on\n" " value4= 1\n" " # with comment \n" " # A comment line \n" " value4= 2\n" " value4= 42\n" " value5= A very long quoted string\n" " value6= \"\"\n" " func= sin(arg1=1,arg2=2)\n" " f= sin(arg1=\"xxx with space\",arg2= 55.123)\n" " }\n" "}\n" ) reader= DefaultReader(self.handler1) reader.parse(str1) #print(self.out.getvalue()) self.assertEqual(self.out.getvalue(), expected) self.assertEqual(self.err.getvalue(), "")
def setUp(self): self.context= Group( { "value1" : Value(self, "val1", int), "value2" : Value(self, "val2", float), "section" : Group( { "value3" : Value(self, "val3", Map({"on" : True, "off" : False})), "value4" : MultiValue(self, "val4", int) }) }) self.val1 = 0 self.val2 = 0. self.val3 = False self.val4 = [] self.log= StringIO() self.err= StringIO() self.reader= DefaultReader( contentHandler= Dispatcher( context=self.context, errorHandler=ErrorHandler(out= self.log, err=self.err ) ))
class DefaultReaderTestCase(unittest.TestCase): def setUp(self): self.context= Group( { "value1" : Value(self, "val1", int), "value2" : Value(self, "val2", float), "section" : Group( { "value3" : Value(self, "val3", Map({"on" : True, "off" : False})), "value4" : MultiValue(self, "val4", int) }) }) self.val1 = 0 self.val2 = 0. self.val3 = False self.val4 = [] self.log= StringIO() self.err= StringIO() self.reader= DefaultReader( contentHandler= Dispatcher( context=self.context, errorHandler=ErrorHandler(out= self.log, err=self.err ) )) def test_case1(self): text=StringIO( "value1 = 5\n" "# Comment line\n" "value2= 4.2 # with comment\n" "# value2= 500.\n" "section { value3= on \n" " value4 = 3\n" " value4\t= 4 ; value4 = 5\n" "}\n" ) self.reader.parse(text) self.assertEqual(self.val1, 5) self.assertEqual(self.val2, 4.2) self.assertEqual(self.val3, True) self.assertEqual(self.val4, [3, 4, 5]) self.assertEqual(self.context["section"].count, 1) self.assertEqual(self.context["value1"].count, 1) self.assertEqual(self.context["value2"].count, 1) self.assertEqual(self.context["section"]["value3"].count, 1) self.assertEqual(self.context["section"]["value4"].count, 3) self.assertEqual(self.log.getvalue(), "") self.assertEqual(self.err.getvalue(), "") def test_case2(self): text=StringIO( "value1 [att1=attr1, att2='attr2'] = 5\n" "value2 [ att3='one', #comment\n" " # another comment\n" " att4='two' ] = 4.2\n" ) self.reader.parse(text) self.assertEqual(self.val1, 5) self.assertEqual(self.val2, 4.2) self.assertEqual(self.log.getvalue(), "") err= self.err.getvalue() # print(err) self.assertTrue("WARNING: In line 1" in err) self.assertTrue("WARNING: In line 4" in err) self.assertEqual(err.count("Ignored attributes"), 2) self.assertTrue("'att1' ('attr1')" in err) self.assertTrue("'att2' ('attr2')" in err) self.assertTrue("'att3' ('one')" in err) self.assertTrue("'att4' ('two')" in err)