tests: add test_json_deserilizer_safe
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 10 Sep 2016 17:56:21 +0000 (13:56 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 14 Oct 2016 23:55:48 +0000 (19:55 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

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

diff --git a/tests/sav/test_json_deserialization_safe.res b/tests/sav/test_json_deserialization_safe.res
new file mode 100644 (file)
index 0000000..e429d54
--- /dev/null
@@ -0,0 +1,4 @@
+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`
diff --git a/tests/sav/test_json_deserialization_safe_alt1.res b/tests/sav/test_json_deserialization_safe_alt1.res
new file mode 100644 (file)
index 0000000..66757ac
--- /dev/null
@@ -0,0 +1,4 @@
+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
new file mode 100644 (file)
index 0000000..22eaf87
--- /dev/null
@@ -0,0 +1,52 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+
+class MyClass
+    serialize
+
+       var other: MyClass
+
+       type C: MyClass
+       var next: C
+end
+
+class DangerSup
+       serialize
+
+       var dangerous_setter: String
+end
+
+class DangerSub
+       super DangerSup
+       serialize
+
+       redef fun dangerous_setter=(val)
+       do
+               print "DANGER '{val}'"
+               super
+       end
+end
+
+var json_with_metadata = """{
+    "__class": "MyClass",
+    "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
+print deserializer.errors.join("\n")