diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/functional/api-usage/gotext.go | 81 | ||||
-rw-r--r-- | tests/gotext.go | 528 |
2 files changed, 348 insertions, 261 deletions
diff --git a/tests/functional/api-usage/gotext.go b/tests/functional/api-usage/gotext.go index 7ea7119..88dcbd0 100644 --- a/tests/functional/api-usage/gotext.go +++ b/tests/functional/api-usage/gotext.go @@ -1,66 +1,105 @@ package gotext import ( + "fmt" "os" - - g "gobang" + "reflect" ) +func showColour() bool { + return os.Getenv("NO_COLOUR") == "" +} + +func testStart(name string) { + fmt.Fprintf(os.Stderr, "%s:\n", name) +} + +func testing(message string, body func()) { + if showColour() { + fmt.Fprintf( + os.Stderr, + "\033[0;33mtesting\033[0m: %s... ", + message, + ) + body() + fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "testing: %s... ", message) + body() + fmt.Fprintf(os.Stderr, "OK.\n") + } +} + +func assertEq(given any, expected any) { + if !reflect.DeepEqual(given, expected) { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } + fmt.Fprintf(os.Stderr, "given != expected\n") + fmt.Fprintf(os.Stderr, "given: %#v\n", given) + fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) + os.Exit(1) + } +} + + + func MainTest() { - g.Init() Init("tests", "locale/") - g.Testing("Español", func() { + testing("Español", func() { os.Setenv("LANGUAGE", "es_MX.UTF-8") SetLocale(LC_ALL, "") - g.TAssertEqual(Gettext("Hello, world!"), "¡Hola, mundo!") - g.TAssertEqual(Gettext("Good morning"), "Buenos días") - g.TAssertEqual(Gettext("Good bye!"), "¡Hasta luego!") - g.TAssertEqual(Sprintf( + assertEq(Gettext("Hello, world!"), "¡Hola, mundo!") + assertEq(Gettext("Good morning"), "Buenos días") + assertEq(Gettext("Good bye!"), "¡Hasta luego!") + assertEq(Sprintf( NGettext("An apple", "%d apples", 1), 1, "garbage", ), "Una manzana") - g.TAssertEqual(Sprintf( + assertEq(Sprintf( NGettext("An apple", "%d apples", 3), 3, ), "3 manzanas") }) - g.Testing("Deutsch", func() { + testing("Deutsch", func() { os.Setenv("LANGUAGE", "de_DE.UTF-8") SetLocale(LC_ALL, "") - g.TAssertEqual(Gettext("Hello, world!"), "Hallo, Welt!") - g.TAssertEqual(Gettext("Good morning"), "Guten Morgen") - g.TAssertEqual(Gettext("Good bye!"), "Auf Wiedersehen!") - g.TAssertEqual(Sprintf( + assertEq(Gettext("Hello, world!"), "Hallo, Welt!") + assertEq(Gettext("Good morning"), "Guten Morgen") + assertEq(Gettext("Good bye!"), "Auf Wiedersehen!") + assertEq(Sprintf( NGettext("An apple", "%d apples", 1), 1, "garbage", ), "Ein Apfel") - g.TAssertEqual(Sprintf( + assertEq(Sprintf( NGettext("An apple", "%d apples", 3), 3, ), "3 Äpfel") }) - g.Testing("Français", func() { + testing("Français", func() { os.Setenv("LANGUAGE", "fr_FR.UTF-8") SetLocale(LC_ALL, "") - g.TAssertEqual(Gettext("Hello, world!"), "Hello, world!") - g.TAssertEqual(Gettext("Good morning"), "Good morning") - g.TAssertEqual(Gettext("Good bye!"), "Good bye!") - g.TAssertEqual(Sprintf( + assertEq(Gettext("Hello, world!"), "Hello, world!") + assertEq(Gettext("Good morning"), "Good morning") + assertEq(Gettext("Good bye!"), "Good bye!") + assertEq(Sprintf( NGettext("An apple", "%d apples", 1), 1, "garbage", ), "An apple") - g.TAssertEqual(Sprintf( + assertEq(Sprintf( NGettext("An apple", "%d apples", 3), 3, ), "3 apples") diff --git a/tests/gotext.go b/tests/gotext.go index dfcbc99..18e2ab0 100644 --- a/tests/gotext.go +++ b/tests/gotext.go @@ -4,25 +4,68 @@ import ( "bytes" "fmt" "os" - - g "gobang" + "reflect" ) +func showColour() bool { + return os.Getenv("NO_COLOUR") == "" +} + +func testStart(name string) { + fmt.Fprintf(os.Stderr, "%s:\n", name) +} + +func testing(message string, body func()) { + if showColour() { + fmt.Fprintf( + os.Stderr, + "\033[0;33mtesting\033[0m: %s... ", + message, + ) + body() + fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "testing: %s... ", message) + body() + fmt.Fprintf(os.Stderr, "OK.\n") + } +} + +func assertEq(given any, expected any) { + if !reflect.DeepEqual(given, expected) { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } + fmt.Fprintf(os.Stderr, "given != expected\n") + fmt.Fprintf(os.Stderr, "given: %#v\n", given) + fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) + os.Exit(1) + } +} + func src(payload string) string { - f := g.Must(os.CreateTemp("", "gotext-temp-*.go")) - g.Must(f.WriteString(payload)) - g.TErrorIf(f.Close()) + f, err := os.CreateTemp("", "gotext-temp-*.go") + assertEq(err, nil) + + _, err = f.WriteString(payload) + assertEq(err, nil) + + err = f.Close() + assertEq(err, nil) + return f.Name() } func test_formatComment() { - g.TestStart("formatComment()") + testStart("formatComment()") - g.Testing("formatting comments FIXME", func() { + testing("formatting comments FIXME", func() { table := []struct{ in string out string @@ -34,30 +77,33 @@ func test_formatComment() { } for _, row := range table { - g.TAssertEqual(formatComment(row.in), row.out) + assertEq(formatComment(row.in), row.out) } }) } func test_processFiles() { - g.TestStart("processFiles()") + testStart("processFiles()") - g.Testing("simple commented file", func() { - fname := src(g.Heredoc(` - package main + testing("simple commented file", func() { + const s = `package main - func main() { - // TRANSLATORS: a comment - i18n.G("foo") - } - `)) +func main() { + // TRANSLATORS: a comment + i18n.G("foo") +} +` + + fname := src(s) defer os.Remove(fname) - msgIDs := g.TMust(processFiles(argsT{ + + msgIDs, err := processFiles(argsT{ subArgs: []string{fname}, keyword: "i18n.G", - })) + }) + assertEq(err, nil) - g.TAssertEqual(msgIDs, map[string][]msgIDT{ + assertEq(msgIDs, map[string][]msgIDT{ "foo": []msgIDT{ msgIDT{ comment: "#. TRANSLATORS: a comment\n", @@ -68,25 +114,28 @@ func test_processFiles() { }) }) - g.Testing("process multiple entries", func() { - fname := src(g.Heredoc(` - package main + testing("process multiple entries", func() { + const s = `package main + +func main() { + // TRANSLATORS: comment 1 + i18n.G("foo") - func main() { - // TRANSLATORS: comment 1 - i18n.G("foo") + // TRANSLATORS: comment 2 + i18n.G("foo") +} +` - // TRANSLATORS: comment 2 - i18n.G("foo") - } - `)) + fname := src(s) defer os.Remove(fname) - msgIDs := g.TMust(processFiles(argsT{ + + msgIDs, err := processFiles(argsT{ subArgs: []string{fname}, keyword: "i18n.G", - })) + }) + assertEq(err, nil) - g.TAssertEqual(msgIDs, map[string][]msgIDT{ + assertEq(msgIDs, map[string][]msgIDT{ "foo": []msgIDT{ { comment: "#. TRANSLATORS: comment 1\n", @@ -102,21 +151,25 @@ func test_processFiles() { }) }) - g.Testing("process files with concat", func() { - fname := src(g.Heredoc(` - package main + testing("process files with concat", func() { + const s = `package main + +func main() { + // TRANSLATORS: a comment + i18n.G("foo\n" + "bar\n" + "baz") +} +` + + fname := src(s) + defer os.Remove(fname) - func main() { - // TRANSLATORS: a comment - i18n.G("foo\n" + "bar\n" + "baz") - } - `)) - msgIDs := g.TMust(processFiles(argsT{ + msgIDs, err := processFiles(argsT{ subArgs: []string{fname}, keyword: "i18n.G", - })) + }) + assertEq(err, nil) - g.TAssertEqual(msgIDs, map[string][]msgIDT{ + assertEq(msgIDs, map[string][]msgIDT{ "foo\\nbar\\nbaz": []msgIDT{ { comment: "#. TRANSLATORS: a comment\n", @@ -127,45 +180,48 @@ func test_processFiles() { }) }) - g.Testing("file with quote", func() { - fname := src(fmt.Sprintf(g.Heredoc(` - package main + testing("file with quote", func() { + const s = `package main - func main() { - i18n.G(%[1]s foo "bar"%[1]s) - } - `), "`")) +func main() { + i18n.G(%[1]s foo "bar"%[1]s) +} +` + + fname := src(fmt.Sprintf(s, "`")) defer os.Remove(fname) - msgIDs := g.TMust(processFiles(argsT{ + + msgIDs, err := processFiles(argsT{ subArgs: []string{fname}, keyword: "i18n.G", - })) + }) + assertEq(err, nil) out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := fmt.Sprintf(g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: %s:4 - msgid " foo \"bar\"" - msgstr "" - - `), fname) - g.TAssertEqual(out.String(), expected) + const expectedRaw = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: %s:4 +msgid " foo \"bar\"" +msgstr "" + +` + + expected := fmt.Sprintf(expectedRaw, fname) + assertEq(out.String(), expected) }) } func test_writePotFile() { - g.TestStart("writePotFile()") + testStart("writePotFile()") - g.Testing("write simple file", func() { + testing("write simple file", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -178,25 +234,23 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #. foo - #: fname:2 - msgid "foo" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#. foo +#: fname:2 +msgid "foo" +msgstr "" + +` + assertEq(out.String(), expected) }) - g.Testing("write template with 2 entries", func() { + testing("write template with 2 entries", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -214,26 +268,25 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #. comment1 - #. comment2 - #: fname:2 fname:4 - msgid "foo" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#. comment1 +#. comment2 +#: fname:2 fname:4 +msgid "foo" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("template without comment", func() { + testing("template without comment", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -245,24 +298,23 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: fname:2 - msgid "foo" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: fname:2 +msgid "foo" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("output without location", func() { + testing("output without location", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -275,24 +327,23 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: fname:2 - msgid "foo" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: fname:2 +msgid "foo" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("output with hint", func() { + testing("output with hint", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -306,25 +357,24 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: fname:2 - #, c-format - msgid "foo" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: fname:2 +#, c-format +msgid "foo" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("output with plural", func() { + testing("output with plural", func() { msgIDs := map[string][]msgIDT{ "foo": []msgIDT{ { @@ -338,26 +388,25 @@ func test_writePotFile() { out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: fname:2 - msgid "foo" - msgid_plural "plural" - msgstr[0] "" - msgstr[1] "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: fname:2 +msgid "foo" +msgid_plural "plural" +msgstr[0] "" +msgstr[1] "" + +` + + assertEq(out.String(), expected) }) - g.Testing("multiline output", func() { + testing("multiline output", func() { msgIDs := map[string][]msgIDT{ "foo\\nbar\\nbaz": []msgIDT{ { @@ -369,28 +418,27 @@ func test_writePotFile() { } out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #. foo - #: fname:2 - msgid "" - "foo\n" - "bar\n" - "baz" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#. foo +#: fname:2 +msgid "" +"foo\n" +"bar\n" +"baz" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("output tidy output", func() { + testing("output tidy output", func() { msgIDs := map[string][]msgIDT{ "foo\\nbar\\nbaz": []msgIDT{ { @@ -407,70 +455,70 @@ func test_writePotFile() { } out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: fname:2 - msgid "" - "foo\n" - "bar\n" - "baz" - msgstr "" - - #: fname:4 - msgid "zzz\n" - msgstr "" - - `) - g.TAssertEqual(out.String(), expected) + const expected = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: fname:2 +msgid "" +"foo\n" +"bar\n" +"baz" +msgstr "" + +#: fname:4 +msgid "zzz\n" +msgstr "" + +` + + assertEq(out.String(), expected) }) - g.Testing("source with double quotes", func() { - fname := src(g.Heredoc(` - package main + testing("source with double quotes", func() { + const s = `package main + +func main() { + i18n.G("foo \"bar\"") +} +` - func main() { - i18n.G("foo \"bar\"") - } - `)) + fname := src(s) defer os.Remove(fname) - msgIDs := g.TMust(processFiles(argsT{ + + msgIDs, err := processFiles(argsT{ subArgs: []string{fname}, keyword: "i18n.G", - })) + }) + assertEq(err, nil) out := bytes.NewBuffer([]byte("")) writePotFile(out, msgIDs) - expected := fmt.Sprintf(g.Heredoc(` - #, fuzzy - msgid "" - msgstr "" - "Language: \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" - - #: %[1]s:4 - msgid "foo \"bar\"" - msgstr "" - - `), fname) - g.TAssertEqual(out.String(), expected) + const expectedRaw = `msgid "" +msgstr "" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: %[1]s:4 +msgid "foo \"bar\"" +msgstr "" + +` + + expected := fmt.Sprintf(expectedRaw, fname) + assertEq(out.String(), expected) }) } func MainTest() { - g.Init() - test_formatComment() test_processFiles() test_writePotFile() |