diff options
Diffstat (limited to 'tests/papod.go')
-rw-r--r-- | tests/papod.go | 540 |
1 files changed, 540 insertions, 0 deletions
diff --git a/tests/papod.go b/tests/papod.go new file mode 100644 index 0000000..af2a5f7 --- /dev/null +++ b/tests/papod.go @@ -0,0 +1,540 @@ +package papod + +import ( + "bufio" + "database/sql" + "errors" + "os" + "reflect" + "strings" + "testing" + "testing/internal/testdeps" + + g "gobang" +) + + +func errorIf(t *testing.T, err error) { + if err != nil { + t.Errorf("Unexpected error: %#v\n", err) + } +} + +func errorIfNotI(t *testing.T, i int, err error) { + if err == nil { + t.Errorf("Expected error, got nil (i = %d)\n", i) + } +} + +func assertEqualI(t *testing.T, i int, given any, expected any) { + if !reflect.DeepEqual(given, expected) { + t.Errorf("given != expected (i = %d)\n", i) + t.Errorf("given: %#v\n", given) + t.Errorf("expected: %#v\n", expected) + } +} + +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) { + type tableT struct { + input string + expected []string + } + 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", "" }, + }, + } + + for i, entry := range table { + var given []string + scanner := bufio.NewScanner(strings.NewReader(entry.input)) + scanner.Split(SplitOnCRLF) + for scanner.Scan() { + given = append(given, scanner.Text()) + } + + err := scanner.Err() + errorIf(t, err) + assertEqualI(t, i, given, entry.expected) + } +} + +func TestSplitOnRawMessage(t *testing.T) { + type tableT struct { + input string + expected []string + } + 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" }, + }, + } + + + for i, entry := range table { + var given []string + scanner := bufio.NewScanner(strings.NewReader(entry.input)) + scanner.Split(SplitOnRawMessage) + for scanner.Scan() { + given = append(given, scanner.Text()) + } + + err := scanner.Err() + errorIf(t, err) + assertEqualI(t, i, given, entry.expected) + } +} + +func TestParseMessageParams(t *testing.T) { + type tableT struct { + input string + expected MessageParams + } + table := []tableT { + { + "", + MessageParams { + Middle: []string { }, + Trailing: "", + }, + }, + { + " ", + MessageParams { + Middle: []string { }, + Trailing: "", + }, + }, + { + " :", + MessageParams { + Middle: []string { }, + Trailing: "", + }, + }, + { + " : ", + MessageParams { + Middle: []string { }, + Trailing: " ", + }, + }, + { + ": ", + MessageParams { + Middle: []string { ":" }, + Trailing: "", + }, + }, + { + ": ", + MessageParams { + Middle: []string { ":" }, + Trailing: "", + }, + }, + { + " : ", + MessageParams { + Middle: []string { }, + Trailing: " ", + }, + }, + { + " :", + MessageParams { + Middle: []string { }, + Trailing: "", + }, + }, + { + " :", + MessageParams { + Middle: []string { }, + Trailing: "", + }, + }, + { + "a", + MessageParams { + Middle: []string { "a" }, + Trailing: "", + }, + }, + { + "ab", + MessageParams { + Middle: []string { "ab" }, + Trailing: "", + }, + }, + { + "a b", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: "", + }, + }, + { + "a b c", + MessageParams { + Middle: []string { "a", "b", "c" }, + Trailing: "", + }, + }, + { + "a b:c", + MessageParams { + Middle: []string { "a", "b:c" }, + Trailing: "", + }, + }, + { + "a b:c:", + MessageParams { + Middle: []string { "a", "b:c:" }, + Trailing: "", + }, + }, + { + "a b :c", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c", + }, + }, + { + "a b :c:", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c:", + }, + }, + { + "a b :c ", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: "c ", + }, + }, + { + "a b : c", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c", + }, + }, + { + "a b : c ", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c ", + }, + }, + { + "a b : c :", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :", + }, + }, + { + "a b : c : ", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c : ", + }, + }, + { + "a b : c :d", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :d", + }, + }, + { + "a b : c :d ", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c :d ", + }, + }, + { + "a b : c : d ", + MessageParams { + Middle: []string { "a", "b" }, + Trailing: " c : d ", + }, + }, + } + + for i, entry := range table { + given := ParseMessageParams(entry.input) + assertEqualI(t, i, given, entry.expected) + } +} + +func TestParseMessage(t *testing.T) { + type tableTOK struct { + input string + expected Message + } + tableOK := []tableTOK {{ + "NICK joebloe ", + Message { + Prefix: "", + Command: "NICK", + Params: MessageParams { + Middle: []string { "joebloe" }, + Trailing: "", + }, + Raw: "NICK joebloe ", + }, + }, { + "USER joebloe 0.0.0.0 joe :Joe Bloe", + Message { + Prefix: "", + Command: "USER", + Params: MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: "Joe Bloe", + }, + Raw: "USER joebloe 0.0.0.0 joe :Joe Bloe", + }, + }, { + ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: "Joe Bloe", + }, + Raw: ":pre USER joebloe 0.0.0.0 joe :Joe Bloe", + }, + }, { + ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { + "joebloe", "0.0.0.0", "joe", + }, + Trailing: " Joe Bloe ", + }, + Raw: ":pre USER joebloe 0.0.0.0 joe : Joe Bloe ", + }, + }, { + ":pre USER joebloe: 0:0:0:1 joe::a: : Joe Bloe ", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { + "joebloe:", "0:0:0:1", "joe::a:", + }, + Trailing: " Joe Bloe ", + }, + Raw: ":pre USER joebloe: 0:0:0:1 joe::a: : Joe Bloe ", + }, + }, { + ":pre USER :Joe Bloe", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { }, + Trailing: "Joe Bloe", + }, + Raw: ":pre USER :Joe Bloe", + }, + }, { + ":pre USER : Joe Bloe", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", + }, + }, { + ":pre USER : Joe Bloe", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { }, + Trailing: " Joe Bloe", + }, + Raw: ":pre USER : Joe Bloe", + }, + }, { + ":pre USER : ", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { }, + Trailing: " ", + }, + Raw: ":pre USER : ", + }, + }, { + ":pre USER :", + Message { + Prefix: "pre", + Command: "USER", + Params: MessageParams { + Middle: []string { }, + Trailing: "", + }, + Raw: ":pre USER :", + }, + }} + + for i, entry := range tableOK { + given, err := ParseMessage(entry.input) + errorIf(t, err) + assertEqualI(t, i, given, entry.expected) + } + + + type tableErrorT struct { + input string + expected error + } + parseErr := errors.New("Can't parse message") + tableError := []tableErrorT { + { + ":pre", + parseErr, + }, + { + ": pre", + parseErr, + }, + { + ":pre N1CK", + parseErr, + }, + } + + for i, entry := range tableError { + _, given := ParseMessage(entry.input) + assertEqualI(t, i, given, entry.expected) + } +} + +func TestInitMigrations(t *testing.T) { + const query = `SELECT filename FROM migrations;` + + db, err := sql.Open("sqlite3", ":memory:") + g.FatalIf(err) + + _, err = db.Query(query) + assertEqual(t, err.Error(), "no such table: migrations") + + for i := 0; i < 5; i++ { + InitMigrations(db) + rows, err := db.Query(query) + g.FatalIf(err) + assertEqual(t, rows.Next(), false) + g.FatalIf(rows.Err()) + } +} + +func TestPendingMigrations(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + g.FatalIf(err) + + InitMigrations(db) + pending1 := PendingMigrations(db) + pending2 := PendingMigrations(db) + + assertEqual(t, pending1, pending2) +} + +func TestRunMigrations(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + g.FatalIf(err) + + for i := 0; i < 5; i++ { + RunMigrations(db) + } +} + + + +func MainTest() { + tests := []testing.InternalTest { + { "TestSplitOnCRLF", TestSplitOnCRLF }, + { "TestSplitOnRawMessage", TestSplitOnRawMessage }, + { "TestParseMessageParams", TestParseMessageParams }, + { "TestParseMessage", TestParseMessage }, + { "TestInitMigrations", TestInitMigrations }, + { "TestPendingMigrations", TestPendingMigrations }, + { "TestRunMigrations", TestRunMigrations }, + } + + benchmarks := []testing.InternalBenchmark {} + fuzzTargets := []testing.InternalFuzzTarget {} + examples := []testing.InternalExample {} + m := testing.MainStart( + testdeps.TestDeps {}, + tests, + benchmarks, + fuzzTargets, + examples, + ) + os.Exit(m.Run()) +} |