diff --git a/x/logic/predicate/json_test.go b/x/logic/predicate/json_test.go index c2af4dc4..dac1b61c 100644 --- a/x/logic/predicate/json_test.go +++ b/x/logic/predicate/json_test.go @@ -147,6 +147,14 @@ func TestJsonProlog(t *testing.T) { }, // ** JSON -> Prolog ** // Array + { + description: "convert empty json array into prolog", + query: `json_prolog('[]', Term).`, + wantResult: []types.TermResults{{ + "Term": "@([])", + }}, + wantSuccess: true, + }, { description: "convert json array into prolog", query: `json_prolog('["foo", "bar"]', Term).`, @@ -155,6 +163,14 @@ func TestJsonProlog(t *testing.T) { }}, wantSuccess: true, }, + { + description: "convert json array with null element into prolog", + query: `json_prolog('[null]', Term).`, + wantResult: []types.TermResults{{ + "Term": "[@(null)]", + }}, + wantSuccess: true, + }, { description: "convert json string array into prolog", query: `json_prolog('["string with space", "bar"]', Term).`, @@ -174,6 +190,22 @@ func TestJsonProlog(t *testing.T) { }}, wantSuccess: true, }, + { + description: "convert atom term to json", + query: `json_prolog(Json, foo).`, + wantResult: []types.TermResults{{ + "Json": "'\"foo\"'", + }}, + wantSuccess: true, + }, + { + description: "convert string with space to json", + query: `json_prolog(Json, 'foo bar').`, + wantResult: []types.TermResults{{ + "Json": "'\"foo bar\"'", + }}, + wantSuccess: true, + }, { description: "convert string with space to json", query: `json_prolog(Json, 'foo bar').`, @@ -182,6 +214,14 @@ func TestJsonProlog(t *testing.T) { }}, wantSuccess: true, }, + { + description: "convert empty-list atom term to json", + query: `json_prolog(Json, []).`, + wantResult: []types.TermResults{{ + "Json": "'\"[]\"'", + }}, + wantSuccess: true, + }, // ** Prolog -> JSON ** // Object { @@ -252,6 +292,14 @@ func TestJsonProlog(t *testing.T) { }, // ** Prolog -> Json ** // Array + { + description: "convert empty json array from prolog", + query: `json_prolog(Json, @([])).`, + wantResult: []types.TermResults{{ + "Json": "[]", + }}, + wantSuccess: true, + }, { description: "convert json array from prolog", query: `json_prolog(Json, [foo,bar]).`, @@ -260,6 +308,14 @@ func TestJsonProlog(t *testing.T) { }}, wantSuccess: true, }, + { + description: "convert json array with null element from prolog", + query: `json_prolog(Json, [@(null)]).`, + wantResult: []types.TermResults{{ + "Json": "'[null]'", + }}, + wantSuccess: true, + }, { description: "convert json string array from prolog", query: `json_prolog(Json, ['string with space',bar]).`, @@ -340,7 +396,6 @@ func TestJsonProlog(t *testing.T) { So(sols.Err(), ShouldBeNil) if tc.wantSuccess { - So(len(got), ShouldBeGreaterThan, 0) So(len(got), ShouldEqual, len(tc.wantResult)) for iGot, resultGot := range got { for varGot, termGot := range resultGot { @@ -404,6 +459,11 @@ func TestJsonPrologWithMoreComplexStructBidirectional(t *testing.T) { term: "json([a-b])", wantSuccess: false, }, + { + json: `'{"key1":null,"key2":[],"key3":{"nestedKey1":null,"nestedKey2":[],"nestedKey3":["a",null,null]}}'`, + term: `json([key1- @(null),key2- @([]),key3-json([nestedKey1- @(null),nestedKey2- @([]),nestedKey3-[a,@(null),@(null)]])])`, + wantSuccess: true, + }, } for nc, tc := range cases { Convey(fmt.Sprintf("#%d : given the json: %s and the term %s", nc, tc.json, tc.term), func() {