gotext/po_test.go

140 lines
3 KiB
Go
Raw Normal View History

2016-06-19 22:36:33 +00:00
package gotext
import (
"os"
"path"
"testing"
)
func TestPo(t *testing.T) {
// Set PO content
str := `# Some comment
msgid "My text"
msgstr "Translated text"
# More comments
msgid "Another string"
msgstr ""
msgid "One with var: %s"
2016-06-23 14:41:38 +00:00
msgid_plural "Several with vars: %s"
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"
2016-06-19 22:36:33 +00:00
2016-06-26 14:54:28 +00:00
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'
2016-06-19 22:36:33 +00:00
`
// Write PO content to file
filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
f, err := os.Create(filename)
if err != nil {
t.Fatalf("Can't create test file: %s", err.Error())
}
defer f.Close()
_, err = f.WriteString(str)
if err != nil {
t.Fatalf("Can't write to test file: %s", err.Error())
}
2016-06-26 14:54:28 +00:00
// Create po object
2016-06-19 22:36:33 +00:00
po := new(Po)
2016-06-26 14:54:28 +00:00
// Try to parse a directory
po.ParseFile(path.Clean(os.TempDir()))
// Parse file
2016-06-19 22:36:33 +00:00
po.ParseFile(filename)
// Test translations
tr := po.Get("My text")
if tr != "Translated text" {
t.Errorf("Expected 'Translated text' but got '%s'", tr)
}
v := "Variable"
tr = po.Get("One with var: %s", v)
2016-06-23 14:41:38 +00:00
if tr != "This one is the singular: Variable" {
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
}
// Test plural
tr = po.GetN("One with var: %s", "Several with vars: %s", 2, v)
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)
2016-06-19 22:36:33 +00:00
}
2016-06-26 14:54:28 +00:00
// 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)
}
2016-06-19 22:36:33 +00:00
}
2016-06-24 20:45:12 +00:00
func TestPoRace(t *testing.T) {
// Set PO content
str := `# Some comment
msgid "My text"
msgstr "Translated text"
# More comments
msgid "Another string"
msgstr ""
msgid "One with var: %s"
msgid_plural "Several with vars: %s"
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"
`
// Create Po object
po := new(Po)
// Create sync channels
pc := make(chan bool)
rc := make(chan bool)
// Parse po content in a goroutine
go func(po *Po, done chan bool) {
po.Parse(str)
done <- true
}(po, pc)
// Read some translation on a goroutine
go func(po *Po, done chan bool) {
2016-06-26 14:54:28 +00:00
po.Get("My text")
2016-06-24 20:45:12 +00:00
done <- true
}(po, rc)
// Read something at top level
2016-06-26 14:54:28 +00:00
po.Get("My text")
2016-06-24 20:45:12 +00:00
// Wait for goroutines to finish
<-pc
<-rc
}