diff options
author | EuAndreh <eu@euandre.org> | 2025-05-13 06:03:56 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-05-14 07:05:02 -0300 |
commit | 5409f0d17026fe473b0a42264323ecb8ab78a5c5 (patch) | |
tree | 3fa5e436b5cd0cd725c8daeaadcd1cdeb8ee85b5 /tests | |
parent | Absorb gettext_test.go (diff) | |
download | gotext-5409f0d17026fe473b0a42264323ecb8ab78a5c5.tar.gz gotext-5409f0d17026fe473b0a42264323ecb8ab78a5c5.tar.xz |
Absorb go-xgettext/main_test.go
Diffstat (limited to 'tests')
-rw-r--r-- | tests/functional/extraction/gotext.go | 4 | ||||
l--------- | tests/functional/extraction/main.go | 1 | ||||
-rw-r--r-- | tests/functional/runtime/gotext.go | 4 | ||||
l--------- | tests/functional/runtime/main.go | 1 | ||||
-rw-r--r-- | tests/gotext.go | 509 | ||||
-rwxr-xr-x | tests/integration.sh | 65 |
6 files changed, 584 insertions, 0 deletions
diff --git a/tests/functional/extraction/gotext.go b/tests/functional/extraction/gotext.go new file mode 100644 index 0000000..36d1327 --- /dev/null +++ b/tests/functional/extraction/gotext.go @@ -0,0 +1,4 @@ +package gotext + +func MainTest() { +} diff --git a/tests/functional/extraction/main.go b/tests/functional/extraction/main.go new file mode 120000 index 0000000..f67563d --- /dev/null +++ b/tests/functional/extraction/main.go @@ -0,0 +1 @@ +../../main.go
\ No newline at end of file diff --git a/tests/functional/runtime/gotext.go b/tests/functional/runtime/gotext.go new file mode 100644 index 0000000..36d1327 --- /dev/null +++ b/tests/functional/runtime/gotext.go @@ -0,0 +1,4 @@ +package gotext + +func MainTest() { +} diff --git a/tests/functional/runtime/main.go b/tests/functional/runtime/main.go new file mode 120000 index 0000000..f67563d --- /dev/null +++ b/tests/functional/runtime/main.go @@ -0,0 +1 @@ +../../main.go
\ No newline at end of file diff --git a/tests/gotext.go b/tests/gotext.go index 36d1327..e679bda 100644 --- a/tests/gotext.go +++ b/tests/gotext.go @@ -1,4 +1,513 @@ package gotext +import ( + "bytes" + "fmt" + "os" + + g "gobang" +) + + + +func src(payload string) string { + f := g.Must(os.CreateTemp("", "gotext-temp-*.go")) + g.Must(f.WriteString(payload)) + g.TErrorIf(f.Close()) + return f.Name() +} + + + +func test_formatComment() { + g.TestStart("formatComment()") + + g.Testing("formatting comments FIXME", func() { + table := []struct{ + in string + out string + }{ + {"// foo ", "#. foo\n"}, + {"/* foo */", "#. foo\n"}, + {"/* foo\n */", "#. foo\n"}, + {"/* foo\nbar */", "#. foo\n#. bar\n"}, + } + + for _, row := range table { + g.TAssertEqual(formatComment(row.in), row.out) + } + }) +} + +func test_processFiles() { + g.TestStart("processFiles()") + + g.Testing("simple commented file", func() { + fname := src(g.Heredoc(` + package main + + func main() { + // TRANSLATORS: a comment + i18n.G("foo") + } + `)) + defer os.Remove(fname) + g.TErrorIf(processFiles([]string{fname})) + + g.TAssertEqual(msgIDs, map[string][]msgID{ + "foo": []msgID{ + msgID{ + comment: "#. TRANSLATORS: a comment\n", + fname: fname, + line: 5, + }, + }, + }) + }) + + g.Testing("process multiple entries", func() { + fname := src(g.Heredoc(` + package main + + func main() { + // TRANSLATORS: comment 1 + i18n.G("foo") + + // TRANSLATORS: comment 2 + i18n.G("foo") + } + `)) + defer os.Remove(fname) + g.TErrorIf(processFiles([]string{fname})) + + g.TAssertEqual(msgIDs, map[string][]msgID{ + "foo": []msgID{ + { + comment: "#. TRANSLATORS: comment 1\n", + fname: fname, + line: 5, + }, + { + comment: "#. TRANSLATORS: comment 2\n", + fname: fname, + line: 8, + }, + }, + }) + }) + + g.Testing("process files with concat", func() { + msgIDs = nil + opts.NoLocation = true + fname := src(g.Heredoc(` + package main + + func main() { + // TRANSLATORS: a comment + i18n.G("foo\n" + "bar\n" + "baz") + } + `)) + g.TErrorIf(processFiles([]string{fname})) + opts.NoLocation = false + + g.TAssertEqual(msgIDs, map[string][]msgID{ + "foo\\nbar\\nbaz": []msgID{ + { + comment: "#. TRANSLATORS: a comment\n", + fname: fname, + line: 5, + }, + }, + }) + }) + + g.Testing("file with quote", func() { + fname := src(fmt.Sprintf(g.Heredoc(` + package main + + func main() { + i18n.G(%[1]s foo "bar"%[1]s) + } + `), "`")) + defer os.Remove(fname) + g.TErrorIf(processFiles([]string{fname})) + + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) +} + +func test_writePotFile() { + g.TestStart("writePotFile()") + + g.Testing("write simple file", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + fname: "fname", + line: 2, + comment: "#. foo\n", + }, + }, + } + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) + + g.Testing("write template with 2 entries", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + fname: "fname", + line: 2, + comment: "#. comment1\n", + }, + { + fname: "fname", + line: 4, + comment: "#. comment2\n", + }, + }, + } + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) + + g.Testing("template without comment", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + fname: "fname", + line: 2, + }, + }, + } + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) + + g.Testing("output without location", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + fname: "fname", + line: 2, + }, + }, + } + + opts.NoLocation = true + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + opts.NoLocation = false + + 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" + + msgid "foo" + msgstr "" + + `) + g.TAssertEqual(out.String(), expected) + }) + + g.Testing("output with hint", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + fname: "fname", + line: 2, + formatHint: "c-format", + }, + }, + } + + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) + + g.Testing("output with plural", func() { + msgIDs = map[string][]msgID{ + "foo": []msgID{ + { + msgidPlural: "plural", + fname: "fname", + line: 2, + }, + }, + } + + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) + + g.Testing("sorted output", func() { + msgIDs = map[string][]msgID{ + "aaa": []msgID{ + { + fname: "fname", + line: 2, + }, + }, + "zzz": []msgID{ + { + fname: "fname", + line: 2, + }, + }, + } + + opts.SortOutput = true + // we need to run this a bunch of times as the ordering might + // be right by pure chance + // FIXME + for i := 0; i < 10; i++ { + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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 "aaa" + msgstr "" + + #: fname:2 + msgid "zzz" + msgstr "" + + `) + g.TAssertEqual(out.String(), expected) + } + opts.SortOutput = false + }) + + g.Testing("multiline output", func() { + msgIDs = map[string][]msgID{ + "foo\\nbar\\nbaz": []msgID{ + { + fname: "fname", + line: 2, + comment: "#. foo\n", + }, + }, + } + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + 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) + }) + + g.Testing("output tidy output", func() { + msgIDs = map[string][]msgID{ + "foo\\nbar\\nbaz": []msgID{ + { + fname: "fname", + line: 2, + }, + }, + "zzz\\n": []msgID{ + { + fname: "fname", + line: 4, + }, + }, + } + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + 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) + }) + + g.Testing("source with double quotes", func() { + fname := src(g.Heredoc(` + package main + + func main() { + i18n.G("foo \"bar\"") + } + `)) + defer os.Remove(fname) + g.TErrorIf(processFiles([]string{fname})) + + out := bytes.NewBuffer([]byte("")) + writePotFile(out) + + 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) + }) +} + + + func MainTest() { + g.Init() + + test_formatComment() + test_processFiles() + test_writePotFile() } diff --git a/tests/integration.sh b/tests/integration.sh index e69de29..fb22072 100755 --- a/tests/integration.sh +++ b/tests/integration.sh @@ -0,0 +1,65 @@ +#!/bin/sh +set -euo pipefail + +exit + +// FIXME +// func (s *xgettextTestSuite) TestIntegration(c *C) { +// fname := makeGoSourceFile(c, []byte(`package main +// +// func main() { +// // TRANSLATORS: foo comment +// // with multiple lines +// i18n.G("foo") +// +// // this comment has no translators tag +// i18n.G("abc") +// +// // TRANSLATORS: plural +// i18n.NG("singular", "plural", 99) +// +// i18n.G("zz %s") +// } +// `)) +// +// // a real integration test :) +// outName := filepath.Join(c.MkDir(), "snappy.pot") +// os.Args = []string{"test-binary", +// "--output", outName, +// "--keyword", "i18n.G", +// "--keyword-plural", "i18n.NG", +// "--msgid-bugs-address", "snappy-devel@lists.ubuntu.com", +// "--package-name", "snappy", +// fname, +// } +// main() +// +// // verify its what we expect +// got, err := ioutil.ReadFile(outName) +// c.Assert(err, IsNil) +// expected := fmt.Sprintf(`%s +// #: %[2]s:9 +// msgid "abc" +// msgstr "" +// +// #. TRANSLATORS: foo comment +// #. with multiple lines +// #: %[2]s:6 +// msgid "foo" +// msgstr "" +// +// #. TRANSLATORS: plural +// #: %[2]s:12 +// msgid "singular" +// msgid_plural "plural" +// msgstr[0] "" +// msgstr[1] "" +// +// #: %[2]s:14 +// #, c-format +// msgid "zz %%s" +// msgstr "" +// +// `, header, fname) +// c.Assert(string(got), Equals, expected) +// } |