| 1 |
package message |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestSegmentExtratorExtract(t *testing.T) { |
| 9 |
type testData struct { |
| 10 |
in string |
| 11 |
out []string |
| 12 |
err error |
| 13 |
} |
| 14 |
|
| 15 |
tests := []testData{ |
| 16 |
{ |
| 17 |
"HNHBK:1:3+abc'HNDGC:2:3+def'", |
| 18 |
[]string{ |
| 19 |
"HNHBK:1:3+abc'", |
| 20 |
"HNDGC:2:3+def'", |
| 21 |
}, |
| 22 |
nil, |
| 23 |
}, |
| 24 |
{ |
| 25 |
"HNHBK:1:3+abc'HNDGC:2:3+de?'f'", |
| 26 |
[]string{ |
| 27 |
"HNHBK:1:3+abc'", |
| 28 |
"HNDGC:2:3+de?'f'", |
| 29 |
}, |
| 30 |
nil, |
| 31 |
}, |
| 32 |
} |
| 33 |
|
| 34 |
for _, test := range tests { |
| 35 |
extractor := NewSegmentExtractor([]byte(test.in)) |
| 36 |
|
| 37 |
extracted, err := extractor.Extract() |
| 38 |
|
| 39 |
if test.err != nil && err == nil { |
| 40 |
t.Logf("Expected error, got nil\n") |
| 41 |
t.Fail() |
| 42 |
} |
| 43 |
|
| 44 |
if test.err == nil && err != nil { |
| 45 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 46 |
t.Fail() |
| 47 |
} |
| 48 |
|
| 49 |
actual := make([]string, len(extracted)) |
| 50 |
for i, b := range extracted { |
| 51 |
actual[i] = string(b) |
| 52 |
} |
| 53 |
|
| 54 |
if !reflect.DeepEqual(test.out, actual) { |
| 55 |
t.Logf("Extract: \n%q\n", extracted) |
| 56 |
t.Logf("Expected result to equal\n%q\n\tgot\n%q\n", test.out, actual) |
| 57 |
t.Fail() |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestSegmentExtratorFindSegment(t *testing.T) { |
| 63 |
test := "HNHBK:1:3+abc'HNDGC:2:3+def'" |
| 64 |
|
| 65 |
extractor := NewSegmentExtractor([]byte(test)) |
| 66 |
extractor.Extract() |
| 67 |
|
| 68 |
segment := extractor.FindSegment("HNHBK") |
| 69 |
|
| 70 |
if segment == nil { |
| 71 |
t.Logf("Expected segment not to be nil") |
| 72 |
t.Fail() |
| 73 |
} |
| 74 |
|
| 75 |
expected := []byte("HNHBK:1:3+abc'") |
| 76 |
|
| 77 |
if !reflect.DeepEqual(expected, segment) { |
| 78 |
t.Logf("Expected segment to equal\n%q\n\tgot\n%q\n", expected, segment) |
| 79 |
t.Fail() |
| 80 |
} |
| 81 |
|
| 82 |
segment = extractor.FindSegment("HNDGC") |
| 83 |
|
| 84 |
if segment == nil { |
| 85 |
t.Logf("Expected segment not to be nil") |
| 86 |
t.Fail() |
| 87 |
} |
| 88 |
|
| 89 |
expected = []byte("HNDGC:2:3+def'") |
| 90 |
|
| 91 |
if !reflect.DeepEqual(expected, segment) { |
| 92 |
t.Logf("Expected segment to equal\n%q\n\tgot\n%q\n", expected, segment) |
| 93 |
t.Fail() |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestSegmentExtratorFindSegments(t *testing.T) { |
| 98 |
test := "HNHBK:1:3+abc'HNDGC:2:3+def'HNDGC:3:3+xyz'" |
| 99 |
|
| 100 |
extractor := NewSegmentExtractor([]byte(test)) |
| 101 |
extractor.Extract() |
| 102 |
|
| 103 |
segments := extractor.FindSegments("HNDGC") |
| 104 |
|
| 105 |
if segments == nil { |
| 106 |
t.Logf("Expected segment not to be nil") |
| 107 |
t.Fail() |
| 108 |
} |
| 109 |
|
| 110 |
expected := [][]byte{[]byte("HNDGC:2:3+def'"), []byte("HNDGC:3:3+xyz'")} |
| 111 |
|
| 112 |
if !reflect.DeepEqual(expected, segments) { |
| 113 |
t.Logf("Expected segment to equal\n%q\n\tgot\n%q\n", expected, segments) |
| 114 |
t.Fail() |
| 115 |
} |
| 116 |
} |