aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--deps.mk18
-rw-r--r--go-xgettext/main_test.go523
-rw-r--r--src/gotext.go66
-rw-r--r--tests/functional/extraction/gotext.go4
l---------tests/functional/extraction/main.go1
-rw-r--r--tests/functional/runtime/gotext.go4
l---------tests/functional/runtime/main.go1
-rw-r--r--tests/gotext.go509
-rwxr-xr-xtests/integration.sh65
9 files changed, 640 insertions, 551 deletions
diff --git a/deps.mk b/deps.mk
index 6ce7714..e7357e4 100644
--- a/deps.mk
+++ b/deps.mk
@@ -2,6 +2,8 @@ libs.go = \
src/gotext.go \
tests/benchmarks/gettext/gotext.go \
tests/functional/api-usage/gotext.go \
+ tests/functional/extraction/gotext.go \
+ tests/functional/runtime/gotext.go \
tests/fuzz/api/gotext.go \
tests/gotext.go \
@@ -9,6 +11,8 @@ mains.go = \
src/main.go \
tests/benchmarks/gettext/main.go \
tests/functional/api-usage/main.go \
+ tests/functional/extraction/main.go \
+ tests/functional/runtime/main.go \
tests/fuzz/api/main.go \
tests/main.go \
@@ -26,9 +30,13 @@ sources.po = \
functional-tests/lib.go = \
tests/functional/api-usage/gotext.go \
+ tests/functional/extraction/gotext.go \
+ tests/functional/runtime/gotext.go \
functional-tests/main.go = \
tests/functional/api-usage/main.go \
+ tests/functional/extraction/main.go \
+ tests/functional/runtime/main.go \
fuzz-targets/lib.go = \
tests/fuzz/api/gotext.go \
@@ -48,6 +56,10 @@ tests/benchmarks/gettext/gotext.a: tests/benchmarks/gettext/gotext.go
tests/benchmarks/gettext/main.a: tests/benchmarks/gettext/main.go
tests/functional/api-usage/gotext.a: tests/functional/api-usage/gotext.go
tests/functional/api-usage/main.a: tests/functional/api-usage/main.go
+tests/functional/extraction/gotext.a: tests/functional/extraction/gotext.go
+tests/functional/extraction/main.a: tests/functional/extraction/main.go
+tests/functional/runtime/gotext.a: tests/functional/runtime/gotext.go
+tests/functional/runtime/main.a: tests/functional/runtime/main.go
tests/fuzz/api/gotext.a: tests/fuzz/api/gotext.go
tests/fuzz/api/main.a: tests/fuzz/api/main.go
tests/gotext.a: tests/gotext.go
@@ -55,15 +67,21 @@ tests/main.a: tests/main.go
src/main.bin: src/main.a
tests/benchmarks/gettext/main.bin: tests/benchmarks/gettext/main.a
tests/functional/api-usage/main.bin: tests/functional/api-usage/main.a
+tests/functional/extraction/main.bin: tests/functional/extraction/main.a
+tests/functional/runtime/main.bin: tests/functional/runtime/main.a
tests/fuzz/api/main.bin: tests/fuzz/api/main.a
tests/main.bin: tests/main.a
src/main.bin-check: src/main.bin
tests/benchmarks/gettext/main.bin-check: tests/benchmarks/gettext/main.bin
tests/functional/api-usage/main.bin-check: tests/functional/api-usage/main.bin
+tests/functional/extraction/main.bin-check: tests/functional/extraction/main.bin
+tests/functional/runtime/main.bin-check: tests/functional/runtime/main.bin
tests/fuzz/api/main.bin-check: tests/fuzz/api/main.bin
tests/main.bin-check: tests/main.bin
src/main.a: src/$(NAME).a
tests/benchmarks/gettext/main.a: tests/benchmarks/gettext/$(NAME).a
tests/functional/api-usage/main.a: tests/functional/api-usage/$(NAME).a
+tests/functional/extraction/main.a: tests/functional/extraction/$(NAME).a
+tests/functional/runtime/main.a: tests/functional/runtime/$(NAME).a
tests/fuzz/api/main.a: tests/fuzz/api/$(NAME).a
tests/main.a: tests/$(NAME).a
diff --git a/go-xgettext/main_test.go b/go-xgettext/main_test.go
deleted file mode 100644
index 59159dc..0000000
--- a/go-xgettext/main_test.go
+++ /dev/null
@@ -1,523 +0,0 @@
-// -*- Mode: Go; indent-tabs-mode: t -*-
-
-/*
- * Copyright (C) 2015-2016 Canonical Ltd
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use, copy,
- * modify, merge, publish, distribute, sublicense, and/or sell copies
- * of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
-
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package main
-
-import (
- "bytes"
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
-
- . "gopkg.in/check.v1"
-)
-
-// Hook up check.v1 into the "go test" runner
-func Test(t *testing.T) { TestingT(t) }
-
-type xgettextTestSuite struct {
-}
-
-var _ = Suite(&xgettextTestSuite{})
-
-// test helper
-func makeGoSourceFile(c *C, content []byte) string {
- fname := filepath.Join(c.MkDir(), "foo.go")
- err := ioutil.WriteFile(fname, []byte(content), 0644)
- c.Assert(err, IsNil)
-
- return fname
-}
-
-func (s *xgettextTestSuite) SetUpTest(c *C) {
- // our test defaults
- opts.NoLocation = false
- opts.AddCommentsTag = "TRANSLATORS:"
- opts.Keyword = "i18n.G"
- opts.KeywordPlural = "i18n.NG"
- opts.SortOutput = true
- opts.PackageName = "snappy"
- opts.MsgIDBugsAddress = "snappy-devel@lists.ubuntu.com"
-
- // mock time
- formatTime = func() string {
- return "2015-06-30 14:48+0200"
- }
-}
-
-func (s *xgettextTestSuite) TestFormatComment(c *C) {
- var tests = []struct {
- in string
- out string
- }{
- {in: "// foo ", out: "#. foo\n"},
- {in: "/* foo */", out: "#. foo\n"},
- {in: "/* foo\n */", out: "#. foo\n"},
- {in: "/* foo\nbar */", out: "#. foo\n#. bar\n"},
- }
-
- for _, test := range tests {
- c.Assert(formatComment(test.in), Equals, test.out)
- }
-}
-
-func (s *xgettextTestSuite) TestProcessFilesSimple(c *C) {
- fname := makeGoSourceFile(c, []byte(`package main
-
-func main() {
- // TRANSLATORS: foo comment
- i18n.G("foo")
-}
-`))
- err := processFiles([]string{fname})
- c.Assert(err, IsNil)
-
- c.Assert(msgIDs, DeepEquals, map[string][]msgID{
- "foo": []msgID{
- {
- comment: "#. TRANSLATORS: foo comment\n",
- fname: fname,
- line: 5,
- },
- },
- })
-}
-
-func (s *xgettextTestSuite) TestProcessFilesMultiple(c *C) {
- fname := makeGoSourceFile(c, []byte(`package main
-
-func main() {
- // TRANSLATORS: foo comment
- i18n.G("foo")
-
- // TRANSLATORS: bar comment
- i18n.G("foo")
-}
-`))
- err := processFiles([]string{fname})
- c.Assert(err, IsNil)
-
- c.Assert(msgIDs, DeepEquals, map[string][]msgID{
- "foo": []msgID{
- {
- comment: "#. TRANSLATORS: foo comment\n",
- fname: fname,
- line: 5,
- },
- {
- comment: "#. TRANSLATORS: bar comment\n",
- fname: fname,
- line: 8,
- },
- },
- })
-}
-
-const header = `# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
-#
-#, fuzzy
-msgid ""
-msgstr "Project-Id-Version: snappy\n"
- "Report-Msgid-Bugs-To: snappy-devel@lists.ubuntu.com\n"
- "POT-Creation-Date: 2015-06-30 14:48+0200\n"
- "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
- "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
- "Language-Team: LANGUAGE <LL@li.org>\n"
- "Language: \n"
- "MIME-Version: 1.0\n"
- "Content-Type: text/plain; charset=CHARSET\n"
- "Content-Transfer-Encoding: 8bit\n"
-`
-
-func (s *xgettextTestSuite) TestWriteOutputSimple(c *C) {
- msgIDs = map[string][]msgID{
- "foo": []msgID{
- {
- fname: "fname",
- line: 2,
- comment: "#. foo\n",
- },
- },
- }
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#. foo
-#: fname:2
-msgid "foo"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputMultiple(c *C) {
- 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 := fmt.Sprintf(`%s
-#. comment1
-#. comment2
-#: fname:2 fname:4
-msgid "foo"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputNoComment(c *C) {
- msgIDs = map[string][]msgID{
- "foo": []msgID{
- {
- fname: "fname",
- line: 2,
- },
- },
- }
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: fname:2
-msgid "foo"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputNoLocation(c *C) {
- msgIDs = map[string][]msgID{
- "foo": []msgID{
- {
- fname: "fname",
- line: 2,
- },
- },
- }
-
- opts.NoLocation = true
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-msgid "foo"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputFormatHint(c *C) {
- msgIDs = map[string][]msgID{
- "foo": []msgID{
- {
- fname: "fname",
- line: 2,
- formatHint: "c-format",
- },
- },
- }
-
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: fname:2
-#, c-format
-msgid "foo"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputPlural(c *C) {
- msgIDs = map[string][]msgID{
- "foo": []msgID{
- {
- msgidPlural: "plural",
- fname: "fname",
- line: 2,
- },
- },
- }
-
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: fname:2
-msgid "foo"
-msgid_plural "plural"
-msgstr[0] ""
-msgstr[1] ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputSorted(c *C) {
- 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
- for i := 0; i < 10; i++ {
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: fname:2
-msgid "aaa"
-msgstr ""
-
-#: fname:2
-msgid "zzz"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
- }
-}
-
-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)
-}
-
-func (s *xgettextTestSuite) TestProcessFilesConcat(c *C) {
- fname := makeGoSourceFile(c, []byte(`package main
-
-func main() {
- // TRANSLATORS: foo comment
- i18n.G("foo\n" + "bar\n" + "baz")
-}
-`))
- err := processFiles([]string{fname})
- c.Assert(err, IsNil)
-
- c.Assert(msgIDs, DeepEquals, map[string][]msgID{
- "foo\\nbar\\nbaz": []msgID{
- {
- comment: "#. TRANSLATORS: foo comment\n",
- fname: fname,
- line: 5,
- },
- },
- })
-}
-
-func (s *xgettextTestSuite) TestProcessFilesWithQuote(c *C) {
- fname := makeGoSourceFile(c, []byte(fmt.Sprintf(`package main
-
-func main() {
- i18n.G(%[1]s foo "bar"%[1]s)
-}
-`, "`")))
- err := processFiles([]string{fname})
- c.Assert(err, IsNil)
-
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: %[2]s:4
-msgid " foo \"bar\""
-msgstr ""
-
-`, header, fname)
- c.Check(out.String(), Equals, expected)
-
-}
-
-func (s *xgettextTestSuite) TestWriteOutputMultilines(c *C) {
- msgIDs = map[string][]msgID{
- "foo\\nbar\\nbaz": []msgID{
- {
- fname: "fname",
- line: 2,
- comment: "#. foo\n",
- },
- },
- }
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
- expected := fmt.Sprintf(`%s
-#. foo
-#: fname:2
-msgid "foo\n"
- "bar\n"
- "baz"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestWriteOutputTidy(c *C) {
- 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 := fmt.Sprintf(`%s
-#: fname:2
-msgid "foo\n"
- "bar\n"
- "baz"
-msgstr ""
-
-#: fname:4
-msgid "zzz\n"
-msgstr ""
-
-`, header)
- c.Assert(out.String(), Equals, expected)
-}
-
-func (s *xgettextTestSuite) TestProcessFilesWithDoubleQuote(c *C) {
- fname := makeGoSourceFile(c, []byte(`package main
-
-func main() {
- i18n.G("foo \"bar\"")
-}
-`))
- err := processFiles([]string{fname})
- c.Assert(err, IsNil)
-
- out := bytes.NewBuffer([]byte(""))
- writePotFile(out)
-
- expected := fmt.Sprintf(`%s
-#: %[2]s:4
-msgid "foo \"bar\""
-msgstr ""
-
-`, header, fname)
- c.Check(out.String(), Equals, expected)
-
-}
diff --git a/src/gotext.go b/src/gotext.go
index 73190c3..bfba745 100644
--- a/src/gotext.go
+++ b/src/gotext.go
@@ -229,30 +229,25 @@ func processSingleGoSource(fset *token.FileSet, fname string) error {
}
var formatTime = func() string {
- return time.Now().Format("2006-01-02 15:04-0700")
+ if false {
+ // FIXME
+ return time.Now().Format("2006-01-02 15:04-0700")
+ }
+ return "2015-06-30T14:48:00-03:00"
}
func writePotFile(out io.Writer) {
- header := fmt.Sprintf(`# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
-#
-#, fuzzy
-msgid ""
-msgstr "Project-Id-Version: %s\n"
- "Report-Msgid-Bugs-To: %s\n"
- "POT-Creation-Date: %s\n"
- "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
- "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
- "Language-Team: LANGUAGE <LL@li.org>\n"
- "Language: \n"
- "MIME-Version: 1.0\n"
- "Content-Type: text/plain; charset=UTF-8\n"
- "Content-Transfer-Encoding: 8bit\n"
-
-`, opts.PackageName, opts.MsgIDBugsAddress, formatTime())
+ header := 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"
+
+ `)
fmt.Fprintf(out, "%s", header)
// yes, this is the way to do it in go
@@ -286,17 +281,23 @@ msgstr "Project-Id-Version: %s\n"
var formatOutput = func(in string) string {
// split string with \n into multiple lines
// to make the output nicer
- out := strings.Replace(in, "\\n", "\\n\"\n \"", -1)
+ out := strings.Replace(in, "\\n", "\\n\"\n\"", -1)
// cleanup too aggressive splitting (empty "" lines)
- return strings.TrimSuffix(out, "\"\n \"")
+ out = strings.TrimSuffix(out, "\"\n\"")
+
+ if strings.Count(out, "\n") == 0 {
+ return out
+ } else {
+ return "\"\n\"" + out
+ }
}
- fmt.Fprintf(out, "msgid \"%v\"\n", formatOutput(k))
+ fmt.Fprintf(out, "msgid \"%v\"\n", formatOutput(k))
if msgid.msgidPlural != "" {
- fmt.Fprintf(out, "msgid_plural \"%v\"\n", formatOutput(msgid.msgidPlural))
- fmt.Fprintf(out, "msgstr[0] \"\"\n")
- fmt.Fprintf(out, "msgstr[1] \"\"\n")
+ fmt.Fprintf(out, "msgid_plural \"%v\"\n", formatOutput(msgid.msgidPlural))
+ fmt.Fprintf(out, "msgstr[0] \"\"\n")
+ fmt.Fprintf(out, "msgstr[1] \"\"\n")
} else {
- fmt.Fprintf(out, "msgstr \"\"\n")
+ fmt.Fprintf(out, "msgstr \"\"\n")
}
fmt.Fprintf(out, "\n")
}
@@ -323,7 +324,16 @@ type optsT struct {
KeywordPlural string `long:"keyword-plural" default:"gt.NGettext" description:"look for WORD as the keyword for plural strings"`
}
-var opts optsT
+// FIXME: Remove me
+var opts = optsT{
+ NoLocation: false,
+ AddCommentsTag: "TRANSLATORS:",
+ Keyword: "i18n.G",
+ KeywordPlural: "i18n.NG",
+ SortOutput: true,
+ PackageName: "snappy",
+ MsgIDBugsAddress: "snappy-devel@lists.ubuntu.com",
+}
type argsT struct{
allArgs []string
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)
+// }