Browse Source

[tests] add serializer tests

closes #4698
Simon Krajewski 7 years ago
parent
commit
3526d60168

+ 1 - 0
tests/unit/compile-each.hxml

@@ -4,6 +4,7 @@
 # -cp "C:\Program Files\The Haxe Effect\src/dev/null"
 --resource res1.txt@re/s?!%[]))("'1.txt
 --resource res2.bin@re/s?!%[]))("'1.bin
+--resource serializedValues.txt
 --dce full
 -D analyzer-optimize
 -D analyzer-user-var-fusion

+ 1 - 0
tests/unit/serializedValues.txt

@@ -0,0 +1 @@
+{"int0":"z","int1":"i1","intMap01":"q:0y1:1h","intMapEmpty":"qh","boolFalse":"f","intMap0112":"q:0y1:1:1y1:2h","floatNaN":"k","objectMapEmpty":"Mh","stringMapFooBar":"by3:fooy3:barh","arrayEmpty":"ah","stringMapEmpty":"bh","intMinus1":"i-1","nul":"n","stringMapFooNull":"by3:foonh","floatNegInf":"m","arrayNull":"anh","floatPosInf":"p","boolTrue":"t","array0":"azh","array01":"azi1h"}

+ 51 - 0
tests/unit/src/RunSerializerOutput.hx

@@ -0,0 +1,51 @@
+import haxe.Serializer.run;
+
+class RunSerializerOutput {
+	static function main() {
+		new RunSerializerOutput();
+	}
+
+	var object:{};
+
+	function new() {
+		object = {};
+		add("nul", null);
+		add("int0", 0);
+		add("intMinus1", -1);
+		add("int1", 1);
+		add("floatNaN", Math.NaN);
+		add("floatPosInf", Math.POSITIVE_INFINITY);
+		add("floatNegInf", Math.NEGATIVE_INFINITY);
+		add("boolTrue", true);
+		add("boolFalse", false);
+		add("arrayEmpty", []);
+		add("array0", [0]);
+		add("arrayNull", [null]);
+		add("array01", [0, 1]);
+
+		var smap = new Map<String, String>();
+		add("stringMapEmpty", smap);
+		smap["foo"] = null;
+		add("stringMapFooNull", smap);
+		smap["foo"] = "bar";
+		add("stringMapFooBar", smap);
+
+		var imap = new Map<Int, String>();
+		add("intMapEmpty", imap);
+		imap[0] = "1";
+		add("intMap01", imap);
+		imap[1] = "2";
+		add("intMap0112", imap);
+
+		var omap = new Map<{}, String>();
+		add("objectMapEmpty", omap);
+		// TODO
+
+		var s = haxe.Json.stringify(object);
+		sys.io.File.saveContent("../serializedValues.txt", s);
+	}
+
+	function add<T>(key:String, value:T) {
+		Reflect.setField(object, key, run(value));
+	}
+}

+ 1 - 0
tests/unit/src/unit/TestMain.hx

@@ -57,6 +57,7 @@ class TestMain {
 			new TestInt64(),
 			new TestReflect(),
 			new TestSerialize(),
+			new TestSerializerCrossTarget(),
 			new TestMeta(),
 			new TestType(),
 			new TestOrder(),

+ 1 - 1
tests/unit/src/unit/TestResource.hx

@@ -5,7 +5,7 @@ class TestResource extends Test {
 	static var STR = "Héllo World !";
 
 	function testResources() {
-		var names = haxe.Resource.listNames();
+		var names = haxe.Resource.listNames().filter(function(name) return name != "serializedValues.txt");
 		eq( names.length, 2 );
 		if( names[0] == "re/s?!%[]))(\"'1.txt" ) {
 			 // redundant, but let's avoid different test numbers

+ 109 - 0
tests/unit/src/unit/TestSerializerCrossTarget.hx

@@ -0,0 +1,109 @@
+package unit;
+import haxe.Unserializer;
+import haxe.Resource;
+import haxe.Json;
+
+class TestSerializerCrossTarget extends Test {
+	function testCrossTarget() {
+		var values = Resource.getString("serializedValues.txt");
+		var json = Json.parse(values);
+		for (fieldName in Reflect.fields(json)) {
+			var valueString = Reflect.field(json, fieldName);
+			var func = Reflect.field(this, fieldName);
+			if (func == null) {
+				Test.report("Missing field: " + fieldName);
+				Test.success = false;
+			} else {
+				Reflect.callMethod(this, func, [Unserializer.run(valueString)]);
+			}
+		}
+	}
+
+	function nul<T>(value:T) {
+		eq(null, value);
+	}
+
+	function int0(value:Int) {
+		eq(0, value);
+	}
+
+	function int1(value:Int) {
+		eq(1, value);
+	}
+
+	function intMinus1(value:Int) {
+		eq(-1, value);
+	}
+
+	function floatNaN(value:Float) {
+		t(Math.isNaN(value));
+	}
+
+	function floatNegInf(value:Float) {
+		f(Math.isFinite(value));
+		t(value < 0);
+	}
+
+	function floatPosInf(value:Float) {
+		f(Math.isFinite(value));
+		t(value > 0);
+	}
+
+	function boolFalse(value:Bool) {
+		f(value);
+	}
+
+	function boolTrue(value:Bool) {
+		t(value);
+	}
+
+	function arrayEmpty<T>(value:Array<T>) {
+		eq(0, value.length);
+	}
+
+	function arrayNull<T>(value:Array<T>) {
+		eq(1, value.length);
+		eq(null, value[0]);
+	}
+
+	function array0(value:Array<Int>) {
+		eq(1, value.length);
+		eq(0, value[0]);
+	}
+
+	function array01(value:Array<Int>) {
+		eq(2, value.length);
+		eq(0, value[0]);
+		eq(1, value[1]);
+	}
+
+	function stringMapEmpty(value:Map<String, String>) {
+		f(value.keys().hasNext());
+	}
+
+	function stringMapFooNull(value:Map<String, String>) {
+		eq(null, value["foo"]);
+	}
+
+	function stringMapFooBar(value:Map<String, String>) {
+		eq("bar", value["foo"]);
+	}
+
+	function intMapEmpty(value:Map<Int, String>) {
+		f(value.keys().hasNext());
+	}
+
+	function intMap01(value:Map<Int, String>) {
+		eq("1", value[0]);
+	}
+
+	function intMap0112(value:Map<Int, String>) {
+		eq("1", value[0]);
+		eq("2", value[1]);
+	}
+
+	function objectMapEmpty(value:Map<{}, String>) {
+		var keys = value.keys();
+		f(keys.hasNext());
+	}
+}

+ 1 - 0
tests/unit/unit.hxproj

@@ -60,6 +60,7 @@
     <hidden path="runjava.n" />
     <hidden path="server.bat" />
     <hidden path="obj" />
+    <hidden path="serializedValues.txt" />
   </hiddenPaths>
   <!-- Executed before build -->
   <preBuildCommand>"$(CompilerPath)/haxe.exe" compile$(TargetBuild).hxml</preBuildCommand>