From 21c6bc86cbfb01df1b87696d2089742f8ba382c2 Mon Sep 17 00:00:00 2001 From: Leonel Quinteros Date: Sun, 26 Jun 2016 12:15:08 -0300 Subject: [PATCH] Improve test coverage to ~100% --- gotext_test.go | 48 ++++++++++++++++++++++++------------------------ locale_test.go | 6 +++--- po_test.go | 31 +++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/gotext_test.go b/gotext_test.go index 3081237..41f1b43 100644 --- a/gotext_test.go +++ b/gotext_test.go @@ -7,26 +7,26 @@ import ( ) func TestGettersSetters(t *testing.T) { - SetDomain("test") - dom := GetDomain() - - if dom != "test" { - t.Errorf("Expected GetDomain to return 'test', but got '%s'", dom) - } - - SetLibrary("/tmp/test") - lib := GetLibrary() - - if lib != "/tmp/test" { - t.Errorf("Expected GetLibrary to return '/tmp/test', but got '%s'", lib) - } - - SetLanguage("es") - lang := GetLanguage() - - if lang != "es" { - t.Errorf("Expected GetLanguage to return 'es', but got '%s'", lang) - } + SetDomain("test") + dom := GetDomain() + + if dom != "test" { + t.Errorf("Expected GetDomain to return 'test', but got '%s'", dom) + } + + SetLibrary("/tmp/test") + lib := GetLibrary() + + if lib != "/tmp/test" { + t.Errorf("Expected GetLibrary to return '/tmp/test', but got '%s'", lib) + } + + SetLanguage("es") + lang := GetLanguage() + + if lang != "es" { + t.Errorf("Expected GetLanguage to return 'es', but got '%s'", lang) + } } func TestPackageFunctions(t *testing.T) { @@ -46,10 +46,10 @@ msgstr[1] "This one is the plural: %s" msgstr[2] "And this is the second plural form: %s" ` - - // Set default configuration - Configure("/tmp", "en_US", "default") - + + // Set default configuration + Configure("/tmp", "en_US", "default") + // Create Locales directory on default location dirname := path.Clean(library + string(os.PathSeparator) + "en_US") err := os.MkdirAll(dirname, os.ModePerm) diff --git a/locale_test.go b/locale_test.go index b149a3e..371f84c 100644 --- a/locale_test.go +++ b/locale_test.go @@ -47,7 +47,7 @@ msgstr[2] "And this is the second plural form: %s" // Create Locale with full language code l := NewLocale("/tmp", "en_US") - + // Force nil domain storage l.domains = nil @@ -71,13 +71,13 @@ msgstr[2] "And this is the second plural form: %s" if tr != "And this is the second plural form: Variable" { t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr) } - + // Test non-existent "deafult" domain responses tr = l.Get("My text") if tr != "My text" { t.Errorf("Expected 'My text' but got '%s'", tr) } - + tr = l.GetN("One with var: %s", "Several with vars: %s", 2, v) if tr != "Several with vars: Variable" { t.Errorf("Expected 'Several with vars: Variable' but got '%s'", tr) diff --git a/po_test.go b/po_test.go index 31247bf..0fb71d7 100644 --- a/po_test.go +++ b/po_test.go @@ -45,10 +45,10 @@ msgstr[0] "Badly formatted string' // Create po object po := new(Po) - + // Try to parse a directory po.ParseFile(path.Clean(os.TempDir())) - + // Parse file po.ParseFile(filename) @@ -69,30 +69,49 @@ msgstr[0] "Badly formatted string' if tr != "And this is the second plural form: Variable" { t.Errorf("Expected 'And this is the second plural form: Variable' but got '%s'", tr) } - + // Test inexistent translations tr = po.Get("This is a test") if tr != "This is a test" { t.Errorf("Expected 'This is a test' but got '%s'", tr) } - + tr = po.GetN("This is a test", "This are tests", 1) if tr != "This are tests" { t.Errorf("Expected 'This are tests' but got '%s'", tr) } - + // Test syntax error parsed translations tr = po.Get("This one has invalid syntax translations") if tr != "" { t.Errorf("Expected '' but got '%s'", tr) } - + tr = po.GetN("This one has invalid syntax translations", "This are tests", 1) if tr != "Plural index" { t.Errorf("Expected 'Plural index' but got '%s'", tr) } } +func TestTranslationObject(t *testing.T) { + tr := newTranslation() + str := tr.get() + + if str != "" { + t.Errorf("Expected '' but got '%s'", str) + } + + // Set id + tr.id = "Text" + + // Get again + str = tr.get() + + if str != "Text" { + t.Errorf("Expected 'Text' but got '%s'", str) + } +} + func TestPoRace(t *testing.T) { // Set PO content str := `# Some comment