diff options
Diffstat (limited to 'tests/lib_test.go')
-rw-r--r-- | tests/lib_test.go | 567 |
1 files changed, 330 insertions, 237 deletions
diff --git a/tests/lib_test.go b/tests/lib_test.go index 71ff6d0..fdc663b 100644 --- a/tests/lib_test.go +++ b/tests/lib_test.go @@ -16,335 +16,428 @@ func errorIf(t *testing.T, err error) { } } -func assertEqual(t *testing.T, given any, expected any) { +func assertEqualI(t *testing.T, i int, given any, expected any) { if !reflect.DeepEqual(given, expected) { - t.Errorf("given != expected") + t.Errorf("given != expected (i = %d)\n", i) t.Errorf("given: %#v\n", given) t.Errorf("expected: %#v\n", expected) } } func TestSplitOnCRLF(t *testing.T) { - inputs := []string { - "", - "\r\n", - "abc\r\n", - "abc\r\n ", - "abc\r\n \r\n", - " \r\n \r\n", - "aaa\r\nbbb\r\nccc\r\n", - "\r\nsplit \r \n CRLF\r\n\r\n", + type tableT struct { + input string + expected []string } - expected := [][]string { - nil, - { "" }, - { "abc" }, - { "abc" }, - { "abc", " " }, - { " ", " " }, - { "aaa", "bbb", "ccc" }, - { "", "split \r \n CRLF", "" }, + table := []tableT { + { + "", + nil, + }, + { + "\r\n", + []string { "" }, + }, + { + "abc\r\n", + []string { "abc" }, + }, + { + "abc\r\n ", + []string { "abc" }, + }, + { + "abc\r\n \r\n", + []string { "abc", " " }, + }, + { + " \r\n \r\n", + []string { " ", " " }, + }, + { + "aaa\r\nbbb\r\nccc\r\n", + []string { "aaa", "bbb", "ccc" }, + }, + { + "\r\nsplit \r \n CRLF\r\n\r\n", + []string { "", "split \r \n CRLF", "" }, + }, } - given := make([][]string, len(inputs)) - for i, input := range inputs { - scanner := bufio.NewScanner(strings.NewReader(input)) + for i, entry := range table { + var given []string + scanner := bufio.NewScanner(strings.NewReader(entry.input)) scanner.Split(papo.SplitOnCRLF) for scanner.Scan() { - given[i] = append(given[i], scanner.Text()) + given = append(given, scanner.Text()) } err := scanner.Err() errorIf(t, err) + assertEqualI(t, i, given, entry.expected) } - - assertEqual(t, given, expected) } func TestSplitOnRawMessage(t *testing.T) { - inputs := []string { - "first message\r\nsecond message\r\n", - "message 1\r\n\r\nmessage 2\r\n\r\nignored", + type tableT struct { + input string + expected []string } - expected := [][]string { - { "first message", "second message" }, - { "message 1", "message 2" }, + table := []tableT { + { + "first message\r\nsecond message\r\n", + []string { "first message", "second message" }, + }, + { + "message 1\r\n\r\nmessage 2\r\n\r\nignored", + []string { "message 1", "message 2" }, + }, } - given := make([][]string, len(inputs)) - for i, input := range inputs { - scanner := bufio.NewScanner(strings.NewReader(input)) + + for i, entry := range table { + var given []string + scanner := bufio.NewScanner(strings.NewReader(entry.input)) scanner.Split(papo.SplitOnRawMessage) for scanner.Scan() { - given[i] = append(given[i], scanner.Text()) + given = append(given, scanner.Text()) } err := scanner.Err() errorIf(t, err) + assertEqualI(t, i, given, entry.expected) } - - assertEqual(t, given, expected) } func TestParseMessageParams(t *testing.T) { - inputs := []string { - "", - " ", - " :", - " : ", - ": ", - ": ", - " : ", - " :", - " :", - "a", - "ab", - "a b", - "a b c", - "a b:c", - "a b:c:", - "a b :c", - "a b :c:", - "a b :c ", - "a b : c", - "a b : c ", - "a b : c :", - "a b : c : ", - "a b : c :d", - "a b : c :d ", - "a b : c : d ", + type tableT struct { + input string + expected papo.MessageParams } - expected := []papo.MessageParams { - papo.MessageParams { - Middle: []string { }, - Trailing: "", + table := []tableT { + { + "", + papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: "", + { + " ", + papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: "", + { + " :", + papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: " ", + { + " : ", + papo.MessageParams { + Middle: []string { }, + Trailing: " ", + }, }, - papo.MessageParams { - Middle: []string { ":" }, - Trailing: "", + { + ": ", + papo.MessageParams { + Middle: []string { ":" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { ":" }, - Trailing: "", + { + ": ", + papo.MessageParams { + Middle: []string { ":" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: " ", + { + " : ", + papo.MessageParams { + Middle: []string { }, + Trailing: " ", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: "", + { + " :", + papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { }, - Trailing: "", + { + " :", + papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a" }, - Trailing: "", + { + "a", + papo.MessageParams { + Middle: []string { "a" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "ab" }, - Trailing: "", + { + "ab", + papo.MessageParams { + Middle: []string { "ab" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "", + { + "a b", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a", "b", "c" }, - Trailing: "", + { + "a b c", + papo.MessageParams { + Middle: []string { "a", "b", "c" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a", "b:c" }, - Trailing: "", + { + "a b:c", + papo.MessageParams { + Middle: []string { "a", "b:c" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a", "b:c:" }, - Trailing: "", + { + "a b:c:", + papo.MessageParams { + Middle: []string { "a", "b:c:" }, + Trailing: "", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c", + { + "a b :c", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c:", + { + "a b :c:", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c:", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c ", + { + "a b :c ", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c ", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c", + { + "a b : c", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c ", + { + "a b : c ", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c ", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :", + { + "a b : c :", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c : ", + { + "a b : c : ", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c : ", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :d", + { + "a b : c :d", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :d", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :d ", + { + "a b : c :d ", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :d ", + }, }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c : d ", + { + "a b : c : d ", + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c : d ", + }, }, } - given := make([]papo.MessageParams, len(inputs)) - for i, input := range inputs { - given[i] = papo.ParseMessageParams(input) + for i, entry := range table { + given := papo.ParseMessageParams(entry.input) + assertEqualI(t, i, given, entry.expected) } - - assertEqual(t, given, expected) } func TestParseMessage(t *testing.T) { - inputs := []string { - "NICK joebloe ", - "USER joebloe 0.0.0.0 joe :Joe Bloe", - ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", - ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", - ":pre USER joebloe: 0:0:0:0 joe::a: : Joe Bloe ", - ":pre USER :Joe Bloe", - ":pre USER : Joe Bloe", - ":pre USER : Joe Bloe", - ":pre USER : ", - ":pre USER :", + type tableT struct { + input string + expected papo.Message } - expected := []papo.Message { - papo.Message { - Prefix: "", - Command: "NICK", - Params: papo.MessageParams { - Middle: []string { "joebloe" }, - Trailing: "", + table := []tableT { + { + "NICK joebloe ", + papo.Message { + Prefix: "", + Command: "NICK", + Params: papo.MessageParams { + Middle: []string { "joebloe" }, + Trailing: "", + }, + Raw: "NICK joebloe ", }, - Raw: "NICK joebloe ", }, - papo.Message { - Prefix: "", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { - "joebloe", "0.0.0.0", "joe", + { + "USER joebloe 0.0.0.0 joe :Joe Bloe", + papo.Message { + Prefix: "", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: "Joe Bloe", }, - Trailing: "Joe Bloe", + Raw: "USER joebloe 0.0.0.0 joe :Joe Bloe", }, - Raw: "USER joebloe 0.0.0.0 joe :Joe Bloe", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { - "joebloe", "0.0.0.0", "joe", + { + ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: "Joe Bloe", }, - Trailing: "Joe Bloe", + Raw: ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", }, - Raw: ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { - "joebloe", "0.0.0.0", "joe", + { + ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: " Joe Bloe ", }, - Trailing: " Joe Bloe ", + Raw: ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", }, - Raw: ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { - "joebloe:", "0:0:0:0", "joe::a:", + { + ":pre USER joebloe: 0:0:0:0 joe::a: : Joe Bloe ", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { + "joebloe:", "0:0:0:0", "joe::a:", + }, + Trailing: " Joe Bloe ", }, - Trailing: " Joe Bloe ", + Raw: ":pre USER joebloe: 0:0:0:0 joe::a: : Joe Bloe ", }, - Raw: ":pre USER joebloe: 0:0:0:0 joe::a: : Joe Bloe ", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: "Joe Bloe", + { + ":pre USER :Joe Bloe", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: "Joe Bloe", + }, + Raw: ":pre USER :Joe Bloe", }, - Raw: ":pre USER :Joe Bloe", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", + { + ":pre USER : Joe Bloe", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", }, - Raw: ":pre USER : Joe Bloe", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", + { + ":pre USER : Joe Bloe", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", }, - Raw: ":pre USER : Joe Bloe", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " ", + { + ":pre USER : ", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " ", + }, + Raw: ":pre USER : ", }, - Raw: ":pre USER : ", }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: "", + { + ":pre USER :", + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, + Raw: ":pre USER :", }, - Raw: ":pre USER :", }, } - given := make([]papo.Message, len(inputs)) - for i, input := range inputs { - parsed, err := papo.ParseMessage(input) + for i, entry := range table { + parsed, err := papo.ParseMessage(entry.input) errorIf(t, err) - given[i] = parsed + given := parsed + assertEqualI(t, i, given, entry.expected) } - - assertEqual(t, given, expected) } |