Skip to content

Commit

Permalink
test(logic): improve prolog json test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 25, 2023
1 parent 054f854 commit b530cfa
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion x/logic/predicate/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).`,
Expand All @@ -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).`,
Expand All @@ -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').`,
Expand All @@ -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
{
Expand Down Expand Up @@ -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]).`,
Expand All @@ -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]).`,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit b530cfa

Please sign in to comment.