Improve test coverage to ~100%

This commit is contained in:
Leonel Quinteros 2016-06-26 11:54:28 -03:00
parent 5f34149d25
commit 2c5ca9c0e6
3 changed files with 82 additions and 9 deletions

View file

@ -6,6 +6,29 @@ import (
"testing"
)
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)
}
}
func TestPackageFunctions(t *testing.T) {
// Set PO content
str := `# Some comment
@ -23,7 +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")
// Create Locales directory on default location
dirname := path.Clean(library + string(os.PathSeparator) + "en_US")
err := os.MkdirAll(dirname, os.ModePerm)
@ -109,14 +135,14 @@ msgstr[2] "And this is the second plural form: %s"
// Test translations
go func(done chan bool) {
println(Get("My text"))
Get("My text")
done <- true
}(c1)
go func(done chan bool) {
println(Get("My text"))
Get("My text")
done <- true
}(c2)
println(Get("My text"))
Get("My text")
}

View file

@ -47,6 +47,9 @@ 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
// Add domain
l.AddDomain("my_domain")
@ -68,6 +71,17 @@ 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)
}
}
func TestLocaleRace(t *testing.T) {
@ -124,12 +138,12 @@ msgstr[2] "And this is the second plural form: %s"
// Get translations in goroutine
go func(l *Locale, done chan bool) {
println(l.GetD("race", "My text"))
l.GetD("race", "My text")
done <- true
}(l, rc)
// Get translations at top level
println(l.GetD("race", "My text"))
l.GetD("race", "My text")
// Wait for goroutines to finish
<-ac

View file

@ -22,6 +22,12 @@ msgstr[0] "This one is the singular: %s"
msgstr[1] "This one is the plural: %s"
msgstr[2] "And this is the second plural form: %s"
msgid "This one has invalid syntax translations"
msgid_plural "Plural index"
msgstr[abc] "Wrong index"
msgstr[1 "Forgot to close brackets"
msgstr[0] "Badly formatted string'
`
// Write PO content to file
filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
@ -37,8 +43,13 @@ msgstr[2] "And this is the second plural form: %s"
t.Fatalf("Can't write to test file: %s", err.Error())
}
// Parse po file
// Create po object
po := new(Po)
// Try to parse a directory
po.ParseFile(path.Clean(os.TempDir()))
// Parse file
po.ParseFile(filename)
// Test translations
@ -58,6 +69,28 @@ 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 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 TestPoRace(t *testing.T) {
@ -93,12 +126,12 @@ msgstr[2] "And this is the second plural form: %s"
// Read some translation on a goroutine
go func(po *Po, done chan bool) {
println(po.Get("My text"))
po.Get("My text")
done <- true
}(po, rc)
// Read something at top level
println(po.Get("My text"))
po.Get("My text")
// Wait for goroutines to finish
<-pc