Skip to content

Commit 309e7c7

Browse files
authored
Add the &any-order flag to should-emit (#19)
1 parent ce8f0d5 commit 309e7c7

File tree

5 files changed

+128
-12
lines changed

5 files changed

+128
-12
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ In the default console reporter, the **test output** - on both _stdout_ and _std
177177

178178
The input mechanism and the equality logic are the same as those described for `should-be`.
179179

180-
- `should-emit`: ensures that the values passed via pipe (`|`) are _exactly the values_ in the `expected` list; _emission order matters_, unless the `order-key` option is set; on the other hand, the `strict` option works according to the equality rules described within the context of `should-be`.
180+
- `should-emit`: ensures that the values passed via pipe (`|`) are _exactly the values_ in the `expected` list; _emission order matters_, unless the `order-key` option or its mutally-exclusive `any-order` counterpart flag is set; on the other hand, the `strict` option works according to the equality rules described within the context of `should-be`.
181181

182182
The overall command is equivalent to:
183183

@@ -209,6 +209,19 @@ In the default console reporter, the **test output** - on both _stdout_ and _std
209209
Hello
210210
World
211211
]
212+
213+
all [
214+
92
215+
90
216+
(num 98)
217+
95
218+
] |
219+
should-emit &any-order [
220+
90
221+
92
222+
95
223+
98
224+
]
212225
```
213226

214227
**Please, note**: in lieu of a `should-emit` with just one value, it's more expressive to use `should-be`.

assertions/shared.elv

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,18 @@ fn create-assertion-on-tested-entries { |test description fail-message|
105105
fail $fail-message
106106
}
107107
}
108+
}
109+
110+
fn equalize { |&strict=$false &order-key=$nil|
111+
all | each { |value|
112+
get-minimal &strict=$strict $value
113+
} |
114+
{
115+
if $order-key {
116+
all |
117+
order &key=$order-key
118+
} else {
119+
all
120+
}
121+
}
108122
}

assertions/shared.test.elv

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,61 @@ use ./shared
6565
]
6666
}
6767
}
68+
69+
>> 'getting an equalized list' {
70+
var source = [
71+
(num 95)
72+
90
73+
98
74+
(num 92)
75+
]
76+
77+
>> 'when the strict flag is disabled' {
78+
>> 'when the order key is missing' {
79+
all $source |
80+
shared:equalize |
81+
should-emit &strict [
82+
95
83+
90
84+
98
85+
92
86+
]
87+
}
88+
89+
>> 'when the order key is passed' {
90+
all $source |
91+
shared:equalize &order-key=$to-string~ |
92+
should-emit &strict [
93+
90
94+
92
95+
95
96+
98
97+
]
98+
}
99+
}
100+
101+
>> 'when the strict flag is enabled' {
102+
>> 'when the order key is missing' {
103+
all $source |
104+
shared:equalize &strict |
105+
should-emit &strict [
106+
(num 95)
107+
90
108+
98
109+
(num 92)
110+
]
111+
}
112+
113+
>> 'when the order key is passed' {
114+
all $source |
115+
shared:equalize &strict &order-key=$to-string~ |
116+
should-emit &strict [
117+
90
118+
(num 92)
119+
(num 95)
120+
98
121+
]
122+
}
123+
}
124+
}
68125
}

assertions/should-emit.elv

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ use ./shared
22

33
var -error-message-base = 'should-emit assertion failed'
44

5-
fn should-emit { |&strict=$false &order-key=$nil expected|
5+
fn should-emit { |&strict=$false &order-key=$nil &any-order=$false expected|
66
if (not-eq (kind-of $expected) list) {
77
fail 'The expected argument must be a list of values'
88
}
99

10-
var actual = (
10+
if $any-order {
1111
if $order-key {
12-
put [(
13-
all |
14-
order &key=$order-key
15-
)]
12+
fail 'The &any-order flag and the &order-key option are mutually exclusive!'
1613
} else {
17-
put [(all)]
18-
} |
19-
shared:get-minimal &strict=$strict
20-
)
14+
set order-key = $to-string~
15+
}
16+
}
17+
18+
var actual = [(
19+
all |
20+
shared:equalize &strict=$strict &order-key=$order-key
21+
)]
2122

22-
var expected = (shared:get-minimal &strict=$strict $expected)
23+
var expected = [(
24+
all $expected |
25+
shared:equalize &strict=$strict &order-key=$order-key
26+
)]
2327

2428
if (not-eq $actual $expected) {
2529
shared:contrast [

assertions/should-emit.test.elv

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,34 @@ var expect-failure~ = (shared:create-expect-failure $should-emit~ $should-emit:-
124124
]
125125
}
126126

127+
>> 'when any order is admitted' {
128+
>> 'when the actual items and the expected items are the same, but in different order' {
129+
all [
130+
92
131+
90
132+
(num 98)
133+
95
134+
] |
135+
should-emit &any-order [
136+
90
137+
92
138+
95
139+
98
140+
]
141+
}
142+
143+
>> 'when passing an order-key, too' {
144+
fails {
145+
all [
146+
90
147+
92
148+
] |
149+
should-emit &order-key=$num~ &any-order [ 92 90 ]
150+
} |
151+
should-be 'The &any-order flag and the &order-key option are mutually exclusive!'
152+
}
153+
}
154+
127155
>> 'when failing' {
128156
>> 'the output should describe the context' {
129157
var output-tester = (

0 commit comments

Comments
 (0)