1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestExtract(t *testing.T) {
tests := []struct {
name string
fileName string
file string
activeFile []byte
}{
{
name: "no translations",
fileName: "file.go",
file: `package main`,
},
{
name: "global declaration",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var m = &i18n.Message{
ID: "Plural ID",
}
`,
},
{
name: "escape newline",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var hasnewline = &i18n.Message{
ID: "hasnewline",
Other: "\nfoo\nbar\\",
}
`,
activeFile: []byte(`hasnewline = "\nfoo\nbar\\"
`),
},
{
name: "escape",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var a = &i18n.Message{
ID: "a",
Other: "a \" b",
}
var b = &i18n.Message{
ID: "b",
Other: ` + "`" + `a " b` + "`" + `,
}
`,
activeFile: []byte(`a = "a \" b"
b = "a \" b"
`),
},
{
name: "array",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var a = []*i18n.Message{
{
ID: "a",
Other: "a",
},
{
ID: "b",
Other: "b",
},
}
`,
activeFile: []byte(`a = "a"
b = "b"
`),
},
{
name: "map",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var a = map[string]*i18n.Message{
"a": {
ID: "a",
Other: "a",
},
"b": {
ID: "b",
Other: "b",
},
}
`,
activeFile: []byte(`a = "a"
b = "b"
`),
},
{
name: "no extract from test",
fileName: "file_test.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
func main() {
bundle := i18n.NewBundle(language.English)
l := i18n.NewLocalizer(bundle, "en")
l.Localize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
}
`,
},
{
name: "must short form id only",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
func main() {
bundle := i18n.NewBundle(language.English)
l := i18n.NewLocalizer(bundle, "en")
l.MustLocalize(&i18n.LocalizeConfig{MessageID: "Plural ID"})
}
`,
},
{
name: "custom package name",
fileName: "file.go",
file: `package main
import bar "github.com/nicksnyder/go-i18n/v2/i18n"
func main() {
_ := &bar.Message{
ID: "Plural ID",
}
}
`,
},
{
name: "exhaustive plural translation",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
func main() {
_ := &i18n.Message{
ID: "Plural ID",
Description: "Plural description",
Zero: "Zero translation",
One: "One translation",
Two: "Two translation",
Few: "Few translation",
Many: "Many translation",
Other: "Other translation",
}
}
`,
activeFile: []byte(`["Plural ID"]
description = "Plural description"
few = "Few translation"
many = "Many translation"
one = "One translation"
other = "Other translation"
two = "Two translation"
zero = "Zero translation"
`),
},
{
name: "concat id",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
func main() {
_ := &i18n.Message{
ID: "Plural" +
" " +
"ID",
}
}
`,
},
{
name: "global declaration",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
const constID = "ConstantID"
var m = &i18n.Message{
ID: constID,
Other: "ID is a constant",
}
`,
activeFile: []byte(`ConstantID = "ID is a constant"
`),
},
{
name: "undefined identifier in composite lit",
fileName: "file.go",
file: `package main
import "github.com/nicksnyder/go-i18n/v2/i18n"
var m = &i18n.LocalizeConfig{
Funcs: Funcs,
}
`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
indir := mustTempDir("TestExtractCommandIn")
defer os.RemoveAll(indir)
outdir := mustTempDir("TestExtractCommandOut")
defer os.RemoveAll(outdir)
inpath := filepath.Join(indir, test.fileName)
if err := ioutil.WriteFile(inpath, []byte(test.file), 0666); err != nil {
t.Fatal(err)
}
if code := testableMain([]string{"extract", "-outdir", outdir, indir}); code != 0 {
t.Fatalf("expected exit code 0; got %d\n", code)
}
files, err := ioutil.ReadDir(outdir)
if err != nil {
t.Fatal(err)
}
if len(files) != 1 {
t.Fatalf("expected 1 file; got %#v", files)
}
actualFile := files[0]
expectedName := "active.en.toml"
if actualFile.Name() != expectedName {
t.Fatalf("expected %s; got %s", expectedName, actualFile.Name())
}
outpath := filepath.Join(outdir, actualFile.Name())
actual, err := ioutil.ReadFile(outpath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(actual, test.activeFile) {
t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", test.activeFile, actual)
}
})
}
}
func TestExtractCommand(t *testing.T) {
outdir, err := ioutil.TempDir("", "TestExtractCommand")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(outdir)
if code := testableMain([]string{"extract", "-outdir", outdir, "../example/"}); code != 0 {
t.Fatalf("expected exit code 0; got %d", code)
}
actual, err := ioutil.ReadFile(filepath.Join(outdir, "active.en.toml"))
if err != nil {
t.Fatal(err)
}
expected := []byte(`HelloPerson = "Hello {{.Name}}"
[MyUnreadEmails]
description = "The number of unread emails I have"
one = "I have {{.PluralCount}} unread email."
other = "I have {{.PluralCount}} unread emails."
[PersonUnreadEmails]
description = "The number of unread emails a person has"
one = "{{.Name}} has {{.UnreadEmailCount}} unread email."
other = "{{.Name}} has {{.UnreadEmailCount}} unread emails."
`)
if !bytes.Equal(actual, expected) {
t.Fatalf("files not equal\nactual:\n%s\nexpected:\n%s", actual, expected)
}
}
|