-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_test.go
71 lines (57 loc) · 1.54 KB
/
core_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 main
import "testing"
import "fmt"
// Ensure that the intrinsic types are available
func TestTypes(t *testing.T) {
types := []string{"Int", "Float", "String", "Bool", "List", "Dict", "Keyword",
"Symbol", "NativeFunction", "NativeFunctionB"}
for _, name := range types {
result, err := EvaluateString(fmt.Sprintf("(type %s)", name), MainContext)
if err != nil {
t.Error(err.Error())
continue
}
_, ok := result.(DataType)
if !ok {
t.Errorf("(type %s) did not yield a type objet: %s", name, result.String())
continue
}
}
}
// Check some instances of the instrinsic types
func TestTypeInstances(t *testing.T) {
instances := []string{"8", "0.5", "\"Hallo Welt\"", "true", "{:version 800}", ":keyword", "(symbol \"x\")", "map", "def"}
for _, inst := range instances {
result, err := EvaluateString(inst, MainContext)
if err != nil {
t.Error(err.Error())
continue
}
expected := inst
switch inst {
case "(symbol \"x\")":
expected = "x"
case "map":
expected = "native function"
case "def":
expected = "native function"
}
if result.String() != expected {
t.Errorf("Expected %s, found %s", inst, result.String())
}
}
}
func TestContext(t *testing.T) {
a := NewContext()
a.Define(Symbol{"print"}, String{"SOME_VALUE"})
b := NewContext()
b.parent = a
if !b.IsDefined(Symbol{"print"}) {
t.Error("Subcontext must inherit parent's symbols")
}
c := NewContext()
c.parent = MainContext
if !c.IsDefined(Symbol{"type"}) {
t.Error("Subcontext doesn't contain MainContext symbols")
}
}