From 1f6178fd0ced5def8ac4a356cac4a04068782dd0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 22 Jul 2017 08:39:57 -0400 Subject: [PATCH] tests: add formal types to serialization tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- tests/sav/nitce/test_json_deserialization_safe.res | 23 ++++++ tests/sav/test_json_deserialization_safe.res | 26 +++++- tests/sav/test_json_deserialization_safe_alt1.res | 4 - tests/test_json_deserialization_safe.nit | 87 ++++++++++++++++++-- 4 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 tests/sav/nitce/test_json_deserialization_safe.res delete mode 100644 tests/sav/test_json_deserialization_safe_alt1.res diff --git a/tests/sav/nitce/test_json_deserialization_safe.res b/tests/sav/nitce/test_json_deserialization_safe.res new file mode 100644 index 0000000..fc7aae9 --- /dev/null +++ b/tests/sav/nitce/test_json_deserialization_safe.res @@ -0,0 +1,23 @@ +Deserialization Error: `DangerSub` is not a subtype of the static type `nullable A` +Deserialization Error: `DangerSub` is not a subtype of the static type `` + +--- +DANGER 'My text 1' +DANGER 'My text 2' +Deserialization Error: Wrong type on `A::other` expected `nullable A`, got `DangerSub` +Deserialization Error: Wrong type on `A::next` expected `C`, got `DangerSub` + +--- +Deserialization Error: `B` is not a subtype of the static type `` + +--- +Deserialization Error: `A` is not a subtype of the static type `` + +--- +Deserialization Error: `A` is not a subtype of the static type `` +Deserialization Error: Wrong type on `G::e` expected `E`, got `null` + +--- +Deserialization Error: `A` is not a subtype of the static type `` +Deserialization Error: Wrong type on `G::e` expected `E`, got `null` + diff --git a/tests/sav/test_json_deserialization_safe.res b/tests/sav/test_json_deserialization_safe.res index e429d54..7d64cc0 100644 --- a/tests/sav/test_json_deserialization_safe.res +++ b/tests/sav/test_json_deserialization_safe.res @@ -1,4 +1,22 @@ -Deserialization Error: `DangerSub` is not a subtype of the static type `MyClass` -Deserialization Error: Wrong type on `MyClass::other` expected `MyClass`, got `null` -Deserialization Error: `DangerSub` is not a subtype of the static type `MyClass` -Deserialization Error: Wrong type on `MyClass::next` expected `MyClass`, got `null` +Deserialization Error: `DangerSub` is not a subtype of the static type `nullable A` +Deserialization Error: `DangerSub` is not a subtype of the static type `nullable A` + +--- +DANGER 'My text 1' +DANGER 'My text 2' +Deserialization Error: Wrong type on `A::other` expected `nullable A`, got `DangerSub` +Deserialization Error: Wrong type on `A::next` expected `C`, got `DangerSub` + +--- + +> +--- +Deserialization Error: `A` is not a subtype of the static type `nullable B` + +--- + +> +--- +Deserialization Error: `A` is not a subtype of the static type `B` +Deserialization Error: Wrong type on `G[B]::e` expected `E`, got `null` + diff --git a/tests/sav/test_json_deserialization_safe_alt1.res b/tests/sav/test_json_deserialization_safe_alt1.res deleted file mode 100644 index 66757ac..0000000 --- a/tests/sav/test_json_deserialization_safe_alt1.res +++ /dev/null @@ -1,4 +0,0 @@ -DANGER 'My text 1' -DANGER 'My text 2' -Deserialization Error: Wrong type on `MyClass::other` expected `MyClass`, got `DangerSub` -Deserialization Error: Wrong type on `MyClass::next` expected `MyClass`, got `DangerSub` diff --git a/tests/test_json_deserialization_safe.nit b/tests/test_json_deserialization_safe.nit index 22eaf87..65bf393 100644 --- a/tests/test_json_deserialization_safe.nit +++ b/tests/test_json_deserialization_safe.nit @@ -14,13 +14,22 @@ import json -class MyClass +class A serialize - var other: MyClass + var other: nullable A - type C: MyClass + type C: nullable A var next: C + + redef fun to_s do return "<{class_name} other:{other or else "null"} next:{next or else "null"}>" +end + +class B + super A + serialize + + redef type C: nullable B end class DangerSup @@ -40,13 +49,75 @@ class DangerSub end end -var json_with_metadata = """{ - "__class": "MyClass", +class G[E: A] + serialize + var e: E + + redef fun to_s do return "<{class_name} e:{if isset _e then e.to_s else "not-set"}>" +end + +redef class Deserializer + redef fun deserialize_class(name) + do + if name == "G[A]" then return new G[A].from_deserializer(self) + if name == "G[B]" then return new G[B].from_deserializer(self) + return super + end +end + +var json = """{ + "__class": "A", "other": {"__class": "DangerSub", "dangerous_setter": "My text 1"}, "next": {"__class": "DangerSub", "dangerous_setter": "My text 2"} }""" -var deserializer = new JsonDeserializer(json_with_metadata) -#alt1#deserializer.check_subtypes = false -deserializer.deserialize +# OK, accept only valid types, so don't try to deserialize DangerSub +var deserializer = new JsonDeserializer(json) +var res = deserializer.deserialize +print deserializer.errors.join("\n") +print res or else "null" + +print "---" + +# Accept any types, so deserialize the dangerous classes +deserializer = new JsonDeserializer(json) +deserializer.check_subtypes = false +res = deserializer.deserialize +print deserializer.errors.join("\n") +print res or else "null" + +print "---" + +# Valid virtual type for `next: B` +json = """{"__class": "B", "next": {"__class": "B"}}""" +deserializer = new JsonDeserializer(json) +res = deserializer.deserialize +print deserializer.errors.join("\n") +print res or else "null" + +print "---" + +# Virtual type error for `next: B` +json = """{"__class": "B", "next": {"__class": "A"}}""" +deserializer = new JsonDeserializer(json) +res = deserializer.deserialize +print deserializer.errors.join("\n") +print res or else "null" + +print "---" + +# Valid parameter type for `e: A` +json = """{"__class": "G[A]", "e": {"__class": "A"}}""" +deserializer = new JsonDeserializer(json) +res = deserializer.deserialize +print deserializer.errors.join("\n") +print res or else "null" + +print "---" + +# Parameter type error for `e: B` +json = """{"__class": "G[B]", "e": {"__class": "A"}}""" +deserializer = new JsonDeserializer(json) +res = deserializer.deserialize print deserializer.errors.join("\n") +print res or else "null" -- 1.7.9.5