test fs alongside os implementation

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg 2023-03-17 11:29:18 +01:00
parent 9d5a301452
commit 8ebc12be37
4 changed files with 320 additions and 231 deletions

View file

@ -77,8 +77,8 @@ func NewLocale(p, l string) *Locale {
}
// NewLocaleFS returns a Locale working with a fs.FS
func NewLocaleFS(p, l string, filesystem fs.FS) *Locale {
loc := NewLocale(p, l)
func NewLocaleFS(l string, filesystem fs.FS) *Locale {
loc := NewLocale("", l)
loc.fs = filesystem
return loc
}

View file

@ -9,6 +9,7 @@ import (
"os"
"path"
"testing"
"testing/fstest"
)
func TestLocale(t *testing.T) {
@ -61,81 +62,91 @@ msgstr "More Translation"
`
// Create Locales directory with simplified language code
dirname := path.Join("/tmp", "en", "LC_MESSAGES")
err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
t.Fatalf("Can't create test directory: %s", err.Error())
var locales []*Locale
{ // test os
// Create Locales directory with simplified language code
dirname := path.Join("/tmp", "en", "LC_MESSAGES")
err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
t.Fatalf("Can't create test directory: %s", err.Error())
}
// Write PO content to file
filename := path.Join(dirname, "my_domain.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())
}
// Create Locale with full language code
locales = append(locales, NewLocale("/tmp", "en_US"))
}
{ // test fs
fs := make(fstest.MapFS)
fs["en/LC_MESSAGES/my_domain.po"] = &fstest.MapFile{Data: []byte(str)}
locales = append(locales, NewLocaleFS("en_US", fs))
}
// Write PO content to file
filename := path.Join(dirname, "my_domain.po")
for _, l := range locales {
// Force nil domain storage
l.Domains = nil
f, err := os.Create(filename)
if err != nil {
t.Fatalf("Can't create test file: %s", err.Error())
}
defer f.Close()
// Add domain
l.AddDomain("my_domain")
_, err = f.WriteString(str)
if err != nil {
t.Fatalf("Can't write to test file: %s", err.Error())
}
// Test translations
tr := l.GetD("my_domain", "My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Create Locale with full language code
l := NewLocale("/tmp", "en_US")
v := "Variable"
tr = l.GetD("my_domain", "One with var: %s", v)
if tr != "This one is the singular: Variable" {
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
}
// Force nil domain storage
l.Domains = nil
// Test plural
tr = l.GetND("my_domain", "One with var: %s", "Several with vars: %s", 7, v)
if tr != "This one is the plural: Variable" {
t.Errorf("Expected 'This one is the plural: Variable' but got '%s'", tr)
}
// Add domain
l.AddDomain("my_domain")
// Test context translations
tr = l.GetC("Some random in a context", "Ctx")
if tr != "Some random Translation in a context" {
t.Errorf("Expected 'Some random Translation in a context'. Got '%s'", tr)
}
// Test translations
tr := l.GetD("my_domain", "My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
v = "Test"
tr = l.GetNC("One with var: %s", "Several with vars: %s", 23, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test'. Got '%s'", tr)
}
v := "Variable"
tr = l.GetD("my_domain", "One with var: %s", v)
if tr != "This one is the singular: Variable" {
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
}
tr = l.GetDC("my_domain", "One with var: %s", "Ctx", v)
if tr != "This one is the singular in a Ctx context: Test" {
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
}
// Test plural
tr = l.GetND("my_domain", "One with var: %s", "Several with vars: %s", 7, v)
if tr != "This one is the plural: Variable" {
t.Errorf("Expected 'This one is the plural: Variable' but got '%s'", tr)
}
// Test plural
tr = l.GetNDC("my_domain", "One with var: %s", "Several with vars: %s", 3, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
}
// Test context translations
tr = l.GetC("Some random in a context", "Ctx")
if tr != "Some random Translation in a context" {
t.Errorf("Expected 'Some random Translation in a context'. Got '%s'", tr)
}
v = "Test"
tr = l.GetNC("One with var: %s", "Several with vars: %s", 23, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test'. Got '%s'", tr)
}
tr = l.GetDC("my_domain", "One with var: %s", "Ctx", v)
if tr != "This one is the singular in a Ctx context: Test" {
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
}
// Test plural
tr = l.GetNDC("my_domain", "One with var: %s", "Several with vars: %s", 3, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
}
// Test last Translation
tr = l.GetD("my_domain", "More")
if tr != "More Translation" {
t.Errorf("Expected 'More Translation' but got '%s'", tr)
// Test last Translation
tr = l.GetD("my_domain", "More")
if tr != "More Translation" {
t.Errorf("Expected 'More Translation' but got '%s'", tr)
}
}
}
@ -424,52 +435,62 @@ msgstr[2] "And this is the second plural form: %s"
`
// Create Locales directory with simplified language code
dirname := path.Join("/tmp", "es")
err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
t.Fatalf("Can't create test directory: %s", err.Error())
var locales []*Locale
{ // test os
// Create Locales directory with simplified language code
dirname := path.Join("/tmp", "es")
err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
t.Fatalf("Can't create test directory: %s", err.Error())
}
// Write PO content to file
filename := path.Join(dirname, "race.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())
}
// Create Locale
locales = append(locales, NewLocale("/tmp", "es"))
}
{ // test fs
fs := make(fstest.MapFS)
fs["es/LC_MESSAGES/race.po"] = &fstest.MapFile{Data: []byte(str)}
locales = append(locales, NewLocaleFS("es", fs))
}
// Write PO content to file
filename := path.Join(dirname, "race.po")
for _, l := range locales {
// Init sync channels
ac := make(chan bool)
rc := make(chan bool)
f, err := os.Create(filename)
if err != nil {
t.Fatalf("Can't create test file: %s", err.Error())
}
defer f.Close()
// Add domain in goroutine
go func(l *Locale, done chan bool) {
l.AddDomain("race")
done <- true
}(l, ac)
_, err = f.WriteString(str)
if err != nil {
t.Fatalf("Can't write to test file: %s", err.Error())
}
// Get translations in goroutine
go func(l *Locale, done chan bool) {
l.GetD("race", "My text")
done <- true
}(l, rc)
// Create Locale
l := NewLocale("/tmp", "es")
// Init sync channels
ac := make(chan bool)
rc := make(chan bool)
// Add domain in goroutine
go func(l *Locale, done chan bool) {
l.AddDomain("race")
done <- true
}(l, ac)
// Get translations in goroutine
go func(l *Locale, done chan bool) {
// Get translations at top level
l.GetD("race", "My text")
done <- true
}(l, rc)
// Get translations at top level
l.GetD("race", "My text")
// Wait for goroutines to finish
<-ac
<-rc
// Wait for goroutines to finish
<-ac
<-rc
}
}
func TestAddTranslator(t *testing.T) {
@ -497,127 +518,186 @@ func TestAddTranslator(t *testing.T) {
}
}
func TestArabicTranslation(t *testing.T) {
func TestAddTranslatorFS(t *testing.T) {
fs := os.DirFS("fixtures")
// Create po object
po := NewPoFS(fs)
// Parse file
po.ParseFile("en_US/default.po")
// Create Locale
l := NewLocale("fixtures/", "ar")
l := NewLocaleFS("en", fs)
// Add domain
l.AddDomain("categories")
// Add PO Translator to Locale object
l.AddTranslator("default", po)
// Plurals formula missing + Plural translation string missing
tr := l.GetD("categories", "Alcohol & Tobacco")
if tr != "الكحول والتبغ" {
t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
// Test translations
tr := l.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Plural translation string present without translations, should get the msgid_plural
tr = l.GetND("categories", "%d selected", "%d selected", 10)
if tr != "%d selected" {
t.Errorf("Expected to get '%%d selected', but got '%s'", tr)
// Test translations
tr = l.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
//Plurals formula present + Plural translation string present and complete
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 0)
if tr != "حمّل %d مستندات إضافيّة" {
t.Errorf("Expected to get 'msgstr[0]', but got '%s'", tr)
func TestArabicTranslation(t *testing.T) {
var locales []*Locale
{ // test os
// Create Locale
locales = append(locales, NewLocale("fixtures/", "ar"))
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 1)
if tr != "حمّل مستند واحد إضافي" {
t.Errorf("Expected to get 'msgstr[1]', but got '%s'", tr)
{ // test fs
locales = append(locales, NewLocaleFS("ar", os.DirFS("fixtures")))
}
for _, l := range locales {
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 2)
if tr != "حمّل مستندين إضافيين" {
t.Errorf("Expected to get 'msgstr[2]', but got '%s'", tr)
// Add domain
l.AddDomain("categories")
// Plurals formula missing + Plural translation string missing
tr := l.GetD("categories", "Alcohol & Tobacco")
if tr != "الكحول والتبغ" {
t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
}
// Plural translation string present without translations, should get the msgid_plural
tr = l.GetND("categories", "%d selected", "%d selected", 10)
if tr != "%d selected" {
t.Errorf("Expected to get '%%d selected', but got '%s'", tr)
}
//Plurals formula present + Plural translation string present and complete
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 0)
if tr != "حمّل %d مستندات إضافيّة" {
t.Errorf("Expected to get 'msgstr[0]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 1)
if tr != "حمّل مستند واحد إضافي" {
t.Errorf("Expected to get 'msgstr[1]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 2)
if tr != "حمّل مستندين إضافيين" {
t.Errorf("Expected to get 'msgstr[2]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 6)
if tr != "حمّل %d مستندات إضافيّة" {
t.Errorf("Expected to get 'msgstr[3]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 116)
if tr != "حمّل %d مستندا إضافيّا" {
t.Errorf("Expected to get 'msgstr[4]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 102)
if tr != "حمّل %d مستند إضافي" {
t.Errorf("Expected to get 'msgstr[5]', but got '%s'", tr)
}
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 6)
if tr != "حمّل %d مستندات إضافيّة" {
t.Errorf("Expected to get 'msgstr[3]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 116)
if tr != "حمّل %d مستندا إضافيّا" {
t.Errorf("Expected to get 'msgstr[4]', but got '%s'", tr)
}
tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 102)
if tr != "حمّل %d مستند إضافي" {
t.Errorf("Expected to get 'msgstr[5]', but got '%s'", tr)
}
}
func TestArabicMissingPluralForm(t *testing.T) {
// Create Locale
l := NewLocale("fixtures/", "ar")
var locales []*Locale
{ // test os
// Create Locale
locales = append(locales, NewLocale("fixtures/", "ar"))
}
{ // test fs
locales = append(locales, NewLocaleFS("ar", os.DirFS("fixtures")))
}
// Add domain
l.AddDomain("no_plural_header")
for _, l := range locales {
// Add domain
l.AddDomain("no_plural_header")
// Get translation
tr := l.GetD("no_plural_header", "Alcohol & Tobacco")
if tr != "الكحول والتبغ" {
t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
// Get translation
tr := l.GetD("no_plural_header", "Alcohol & Tobacco")
if tr != "الكحول والتبغ" {
t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr)
}
}
}
func TestLocaleBinaryEncoding(t *testing.T) {
// Create Locale
l := NewLocale("fixtures/", "en_US")
l.AddDomain("default")
buff, err := l.MarshalBinary()
if err != nil {
t.Fatal(err)
var locales []*Locale
{ // test os
// Create Locale
locales = append(locales, NewLocale("fixtures/", "en_US"))
}
{ // test fs
locales = append(locales, NewLocaleFS("en_US", os.DirFS("fixtures")))
}
l2 := new(Locale)
err = l2.UnmarshalBinary(buff)
if err != nil {
t.Fatal(err)
}
for _, l := range locales {
l.AddDomain("default")
// Check object properties
if l.path != l2.path {
t.Fatalf("path doesn't match: '%s' vs '%s'", l.path, l2.path)
}
if l.lang != l2.lang {
t.Fatalf("lang doesn't match: '%s' vs '%s'", l.lang, l2.lang)
}
if l.defaultDomain != l2.defaultDomain {
t.Fatalf("defaultDomain doesn't match: '%s' vs '%s'", l.defaultDomain, l2.defaultDomain)
}
buff, err := l.MarshalBinary()
if err != nil {
t.Fatal(err)
}
// Check translations
if l.Get("My text") != l2.Get("My text") {
t.Errorf("'%s' is different from '%s", l.Get("My text"), l2.Get("My text"))
}
if l.Get("More") != l2.Get("More") {
t.Errorf("'%s' is different from '%s", l.Get("More"), l2.Get("More"))
}
if l.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE") != l2.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE") {
t.Errorf("'%s' is different from '%s", l.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"), l2.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"))
l2 := new(Locale)
err = l2.UnmarshalBinary(buff)
if err != nil {
t.Fatal(err)
}
// Check object properties
if l.path != l2.path {
t.Fatalf("path doesn't match: '%s' vs '%s'", l.path, l2.path)
}
if l.lang != l2.lang {
t.Fatalf("lang doesn't match: '%s' vs '%s'", l.lang, l2.lang)
}
if l.defaultDomain != l2.defaultDomain {
t.Fatalf("defaultDomain doesn't match: '%s' vs '%s'", l.defaultDomain, l2.defaultDomain)
}
// Check translations
if l.Get("My text") != l2.Get("My text") {
t.Errorf("'%s' is different from '%s", l.Get("My text"), l2.Get("My text"))
}
if l.Get("More") != l2.Get("More") {
t.Errorf("'%s' is different from '%s", l.Get("More"), l2.Get("More"))
}
if l.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE") != l2.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE") {
t.Errorf("'%s' is different from '%s", l.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"), l2.GetN("One with var: %s", "Several with vars: %s", 3, "VALUE"))
}
}
}
func TestLocale_GetTranslations(t *testing.T) {
l := NewLocale("fixtures/", "en_US")
l.AddDomain("default")
all := l.GetTranslations()
if len(all) < 5 {
t.Errorf("length of all translations is too few: %d", len(all))
var locales []*Locale
{ // test os
locales = append(locales, NewLocale("fixtures/", "en_US"))
}
{ // test fs
locales = append(locales, NewLocaleFS("en_US", os.DirFS("fixtures")))
}
const moreMsgID = "More"
more, ok := all[moreMsgID]
if !ok {
t.Error("missing expected translation")
}
if more.Get() != l.Get(moreMsgID) {
t.Errorf("translations of msgid %s do not match: \"%s\" != \"%s\"", moreMsgID, more.Get(), l.Get(moreMsgID))
for _, l := range locales {
l.AddDomain("default")
all := l.GetTranslations()
if len(all) < 5 {
t.Errorf("length of all translations is too few: %d", len(all))
}
const moreMsgID = "More"
more, ok := all[moreMsgID]
if !ok {
t.Error("missing expected translation")
}
if more.Get() != l.Get(moreMsgID) {
t.Errorf("translations of msgid %s do not match: \"%s\" != \"%s\"", moreMsgID, more.Get(), l.Get(moreMsgID))
}
}
}

View file

@ -13,23 +13,28 @@ import (
func TestMo_Get(t *testing.T) {
// Create mo object
mo := NewMo()
// Try to parse a directory
mo.ParseFile(path.Clean(os.TempDir()))
// Parse file
mo.ParseFile("fixtures/en_US/default.mo")
// Test translations
tr := mo.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
mos := []*Mo{
NewMo(),
NewMoFS(os.DirFS(".")),
}
// Test translations
tr = mo.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
for _, mo := range mos {
// Try to parse a directory
mo.ParseFile(path.Clean(os.TempDir()))
// Parse file
mo.ParseFile("fixtures/en_US/default.mo")
// Test translations
tr := mo.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Test translations
tr = mo.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
}

View file

@ -17,24 +17,28 @@ const (
)
func TestPo_Get(t *testing.T) {
// Create po object
po := NewPo()
// Try to parse a directory
po.ParseFile(path.Clean(os.TempDir()))
// Parse file
po.ParseFile("fixtures/en_US/default.po")
// Test translations
tr := po.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
var pos = []*Po{
NewPo(), // test os
NewPoFS(os.DirFS(".")),
}
// Test translations
tr = po.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
for _, po := range pos {
// Try to parse a directory
po.ParseFile(path.Clean(os.TempDir()))
// Parse file
po.ParseFile("fixtures/en_US/default.po")
// Test translations
tr := po.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Test translations
tr = po.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
}