From 80cdec3927ea866aea27ec356ae1d3f525ae94d7 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 14 Aug 2024 17:11:33 -0300 Subject: Use "go tool" to build project --- tests/integration.sh | 4 + tests/lib_test.go | 514 ------------------------------------------------ tests/main.go | 7 + tests/papod.go | 540 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 551 insertions(+), 514 deletions(-) create mode 100755 tests/integration.sh delete mode 100644 tests/lib_test.go create mode 100644 tests/main.go create mode 100644 tests/papod.go (limited to 'tests') diff --git a/tests/integration.sh b/tests/integration.sh new file mode 100755 index 0000000..fcb62ca --- /dev/null +++ b/tests/integration.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu + +exit diff --git a/tests/lib_test.go b/tests/lib_test.go deleted file mode 100644 index 67fbb40..0000000 --- a/tests/lib_test.go +++ /dev/null @@ -1,514 +0,0 @@ -package papod_test - -import ( - "bufio" - "database/sql" - "errors" - "reflect" - "strings" - "testing" - - g "euandre.org/gobang/src" - - "euandre.org/papod/src" -) - - -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(papod.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(papod.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 papod.MessageParams - } - table := []tableT { - { - "", - papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - }, - { - " ", - papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - }, - { - " :", - papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - }, - { - " : ", - papod.MessageParams { - Middle: []string { }, - Trailing: " ", - }, - }, - { - ": ", - papod.MessageParams { - Middle: []string { ":" }, - Trailing: "", - }, - }, - { - ": ", - papod.MessageParams { - Middle: []string { ":" }, - Trailing: "", - }, - }, - { - " : ", - papod.MessageParams { - Middle: []string { }, - Trailing: " ", - }, - }, - { - " :", - papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - }, - { - " :", - papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - }, - { - "a", - papod.MessageParams { - Middle: []string { "a" }, - Trailing: "", - }, - }, - { - "ab", - papod.MessageParams { - Middle: []string { "ab" }, - Trailing: "", - }, - }, - { - "a b", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "", - }, - }, - { - "a b c", - papod.MessageParams { - Middle: []string { "a", "b", "c" }, - Trailing: "", - }, - }, - { - "a b:c", - papod.MessageParams { - Middle: []string { "a", "b:c" }, - Trailing: "", - }, - }, - { - "a b:c:", - papod.MessageParams { - Middle: []string { "a", "b:c:" }, - Trailing: "", - }, - }, - { - "a b :c", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c", - }, - }, - { - "a b :c:", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c:", - }, - }, - { - "a b :c ", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: "c ", - }, - }, - { - "a b : c", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c", - }, - }, - { - "a b : c ", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c ", - }, - }, - { - "a b : c :", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :", - }, - }, - { - "a b : c : ", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c : ", - }, - }, - { - "a b : c :d", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :d", - }, - }, - { - "a b : c :d ", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c :d ", - }, - }, - { - "a b : c : d ", - papod.MessageParams { - Middle: []string { "a", "b" }, - Trailing: " c : d ", - }, - }, - } - - for i, entry := range table { - given := papod.ParseMessageParams(entry.input) - assertEqualI(t, i, given, entry.expected) - } -} - -func TestParseMessage(t *testing.T) { - type tableTOK struct { - input string - expected papod.Message - } - tableOK := []tableTOK {{ - "NICK joebloe ", - papod.Message { - Prefix: "", - Command: "NICK", - Params: papod.MessageParams { - Middle: []string { "joebloe" }, - Trailing: "", - }, - Raw: "NICK joebloe ", - }, - }, { - "USER joebloe 0.0.0.0 joe :Joe Bloe", - papod.Message { - Prefix: "", - Command: "USER", - Params: papod.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", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.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 ", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.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 ", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.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", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.MessageParams { - Middle: []string { }, - Trailing: "Joe Bloe", - }, - Raw: ":pre USER :Joe Bloe", - }, - }, { - ":pre USER : Joe Bloe", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", - }, - Raw: ":pre USER : Joe Bloe", - }, - }, { - ":pre USER : Joe Bloe", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.MessageParams { - Middle: []string { }, - Trailing: " Joe Bloe", - }, - Raw: ":pre USER : Joe Bloe", - }, - }, { - ":pre USER : ", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.MessageParams { - Middle: []string { }, - Trailing: " ", - }, - Raw: ":pre USER : ", - }, - }, { - ":pre USER :", - papod.Message { - Prefix: "pre", - Command: "USER", - Params: papod.MessageParams { - Middle: []string { }, - Trailing: "", - }, - Raw: ":pre USER :", - }, - }} - - for i, entry := range tableOK { - given, err := papod.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 := papod.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++ { - papod.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) - - papod.InitMigrations(db) - pending1 := papod.PendingMigrations(db) - pending2 := papod.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++ { - papod.RunMigrations(db) - } -} diff --git a/tests/main.go b/tests/main.go new file mode 100644 index 0000000..f32854e --- /dev/null +++ b/tests/main.go @@ -0,0 +1,7 @@ +package main + +import "papod" + +func main() { + papod.MainTest() +} 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()) +} -- cgit v1.2.3