-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins_test.go
50 lines (39 loc) · 1.01 KB
/
builtins_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
package main
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func testRuntimeEnv(str string) *RuntimeEnv {
return &RuntimeEnv{
strings.NewReader(str),
bytes.NewBuffer([]byte{}),
bytes.NewBuffer([]byte{}),
}
}
func TestBuiltins(t *testing.T) {
t.Parallel()
greeting := ScopeEntry{TypString, "hello world"}
t.Run("strlen", func(t *testing.T) {
s := &Stack{}
p := []ScopeEntry{greeting}
env := testRuntimeEnv("")
builtins["strlen"](s, p, env)
assert.Equal(t, ScopeEntry{TypNumber, 11}, s.Pop().(ScopeEntry))
})
t.Run("write", func(t *testing.T) {
s := &Stack{}
p := []ScopeEntry{greeting}
env := testRuntimeEnv(greeting.Value.(string))
builtins["write"](s, p, env)
assert.Equal(t, greeting.Value.(string), env.stdout.(*bytes.Buffer).String())
})
t.Run("read", func(t *testing.T) {
s := &Stack{}
p := []ScopeEntry{greeting}
env := testRuntimeEnv(greeting.Value.(string))
builtins["read"](s, p, env)
assert.Equal(t, greeting, s.Pop().(ScopeEntry))
})
}