-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathtoken_test.go
55 lines (48 loc) · 1.86 KB
/
mathtoken_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
package mathtoken_test
import (
"testing"
"github.com/darylnwk/mathtoken"
"github.com/stretchr/testify/assert"
)
func TestToken(t *testing.T) {
tokens, err := mathtoken.Parse("10 +abc")
assert.NoError(t, err)
assert.Equal(t, 3, len(tokens))
assert.Equal(t, mathtoken.TypeConstant, tokens[0].Type)
assert.Equal(t, "10", tokens[0].Value)
assert.Equal(t, mathtoken.TypeOperator, tokens[1].Type)
assert.Equal(t, "+", tokens[1].Value)
assert.Equal(t, mathtoken.TypeVariable, tokens[2].Type)
assert.Equal(t, "abc", tokens[2].Value)
tokens, err = mathtoken.Parse("10.0/abc")
assert.NoError(t, err)
assert.Equal(t, 3, len(tokens))
assert.Equal(t, mathtoken.TypeConstant, tokens[0].Type)
assert.Equal(t, "10.0", tokens[0].Value)
assert.Equal(t, mathtoken.TypeOperator, tokens[1].Type)
assert.Equal(t, "/", tokens[1].Value)
assert.Equal(t, mathtoken.TypeVariable, tokens[2].Type)
assert.Equal(t, "abc", tokens[2].Value)
tokens, err = mathtoken.Parse("x1-1")
assert.NoError(t, err)
assert.Equal(t, 3, len(tokens))
assert.Equal(t, mathtoken.TypeVariable, tokens[0].Type)
assert.Equal(t, "x1", tokens[0].Value)
assert.Equal(t, mathtoken.TypeOperator, tokens[1].Type)
assert.Equal(t, "-", tokens[1].Value)
assert.Equal(t, mathtoken.TypeConstant, tokens[2].Type)
assert.Equal(t, "1", tokens[2].Value)
tokens, err = mathtoken.Parse("(x1-1)")
assert.NoError(t, err)
assert.Equal(t, 5, len(tokens))
assert.Equal(t, mathtoken.TypeLParent, tokens[0].Type)
assert.Equal(t, "(", tokens[0].Value)
assert.Equal(t, mathtoken.TypeVariable, tokens[1].Type)
assert.Equal(t, "x1", tokens[1].Value)
assert.Equal(t, mathtoken.TypeOperator, tokens[2].Type)
assert.Equal(t, "-", tokens[2].Value)
assert.Equal(t, mathtoken.TypeConstant, tokens[3].Type)
assert.Equal(t, "1", tokens[3].Value)
assert.Equal(t, mathtoken.TypeRParent, tokens[4].Type)
assert.Equal(t, ")", tokens[4].Value)
}