Skip to content

Commit

Permalink
test(logic): put string_bytes/3 under test
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jan 6, 2024
1 parent 2e2bac2 commit 6644a9c
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions x/logic/predicate/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,125 @@ func TestReadString(t *testing.T) {
}
})
}

func TestStringBytes(t *testing.T) {
Convey("Given a test cases", t, func() {
cases := []struct {
program string
query string
wantError error
wantSuccess bool
}{
// inspired from https://github.com/SWI-Prolog/swipl-devel/blob/V9.1.21/src/Tests/core/test_string.pl#L91
{
query: "string_bytes(aap, [97, 97, 112], ascii).",
wantSuccess: true,
},
{
program: `test :- string_bytes(aap, B, utf8), B == [97, 97, 112].`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(S, [97, 97, 112], utf8), S == "aap".`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(aap, B, 'utf-16be'), B == [0, 97, 0, 97, 0, 112].`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(S, [0, 97, 0, 97, 0, 112], 'utf-16be'), S == "aap".`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(aap, B, 'utf-16le'), B ==[97, 0, 97, 0, 112, 0].`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(S, [97, 0, 97, 0, 112, 0], 'utf-16le'), S == "aap".`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(今日は, B, utf8), B == [228,187,138,230,151,165,227,129,175].`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(S, [228,187,138,230,151,165,227,129,175], utf8), S == "今日は".`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(今日は, B, 'utf-16le'), B == [202,78,229,101,111,48].`,
query: "test.",
wantSuccess: true,
},
{
program: `test :- string_bytes(S, [202,78,229,101,111,48], 'utf-16le'), S == "今日は".`,
query: "test.",
wantSuccess: true,
},
// error cases
{
query: `string_bytes(_, [202,78,229,101,111,48], foo).`,
wantSuccess: false,
wantError: fmt.Errorf("string_bytes/3: invalid encoding: foo"),
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() {
Convey("and a context", func() {
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

Convey("and a vm", func() {
interpreter := testutil.NewLightInterpreterMust(ctx)
interpreter.Register3(engine.NewAtom("string_bytes"), StringBytes)

Convey("and a program", func() {
err := interpreter.Compile(ctx, tc.program)
So(err, ShouldBeNil)

Convey("When the predicate is called", func() {
sols, err := interpreter.QueryContext(ctx, tc.query)

Convey("Then the error should be nil", func() {
So(err, ShouldBeNil)
So(sols, ShouldNotBeNil)

Convey("and the result should be as expected", func() {
if tc.wantError != nil {
sols.Next()
So(sols.Err(), ShouldNotBeNil)
So(sols.Err().Error(), ShouldEqual, tc.wantError.Error())
} else {
nb := 0
for sols.Next() {
m := types.TermResults{}
So(sols.Scan(m), ShouldBeNil)
nb++
}
So(sols.Err(), ShouldBeNil)
if tc.wantSuccess {
So(nb, ShouldEqual, 1)
} else {
So(nb, ShouldEqual, 0)
}
}
})
})
})
})
})
})
})
}
})
}

0 comments on commit 6644a9c

Please sign in to comment.