From cc98b12e78df32f1559daa3a96e089c50380b4fe Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 16 May 2024 11:11:22 -0300 Subject: Rename source files to "lib.go" and "main.go" --- tests/lib_test.go | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/papod_test.go | 379 ---------------------------------------------------- 2 files changed, 379 insertions(+), 379 deletions(-) create mode 100644 tests/lib_test.go delete mode 100644 tests/papod_test.go (limited to 'tests') diff --git a/tests/lib_test.go b/tests/lib_test.go new file mode 100644 index 0000000..9b7cd9f --- /dev/null +++ b/tests/lib_test.go @@ -0,0 +1,379 @@ +package papo_test + +import ( + "bufio" + "bytes" + "encoding/json" + "fmt" + "log/slog" + "reflect" + "strings" + "testing" + + "euandre.org/papo/src" +) + + +func errorIf(t *testing.T, err error) { + if err != nil { + t.Errorf("Unexpected error: %#v\n", err) + } +} + +func assertEqual(t *testing.T, given any, expected any) { + if !reflect.DeepEqual(given, expected) { + t.Errorf("given != expected") + 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", + } + expected := [][]string { + nil, + { "" }, + { "abc" }, + { "abc" }, + { "abc", " " }, + { " ", " " }, + { "aaa", "bbb", "ccc" }, + { "", "split \r \n CRLF", "" }, + } + given := make([][]string, len(inputs)) + + for i, input := range inputs { + scanner := bufio.NewScanner(strings.NewReader(input)) + scanner.Split(papo.SplitOnCRLF) + for scanner.Scan() { + given[i] = append(given[i], scanner.Text()) + } + + err := scanner.Err() + errorIf(t, err) + } + + 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", + } + expected := [][]string { + { "first message", "second message" }, + { "message 1", "message 2" }, + } + given := make([][]string, len(inputs)) + + for i, input := range inputs { + scanner := bufio.NewScanner(strings.NewReader(input)) + scanner.Split(papo.SplitOnRawMessage) + for scanner.Scan() { + given[i] = append(given[i], scanner.Text()) + } + + err := scanner.Err() + errorIf(t, err) + } + + 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 ", + } + expected := []papo.MessageParams { + 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: "", + }, + papo.MessageParams { + Middle: []string { "ab" }, + Trailing: "", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "", + }, + papo.MessageParams { + Middle: []string { "a", "b", "c" }, + Trailing: "", + }, + papo.MessageParams { + Middle: []string { "a", "b:c" }, + Trailing: "", + }, + papo.MessageParams { + Middle: []string { "a", "b:c:" }, + Trailing: "", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c:", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c ", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c ", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c : ", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :d", + }, + papo.MessageParams { + Middle: []string { "a", "b" }, + Trailing: " 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) + } + + 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 :", + } + expected := []papo.Message { + papo.Message { + Prefix: "", + Command: "NICK", + Params: papo.MessageParams { + Middle: []string { "joebloe" }, + Trailing: "", + }, + Raw: "NICK joebloe ", + }, + papo.Message { + Prefix: "", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: "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", + }, + Trailing: "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", + }, + Trailing: " 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:", + }, + Trailing: " 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", + }, + Raw: ":pre USER :Joe Bloe", + }, + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", + }, + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", + }, + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: " ", + }, + Raw: ":pre USER : ", + }, + papo.Message { + Prefix: "pre", + Command: "USER", + Params: papo.MessageParams { + Middle: []string { }, + Trailing: "", + }, + Raw: ":pre USER :", + }, + } + given := make([]papo.Message, len(inputs)) + + for i, input := range inputs { + parsed, err := papo.ParseMessage(input) + errorIf(t, err) + given[i] = parsed + } + + assertEqual(t, given, expected) +} + +func TestSetLoggerOutput(t *testing.T) { + return + type entry struct { + msg string `json:"msg"` + aKey string `json:"a-key"` + } + var e entry + var buf bytes.Buffer + papo.SetLoggerOutput(&buf) + slog.Error("the message", "a-key", "a-value") + + s := buf.String() + // fmt.Println(s) + // fmt.Println(e) + err := json.Unmarshal([]byte(s), &e) + if err != nil { + t.Fail() + } + if e.msg != "the message" { + t.Fail() + } + fmt.Println(1) + // fmt.Println(e) +} diff --git a/tests/papod_test.go b/tests/papod_test.go deleted file mode 100644 index 9b7cd9f..0000000 --- a/tests/papod_test.go +++ /dev/null @@ -1,379 +0,0 @@ -package papo_test - -import ( - "bufio" - "bytes" - "encoding/json" - "fmt" - "log/slog" - "reflect" - "strings" - "testing" - - "euandre.org/papo/src" -) - - -func errorIf(t *testing.T, err error) { - if err != nil { - t.Errorf("Unexpected error: %#v\n", err) - } -} - -func assertEqual(t *testing.T, given any, expected any) { - if !reflect.DeepEqual(given, expected) { - t.Errorf("given != expected") - 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", - } - expected := [][]string { - nil, - { "" }, - { "abc" }, - { "abc" }, - { "abc", " " }, - { " ", " " }, - { "aaa", "bbb", "ccc" }, - { "", "split \r \n CRLF", "" }, - } - given := make([][]string, len(inputs)) - - for i, input := range inputs { - scanner := bufio.NewScanner(strings.NewReader(input)) - scanner.Split(papo.SplitOnCRLF) - for scanner.Scan() { - given[i] = append(given[i], scanner.Text()) - } - - err := scanner.Err() - errorIf(t, err) - } - - 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", - } - expected := [][]string { - { "first message", "second message" }, - { "message 1", "message 2" }, - } - given := make([][]string, len(inputs)) - - for i, input := range inputs { - scanner := bufio.NewScanner(strings.NewReader(input)) - scanner.Split(papo.SplitOnRawMessage) - for scanner.Scan() { - given[i] = append(given[i], scanner.Text()) - } - - err := scanner.Err() - errorIf(t, err) - } - - 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 ", - } - expected := []papo.MessageParams { - 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: "", - }, - papo.MessageParams { - Middle: []string { "ab" }, - Trailing: "", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "", - }, - papo.MessageParams { - Middle: []string { "a", "b", "c" }, - Trailing: "", - }, - papo.MessageParams { - Middle: []string { "a", "b:c" }, - Trailing: "", - }, - papo.MessageParams { - Middle: []string { "a", "b:c:" }, - Trailing: "", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c:", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c ", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c ", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c : ", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :d", - }, - papo.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " 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) - } - - 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 :", - } - expected := []papo.Message { - papo.Message { - Prefix: "", - Command: "NICK", - Params: papo.MessageParams { - Middle: []string { "joebloe" }, - Trailing: "", - }, - Raw: "NICK joebloe ", - }, - papo.Message { - Prefix: "", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { - "joebloe", "0.0.0.0", "joe", - }, - Trailing: "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", - }, - Trailing: "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", - }, - Trailing: " 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:", - }, - Trailing: " 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", - }, - Raw: ":pre USER :Joe Bloe", - }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", - }, - Raw: ":pre USER : Joe Bloe", - }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", - }, - Raw: ":pre USER : Joe Bloe", - }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: " ", - }, - Raw: ":pre USER : ", - }, - papo.Message { - Prefix: "pre", - Command: "USER", - Params: papo.MessageParams { - Middle: []string { }, - Trailing: "", - }, - Raw: ":pre USER :", - }, - } - given := make([]papo.Message, len(inputs)) - - for i, input := range inputs { - parsed, err := papo.ParseMessage(input) - errorIf(t, err) - given[i] = parsed - } - - assertEqual(t, given, expected) -} - -func TestSetLoggerOutput(t *testing.T) { - return - type entry struct { - msg string `json:"msg"` - aKey string `json:"a-key"` - } - var e entry - var buf bytes.Buffer - papo.SetLoggerOutput(&buf) - slog.Error("the message", "a-key", "a-value") - - s := buf.String() - // fmt.Println(s) - // fmt.Println(e) - err := json.Unmarshal([]byte(s), &e) - if err != nil { - t.Fail() - } - if e.msg != "the message" { - t.Fail() - } - fmt.Println(1) - // fmt.Println(e) -} -- cgit v1.2.3