summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nicksnyder/go-i18n/i18n/translation
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/nicksnyder/go-i18n/i18n/translation')
-rw-r--r--vendor/github.com/nicksnyder/go-i18n/i18n/translation/plural_translation_test.go308
-rw-r--r--vendor/github.com/nicksnyder/go-i18n/i18n/translation/template_test.go146
-rw-r--r--vendor/github.com/nicksnyder/go-i18n/i18n/translation/translation_test.go17
3 files changed, 471 insertions, 0 deletions
diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/translation/plural_translation_test.go b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/plural_translation_test.go
new file mode 100644
index 000000000..ea7de7fd9
--- /dev/null
+++ b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/plural_translation_test.go
@@ -0,0 +1,308 @@
+package translation
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/nicksnyder/go-i18n/i18n/language"
+)
+
+func mustTemplate(t *testing.T, src string) *template {
+ tmpl, err := newTemplate(src)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return tmpl
+}
+
+func pluralTranslationFixture(t *testing.T, id string, pluralCategories ...language.Plural) *pluralTranslation {
+ templates := make(map[language.Plural]*template, len(pluralCategories))
+ for _, pc := range pluralCategories {
+ templates[pc] = mustTemplate(t, string(pc))
+ }
+ return &pluralTranslation{id, templates}
+}
+
+func verifyDeepEqual(t *testing.T, actual, expected interface{}) {
+ if !reflect.DeepEqual(actual, expected) {
+ t.Fatalf("\n%#v\nnot equal to expected value\n%#v", actual, expected)
+ }
+}
+
+func TestPluralTranslationMerge(t *testing.T) {
+ pt := pluralTranslationFixture(t, "id", language.One, language.Other)
+ oneTemplate, otherTemplate := pt.templates[language.One], pt.templates[language.Other]
+
+ pt.Merge(pluralTranslationFixture(t, "id"))
+ verifyDeepEqual(t, pt.templates, map[language.Plural]*template{
+ language.One: oneTemplate,
+ language.Other: otherTemplate,
+ })
+
+ pt2 := pluralTranslationFixture(t, "id", language.One, language.Two)
+ pt.Merge(pt2)
+ verifyDeepEqual(t, pt.templates, map[language.Plural]*template{
+ language.One: pt2.templates[language.One],
+ language.Two: pt2.templates[language.Two],
+ language.Other: otherTemplate,
+ })
+}
+
+/* Test implementations from old idea
+
+func TestCopy(t *testing.T) {
+ ls := &LocalizedString{
+ ID: "id",
+ Translation: testingTemplate(t, "translation {{.Hello}}"),
+ Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, "plural {{.One}}"),
+ language.Other: testingTemplate(t, "plural {{.Other}}"),
+ },
+ }
+
+ c := ls.Copy()
+ delete(c.Translations, language.One)
+ if _, ok := ls.Translations[language.One]; !ok {
+ t.Errorf("deleting plural translation from copy deleted it from the original")
+ }
+ c.Translations[language.Two] = testingTemplate(t, "plural {{.Two}}")
+ if _, ok := ls.Translations[language.Two]; ok {
+ t.Errorf("adding plural translation to copy added it to the original")
+ }
+}
+
+func TestNormalize(t *testing.T) {
+ oneTemplate := testingTemplate(t, "one {{.One}}")
+ ls := &LocalizedString{
+ Translation: testingTemplate(t, "single {{.Single}}"),
+ Translations: map[language.Plural]*template{
+ language.One: oneTemplate,
+ language.Two: testingTemplate(t, "two {{.Two}}"),
+ },
+ }
+ ls.Normalize(LanguageWithCode("en"))
+ if ls.Translation != nil {
+ t.Errorf("ls.Translation is %#v; expected nil", ls.Translation)
+ }
+ if actual := ls.Translations[language.Two]; actual != nil {
+ t.Errorf("ls.Translation[language.Two] is %#v; expected nil", actual)
+ }
+ if actual := ls.Translations[language.One]; actual != oneTemplate {
+ t.Errorf("ls.Translations[language.One] is %#v; expected %#v", actual, oneTemplate)
+ }
+ if _, ok := ls.Translations[language.Other]; !ok {
+ t.Errorf("ls.Translations[language.Other] shouldn't be empty")
+ }
+}
+
+func TestMergeTranslation(t *testing.T) {
+ ls := &LocalizedString{}
+
+ translation := testingTemplate(t, "one {{.Hello}}")
+ ls.Merge(&LocalizedString{
+ Translation: translation,
+ })
+ if ls.Translation != translation {
+ t.Errorf("expected %#v; got %#v", translation, ls.Translation)
+ }
+
+ ls.Merge(&LocalizedString{})
+ if ls.Translation != translation {
+ t.Errorf("expected %#v; got %#v", translation, ls.Translation)
+ }
+
+ translation = testingTemplate(t, "two {{.Hello}}")
+ ls.Merge(&LocalizedString{
+ Translation: translation,
+ })
+ if ls.Translation != translation {
+ t.Errorf("expected %#v; got %#v", translation, ls.Translation)
+ }
+}
+
+func TestMergeTranslations(t *testing.T) {
+ ls := &LocalizedString{}
+
+ oneTemplate := testingTemplate(t, "one {{.One}}")
+ otherTemplate := testingTemplate(t, "other {{.Other}}")
+ ls.Merge(&LocalizedString{
+ Translations: map[language.Plural]*template{
+ language.One: oneTemplate,
+ language.Other: otherTemplate,
+ },
+ })
+ if actual := ls.Translations[language.One]; actual != oneTemplate {
+ t.Errorf("ls.Translations[language.One] expected %#v; got %#v", oneTemplate, actual)
+ }
+ if actual := ls.Translations[language.Other]; actual != otherTemplate {
+ t.Errorf("ls.Translations[language.Other] expected %#v; got %#v", otherTemplate, actual)
+ }
+
+ ls.Merge(&LocalizedString{
+ Translations: map[language.Plural]*template{},
+ })
+ if actual := ls.Translations[language.One]; actual != oneTemplate {
+ t.Errorf("ls.Translations[language.One] expected %#v; got %#v", oneTemplate, actual)
+ }
+ if actual := ls.Translations[language.Other]; actual != otherTemplate {
+ t.Errorf("ls.Translations[language.Other] expected %#v; got %#v", otherTemplate, actual)
+ }
+
+ twoTemplate := testingTemplate(t, "two {{.Two}}")
+ otherTemplate = testingTemplate(t, "second other {{.Other}}")
+ ls.Merge(&LocalizedString{
+ Translations: map[language.Plural]*template{
+ language.Two: twoTemplate,
+ language.Other: otherTemplate,
+ },
+ })
+ if actual := ls.Translations[language.One]; actual != oneTemplate {
+ t.Errorf("ls.Translations[language.One] expected %#v; got %#v", oneTemplate, actual)
+ }
+ if actual := ls.Translations[language.Two]; actual != twoTemplate {
+ t.Errorf("ls.Translations[language.Two] expected %#v; got %#v", twoTemplate, actual)
+ }
+ if actual := ls.Translations[language.Other]; actual != otherTemplate {
+ t.Errorf("ls.Translations[language.Other] expected %#v; got %#v", otherTemplate, actual)
+ }
+}
+
+func TestMissingTranslations(t *testing.T) {
+ en := LanguageWithCode("en")
+
+ tests := []struct {
+ localizedString *LocalizedString
+ language *Language
+ expected bool
+ }{
+ {
+ &LocalizedString{},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translation: testingTemplate(t, "single {{.Single}}")},
+ en,
+ false,
+ },
+ {
+ &LocalizedString{
+ Translation: testingTemplate(t, "single {{.Single}}"),
+ Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, "one {{.One}}"),
+ }},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, "one {{.One}}"),
+ }},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: nil,
+ language.Other: nil,
+ }},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, ""),
+ language.Other: testingTemplate(t, ""),
+ }},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, "one {{.One}}"),
+ language.Other: testingTemplate(t, "other {{.Other}}"),
+ }},
+ en,
+ false,
+ },
+ }
+
+ for _, tt := range tests {
+ if actual := tt.localizedString.MissingTranslations(tt.language); actual != tt.expected {
+ t.Errorf("expected %t got %t for %s, %#v",
+ tt.expected, actual, tt.language.code, tt.localizedString)
+ }
+ }
+}
+
+func TestHasTranslations(t *testing.T) {
+ en := LanguageWithCode("en")
+
+ tests := []struct {
+ localizedString *LocalizedString
+ language *Language
+ expected bool
+ }{
+ {
+ &LocalizedString{},
+ en,
+ false,
+ },
+ {
+ &LocalizedString{Translation: testingTemplate(t, "single {{.Single}}")},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{
+ Translation: testingTemplate(t, "single {{.Single}}"),
+ Translations: map[language.Plural]*template{}},
+ en,
+ false,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, "one {{.One}}"),
+ }},
+ en,
+ true,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.Two: testingTemplate(t, "two {{.Two}}"),
+ }},
+ en,
+ false,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: nil,
+ }},
+ en,
+ false,
+ },
+ {
+ &LocalizedString{Translations: map[language.Plural]*template{
+ language.One: testingTemplate(t, ""),
+ }},
+ en,
+ false,
+ },
+ }
+
+ for _, tt := range tests {
+ if actual := tt.localizedString.HasTranslations(tt.language); actual != tt.expected {
+ t.Errorf("expected %t got %t for %s, %#v",
+ tt.expected, actual, tt.language.code, tt.localizedString)
+ }
+ }
+}
+
+func testingTemplate(t *testing.T, src string) *template {
+ tmpl, err := newTemplate(src)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return tmpl
+}
+*/
diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/translation/template_test.go b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/template_test.go
new file mode 100644
index 000000000..73a923404
--- /dev/null
+++ b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/template_test.go
@@ -0,0 +1,146 @@
+package translation
+
+import (
+ "bytes"
+ "fmt"
+ //"launchpad.net/goyaml"
+ "testing"
+ gotemplate "text/template"
+)
+
+func TestNilTemplate(t *testing.T) {
+ expected := "hello"
+ tmpl := &template{
+ tmpl: nil,
+ src: expected,
+ }
+ if actual := tmpl.Execute(nil); actual != expected {
+ t.Errorf("Execute(nil) returned %s; expected %s", actual, expected)
+ }
+}
+
+func TestMarshalText(t *testing.T) {
+ tmpl := &template{
+ tmpl: gotemplate.Must(gotemplate.New("id").Parse("this is a {{.foo}} template")),
+ src: "boom",
+ }
+ expectedBuf := []byte(tmpl.src)
+ if buf, err := tmpl.MarshalText(); !bytes.Equal(buf, expectedBuf) || err != nil {
+ t.Errorf("MarshalText() returned %#v, %#v; expected %#v, nil", buf, err, expectedBuf)
+ }
+}
+
+func TestUnmarshalText(t *testing.T) {
+ tmpl := &template{}
+ tmpl.UnmarshalText([]byte("hello {{.World}}"))
+ result := tmpl.Execute(map[string]string{
+ "World": "world!",
+ })
+ expected := "hello world!"
+ if result != expected {
+ t.Errorf("expected %#v; got %#v", expected, result)
+ }
+}
+
+/*
+func TestYAMLMarshal(t *testing.T) {
+ src := "hello {{.World}}"
+ tmpl, err := newTemplate(src)
+ if err != nil {
+ t.Fatal(err)
+ }
+ buf, err := goyaml.Marshal(tmpl)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(buf, []byte(src)) {
+ t.Fatalf(`expected "%s"; got "%s"`, src, buf)
+ }
+}
+
+func TestYAMLUnmarshal(t *testing.T) {
+ buf := []byte(`Tmpl: "hello"`)
+
+ var out struct {
+ Tmpl *template
+ }
+ var foo map[string]string
+ if err := goyaml.Unmarshal(buf, &foo); err != nil {
+ t.Fatal(err)
+ }
+ if out.Tmpl == nil {
+ t.Fatalf("out.Tmpl was nil")
+ }
+ if out.Tmpl.tmpl == nil {
+ t.Fatalf("out.Tmpl.tmpl was nil")
+ }
+ if expected := "hello {{.World}}"; out.Tmpl.src != expected {
+ t.Fatalf("expected %s; got %s", expected, out.Tmpl.src)
+ }
+}
+
+func TestGetYAML(t *testing.T) {
+ src := "hello"
+ tmpl := &template{
+ tmpl: nil,
+ src: src,
+ }
+ if tag, value := tmpl.GetYAML(); tag != "" || value != src {
+ t.Errorf("GetYAML() returned (%#v, %#v); expected (%#v, %#v)", tag, value, "", src)
+ }
+}
+
+func TestSetYAML(t *testing.T) {
+ tmpl := &template{}
+ tmpl.SetYAML("tagDoesntMatter", "hello {{.World}}")
+ result := tmpl.Execute(map[string]string{
+ "World": "world!",
+ })
+ expected := "hello world!"
+ if result != expected {
+ t.Errorf("expected %#v; got %#v", expected, result)
+ }
+}
+*/
+
+func BenchmarkExecuteNilTemplate(b *testing.B) {
+ template := &template{src: "hello world"}
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ template.Execute(nil)
+ }
+}
+
+func BenchmarkExecuteHelloWorldTemplate(b *testing.B) {
+ template, err := newTemplate("hello world")
+ if err != nil {
+ b.Fatal(err)
+ }
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ template.Execute(nil)
+ }
+}
+
+// Executing a simple template like this is ~6x slower than Sprintf
+// but it is still only a few microseconds which should be sufficiently fast.
+// The benefit is that we have nice semantic tags in the translation.
+func BenchmarkExecuteHelloNameTemplate(b *testing.B) {
+ template, err := newTemplate("hello {{.Name}}")
+ if err != nil {
+ b.Fatal(err)
+ }
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ template.Execute(map[string]string{
+ "Name": "Nick",
+ })
+ }
+}
+
+func BenchmarkSprintf(b *testing.B) {
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ fmt.Sprintf("hello %s", "nick")
+ }
+}
diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/translation/translation_test.go b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/translation_test.go
new file mode 100644
index 000000000..7380d5a6f
--- /dev/null
+++ b/vendor/github.com/nicksnyder/go-i18n/i18n/translation/translation_test.go
@@ -0,0 +1,17 @@
+package translation
+
+import (
+ "sort"
+ "testing"
+)
+
+// Check this here to avoid unnecessary import of sort package.
+var _ = sort.Interface(make(SortableByID, 0, 0))
+
+func TestNewSingleTranslation(t *testing.T) {
+ t.Skipf("not implemented")
+}
+
+func TestNewPluralTranslation(t *testing.T) {
+ t.Skipf("not implemented")
+}