aboutsummaryrefslogtreecommitdiff
path: root/lex_test.go
blob: 2c8f523a0b5c963aac104157f68b756cdd610798 (plain) (blame)
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
package lexpr

import (
	"context"
	"reflect"
	"testing"
)

func Test_lex_Parse(t *testing.T) {
	type args struct {
		input string
	}
	tests := []struct {
		name string
		args args
		want []lexem
	}{
		{
			name: "math",
			args: args{
				input: "min(3, 2) * max(10, 20) == 40",
			},
			want: []lexem{
				{
					Type:  word,
					Value: "min",
				}, {
					Type:  lp,
					Value: "(",
				}, {
					Type:  number,
					Value: "3",
				}, {
					Type:  sep,
					Value: ",",
				}, {
					Type:  number,
					Value: "2",
				}, {
					Type:  rp,
					Value: ")",
				}, {
					Type:  op,
					Value: "*",
				}, {
					Type:  word,
					Value: "max",
				}, {
					Type:  lp,
					Value: "(",
				}, {
					Type:  number,
					Value: "10",
				}, {
					Type:  sep,
					Value: ",",
				}, {
					Type:  number,
					Value: "20",
				}, {
					Type:  rp,
					Value: ")",
				}, {
					Type:  op,
					Value: "==",
				}, {
					Type:  number,
					Value: "40",
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			l := newLex()
			gotCh := l.parse(context.Background(), tt.args.input)
			got := []lexem{}
			for o := range gotCh {
				got = append(got, lexem{
					Type:  o.Type,
					Value: o.Value,
				})
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("lex.Parse() = %v, want %v", got, tt.want)
			}
		})
	}
}