Browse Source

add tests for the replacer argument of Json.stringify

Dan Korostelev 9 years ago
parent
commit
df9f24e232
1 changed files with 31 additions and 0 deletions
  1. 31 0
      tests/unit/src/unit/TestJson.hx

+ 31 - 0
tests/unit/src/unit/TestJson.hx

@@ -12,6 +12,22 @@ class TestJson extends Test {
 		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
 		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
 		eq( parts.join("#"), "" );
 		eq( parts.join("#"), "" );
 
 
+		var str = haxe.Json.stringify(null, function(k,v) {
+			eq(k, "");
+			return "some";
+		});
+		eq(str, '"some"');
+
+		var keys = [];
+		var str = haxe.Json.stringify({a: {b: 10}}, function(k,v) {
+			keys.push(k);
+			if (k == "b") v = 15;
+			return v;
+		});
+		aeq(["", "a", "b"], keys);
+		eq(str, '{"a":{"b":15}}');
+
+
 		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v, pos);
 		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v, pos);
 		function deepId(v:Dynamic) {
 		function deepId(v:Dynamic) {
 			var str = haxe.Json.stringify(v);
 			var str = haxe.Json.stringify(v);
@@ -62,6 +78,21 @@ class TestJson extends Test {
 		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
 		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
 		eq( parts.join("#"), "" );
 		eq( parts.join("#"), "" );
 
 
+		var str = haxe.format.JsonPrinter.print(null, function(k,v) {
+			eq(k, "");
+			return "some";
+		});
+		eq(str, '"some"');
+
+		var keys = [];
+		var str = haxe.format.JsonPrinter.print({a: {b: 10}}, function(k,v) {
+			keys.push(k);
+			if (k == "b") v = 15;
+			return v;
+		});
+		aeq(["", "a", "b"], keys);
+		eq(str, '{"a":{"b":15}}');
+
 		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.format.JsonParser.parse(haxe.format.JsonPrinter.print(v)),v);
 		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.format.JsonParser.parse(haxe.format.JsonPrinter.print(v)),v);
 		function deepId(v:Dynamic) {
 		function deepId(v:Dynamic) {
 			var str = haxe.format.JsonPrinter.print(v);
 			var str = haxe.format.JsonPrinter.print(v);