-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_parameterized_terms.py
More file actions
262 lines (227 loc) · 8.78 KB
/
Copy pathtest_parameterized_terms.py
File metadata and controls
262 lines (227 loc) · 8.78 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
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
import pytest
from fluent.runtime import FluentBundle, FluentResource
from fluent.runtime.errors import FluentFormatError, FluentReferenceError
from ..utils import dedent_ftl
class TestParameterizedTerms:
@pytest.fixture
def bundle(self):
bundle = FluentBundle(["en-US"], use_isolating=False)
bundle.add_resource(
FluentResource(
dedent_ftl(
"""
-thing = { $article ->
*[definite] the thing
[indefinite] a thing
[none] thing
}
thing-no-arg = { -thing }
thing-no-arg-alt = { -thing() }
thing-with-arg = { -thing(article: "indefinite") }
thing-positional-arg = { -thing("foo") }
thing-fallback = { -thing(article: "somethingelse") }
thing-variable-arg = { -thing(article: $art) }
bad-term = { -missing() }
"""
)
)
)
return bundle
def test_argument_omitted(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("thing-no-arg").value, {})
assert val == "the thing"
assert errs == []
def test_argument_omitted_alt(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-no-arg-alt").value, {}
)
assert val == "the thing"
assert errs == []
def test_with_argument(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-with-arg").value, {}
)
assert val == "a thing"
assert errs == []
def test_positional_arg(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-positional-arg").value, {}
)
assert val == "the thing"
assert errs == [
FluentFormatError("Ignored positional arguments passed to term '-thing'")
]
def test_fallback(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-fallback").value, {}
)
assert val == "the thing"
assert errs == []
def test_variable_named_arg(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-variable-arg").value, {"art": "indefinite"}
)
assert val == "a thing"
assert errs == []
def test_missing_variable_named_arg(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("thing-variable-arg").value, {}
)
assert val == "the thing"
assert errs == [FluentReferenceError('Unknown external: art')]
def test_no_implicit_access_to_external_args(self, bundle):
# The '-thing' term should not get passed article="indefinite"
val, errs = bundle.format_pattern(
bundle.get_message("thing-no-arg").value, {"article": "indefinite"}
)
assert val == "the thing"
assert errs == []
def test_no_implicit_access_to_external_args_but_term_args_still_passed(
self, bundle
):
val, errs = bundle.format_pattern(
bundle.get_message("thing-with-arg").value, {"article": "none"}
)
assert val == "a thing"
assert errs == []
def test_bad_term(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("bad-term").value, {})
assert val == "{-missing}"
assert errs == [FluentReferenceError("Unknown term: -missing")]
class TestParameterizedTermAttributes:
@pytest.fixture
def bundle(self):
bundle = FluentBundle(["en-US"], use_isolating=False)
bundle.add_resource(
FluentResource(
dedent_ftl(
"""
-brand = Cool Thing
.status = { $version ->
[v2] available
*[v1] deprecated
}
attr-with-arg = { -brand } is { -brand.status(version: "v2") ->
[available] available, yay!
*[deprecated] deprecated, sorry
}
-other = { $arg ->
[a] ABC
*[d] DEF
}
missing-attr-ref = { -other.missing(arg: "a") ->
[ABC] ABC option
*[DEF] DEF option
}
"""
)
)
)
return bundle
def test_with_argument(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("attr-with-arg").value, {})
assert val == "Cool Thing is available, yay!"
assert errs == []
def test_missing_attr(self, bundle):
# We don't fall back from attributes, get default.
val, errs = bundle.format_pattern(
bundle.get_message("missing-attr-ref").value, {}
)
assert val == "DEF option"
assert errs == [FluentReferenceError("Unknown attribute: -other.missing")]
class TestNestedParameterizedTerms:
@pytest.fixture
def bundle(self):
bundle = FluentBundle(["en-US"], use_isolating=False)
bundle.add_resource(
FluentResource(
dedent_ftl(
"""
-thing = { $article ->
*[definite] { $first-letter ->
*[lower] the thing
[upper] The thing
}
[indefinite] { $first-letter ->
*[lower] a thing
[upper] A thing
}
}
both-args = { -thing(first-letter: "upper", article: "indefinite") }.
outer-arg = This is { -thing(article: "indefinite") }.
inner-arg = { -thing(first-letter: "upper") }.
neither-arg = { -thing() }.
"""
)
)
)
return bundle
def test_both_args(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("both-args").value, {})
assert val == "A thing."
assert errs == []
def test_outer_arg(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("outer-arg").value, {})
assert val == "This is a thing."
assert errs == []
def test_inner_arg(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("inner-arg").value, {})
assert val == "The thing."
assert errs == []
def test_inner_arg_with_external_args(self, bundle):
val, errs = bundle.format_pattern(
bundle.get_message("inner-arg").value, {"article": "indefinite"}
)
assert val == "The thing."
assert errs == []
def test_neither_arg(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("neither-arg").value, {})
assert val == "the thing."
assert errs == []
class TestTermsCalledFromTerms:
@pytest.fixture
def bundle(self):
bundle = FluentBundle(["en-US"], use_isolating=False)
bundle.add_resource(
FluentResource(
dedent_ftl(
"""
-foo = {$a} {$b}
-bar = {-foo(b: 2)}
-baz = {-foo}
ref-bar = {-bar(a: 1)}
ref-baz = {-baz(a: 1)}
"""
)
)
)
return bundle
def test_term_args_isolated_with_call_syntax(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("ref-bar").value, {})
assert val == "a 2"
assert errs == []
def test_term_args_isolated_without_call_syntax(self, bundle):
val, errs = bundle.format_pattern(bundle.get_message("ref-baz").value, {})
assert val == "a b"
assert errs == []
class TestMessagesCalledFromTerms:
def test_messages_inherit_term_args(self):
bundle = FluentBundle(["en-US"], use_isolating=False)
bundle.add_resource(
FluentResource(
dedent_ftl(
"""
msg = Msg is {$arg}
-foo = {msg}
ref-foo = {-foo(arg: 1)}
"""
)
)
)
# This behaviour may change in future, message calls might be
# disallowed from inside terms
val, errs = bundle.format_pattern(
bundle.get_message("ref-foo").value, {"arg": 2}
)
assert val == "Msg is 1"
assert errs == []