summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nicksnyder/go-i18n/i18n/translations_test.go
blob: 86c580833cd40a55d3ce8a0c96b6b8202603bc87 (plain)
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
package i18n

import (
	"testing"

	"github.com/nicksnyder/go-i18n/i18n/bundle"
)

var bobMap = map[string]interface{}{"Person": "Bob"}
var bobStruct = struct{ Person string }{Person: "Bob"}

var testCases = []struct {
	id   string
	arg  interface{}
	want string
}{
	{"program_greeting", nil, "Hello world"},
	{"person_greeting", bobMap, "Hello Bob"},
	{"person_greeting", bobStruct, "Hello Bob"},

	{"your_unread_email_count", 0, "You have 0 unread emails."},
	{"your_unread_email_count", 1, "You have 1 unread email."},
	{"your_unread_email_count", 2, "You have 2 unread emails."},
	{"my_height_in_meters", "1.7", "I am 1.7 meters tall."},

	{"person_unread_email_count", []interface{}{0, bobMap}, "Bob has 0 unread emails."},
	{"person_unread_email_count", []interface{}{1, bobMap}, "Bob has 1 unread email."},
	{"person_unread_email_count", []interface{}{2, bobMap}, "Bob has 2 unread emails."},
	{"person_unread_email_count", []interface{}{0, bobStruct}, "Bob has 0 unread emails."},
	{"person_unread_email_count", []interface{}{1, bobStruct}, "Bob has 1 unread email."},
	{"person_unread_email_count", []interface{}{2, bobStruct}, "Bob has 2 unread emails."},

	{"person_unread_email_count_timeframe", []interface{}{3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": "0 days",
	}}, "Bob has 3 unread emails in the past 0 days."},
	{"person_unread_email_count_timeframe", []interface{}{3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": "1 day",
	}}, "Bob has 3 unread emails in the past 1 day."},
	{"person_unread_email_count_timeframe", []interface{}{3, map[string]interface{}{
		"Person":    "Bob",
		"Timeframe": "2 days",
	}}, "Bob has 3 unread emails in the past 2 days."},
}

func testFile(t *testing.T, path string) {
	b := bundle.New()
	b.MustLoadTranslationFile(path)

	T, err := b.Tfunc("en-US")
	if err != nil {
		t.Fatal(err)
	}

	for _, tc := range testCases {
		var args []interface{}
		if _, ok := tc.arg.([]interface{}); ok {
			args = tc.arg.([]interface{})
		} else {
			args = []interface{}{tc.arg}
		}

		got := T(tc.id, args...)
		if got != tc.want {
			t.Error("got: %v; want: %v", got, tc.want)
		}
	}
}

func TestJSONParse(t *testing.T) {
	testFile(t, "../goi18n/testdata/expected/en-us.all.json")
}

func TestYAMLParse(t *testing.T) {
	testFile(t, "../goi18n/testdata/en-us.yaml")
}

func TestJSONFlatParse(t *testing.T) {
	testFile(t, "../goi18n/testdata/en-us.flat.json")
}

func TestYAMLFlatParse(t *testing.T) {
	testFile(t, "../goi18n/testdata/en-us.flat.yaml")
}

func TestTOMLFlatParse(t *testing.T) {
	testFile(t, "../goi18n/testdata/en-us.flat.toml")
}