8000 Add tests for pattern selection (#863) · unicode-org/message-format-wg@b4fbf04 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4fbf04

Browse files
eemeliaphillips
andauthored
Add tests for pattern selection (#863)
* Add tests for pattern selection * Add missing errors * Apply suggestions from code review Co-authored-by: Addison Phillips <addison@unicode.org> --------- Co-authored-by: Addison Phillips <addison@unicode.org>
1 parent 92d8940 commit b4fbf04

File tree

3 files changed

+179
-171
lines changed

3 files changed

+179
-171
lines changed

test/README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,34 @@ The function `:test:function` requires a [Number Operand](/spec/registry.md#numb
6565

6666
#### Options
6767

68-
The only _option_ `:test:function` recognizes is `decimalPlaces`,
69-
a _digit size option_ for which only `0` and `1` are valid values.
68+
The following _options_ are available on `:test:function`:
69+
- `decimalPlaces`, a _digit size option_ for which only `0` and `1` are valid values.
70+
- `0`
71+
- `1`
72+
- `fails`
73+
- `never` (default)
74+
- `select`
75+
- `format`
76+
- `always`
7077

7178
All other _options_ and their values are ignored.
7279

7380
#### Behavior
7481

7582
When resolving a `:test:function` expression,
76-
its `Input` and `DecimalPlaces` values are determined as follows:
83+
its `Input`, `DecimalPlaces`, `FailsFormat`, and `FailsSelect` values are determined as follows:
7784

7885
1. Let `DecimalPlaces` be 0.
86+
1. Let `FailsFormat` be `false`.
87+
1. Let `FailsSelect` be `false`.
7988
1. Let `arg` be the resolved value of the _expression_ _operand_.
8089
1. If `arg` is the resolved value of an _expression_
8190
with a `:test:function`, `:test:select`, or `:test:format` _annotation_
8291
for which resolution has succeeded, then
8392
1. Let `Input` be the `Input` value of `arg`.
8493
1. Set `DecimalPlaces` to be `DecimalPlaces` value of `arg`.
94+
1. Set `FailsFormat` to be `FailsFormat` value of `arg`.
95+
1. Set `FailsSelect` to be `FailsSelect` value of `arg`.
8596
1. Else if `arg` is a numerical value
8697
or a string matching the `number-literal` production, then
8798
1. Let `Input` be the numerical value of `arg`.
@@ -96,12 +107,24 @@ its `Input` and `DecimalPlaces` values are determined as follows:
96107
1. Else if its value is not an unresolved value set by _option resolution_,
97108
1. Emit "bad-option" _Resolution Error_.
98109
1. Use a _fallback value_ as the resolved value of the _expression_.
110+
1. If the `fails` _option_ is set, then
111+
1. If its value resolves to the string `'always'`, then
112+
1. Set `FailsFormat` to be `true`.
113+
1. Set `FailsSelect` to be `true`.
114+
1. Else if its value resolves to the string `'format'`, then
115+
1. Set `FailsFormat` to be `true`.
116+
1. Else if its value resolves to the string `'select'`, then
117+
1. Set `FailsSelect` to be `true`.
118+
1. Else if its value does not resolve to the string `'never'`, then
119+
1. Emit "bad-option" _Resolution Error_.
99120

100121
When `:test:function` is used as a _selector_,
101122
the behaviour of calling it as the `rv` value of MatchSelectorKeys(`rv`, `keys`)
102123
(see [Resolve Preferences](/spec/formatting.md#resolve-preferences) for more information)
103-
depends on its `Input` and `DecimalPlaces` values.
124+
depends on its `Input`, `DecimalPlaces` and `FailsSelect` values.
104125

126+
- If `FailsSelect` is `true`,
127+
calling the method will fail and not return any value.
105128
- If the `Input` is 1 and `DecimalPlaces` is 1,
106129
the method will return some slice of the list « `'1.0'`, `'1'` »,
107130
depending on whether those values are included in `keys`.
@@ -128,6 +151,8 @@ If the formatting target is a sequence of parts,
128151
each of the above parts will be emitted separately
129152
rather than being concatenated into a single string.
130153

154+
If `FailsFormat` is `true`,
155+
attempting to format the _placeholder_ to any formatting target will fail.
131156

132157
### `:test:select`
133158

test/tests/functions/number.json

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -209,173 +209,6 @@
209209
}
210210
]
211211
},
212-
{
213-
"src": ".match {$foo :number} one {{one}} * {{other}}",
214-
"params": [
215-
{
216-
"name": "foo",
217-
"value": 1
218-
}
219-
],
220-
"exp": "one"
221-
},
222-
{
223-
"src": ".match {$foo :number} 1 {{=1}} one {{one}} * {{other}}",
224-
"params": [
225-
{
226-
"name": "foo",
227-
"value": 1
228-
}
229-
],
230-
"exp": "=1"
231-
},
232-
{
233-
"src": ".match {$foo :number} one {{one}} 1 {{=1}} * {{other}}",
234-
"params": [
235-
{
236-
"name": "foo",
237-
"value": 1
238-
}
239-
],
240-
"exp": "=1"
241-
},
242-
{
243-
"src": ".match {$foo :number} {$bar :number} one one {{one one}} one * {{one other}} * * {{other}}",
244-
"params": [
245-
{
246-
"name": "foo",
247-
"value": 1
248-
},
249-
{
250-
"name": "bar",
251-
"value": 1
252-
}
253-
],
254-
"exp": "one one"
255-
},
256-
{
257-
"src": ".match {$foo :number} {$bar :number} one one {{one one}} one * {{one other}} * * {{other}}",
258-
"params": [
259-
{
260-
"name": "foo",
261-
"value": 1
262-
},
263-
{
264-
"name": "bar",
265-
"value": 2
266-
}
267-
],
268-
"exp": "one other"
269-
},
270-
{
271-
"src": ".match {$foo :number} {$bar :number} one one {{one one}} one * {{one other}} * * {{other}}",
272-
"params": [
273-
{
274-
"name": "foo",
275-
"value": 2
276-
},
277-
{
278-
"name": "bar",
279-
"value": 2
280-
}
281-
],
282-
"exp": "other"
283-
},
284-
{
285-
"src": ".input {$foo :number} .match {$foo} one {{one}} * {{other}}",
286-
"params": [
287-
{
288-
"name": "foo",
289-
"value": 1
290-
}
291-
],
292-
"exp": "one"
293-
},
294-
{
295-
"src": ".local $foo = {$bar :number} .match {$foo} one {{one}} * {{other}}",
296-
"params": [
297-
{
298-
"name": "bar",
299-
"value": 1
300-
}
301-
],
302-
"exp": "one"
303-
},
304-
{
305-
"src": ".input {$foo :number} .local $bar = {$foo} .match {$bar} one {{one}} * {{other}}",
306-
"params": [
307-
{
308-
"name": "foo",
309-
"value": 1
310-
}
311-
],
312-
"exp": "one"
313-
},
314-
{
315-
"src": ".input {$bar :number} .match {$bar} one {{one}} * {{other}}",
316-
"params": [
317-
{
318-
"name": "bar",
319-
"value": 2
320-
}
321-
],
322-
"exp": "other"
323-
},
324-
{
325-
"src": ".input {$bar} .match {$bar :number} one {{one}} * {{other}}",
326-
"params": [
327-
{
328-
"name": "bar",
329-
"value": 1
330-
}
331-
],
332-
"exp": "one"
333-
},
334-
{
335-
"src": ".input {$bar} .match {$bar :number} one {{one}} * {{other}}",
336-
"params": [
337-
{
338-
"name": "bar",
339-
"value": 2
340-
}
341-
],
342-
"exp": "other"
343-
},
344-
{
345-
"src": ".input {$none} .match {$foo :number} one {{one}} * {{{$none}}}",
346-
"params": [
347-
{
348-
"name": "foo",
349-
"value": 1
350-
}
351-
],
352-
"exp": "one"
353-
},
354-
{
355-
"src": ".local $bar = {$none} .match {$foo :number} one {{one}} * {{{$bar}}}",
356-
"params": [
357-
{
358-
"name": "foo",
359-
"value": 1
360-
}
361-
],
362-
"exp": "one"
363-
},
364-
{
365-
"src": ".local $bar = {$none} .match {$foo :number} one {{one}} * {{{$bar}}}",
366-
"params": [
367-
{
368-
"name": "foo",
369-
"value": 2
370-
}
371-
],
372-
"exp": "{$none}",
373-
"expErrors": [
374-
{
375-
"type": "unresolved-variable"
376-
}
377-
]
378-
},
379212
{
380213
"src": "{42 :number @foo @bar=13}",
381214
"exp": "42",

test/tests/pattern-selection.json

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/unicode-org/message-format-wg/main/test/schemas/v0/tests.schema.json",
3+
"scenario": "Pattern selection",
4+
"description": "Tests for pattern selection",
5+
"defaultTestProperties": {
6+
"locale": "und"
7+
},
8+
"tests": [
9+
{
10+
"src": ".match {1 :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
11+
"exp": "1"
12+
},
13+
{
14+
"src": ".match {0 :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
15+
"exp": "other"
16+
},
17+
{
18+
"src": ".match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
19+
"params": [{ "name": "x", "value": 1 }],
20+
"exp": "1"
21+
},
22+
{
23+
"src": ".match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
24+
"params": [{ "name": "x", "value": 2 }],
25+
"exp": "other"
26+
},
27+
{
28+
"src": ".input {$x} .match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
29+
"params": [{ "name": "x", "value": 1 }],
30+
"exp": "1"
31+
},
32+
{
33+
"src": ".input {$x} .match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
34+
"params": [{ "name": "x", "value": 2 }],
35+
"exp": "other"
36+
},
37+
{
38+
"src": ".input {$x :test:select} .match {$x} 1.0 {{1.0}} 1 {{1}} * {{other}}",
39+
"params": [{ "name": "x", "value": 1 }],
40+
"exp": "1"
41+
},
42+
{
43+
"src": ".input {$x :test:select} .match {$x} 1.0 {{1.0}} 1 {{1}} * {{other}}",
44+
"params": [{ "name": "x", "value": 2 }],
45+
"exp": "other"
46+
},
47+
{
48+
"src": ".input {$x :test:select} .local $y = {$x} .match {$y} 1.0 {{1.0}} 1 {{1}} * {{other}}",
49+
"params": [{ "name": "x", "value": 1 }],
50+
"exp": "1"
51+
},
52+
{
53+
"src": ".input {$x :test:select} .local $y = {$x} .match {$y} 1.0 {{1.0}} 1 {{1}} * {{other}}",
54+
"params": [{ "name": "x", "value": 2 }],
55+
"exp": "other"
56+
},
57+
{
58+
"src": ".match {1 :test:select decimalPlaces=1} 1.0 {{1.0}} 1 {{1}} * {{other}}",
59+
"exp": "1.0"
60+
},
61+
{
62+
"src": ".match {1 :test:select decimalPlaces=1} 1 {{1}} 1.0 {{1.0}} * {{other}}",
63+
"exp": "1.0"
64+
},
65+
{
66+
"src": ".match {1 :test:select decimalPlaces=9} 1.0 {{1.0}} 1 {{1}} * {{bad-option-value}}",
67+
"exp": "bad-option-value",
68+
"expErrors": [{ "type": "bad-option" }, { "type": "bad-selector" }]
69+
},
70+
{
71+
"src": ".input {$x :test:select} .match {$x :test:select decimalPlaces=1} 1.0 {{1.0}} 1 {{1}} * {{other}}",
72+
"params": [{ "name": "x", "value": 1 }],
73+
"exp": "1.0"
74+
},
75+
{
76+
"src": ".input {$x :test:select decimalPlaces=1} .match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
77+
"params": [{ "name": "x", "value": 1 }],
78+
"exp": "1.0"
79+
},
80+
{
81+
"src": ".input {$x :test:select} .local $y = {$x :test:select decimalPlaces=1} .match {$y} 1.0 {{1.0}} 1 {{1}} * {{other}}",
82+
"params": [{ "name": "x", "value": 1 }],
83+
"exp": "1.0"
84+
},
85+
{
86+
"src": ".input {$x :test:select decimalPlaces=1} .local $y = {$x :test:select} .match {$y} 1.0 {{1.0}} 1 {{1}} * {{other}}",
87+
"params": [{ "name": "x", "value": 1 }],
88+
"exp": "1.0"
89+
},
90+
{
91+
"src": ".input {$x :test:select decimalPlaces=9} .match {$x :test:select decimalPlaces=1} 1.0 {{1.0}} 1 {{1}} * {{bad-option-value}}",
92+
"params": [{ "name": "x", "value": 1 }],
93+
"exp": "bad-option-value",
94+
"expErrors": [
95+
{ "type": "bad-option" },
96+
{ "type": "bad-operand" },
97+
{ "type": "bad-selector" }
98+
]
99+
},
100+
{
101+
"src": ".match {1 :test:select fails=select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
102+
"exp": "other",
103+
"expErrors": [{ "type": "bad-selector" }]
104+
},
105+
{
106+
"src": ".match {1 :test:select fails=format} 1.0 {{1.0}} 1 {{1}} * {{other}}",
107+
"exp": "1"
108+
},
109+
{
110+
"src": ".match {1 :test:format} 1.0 {{1.0}} 1 {{1}} * {{other}}",
111+
"exp": "other",
112+
"expErrors": [{ "type": "bad-selector" }]
113+
},
114+
{
115+
"src": ".match {$x :test:select} 1.0 {{1.0}} 1 {{1}} * {{other}}",
116+
"exp": "other",
117+
"expErrors": [
118+
{ "type": "unresolved-variable" },
119+
{ "type": "bad-operand" },
120+
{ "type": "bad-selector" }
121+
]
122+
},
123+
{
124+
"src": ".match {1 :test:select} {1 :test:select} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
125+
"exp": "1,1"
126+
},
127+
{
128+
"src": ".match {1 :test:select} {0 :test:select} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
129+
"exp": "1,*"
130+
},
131+
{
132+
"src": ".match {0 :test:select} {1 :test:select} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
133+
"exp": "*,1"
134+
},
135+
{
136+
"src": ".match {0 :test:select} {0 :test:select} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
137+
"exp": "*,*"
138+
},
139+
{
140+
"src": ".match {1 :test:select fails=select} {1 :test:select} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
141+
"exp": "*,1",
142+
"expErrors": [{ "type": "bad-selector" }]
143+
},
144+
{
145+
"src": ".match {1 :test:select} {1 :test:format} 1 1 {{1,1}} 1 * {{1,*}} * 1 {{*,1}} * * {{*,*}}",
146+
"exp": "1,*",
147+
"expErrors": [{ "type": "bad-selector" }]
148+
}
149+
]
150+
}

0 commit comments

Comments
 (0)
0