-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdot_test.go
71 lines (64 loc) · 1.56 KB
/
dot_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
package gisp
import (
//"fmt"
"reflect"
"testing"
tm "time"
p "github.com/Dwarfartisan/goparsec"
)
func TestDotTime(t *testing.T) {
now := tm.Now()
g := NewGisp(map[string]Toolbox{
"axioms": Axiom,
"props": Propositions,
})
slot := VarSlot(TIMEOPTION)
slot.Set(now)
g.Defvar("now", slot)
year := Int(now.Year())
y, err := g.Parse("(now.Year)") //g.Eval(List{AA("now.Year")})
if err != nil {
t.Fatalf("except (now.Year) equal to now.Year() as %v but got error %v", year, err)
}
if !reflect.DeepEqual(year, Int(y.(int))) {
t.Fatalf("except (now.Year) equal to now.Year() but got %v and %v", year, y)
}
}
func TestDotParser(t *testing.T) {
data := "now.Year"
st := p.MemoryParseState(data)
re, err := p.Bind(AtomParser, DotSuffixParser)(st)
if err != nil {
t.Fatalf("except a Dot but error %v", err)
}
t.Log(re)
}
type Box map[string]interface{}
func (b Box) Get(name string) interface{} {
return b[name]
}
func TestDotMap(t *testing.T) {
box := Box{
"a": Quote{AA("a")},
"b": Quote{AA("bb")},
"c": Quote{AA("ccc")},
}
bv := reflect.ValueOf(box)
get := bv.MethodByName("Get")
res := get.Call([]reflect.Value{reflect.ValueOf("b")})
if !reflect.DeepEqual(res[0].Interface(), box["b"]) {
t.Fatalf("except %v but got %v", box["b"], res[0].Interface())
}
g := NewGisp(map[string]Toolbox{
"axioms": Axiom,
"props": Propositions,
})
g.DefAs("box", box)
c, err := g.Parse(`(box.Get "c")`)
if err != nil {
t.Fatalf("excpet got b but error %v", err)
}
if !reflect.DeepEqual(c, box["c"]) {
t.Fatalf("except %v but got %v", box["c"], c)
}
}