| 1 |
package token |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
type testData struct { |
| 10 |
text string |
| 11 |
typ Type |
| 12 |
value string |
| 13 |
} |
| 14 |
|
| 15 |
func TestLexer(t *testing.T) { |
| 16 |
testInput := "ab??cd\ref+12345+@2@ab'" |
| 17 |
l := NewLexer("", []byte(testInput)) |
| 18 |
var items []Token |
| 19 |
for l.HasNext() { |
| 20 |
item := l.Next() |
| 21 |
items = append(items, item) |
| 22 |
} |
| 23 |
var itemTypes []Type |
| 24 |
for _, item := range items { |
| 25 |
itemTypes = append(itemTypes, item.Type()) |
| 26 |
} |
| 27 |
expectedItemTypes := []Type{ |
| 28 |
TEXT, |
| 29 |
DATA_ELEMENT_SEPARATOR, |
| 30 |
NUMERIC, |
| 31 |
DATA_ELEMENT_SEPARATOR, |
| 32 |
BINARY_DATA, |
| 33 |
SEGMENT_END_MARKER, |
| 34 |
EOF, |
| 35 |
} |
| 36 |
if !reflect.DeepEqual(expectedItemTypes, itemTypes) { |
| 37 |
t.Logf("Expected types to equal \n\t'%s' \ngot: \n\t'%s'\n", expectedItemTypes, itemTypes) |
| 38 |
t.Fail() |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestLexText(t *testing.T) { |
| 43 |
tests := []testData{ |
| 44 |
{"ab\rcd'", TEXT, "ab\rcd"}, |
| 45 |
{"ab\ncd'", TEXT, "ab\ncd"}, |
| 46 |
{"ab\r\ncd'", TEXT, "ab\r\ncd"}, |
| 47 |
{"ab\n\rcd'", TEXT, "ab\n\rcd"}, |
| 48 |
{"ab\n\rcd", ERROR, "Unexpected end of input"}, |
| 49 |
} |
| 50 |
for _, test := range tests { |
| 51 |
l := NewLexer("", []byte(test.text)) |
| 52 |
item := l.Next() |
| 53 |
if item.Type() != test.typ { |
| 54 |
t.Logf("Input: %q\n", test.text) |
| 55 |
t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type()) |
| 56 |
t.Fail() |
| 57 |
} |
| 58 |
if !bytes.Equal(item.Value(), []byte(test.value)) { |
| 59 |
t.Logf("Input: %q\n", test.text) |
| 60 |
t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value()) |
| 61 |
t.Fail() |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestLexAlphaNumeric(t *testing.T) { |
| 67 |
tests := []testData{ |
| 68 |
{"ab'", ALPHA_NUMERIC, "ab"}, |
| 69 |
{"ab123'", ALPHA_NUMERIC, "ab123"}, |
| 70 |
{"ab!)'", ALPHA_NUMERIC, "ab!)"}, |
| 71 |
{"ab!)'", ALPHA_NUMERIC, "ab!)"}, |
| 72 |
{"ab!):", ALPHA_NUMERIC, "ab!)"}, |
| 73 |
{"ab!)+", ALPHA_NUMERIC, "ab!)"}, |
| 74 |
{"ab?''", ALPHA_NUMERIC, "ab?'"}, |
| 75 |
{"ab?@'", ALPHA_NUMERIC, "ab?@"}, |
| 76 |
{"ab?e", ERROR, "Unexpected escape character"}, |
| 77 |
{"ab", ERROR, "Unexpected end of input"}, |
| 78 |
{"ab??", ERROR, "Unexpected end of input"}, |
| 79 |
} |
| 80 |
for _, test := range tests { |
| 81 |
l := NewLexer("", []byte(test.text)) |
| 82 |
item := l.Next() |
| 83 |
if item.Type() != test.typ { |
| 84 |
t.Logf("Input: %q\n", test.text) |
| 85 |
t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type()) |
| 86 |
t.Fail() |
| 87 |
} |
| 88 |
if !bytes.Equal(item.Value(), []byte(test.value)) { |
| 89 |
t.Logf("Input: %q\n", test.text) |
| 90 |
t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value()) |
| 91 |
t.Fail() |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestLexSyntaxSymbol(t *testing.T) { |
| 97 |
tests := []testData{ |
| 98 |
{"'", SEGMENT_END_MARKER, "'"}, |
| 99 |
{"+", DATA_ELEMENT_SEPARATOR, "+"}, |
| 100 |
{":", GROUP_DATA_ELEMENT_SEPARATOR, ":"}, |
| 101 |
} |
| 102 |
for _, test := range tests { |
| 103 |
l := NewLexer("", []byte(test.text)) |
| 104 |
item := l.Next() |
| 105 |
if item.Type() != test.typ { |
| 106 |
t.Logf("Input: %q\n", test.text) |
| 107 |
t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type()) |
| 108 |
t.Fail() |
| 109 |
} |
| 110 |
if !bytes.Equal(item.Value(), []byte(test.value)) { |
| 111 |
t.Logf("Input: %q\n", test.text) |
| 112 |
t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value()) |
| 113 |
t.Fail() |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestLexBinaryData(t *testing.T) { |
| 119 |
tests := []testData{ |
| 120 |
{"@2@ab'", BINARY_DATA, "@2@ab"}, |
| 121 |
{"@@ab'", ERROR, "Binary length can't be empty"}, |
| 122 |
{"@2@a1@", ERROR, "Expected syntax symbol after binary data"}, |
| 123 |
{"@2@abc'", ERROR, "Expected syntax symbol after binary data"}, |
| 124 |
{"@2x@ab'", ERROR, "Binary length must contain of digits only"}, |
| 125 |
{"@2@ab", ERROR, "Unexpected end of input"}, |
| 126 |
} |
| 127 |
for _, test := range tests { |
| 128 |
l := NewLexer("", []byte(test.text)) |
| 129 |
item := l.Next() |
| 130 |
if item.Type() != test.typ { |
| 131 |
t.Logf("Input: %q\n", test.text) |
| 132 |
t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type()) |
| 133 |
t.Fail() |
| 134 |
} |
| 135 |
if !bytes.Equal(item.Value(), []byte(test.value)) { |
| 136 |
t.Logf("Input: %q\n", test.text) |
| 137 |
t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value()) |
| 138 |
t.Fail() |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestLexDigit(t *testing.T) { |
| 144 |
tests := []testData{ |
| 145 |
{"123'", NUMERIC, "123"}, |
| 146 |
{"0123'", DIGIT, "0123"}, |
| 147 |
{"0,123'", FLOAT, "0,123"}, |
| 148 |
{"1,23'", FLOAT, "1,23"}, |
| 149 |
{"1,''", FLOAT, "1,"}, |
| 150 |
{"0'", NUMERIC, "0"}, |
| 151 |
{"0,'", FLOAT, "0,"}, |
| 152 |
{"01,23'", ERROR, "Malformed float"}, |
| 153 |
{"0,12a'", ALPHA_NUMERIC, "0,12a"}, |
| 154 |
{"1,23a'", ALPHA_NUMERIC, "1,23a"}, |
| 155 |
{"012a'", ALPHA_NUMERIC, "012a"}, |
| 156 |
{"12a'", ALPHA_NUMERIC, "12a"}, |
| 157 |
{"12?+'", ALPHA_NUMERIC, "12?+"}, |
| 158 |
{"12", ERROR, "Unexpected end of input"}, |
| 159 |
} |
| 160 |
for _, test := range tests { |
| 161 |
l := NewLexer("", []byte(test.text)) |
| 162 |
item := l.Next() |
| 163 |
if item.Type() != test.typ { |
| 164 |
t.Logf("Input: %q\n", test.text) |
| 165 |
t.Logf("Expected type to equal %s, got %s\n", test.typ, item.Type()) |
| 166 |
t.Fail() |
| 167 |
} |
| 168 |
if !bytes.Equal(item.Value(), []byte(test.value)) { |
| 169 |
t.Logf("Input: %q\n", test.text) |
| 170 |
t.Logf("Expected val to equal %q, got %q\n", test.value, item.Value()) |
| 171 |
t.Fail() |
| 172 |
} |
| 173 |
} |
| 174 |
} |