Skip to content

Commit

Permalink
test(logic): adopt Interpreter instead of VM
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jan 17, 2023
1 parent 130bea3 commit 0c3da6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
10 changes: 5 additions & 5 deletions x/logic/predicate/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func TestBlock(t *testing.T) {
ctx := sdk.NewContext(stateStore, tc.header, false, log.NewNopLogger())

Convey("and a vm", func() {
vm := testutil.NewVMMust(ctx)
vm.Register1(engine.NewAtom("block_height"), BlockHeight)
vm.Register1(engine.NewAtom("block_time"), BlockTime)
testutil.CompileMust(ctx, vm, fmt.Sprintf("test :- %s.", tc.implication))
interpreter := testutil.NewInterpreterMust(ctx)
interpreter.Register1(engine.NewAtom("block_height"), BlockHeight)
interpreter.Register1(engine.NewAtom("block_time"), BlockTime)
testutil.CompileMust(ctx, interpreter, fmt.Sprintf("test :- %s.", tc.implication))

Convey("When the predicate is called", func() {
ok, err := vm.Arrive(engine.NewAtom("test"), []engine.Term{}, engine.Success, nil).Force(ctx)
ok, err := interpreter.Arrive(engine.NewAtom("test"), []engine.Term{}, engine.Success, nil).Force(ctx)

Convey("Then the result should be true and there should be no error", func() {
So(err, ShouldBeNil)
Expand Down
10 changes: 5 additions & 5 deletions x/logic/predicate/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestChainID(t *testing.T) {
stateStore := store.NewCommitMultiStore(db)
ctx := sdk.NewContext(stateStore, tc.header, false, log.NewNopLogger())

Convey("and a vm", func() {
vm := testutil.NewVMMust(ctx)
vm.Register1(engine.NewAtom("chain_id"), ChainID)
testutil.CompileMust(ctx, vm, fmt.Sprintf("test :- %s.", tc.implication))
Convey("and an interpreter", func() {
interpreter := testutil.NewInterpreterMust(ctx)
interpreter.Register1(engine.NewAtom("chain_id"), ChainID)
testutil.CompileMust(ctx, interpreter, fmt.Sprintf("test :- %s.", tc.implication))

Convey("When the predicate is called", func() {
ok, err := vm.Arrive(engine.NewAtom("test"), []engine.Term{}, engine.Success, nil).Force(ctx)
ok, err := interpreter.Arrive(engine.NewAtom("test"), []engine.Term{}, engine.Success, nil).Force(ctx)

Convey("Then the result should be true and there should be no error", func() {
So(err, ShouldBeNil)
Expand Down
21 changes: 11 additions & 10 deletions x/logic/testutil/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package testutil
import (
"context"

"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"
)

// NewVMMust returns a new VM with the given context or panics if it fails.
// The VM is configured with minimal settings to support testing.
func NewVMMust(ctx context.Context) (vm *engine.VM) {
vm = &engine.VM{}
vm.Register3(engine.NewAtom("op"), engine.Op)
vm.Register3(engine.NewAtom("compare"), engine.Compare)
vm.Register2(engine.NewAtom("="), engine.Unify)
// NewInterpreterMust returns a new Interpreter with the given context or panics if it fails.
// The Interpreter is configured with minimal settings to support testing.
func NewInterpreterMust(ctx context.Context) (interpreter *prolog.Interpreter) {
interpreter = &prolog.Interpreter{}
interpreter.Register3(engine.NewAtom("op"), engine.Op)
interpreter.Register3(engine.NewAtom("compare"), engine.Compare)
interpreter.Register2(engine.NewAtom("="), engine.Unify)

err := vm.Compile(ctx, `
err := interpreter.Compile(ctx, `
:-(op(1200, xfx, ':-')).
:-(op(1000, xfy, ',')).
:-(op(700, xfx, '==')).
Expand All @@ -30,8 +31,8 @@ func NewVMMust(ctx context.Context) (vm *engine.VM) {

// CompileMust compiles the given source code and panics if it fails.
// This is a convenience function for testing.
func CompileMust(ctx context.Context, vm *engine.VM, s string, args ...interface{}) {
err := vm.Compile(ctx, s, args...)
func CompileMust(ctx context.Context, interpreter *prolog.Interpreter, s string, args ...interface{}) {
err := interpreter.Compile(ctx, s, args...)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 0c3da6b

Please sign in to comment.