-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prolog_test.go
127 lines (113 loc) · 2.8 KB
/
prolog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pengine
import (
"context"
"os"
"reflect"
"testing"
"github.com/ichiban/prolog"
"github.com/ichiban/prolog/engine"
)
func TestProlog(t *testing.T) {
eng, err := Client{
URL: *penginesServerURL,
SourceText: "'子'(X, List) :- member(X, List).\n",
Debug: true,
}.Create(context.Background(), true)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
as, err := eng.AskProlog(ctx, "'子'(X, ['あ', 1, Y])")
if err != nil {
t.Fatal(err)
}
for as.Next(ctx) {
t.Logf("answer: %+v", as.Current())
cmp, ok := as.Current().(engine.Compound)
if !ok {
t.Fatal("not a compound", as.Current())
}
if cmp.Functor() != "子" {
t.Error("unexpected functor. want: 子 got:", cmp.Functor())
}
}
if err := as.Err(); err != nil {
t.Error(err)
}
}
func TestRPC(t *testing.T) {
p := prolog.New(nil, os.Stdout)
p.Register3("pengine_rpc", RPC)
sols, err := p.Query("pengine_rpc('?', between(1,3,X), [chunk(2), debug(true)]), OK = true.", *penginesServerURL)
if err != nil {
t.Fatal(err)
}
defer sols.Close()
n := 0
var got []int
for sols.Next() {
var solution struct {
X int
OK string
}
if err := sols.Scan(&solution); err != nil {
panic(err)
}
t.Log("solution:", solution)
got = append(got, solution.X)
if solution.OK != "true" {
t.Error("not ok:", solution)
}
n++
}
if err := sols.Err(); err != nil {
t.Error(err)
}
want := []int{1, 2, 3}
if !reflect.DeepEqual(want, got) {
t.Error("bad rpc results. want:", want, "got:", got)
}
t.Run("complex", func(t *testing.T) {
// sol := p.QuerySolution(`pengine_rpc(?, (A=a+b->true ; false), [debug(true)]).`, *penginesServerURL)
sol := p.QuerySolution("pengine_rpc('?', (Foo = bar+baz, (Foo = A+B -> true ; A = x)), [debug(true)]).", *penginesServerURL)
if err := sol.Err(); err != nil {
t.Fatal(err)
}
type result struct {
A string
B string
}
want := result{A: "bar", B: "baz"}
var got result
if err := sol.Scan(&got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got) {
t.Error("bad rpc results. want:", want, "got:", got)
}
})
t.Run("fail", func(t *testing.T) {
sols := p.QuerySolution("pengine_rpc('?', fail, []), OK = false.", *penginesServerURL)
var val struct {
OK string
}
if err := sols.Scan(&val); err != prolog.ErrNoSolutions {
t.Fatal("wanted:", prolog.ErrNoSolutions, "got:", err)
}
if val.OK == "false" {
t.Error("expected empty, got:", val.OK)
}
})
t.Run("throw", func(t *testing.T) {
sols := p.QuerySolution("catch(pengine_rpc('?', throw(hello(world)), [debug(true)]), hello(Planet), (Caught = Planet)).", *penginesServerURL)
var val struct {
Caught string
}
if err := sols.Scan(&val); err != nil {
t.Fatal(err)
}
if val.Caught != "world" {
t.Error("expected world, got:", val.Caught)
}
})
}