tidy up cnfvar.py unit tests
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 7 Feb 2019 14:42:21 +0000 (15:42 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 7 Feb 2019 15:50:39 +0000 (16:50 +0100)
This batch of unit tests was disabled since the switch to Python3
on account of minor encoding troubles. Reactivate it and ensure
encoding and escaping are handled properly.

test/test_cnfvar.py

index 3cdf0fb..2f5355a 100755 (executable)
@@ -21,7 +21,9 @@
 #
 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
 
+import os
 import unittest
+
 from src import cnfvar
 
 #
@@ -174,6 +176,8 @@ demo_jsoncnf = """
 }
 """
 
+demo_jsoncnf_bytes = demo_jsoncnf.encode ("latin-1")
+
 demo_latin1crap = r"""
 { "cnf" : [
     {
@@ -301,7 +305,7 @@ demo_latin1crap = r"""
 ]}
 """
 
-demo_cnf_group = b"""
+demo_cnf_group = """
 1 GROUP,1: "Administratoren"
 2    (1) GROUP_ACCESS_GO_ONLINE_ALLOWED,0: "1"
 3    (1) GROUP_EMAILFILTER_BAN_FILTERLIST_REF,0: "-1"
@@ -309,6 +313,8 @@ demo_cnf_group = b"""
 5    (1) GROUP_PROXY_PROFILE_REF,0: "1"
 """
 
+demo_cnf_group_bytes = demo_cnf_group.encode ("latin-1")
+
 demo_cnf_filter = b"""
 1 EMAILFILTER_BAN_FILTERLIST,1: "Vordefiniert: Alles verboten"
 2    (1) EMAILFILTER_BAN_FILTERLIST_ENCRYPTED,0: "BLOCK"
@@ -331,24 +337,52 @@ demo_cnf_comments = b"""
 8    (1) EMAILFILTER_BAN_FILTERLIST_PREDEFINED_ID,0: "1"
 """
 
+demo_cnf_escaped_quotes = """
+1 HERE_BE_QUOTES,0: "\""
+2 HERE_BE_QUOTES,1: "foo\"bar\"\"\"baz"
+3 HERE_BE_QUOTES,2: "unquo\\\"table"
+4 HERE_BE_QUOTES,3: "unquo\\\\\"\"table"
+"""
+
+demo_json_escaped_quotes = """
+{ "cnf": [ { "number"   : 1,
+             "varname"  : "HERE_BE_QUOTES",
+             "instance" : 0,
+             "data"     : "\\"" },
+           { "number"   : 2,
+             "varname"  : "HERE_BE_QUOTES",
+             "instance" : 1,
+             "data"     : "foo\\"bar\\"\\"\\"baz" },
+           { "number"   : 3,
+             "varname"  : "HERE_BE_QUOTES",
+             "instance" : 2,
+             "data"     : "unquo\\\\\\"table" },
+           { "number"   : 4,
+             "varname"  : "HERE_BE_QUOTES",
+             "instance" : 3,
+             "data"     : "unquo\\\\\\\\\\"\\"table" } ] }
+"""
 
 #
 #                                 test class
 #
 
-@unittest.skip('TODO Philipp: correct (maybe replace all bytes with str using '
-               'type_helpers?)')
 class CnfVarUnittest(unittest.TestCase):
 
     def test_print_cnf(self):
         with open(os.devnull, "w") as devnull:
             cnfvar.print_cnf(demo_cnfvar, out=devnull)
 
-    def test_parse_cnf_simple(self):
+    def test_parse_cnf_simple_str(self):
         cnf = cnfvar.read_cnf(demo_cnf_group)
         with open(os.devnull, "w") as devnull:
             cnfvar.print_cnf_json(cnf, out=devnull)
 
+    def test_parse_cnf_simple_bytes(self):
+        cnf = cnfvar.read_cnf(demo_cnf_group_bytes)
+        with open(os.devnull, "w") as devnull:
+            cnfvar.print_cnf_json(cnf, out=devnull)
+
     def test_parse_cnf_nested(self):
         cnf = cnfvar.read_cnf(demo_cnf_filter)
         with open(os.devnull, "w") as devnull:
@@ -366,16 +400,31 @@ class CnfVarUnittest(unittest.TestCase):
         except cnfvar.InvalidCNF:
             print ("Caught the duplicate line, bravo!")
 
-    def test_read_json(self):
+    def test_read_json_str(self):
         cnf = cnfvar.read_cnf_json(demo_jsoncnf)
         with open(os.devnull, "w") as devnull:
             cnfvar.print_cnf(cnf, out=devnull)
 
+    def test_read_json_bytes(self):
+        cnf = cnfvar.read_cnf_json(demo_jsoncnf_bytes)
+        with open(os.devnull, "w") as devnull:
+            cnfvar.print_cnf(cnf, out=devnull)
+
     def test_read_json_nonascii(self):
         cnf = cnfvar.read_cnf_json(demo_latin1crap)
         with open(os.devnull, "w") as devnull:
             cnfvar.print_cnf(cnf, out=devnull)
 
+    def test_parse_cnf_quotes(self):
+        cnf = cnfvar.read_cnf(demo_cnf_escaped_quotes)
+        with open(os.devnull, "w") as devnull:
+            cnfvar.print_cnf_json(cnf, out=devnull)
+
+    def test_parse_json_quotes(self):
+        cnf = cnfvar.read_cnf_json(demo_json_escaped_quotes)
+        with open(os.devnull, "w") as devnull:
+            cnfvar.print_cnf_json(cnf, out=devnull)
+
 
 class CnfVarUnittestVarnameCase(unittest.TestCase):
     """Tests for verifying that uppercasing/lowercasing of varname works."""