2020-09-10 20:02:24 +00:00
|
|
|
package gotext
|
|
|
|
|
2022-03-29 02:45:05 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2020-09-10 20:02:24 +00:00
|
|
|
|
2020-10-28 23:06:55 +00:00
|
|
|
const (
|
|
|
|
enUSFixture = "fixtures/en_US/default.po"
|
2023-04-04 16:10:33 +00:00
|
|
|
arFixture = "fixtures/ar/categories.po"
|
2020-10-28 23:06:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDomain_GetTranslations(t *testing.T) {
|
|
|
|
po := NewPo()
|
|
|
|
po.ParseFile(enUSFixture)
|
|
|
|
|
|
|
|
domain := po.GetDomain()
|
|
|
|
all := domain.GetTranslations()
|
|
|
|
|
|
|
|
if len(all) != len(domain.translations) {
|
|
|
|
t.Error("lengths should match")
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range domain.translations {
|
|
|
|
if all[k] == v {
|
|
|
|
t.Error("GetTranslations should be returning a copy, but pointers are equal")
|
|
|
|
}
|
|
|
|
if all[k].ID != v.ID {
|
|
|
|
t.Error("IDs should match")
|
|
|
|
}
|
|
|
|
if all[k].PluralID != v.PluralID {
|
|
|
|
t.Error("PluralIDs should match")
|
|
|
|
}
|
|
|
|
if all[k].dirty != v.dirty {
|
|
|
|
t.Error("dirty flag should match")
|
|
|
|
}
|
|
|
|
if len(all[k].Trs) != len(v.Trs) {
|
|
|
|
t.Errorf("Trs length does not match: %d != %d", len(all[k].Trs), len(v.Trs))
|
|
|
|
}
|
|
|
|
if len(all[k].Refs) != len(v.Refs) {
|
|
|
|
t.Errorf("Refs length does not match: %d != %d", len(all[k].Refs), len(v.Refs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 02:45:05 +00:00
|
|
|
|
2023-04-04 16:10:33 +00:00
|
|
|
func TestDomain_IsTranslated(t *testing.T) {
|
|
|
|
englishPo := NewPo()
|
|
|
|
englishPo.ParseFile(enUSFixture)
|
|
|
|
english := englishPo.GetDomain()
|
|
|
|
|
|
|
|
// singular and plural
|
|
|
|
if english.IsTranslated("My Text") {
|
|
|
|
t.Error("'My text' should be reported as translated.")
|
|
|
|
}
|
|
|
|
if english.IsTranslated("Another string") {
|
|
|
|
t.Error("'Another string' should be reported as not translated.")
|
|
|
|
}
|
|
|
|
if !english.IsTranslatedN("Empty plural form singular", 0) {
|
|
|
|
t.Error("'Empty plural form singular' should be reported as translated for n=0.")
|
|
|
|
}
|
|
|
|
if english.IsTranslatedN("Empty plural form singular", 1) {
|
|
|
|
t.Error("'Empty plural form singular' should be reported as not translated for n=1.")
|
|
|
|
}
|
|
|
|
|
|
|
|
arabicPo := NewPo()
|
|
|
|
arabicPo.ParseFile(arFixture)
|
|
|
|
arabic := arabicPo.GetDomain()
|
|
|
|
|
|
|
|
// multiple plurals
|
|
|
|
if !arabic.IsTranslated("Load %d more document") {
|
|
|
|
t.Error("Arabic singular should be reported as translated.")
|
|
|
|
}
|
|
|
|
if !arabic.IsTranslatedN("Load %d more document", 0) {
|
|
|
|
t.Error("Arabic plural should be reported as translated for n=0.")
|
|
|
|
}
|
|
|
|
if !arabic.IsTranslatedN("Load %d more document", 1) {
|
|
|
|
t.Error("Arabic plural should be reported as translated for n=1.")
|
|
|
|
}
|
|
|
|
if !arabic.IsTranslatedN("Load %d more document", 5) {
|
|
|
|
t.Error("Arabic plural should be reported as translated for n=5.")
|
|
|
|
}
|
|
|
|
if arabic.IsTranslatedN("Load %d more document", 6) {
|
|
|
|
t.Error("Arabic plural should be reported as not translated for n=6.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// context
|
|
|
|
if !english.IsTranslatedC("One with var: %s", "Ctx") {
|
|
|
|
t.Error("Context singular should be reported as translated.")
|
|
|
|
}
|
|
|
|
if !english.IsTranslatedNC("One with var: %s", 0, "Ctx") {
|
|
|
|
t.Error("Context plural should be reported as translated for n=0")
|
|
|
|
}
|
|
|
|
if english.IsTranslatedNC("One with var: %s", 2, "Ctx") {
|
|
|
|
t.Error("Context plural should be reported as translated for n=2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 02:45:05 +00:00
|
|
|
func TestDomain_CheckExportFormatting(t *testing.T) {
|
|
|
|
po := NewPo()
|
|
|
|
po.Set("myid", "test string\nwith \"newline\"")
|
|
|
|
poBytes, _ := po.MarshalText()
|
|
|
|
|
|
|
|
expectedOutput := `msgid ""
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
msgid "myid"
|
2023-07-26 11:16:54 +00:00
|
|
|
msgstr ""
|
|
|
|
"test string\n"
|
2022-03-29 02:45:05 +00:00
|
|
|
"with \"newline\""`
|
2023-07-26 11:16:54 +00:00
|
|
|
|
2022-03-29 02:45:05 +00:00
|
|
|
if string(poBytes) != expectedOutput {
|
|
|
|
t.Errorf("Exported PO format does not match. Received:\n\n%v\n\n\nExpected:\n\n%v", string(poBytes), expectedOutput)
|
|
|
|
}
|
|
|
|
}
|