-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_test.go
80 lines (73 loc) · 2 KB
/
lexer_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
72
73
74
75
76
77
78
79
80
package hedi
import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
func TestLexISA(t *testing.T) {
t.Run("Line feed as segment terminator should succeed", func(t *testing.T) {
file, err := os.Open("./test/850_with_new_line_segment_terminator.txt")
assert.NoError(t, err)
defer file.Close()
tokens, separators, err := lexISA(file)
assert.NoError(t, err)
assert.Equal(t, 34, len(tokens))
assert.Equal(t, int32(10), separators.Segment)
assert.Equal(t, int32(42), separators.Element)
assert.Equal(t, int32(62), separators.SubElement)
})
t.Run("Tilde as segment terminator should succeed", func(t *testing.T) {
file, err := os.Open("./test/850_with_tilde_segment_terminator.txt")
assert.NoError(t, err)
defer file.Close()
tokens, separators, err := lexISA(file)
assert.NoError(t, err)
assert.Equal(t, 34, len(tokens))
assert.Equal(t, int32(126), separators.Segment)
assert.Equal(t, int32(42), separators.Element)
assert.Equal(t, int32(62), separators.SubElement)
})
}
func TestLexer_Tokens(t *testing.T) {
t.Run("Success", func(t *testing.T) {
file, err := os.Open("./test/850_with_tilde_segment_terminator.txt")
assert.NoError(t, err)
defer file.Close()
lexer := NewLexer(file)
tokens, err := lexer.Tokens()
assert.NoError(t, err)
assert.Len(t, tokens, 504)
})
tests := []struct {
name string
input string
wantErr error
len int
}{
{
name: "ValidInput",
input: "ISA*00* *00* *ZZ*SENDER *ZZ*RECEIVER *190430*1230*U*00401*000000000*0*T*|\n",
wantErr: nil,
len: 34,
},
{
name: "InvalidISALength",
input: "ISA*00* *00*",
wantErr: ErrInvalidISALength,
len: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lexer := NewLexer(strings.NewReader(tt.input))
tokens, err := lexer.Tokens()
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
assert.Len(t, tokens, tt.len)
})
}
}