-
Notifications
You must be signed in to change notification settings - Fork 8
/
messageformat_test.go
72 lines (64 loc) · 2.3 KB
/
messageformat_test.go
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
// @TODO(gotnospirit) add test on SetCulture, Format, getNamedKey, getFormatter
package messageformat
import (
"fmt"
"testing"
)
// doTest(t, Test{
// "You have {NUM_TASKS, plural, zero {no task} one {one task} two {two tasks} few{few tasks} many {many tasks} other {# tasks} =42 {the answer to the life, the universe and everything tasks}} remaining.",
// []Expectation{
// {map[string]interface{}{"NUM_TASKS": -1}, "You have -1 tasks remaining."},
// {map[string]interface{}{"NUM_TASKS": 0}, "You have no task remaining."},
// {map[string]interface{}{"NUM_TASKS": 1}, "You have one task remaining."},
// {map[string]interface{}{"NUM_TASKS": 2}, "You have two tasks remaining."},
// {map[string]interface{}{"NUM_TASKS": 3}, "You have few tasks remaining."},
// {map[string]interface{}{"NUM_TASKS": 6}, "You have many tasks remaining."},
// {map[string]interface{}{"NUM_TASKS": 15}, "You have 15 tasks remaining."},
// {map[string]interface{}{"NUM_TASKS": 42}, "You have the answer to the life, the universe and everything tasks remaining."},
// },
// })
func doParse(input string) (*MessageFormat, error) {
o, err := New()
if err != nil {
return nil, err
}
mf, err := o.Parse(input)
if err != nil {
return nil, err
}
return mf, nil
}
func TestSetPluralFunction(t *testing.T) {
mf, err := doParse(`{N,plural,one{1}other{2}}`)
if err != nil {
t.Errorf("Unexpected parse failure: `%s`", err.Error())
} else {
// checks we can't unset the default plural function
err := mf.SetPluralFunction(nil)
doTestError(t, "PluralFunctionRequired", err)
data := map[string]interface{}{"N": 1}
result, err := mf.FormatMap(data)
if err != nil {
t.Errorf("Unexpected error : `%s`", err.Error())
} else if result != "1" {
t.Errorf("Unexpected format result : `%s`", result)
} else {
// checks we can set a new plural function and get a different result
err := mf.SetPluralFunction(func(interface{}, bool) string {
return "other"
})
if err != nil {
t.Errorf("Unexpected error <%s>", err)
} else {
result, err := mf.FormatMap(data)
if err != nil {
t.Errorf("Unexpected error : `%s`", err.Error())
} else if result != "2" {
t.Errorf("Unexpected format result : `%s`", result)
} else if testing.Verbose() {
fmt.Printf("- Got expected value <%s>\n", result)
}
}
}
}
}