summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTina Müller <cpan2@tinita.de>2020-05-20 23:24:39 +0200
committerTina Müller <cpan2@tinita.de>2020-05-21 00:54:53 +0200
commit3d64fe99f7bdbc343cd1121146e631b93c5955ba (patch)
treef42e0ee483be8ca3531b506671b3c37b9a82d2f3
parentd0d660d035905d9c49fc0f8dafb579d2cc68c0c8 (diff)
downloadpyyaml-git-perlpunk/schema.tar.gz
Add a test for schemaperlpunk/schema
-rw-r--r--tests/data/yaml11.schema1
-rw-r--r--tests/lib3/test_schema.py147
-rw-r--r--tests/lib3/test_yaml.py2
3 files changed, 150 insertions, 0 deletions
diff --git a/tests/data/yaml11.schema b/tests/data/yaml11.schema
new file mode 100644
index 0000000..ea12169
--- /dev/null
+++ b/tests/data/yaml11.schema
@@ -0,0 +1 @@
+{"!!str +.Inf":["str","+.Inf","'+.Inf'"],"!!float -.Inf":["inf","inf-neg()","-.inf"],"!!str 85.230_15e+03":["str","85.230_15e+03","'85.230_15e+03'"],"0x10":["int","16","16"],"!!str .nan":["str",".nan","'.nan'"],"!!str True":["str","True","'True'"],"190:20:30":["int","685230","685230"],".NaN":["nan","nan()",".nan"],"!!str +23":["str","+23","'+23'"],"!!str NULL":["str","NULL","'NULL'"],"!!str -.INF":["str","-.INF","'-.INF'"],"!!bool False":["bool","false()","false"],"!!str ~":["str","~","'~'"],"!!str Null":["str","Null","'Null'"],"0x0":["int","0","0"],"!!str +0":["str","+0","'+0'"],"010":["int","8","8"],"!!int -0100_200":["int","-32896","-32896"],"-190:20:30":["int","-685230","-685230"],"!!null null":["null","null()","null"],"190:20:30.15":["float","685230.15","685230.15"],"!!str 85_230.15":["str","85_230.15","'85_230.15'"],"!!float .inf":["inf","inf()",".inf"],"!!str .3E-1":["str",".3E-1","'.3E-1'"],"!!int +0b100":["int","4","4"],"0x2_0":["int","32","32"],"yes":["bool","true()","true"],"!!str .inf":["str",".inf","'.inf'"],"!!int 0x42":["int","66","66"],"Yes":["bool","true()","true"],"+190:20:30":["int","685230","685230"],"!!int 0011":["int","9","9"],"+0.3e3":["str","+0.3e3","+0.3e3"],"0.3e3":["str","0.3e3","0.3e3"],"!!str TRUE":["str","TRUE","'TRUE'"],"!!int 0":["int","0","0"],"!!str 190:20:30.15":["str","190:20:30.15","'190:20:30.15'"],"!!str +0b100":["str","+0b100","'+0b100'"],"!!str 00":["str","00","'00'"],"!!int -0b101":["int","-5","-5"],"off":["bool","false()","false"],".nan":["nan","nan()",".nan"],"!!float -3.14":["float","-3.14","-3.14"],"Y":["bool","true()","true"],"!!str Y":["str","Y","'Y'"],"!!str -.Inf":["str","-.Inf","'-.Inf'"],"!!int 010":["int","8","8"],"!!str 0011":["str","0011","'0011'"],"!!int 190:20:30":["int","685230","685230"],"!!float .3E-1":["float","0.03","0.03"],"!!str .3e+3":["str",".3e+3","'.3e+3'"],"!!int 100_000":["int","100000","100000"],"!!str 0b0":["str","0b0","'0b0'"],"!!str on":["str","on","'on'"],"!!str 001.23":["str","001.23","'001.23'"],"0b0":["int","0","0"],"!!float -.inf":["inf","inf-neg()","-.inf"],".NAN":["nan","nan()",".nan"],"!!float +.inf":["inf","inf()",".inf"],"3.3e+3":["float","3300","3300.0"],"!!bool Off":["bool","false()","false"],"85_230.15":["float","85230.15","85230.15"],"!!str 3.3e+3":["str","3.3e+3","'3.3e+3'"],"N":["bool","false()","false"],"null":["null","null()","null"],"!!str 0x2_0":["str","0x2_0","'0x2_0'"],"!!str .0":["str",".0","'.0'"],"!!bool Y":["bool","true()","true"],"!!bool True":["bool","true()","true"],"-0b101":["int","-5","-5"],"!!str 0b100_101":["str","0b100_101","'0b100_101'"],"NO":["bool","false()","false"],"85.230_15e+03":["float","85230.15","85230.15"],"08":["str","08","08"],"!!bool FALSE":["bool","false()","false"],"+.Inf":["inf","inf()",".inf"],"0xa":["int","10","10"],"!!int 0xa":["int","10","10"],"-0":["int","0","0"],"!!str 0x0":["str","0x0","'0x0'"],"!!str -190:20:30":["str","-190:20:30","'-190:20:30'"],"-0x30":["int","-48","-48"],"!!null Null":["null","null()","null"],"!!bool YES":["bool","true()","true"],"!!int 0b100_101":["int","37","37"],"!!str NO":["str","NO","'NO'"],"!!bool true":["bool","true()","true"],"True":["bool","true()","true"],"-0100_200":["int","-32896","-32896"],"!!float .INF":["inf","inf()",".inf"],"!!bool No":["bool","false()","false"],"!!str -0b101":["str","-0b101","'-0b101'"],"n":["bool","false()","false"],"!!float +0.3e+3":["float","300.0","300.0"],"!!str +0.3e3":["str","+0.3e3","+0.3e3"],"!!bool off":["bool","false()","false"],"!!float 190:20:30.15":["float","685230.15","685230.15"],"!!float 3.14":["float","3.14","3.14"],"0b100_101":["int","37","37"],"!!bool ON":["bool","true()","true"],"!!str -0x30":["str","-0x30","'-0x30'"],"!!str 0.0":["str","0.0","'0.0'"],"!!float .0":["float","0.0","0.0"],"0x42":["int","66","66"],".INF":["inf","inf()",".inf"],"!!str -.inf":["str","-.inf","'-.inf'"],".inf":["inf","inf()",".inf"],"TRUE":["bool","true()","true"],"!!int 0x2_0":["int","32","32"],"!!str YES":["str","YES","'YES'"],".Inf":["inf","inf()",".inf"],"!!str 010":["str","010","'010'"],"!!int 0x10":["int","16","16"],"!!int 0b0":["int","0","0"],"!!float +.Inf":["inf","inf()",".inf"],"!!null NULL":["null","null()","null"],"!!float -.INF":["inf","inf-neg()","-.inf"],"!!str -23":["str","-23","'-23'"],"02_0":["int","16","16"],"!!str +190:20:30":["str","+190:20:30","'+190:20:30'"],"!!str 0o7":["str","0o7","0o7"],"3e3":["str","3e3","3e3"],"!!null ~":["null","null()","null"],"!!str 100_000":["str","100_000","'100_000'"],"!!float .3e+3":["float","300.0","300.0"],"!!float 0.0":["float","0.0","0.0"],"!!str 0o10":["str","0o10","0o10"],"+.inf":["inf","inf()",".inf"],"-.INF":["inf","inf-neg()","-.inf"],"0011":["int","9","9"],"!!str ON":["str","ON","'ON'"],"!!str +.inf":["str","+.inf","'+.inf'"],"!!float .NAN":["nan","nan()",".nan"],"!!str yes":["str","yes","'yes'"],"001.23":["float","1.23","1.23"],"-3.14":["float","-3.14","-3.14"],"!!bool y":["bool","true()","true"],"!!str .Inf":["str",".Inf","'.Inf'"],"!!str Off":["str","Off","'Off'"],"!!str 0":["str","0","'0'"],"On":["bool","true()","true"],"!!int -23":["int","-23","-23"],"!!str 3.14":["str","3.14","'3.14'"],"!!int -0":["int","0","0"],"!!bool Yes":["bool","true()","true"],"!!str FALSE":["str","FALSE","'FALSE'"],"!!int 02_0":["int","16","16"],"!!str -3.14":["str","-3.14","'-3.14'"],"!!null #empty":["null","null()","null"],"!!bool n":["bool","false()","false"],"!!str .3e3":["str",".3e3",".3e3"],".14":["float","0.14","0.14"],"!!str +0.3e+3":["str","+0.3e+3","'+0.3e+3'"],"!!str true":["str","true","'true'"],"NULL":["null","null()","null"],"!!str 0.3e3":["str","0.3e3","0.3e3"],"-23":["int","-23","-23"],"!!str +0100_200":["str","+0100_200","'+0100_200'"],"true":["bool","true()","true"],"!!str false":["str","false","'false'"],"!!str False":["str","False","'False'"],"YES":["bool","true()","true"],"07":["int","7","7"],"!!float 001.23":["float","1.23","1.23"],"on":["bool","true()","true"],"false":["bool","false()","false"],"!!str .NAN":["str",".NAN","'.NAN'"],"!!str 02_0":["str","02_0","'02_0'"],"!!float 3.3e+3":["float","3300.0","3300.0"],"+0100_200":["int","32896","32896"],"!!float .NaN":["nan","nan()",".nan"],"!!str Yes":["str","Yes","'Yes'"],"+0b100":["int","4","4"],"Null":["null","null()","null"],"!!str null":["str","null","'null'"],".3E-1":["float","0.03","0.03"],"ON":["bool","true()","true"],"!!str 190:20:30":["str","190:20:30","'190:20:30'"],"!!str 0xa":["str","0xa","'0xa'"],"!!str .NaN":["str",".NaN","'.NaN'"],"!!int 07":["int","7","7"],"!!str -0100_200":["str","-0100_200","'-0100_200'"],"0":["int","0","0"],"False":["bool","false()","false"],"!!bool false":["bool","false()","false"],"#empty":["null","null()","null"],"100_000":["int","100000","100000"],"!!int +23":["int","23","23"],"!!str y":["str","y","'y'"],"!!int +0100_200":["int","32896","32896"],"!!bool TRUE":["bool","true()","true"],"!!bool yes":["bool","true()","true"],"!!str off":["str","off","'off'"],"!!str 07":["str","07","'07'"],"!!bool On":["bool","true()","true"],"!!int 00":["int","0","0"],"0o7":["str","0o7","0o7"],"!!str -0":["str","-0","'-0'"],"!!bool on":["bool","true()","true"],"+.INF":["inf","inf()",".inf"],"!!int -0x30":["int","-48","-48"],"+3.14":["float","3.14","3.14"],"!!bool N":["bool","false()","false"],"!!str no":["str","no","'no'"],"!!str 23":["str","23","'23'"],"0o10":["str","0o10","0o10"],"!!str n":["str","n","'n'"],"~":["null","null()","null"],"00":["int","0","0"],"!!int +190:20:30":["int","685230","685230"],".3e3":["str",".3e3",".3e3"],"!!float .nan":["nan","nan()",".nan"],"FALSE":["bool","false()","false"],"OFF":["bool","false()","false"],"+0.3e+3":["float","300.0","300.0"],"!!float 85.230_15e+03":["float","85230.15","85230.15"],"Off":["bool","false()","false"],"!!int +0":["int","0","0"],"-.inf":["inf","inf-neg()","-.inf"],"!!str +.INF":["str","+.INF","'+.INF'"],"!!int -190:20:30":["int","-685230","-685230"],".3e+3":["float","300.0","300.0"],"!!bool no":["bool","false()","false"],"no":["bool","false()","false"],".0":["float","0.0","0.0"],"!!bool OFF":["bool","false()","false"],"!!str On":["str","On","'On'"],"0o0":["str","0o0","0o0"],"!!str N":["str","N","'N'"],"y":["bool","true()","true"],"!!str 0o0":["str","0o0","0o0"],"!!float 85_230.15":["float","85230.15","85230.15"],"3.14":["float","3.14","3.14"],"!!float .Inf":["inf","inf()",".inf"],"-.Inf":["inf","inf-neg()","-.inf"],"!!str OFF":["str","OFF","'OFF'"],"!!int 23":["int","23","23"],"!!str #empty":["str","","''"],"!!int 0x0":["int","0","0"],"23":["int","23","23"],"0.0":["float","0.0","0.0"],"+23":["int","23","23"],"!!bool NO":["bool","false()","false"],"+0":["int","0","0"],"!!str .INF":["str",".INF","'.INF'"],"!!float +.INF":["inf","inf()",".inf"]} \ No newline at end of file
diff --git a/tests/lib3/test_schema.py b/tests/lib3/test_schema.py
new file mode 100644
index 0000000..472565c
--- /dev/null
+++ b/tests/lib3/test_schema.py
@@ -0,0 +1,147 @@
+import yaml
+import sys
+import pprint
+
+def check_bool(value, expected):
+ if expected == 'false()' and value is False:
+ return 1
+ if expected == 'true()' and value is True:
+ return 1
+ print(value)
+ print(expected)
+ return 0
+
+def check_int(value, expected):
+ if (int(expected) == value):
+ return 1
+ print(value)
+ print(expected)
+ return 0
+
+def check_float(value, expected):
+ if expected == 'inf()':
+ if value == float("inf"):
+ return 1
+ elif expected == 'inf-neg()':
+ if value == float("-inf"):
+ return 1
+ elif expected == 'nan()':
+ if value != value or (value == 0.0 and value == 1.0):
+ return 1
+ elif (float(expected) == value):
+ return 1
+ else:
+ print(value)
+ print(expected)
+ return 0
+
+def check_str(value, expected):
+ if value == expected:
+ return 1
+ print(value)
+ print(expected)
+ return 0
+
+
+def _fail(input, test):
+ print("Input: >>" + input + "<<");
+ print(test);
+
+def test_implicit_resolver(data_filename, verbose=False):
+ skip = {
+ 'Y': 1, 'y': 1, 'N': 1, 'n': 1,
+ '!!bool Y': 1, '!!bool N': 1, '!!bool n': 1, '!!bool y': 1,
+ }
+ skip_dump = {
+ '!!str N': 1, '!!str Y': 1, '!!str n': 1, '!!str y': 1,
+ }
+ types = {
+ 'str': [str, check_str],
+ 'int': [int, check_int],
+ 'float': [float, check_float],
+ 'inf': [float, check_float],
+ 'nan': [float, check_float],
+ 'bool': [bool, check_bool],
+ }
+ if verbose:
+ print(skip)
+ tests = yaml.load(open(data_filename, 'rb'), Loader=yaml.SafeLoader)
+
+ i = 0;
+ fail = 0;
+ for input, test in sorted(tests.items()):
+ if verbose:
+ print('-------------------- ' + str(i))
+ test = tests[input]
+ i += 1
+ if input in skip:
+ continue
+ exp_type = test[0];
+ data = test[1];
+ exp_dump = test[2];
+
+ try:
+ loaded = yaml.safe_load(input)
+ except:
+ print("Error:", sys.exc_info()[0])
+ fail+=1
+ _fail(input, test)
+ continue
+
+ if verbose:
+ print(input)
+ print(test)
+ print(loaded)
+ print(type(loaded))
+
+ if exp_type == 'null':
+ if loaded is None:
+ pass
+ else:
+ fail+=1
+ _fail(input, test)
+ else:
+ t = types[exp_type][0]
+ code = types[exp_type][1]
+ if isinstance(loaded, t):
+ if code(loaded, data):
+ pass
+ else:
+ fail+=1
+ _fail(input, test)
+ else:
+ fail+=1
+ _fail(input, test)
+
+ if input in skip_dump:
+ continue
+
+ dump = yaml.safe_dump(loaded, explicit_end=False)
+ if dump.endswith('\n...\n'):
+ dump = dump[:-5]
+ if dump.endswith('\n'):
+ dump = dump[:-1]
+ if dump == exp_dump:
+ pass
+ else:
+ print("Compare: >>" + dump + "<< >>" + exp_dump + "<<");
+ fail+=1
+ _fail(input, test)
+
+# if i >= 80:
+# break
+
+ if fail > 0:
+ print("Failed " + str(fail) + " / " + str(i) + " tests");
+ assert(False)
+ else:
+ print("Passed " + str(i) + " tests");
+ print("Skipped " + str(len(skip)) + " load tests");
+ print("Skipped " + str(len(skip_dump)) + " dump tests");
+
+test_implicit_resolver.unittest = ['.schema']
+
+if __name__ == '__main__':
+ import test_appliance
+ test_appliance.run(globals())
+
diff --git a/tests/lib3/test_yaml.py b/tests/lib3/test_yaml.py
index 352cd8d..7b3d8f9 100644
--- a/tests/lib3/test_yaml.py
+++ b/tests/lib3/test_yaml.py
@@ -14,6 +14,8 @@ from test_input_output import *
from test_sort_keys import *
from test_multi_constructor import *
+from test_schema import *
+
if __name__ == '__main__':
import test_appliance
test_appliance.run(globals())