tests: add formal types to serialization tests
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 22 Jul 2017 12:39:57 +0000 (08:39 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 26 Jul 2017 15:03:45 +0000 (11:03 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

tests/sav/nitce/test_json_deserialization_safe.res [new file with mode: 0644]
tests/sav/test_json_deserialization_safe.res
tests/sav/test_json_deserialization_safe_alt1.res [deleted file]
tests/test_json_deserialization_safe.nit

diff --git a/tests/sav/nitce/test_json_deserialization_safe.res b/tests/sav/nitce/test_json_deserialization_safe.res
new file mode 100644 (file)
index 0000000..fc7aae9
--- /dev/null
@@ -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 ``
+<A other:null next:null>
+---
+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`
+<A other:null next:null>
+---
+Deserialization Error: `B` is not a subtype of the static type ``
+<B other:null next:null>
+---
+Deserialization Error: `A` is not a subtype of the static type ``
+<B other:null next:null>
+---
+Deserialization Error: `A` is not a subtype of the static type ``
+Deserialization Error: Wrong type on `G::e` expected `E`, got `null`
+<G e:not-set>
+---
+Deserialization Error: `A` is not a subtype of the static type ``
+Deserialization Error: Wrong type on `G::e` expected `E`, got `null`
+<G e:not-set>
index e429d54..7d64cc0 100644 (file)
@@ -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`
+<A other:null next:null>
+---
+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`
+<A other:null next:null>
+---
+
+<B other:null next:<B other:null next:null>>
+---
+Deserialization Error: `A` is not a subtype of the static type `nullable B`
+<B other:null next:null>
+---
+
+<G[A] e:<A other:null next:null>>
+---
+Deserialization Error: `A` is not a subtype of the static type `B`
+Deserialization Error: Wrong type on `G[B]::e` expected `E`, got `null`
+<G[B] e:not-set>
diff --git a/tests/sav/test_json_deserialization_safe_alt1.res b/tests/sav/test_json_deserialization_safe_alt1.res
deleted file mode 100644 (file)
index 66757ac..0000000
+++ /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`
index 22eaf87..65bf393 100644 (file)
 
 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"