summaryrefslogtreecommitdiffstats
path: root/contrib/libucl/python/tests/test_validation.py
diff options
context:
space:
mode:
authorbapt <bapt@FreeBSD.org>2016-10-22 20:49:07 +0000
committerbapt <bapt@FreeBSD.org>2016-10-22 20:49:07 +0000
commitffafe3ab704ae77aebac04aed2a2f970475312d8 (patch)
tree1c03c20541f231404e89ca3807016bc85a56cb7d /contrib/libucl/python/tests/test_validation.py
parent62cb4b26b932c652b62b8eba73976e4b9584ec97 (diff)
downloadFreeBSD-src-ffafe3ab704ae77aebac04aed2a2f970475312d8.zip
FreeBSD-src-ffafe3ab704ae77aebac04aed2a2f970475312d8.tar.gz
MFC r306544:
Import libucl 20160812
Diffstat (limited to 'contrib/libucl/python/tests/test_validation.py')
-rw-r--r--contrib/libucl/python/tests/test_validation.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/contrib/libucl/python/tests/test_validation.py b/contrib/libucl/python/tests/test_validation.py
new file mode 100644
index 0000000..f7c853a
--- /dev/null
+++ b/contrib/libucl/python/tests/test_validation.py
@@ -0,0 +1,50 @@
+from .compat import unittest
+import ucl
+import json
+import os.path
+import glob
+import re
+
+TESTS_SCHEMA_FOLDER = '../tests/schema/*.json'
+
+comment_re = re.compile('\/\*((?!\*\/).)*?\*\/', re.DOTALL | re.MULTILINE)
+def json_remove_comments(content):
+ return comment_re.sub('', content)
+
+class ValidationTest(unittest.TestCase):
+ def validate(self, jsonfile):
+ def perform_test(schema, data, valid, description):
+ msg = '%s (valid=%r)' % (description, valid)
+ if valid:
+ self.assertTrue(ucl.validate(schema, data), msg)
+ else:
+ with self.assertRaises(ucl.SchemaError):
+ ucl.validate(schema, data)
+ self.fail(msg) # fail() will be called only if SchemaError is not raised
+
+ with open(jsonfile) as f:
+ try:
+ # data = json.load(f)
+ data = json.loads(json_remove_comments(f.read()))
+ except ValueError as e:
+ raise self.skipTest('Failed to load JSON: %s' % str(e))
+
+ for testgroup in data:
+ for test in testgroup['tests']:
+ perform_test(testgroup['schema'], test['data'],
+ test['valid'], test['description'])
+
+ @classmethod
+ def setupValidationTests(cls):
+ """Creates each test dynamically from a folder"""
+ def test_gen(filename):
+ def run_test(self):
+ self.validate(filename)
+ return run_test
+
+ for jsonfile in glob.glob(TESTS_SCHEMA_FOLDER):
+ testname = os.path.splitext(os.path.basename(jsonfile))[0]
+ setattr(cls, 'test_%s' % testname, test_gen(jsonfile))
+
+
+ValidationTest.setupValidationTests()
OpenPOWER on IntegriCloud