| 1 |
package element |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func Test_ExtractElements(t *testing.T) { |
| 9 |
type testData struct { |
| 10 |
in string |
| 11 |
out []string |
| 12 |
err error |
| 13 |
} |
| 14 |
tests := []testData{ |
| 15 |
{ |
| 16 |
"abcde:123:012'", |
| 17 |
[]string{ |
| 18 |
"abcde", |
| 19 |
"123", |
| 20 |
"012", |
| 21 |
}, |
| 22 |
nil, |
| 23 |
}, |
| 24 |
{ |
| 25 |
"abcde:123:012+", |
| 26 |
[]string{ |
| 27 |
"abcde", |
| 28 |
"123", |
| 29 |
"012", |
| 30 |
}, |
| 31 |
nil, |
| 32 |
}, |
| 33 |
{ |
| 34 |
"abcde:123:012+de+1'", |
| 35 |
[]string{ |
| 36 |
"abcde", |
| 37 |
"123", |
| 38 |
"012", |
| 39 |
"de", |
| 40 |
"1", |
| 41 |
}, |
| 42 |
nil, |
| 43 |
}, |
| 44 |
{ |
| 45 |
"de+1+abcde:123:012'", |
| 46 |
[]string{ |
| 47 |
"de", |
| 48 |
"1", |
| 49 |
"abcde", |
| 50 |
"123", |
| 51 |
"012", |
| 52 |
}, |
| 53 |
nil, |
| 54 |
}, |
| 55 |
{ |
| 56 |
"abcde:123:012", |
| 57 |
[]string{ |
| 58 |
"abcde", |
| 59 |
"123", |
| 60 |
"012", |
| 61 |
}, |
| 62 |
nil, |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
for _, test := range tests { |
| 67 |
extracted, err := ExtractElements([]byte(test.in)) |
| 68 |
|
| 69 |
if err != nil { |
| 70 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 71 |
t.Fail() |
| 72 |
} |
| 73 |
|
| 74 |
if extracted == nil { |
| 75 |
t.Logf("Expected result not to be nil\n") |
| 76 |
t.Fail() |
| 77 |
} |
| 78 |
|
| 79 |
actual := make([]string, len(extracted)) |
| 80 |
for i, b := range extracted { |
| 81 |
actual[i] = string(b) |
| 82 |
} |
| 83 |
|
| 84 |
if !reflect.DeepEqual(test.out, actual) { |
| 85 |
t.Logf("Extract: \n%q\n", extracted) |
| 86 |
t.Logf("Expected result to equal\n%q\n\tgot\n%q\n", test.out, actual) |
| 87 |
t.Fail() |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
} |