File: lexer_test.go

package info (click to toggle)
golang-github-mitch000001-go-hbci 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,468 kB
  • sloc: java: 1,092; makefile: 5
file content (174 lines) | stat: -rw-r--r-- 4,703 bytes parent folder | download | duplicates (2)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package token

import (
	"bytes"
	"reflect"
	"testing"
)

type testData struct {
	text  string
	typ   Type
	value string
}

func TestLexer(t *testing.T) {
	testInput := "ab??cd\ref+12345+@2@ab'"
	l := NewLexer("", []byte(testInput))
	var items []Token
	for l.HasNext() {
		item := l.Next()
		items = append(items, item)
	}
	var itemTypes []Type
	for _, item := range items {
		itemTypes = append(itemTypes, item.Type())
	}
	expectedItemTypes := []Type{
		TEXT,
		DATA_ELEMENT_SEPARATOR,
		NUMERIC,
		DATA_ELEMENT_SEPARATOR,
		BINARY_DATA,
		SEGMENT_END_MARKER,
		EOF,
	}
	if !reflect.DeepEqual(expectedItemTypes, itemTypes) {
		t.Logf("Expected types to equal \n\t'%s' \ngot: \n\t'%s'\n", expectedItemTypes, itemTypes)
		t.Fail()
	}
}

func TestLexText(t *testing.T) {
	tests := []testData{
		{"ab\rcd'", TEXT, "ab\rcd"},
		{"ab\ncd'", TEXT, "ab\ncd"},
		{"ab\r\ncd'", TEXT, "ab\r\ncd"},
		{"ab\n\rcd'", TEXT, "ab\n\rcd"},
		{"ab\n\rcd", ERROR, "Unexpected end of input"},
	}
	for _, test := range tests {
		l := NewLexer("", []byte(test.text))
		item := l.Next()
		if item.Type() != test.typ {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type())
			t.Fail()
		}
		if !bytes.Equal(item.Value(), []byte(test.value)) {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value())
			t.Fail()
		}
	}
}

func TestLexAlphaNumeric(t *testing.T) {
	tests := []testData{
		{"ab'", ALPHA_NUMERIC, "ab"},
		{"ab123'", ALPHA_NUMERIC, "ab123"},
		{"ab!)'", ALPHA_NUMERIC, "ab!)"},
		{"ab!)'", ALPHA_NUMERIC, "ab!)"},
		{"ab!):", ALPHA_NUMERIC, "ab!)"},
		{"ab!)+", ALPHA_NUMERIC, "ab!)"},
		{"ab?''", ALPHA_NUMERIC, "ab?'"},
		{"ab?@'", ALPHA_NUMERIC, "ab?@"},
		{"ab?e", ERROR, "Unexpected escape character"},
		{"ab", ERROR, "Unexpected end of input"},
		{"ab??", ERROR, "Unexpected end of input"},
	}
	for _, test := range tests {
		l := NewLexer("", []byte(test.text))
		item := l.Next()
		if item.Type() != test.typ {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type())
			t.Fail()
		}
		if !bytes.Equal(item.Value(), []byte(test.value)) {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value())
			t.Fail()
		}
	}
}

func TestLexSyntaxSymbol(t *testing.T) {
	tests := []testData{
		{"'", SEGMENT_END_MARKER, "'"},
		{"+", DATA_ELEMENT_SEPARATOR, "+"},
		{":", GROUP_DATA_ELEMENT_SEPARATOR, ":"},
	}
	for _, test := range tests {
		l := NewLexer("", []byte(test.text))
		item := l.Next()
		if item.Type() != test.typ {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type())
			t.Fail()
		}
		if !bytes.Equal(item.Value(), []byte(test.value)) {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value())
			t.Fail()
		}
	}
}

func TestLexBinaryData(t *testing.T) {
	tests := []testData{
		{"@2@ab'", BINARY_DATA, "@2@ab"},
		{"@@ab'", ERROR, "Binary length can't be empty"},
		{"@2@a1@", ERROR, "Expected syntax symbol after binary data"},
		{"@2@abc'", ERROR, "Expected syntax symbol after binary data"},
		{"@2x@ab'", ERROR, "Binary length must contain of digits only"},
		{"@2@ab", ERROR, "Unexpected end of input"},
	}
	for _, test := range tests {
		l := NewLexer("", []byte(test.text))
		item := l.Next()
		if item.Type() != test.typ {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type())
			t.Fail()
		}
		if !bytes.Equal(item.Value(), []byte(test.value)) {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value())
			t.Fail()
		}
	}
}

func TestLexDigit(t *testing.T) {
	tests := []testData{
		{"123'", NUMERIC, "123"},
		{"0123'", DIGIT, "0123"},
		{"0,123'", FLOAT, "0,123"},
		{"1,23'", FLOAT, "1,23"},
		{"1,''", FLOAT, "1,"},
		{"0'", NUMERIC, "0"},
		{"0,'", FLOAT, "0,"},
		{"01,23'", ERROR, "Malformed float"},
		{"0,12a'", ALPHA_NUMERIC, "0,12a"},
		{"1,23a'", ALPHA_NUMERIC, "1,23a"},
		{"012a'", ALPHA_NUMERIC, "012a"},
		{"12a'", ALPHA_NUMERIC, "12a"},
		{"12?+'", ALPHA_NUMERIC, "12?+"},
		{"12", ERROR, "Unexpected end of input"},
	}
	for _, test := range tests {
		l := NewLexer("", []byte(test.text))
		item := l.Next()
		if item.Type() != test.typ {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type())
			t.Fail()
		}
		if !bytes.Equal(item.Value(), []byte(test.value)) {
			t.Logf("Input: %q\n", test.text)
			t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value())
			t.Fail()
		}
	}
}