-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitives_test.go
More file actions
137 lines (122 loc) · 3.35 KB
/
Copy pathprimitives_test.go
File metadata and controls
137 lines (122 loc) · 3.35 KB
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
package fluent_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Ported from primitives_test.js.
// TestUnicodeEscapeOutOfRange covers a \U escape whose codepoint exceeds the
// Unicode maximum (U+10FFFF). It must render as U+FFFD REPLACEMENT CHARACTER
// without panicking, and parsing must continue normally afterwards.
func TestUnicodeEscapeOutOfRange(t *testing.T) {
// \U110000 is one past the maximum valid codepoint U+10FFFF.
b := newTestBundle(t, "x = { \"\\U110000\" }\nnext = After\n")
got, err := format(t, b, "x", nil)
assert.Equal(t, "�", got, "an out-of-range \\U escape becomes U+FFFD")
assert.NoError(t, err)
// Parsing continued past the broken escape: the next entry is intact.
got, err = format(t, b, "next", nil)
assert.Equal(t, "After", got)
assert.NoError(t, err)
}
func TestPrimitiveNumbers(t *testing.T) {
src := "one = { 1 }\n" +
"select = { 1 ->\n" +
" *[0] Zero\n" +
" [1] One\n" +
"}\n"
b := newTestBundle(t, src)
got, err := format(t, b, "one", nil)
assert.Equal(t, "1", got)
assert.NoError(t, err)
got, err = format(t, b, "select", nil)
assert.Equal(t, "One", got)
assert.NoError(t, err)
}
func TestPrimitiveSimpleStrings(t *testing.T) {
src := "foo = Foo\n" +
"\n" +
"placeable-literal = { \"Foo\" } Bar\n" +
"placeable-message = { foo } Bar\n" +
"\n" +
"selector-literal = { \"Foo\" ->\n" +
" *[Foo] Member 1\n" +
"}\n" +
"\n" +
"bar =\n" +
" .attr = Bar Attribute\n" +
"\n" +
"placeable-attr = { bar.attr }\n" +
"\n" +
"-baz = Baz\n" +
" .attr = BazAttribute\n" +
"\n" +
"selector-attr = { -baz.attr ->\n" +
" *[BazAttribute] Member 3\n" +
"}\n"
b := newTestBundle(t, src)
tests := []struct {
id string
want string
}{
{"foo", "Foo"},
{"placeable-literal", "Foo Bar"},
{"placeable-message", "Foo Bar"},
{"selector-literal", "Member 1"},
{"placeable-attr", "Bar Attribute"},
{"selector-attr", "Member 3"},
}
for _, tc := range tests {
t.Run(tc.id, func(t *testing.T) {
got, err := format(t, b, tc.id, nil)
assert.Equal(t, tc.want, got)
assert.NoError(t, err)
})
}
// Attribute value directly.
msg, _ := b.Message("bar")
attr, _ := msg.Attribute("attr")
got, err := b.FormatPattern(attr, nil)
assert.Equal(t, "Bar Attribute", got)
assert.NoError(t, err)
}
func TestPrimitiveComplexStrings(t *testing.T) {
src := "foo = Foo\n" +
"bar = { foo }Bar\n" +
"\n" +
"placeable-message = { bar }Baz\n" +
"\n" +
"baz =\n" +
" .attr = { bar }BazAttribute\n" +
"\n" +
"placeable-attr = { baz.attr }\n" +
"\n" +
"selector-attr = { baz.attr ->\n" +
" [FooBarBazAttribute] FooBarBaz\n" +
" *[other] Other\n" +
"}\n"
b := newTestBundle(t, src)
tests := []struct {
id string
want string
}{
{"bar", "FooBar"},
{"placeable-message", "FooBarBaz"},
{"placeable-attr", "FooBarBazAttribute"},
{"selector-attr", "FooBarBaz"},
}
for _, tc := range tests {
t.Run(tc.id, func(t *testing.T) {
got, err := format(t, b, tc.id, nil)
assert.Equal(t, tc.want, got)
assert.NoError(t, err)
})
}
// Attribute value directly.
t.Run("attribute directly", func(t *testing.T) {
msg, _ := b.Message("baz")
attr, _ := msg.Attribute("attr")
got, err := b.FormatPattern(attr, nil)
assert.Equal(t, "FooBarBazAttribute", got)
assert.NoError(t, err)
})
}