From 6644a9cab24a40d258ff19d06461b605c717a16d Mon Sep 17 00:00:00 2001 From: ccamel Date: Sat, 6 Jan 2024 18:59:16 +0100 Subject: [PATCH] test(logic): put string_bytes/3 under test --- x/logic/predicate/string_test.go | 122 +++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/x/logic/predicate/string_test.go b/x/logic/predicate/string_test.go index 48cb80ee..2c2a934f 100644 --- a/x/logic/predicate/string_test.go +++ b/x/logic/predicate/string_test.go @@ -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) + } + } + }) + }) + }) + }) + }) + }) + }) + } + }) +}