Skip to content

Commit c2cb2cc

Browse files
authored
word-count: improve custom assertion so that the actual and expected values are obvious (#84)
1 parent aba8ec4 commit c2cb2cc

3 files changed

Lines changed: 47 additions & 47 deletions

File tree

exercises/practice/word-count/.meta/spec_generator.moon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ kv_table = (tbl, level) ->
1515
test_helpers: [[
1616
-- ----------------------------------------------------------
1717
same_kv = (state, arguments) ->
18-
expected = arguments[1]
19-
actual = arguments[2]
18+
actual = arguments[1]
19+
expected = arguments[2]
2020
return false if #expected != #actual
2121
for k, v in pairs expected
2222
return false if actual[k] != v
2323
true
2424
2525
say = require 'say'
26-
say\set 'assertion.same_kv.positive', 'Expected\n%s\nto have the same keys and values as\n%s'
27-
say\set 'assertion.same_kv.negative', 'Expected\n%s\nto not have the same keys and values as\n%s'
26+
say\set 'assertion.same_kv.positive', 'Actual result\n%s\ndoes not have the same keys and values as expected\n%s'
27+
say\set 'assertion.same_kv.negative', 'Actual result\n%s\nwas not supposed to be the same as expected\n%s'
2828
assert\register 'assertion', 'same_kv', same_kv, 'assertion.same_kv.positive', 'assertion.same_kv.negative'
2929
-- ----------------------------------------------------------
3030
]]
@@ -33,7 +33,7 @@ kv_table = (tbl, level) ->
3333
lines = {
3434
"result = count_words #{json.encode case.input.sentence}",
3535
"expected = #{kv_table case.expected, level}",
36-
"assert.has.same_kv expected, result"
36+
"assert.has.same_kv result, expected"
3737
}
3838
table.concat [indent line, level for line in *lines], '\n'
3939
}

exercises/practice/word-count/word_count_spec.moon

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import count_words from require 'word_count'
33
describe 'word-count', ->
44
-- ----------------------------------------------------------
55
same_kv = (state, arguments) ->
6-
expected = arguments[1]
7-
actual = arguments[2]
6+
actual = arguments[1]
7+
expected = arguments[2]
88
return false if #expected != #actual
99
for k, v in pairs expected
1010
return false if actual[k] != v
1111
true
1212

1313
say = require 'say'
14-
say\set 'assertion.same_kv.positive', 'Expected\n%s\nto have the same keys and values as\n%s'
15-
say\set 'assertion.same_kv.negative', 'Expected\n%s\nto not have the same keys and values as\n%s'
14+
say\set 'assertion.same_kv.positive', 'Actual result\n%s\ndoes not have the same keys and values as expected\n%s'
15+
say\set 'assertion.same_kv.negative', 'Actual result\n%s\nwas not supposed to be the same as the expected value.'
1616
assert\register 'assertion', 'same_kv', same_kv, 'assertion.same_kv.positive', 'assertion.same_kv.negative'
1717
-- ----------------------------------------------------------
1818

@@ -21,135 +21,135 @@ describe 'word-count', ->
2121
expected = {
2222
word: 1,
2323
}
24-
assert.has.same_kv expected, result
24+
assert.has.same_kv result, expected
2525

2626
pending 'count one of each word', ->
2727
result = count_words "one of each"
2828
expected = {
29-
each: 1,
3029
one: 1,
30+
each: 1,
3131
of: 1,
3232
}
33-
assert.has.same_kv expected, result
33+
assert.has.same_kv result, expected
3434

3535
pending 'multiple occurrences of a word', ->
3636
result = count_words "one fish two fish red fish blue fish"
3737
expected = {
38+
one: 1,
3839
fish: 4,
3940
blue: 1,
40-
one: 1,
41-
two: 1,
4241
red: 1,
42+
two: 1,
4343
}
44-
assert.has.same_kv expected, result
44+
assert.has.same_kv result, expected
4545

4646
pending 'handles cramped lists', ->
4747
result = count_words "one,two,three"
4848
expected = {
49-
two: 1,
5049
one: 1,
50+
two: 1,
5151
three: 1,
5252
}
53-
assert.has.same_kv expected, result
53+
assert.has.same_kv result, expected
5454

5555
pending 'handles expanded lists', ->
5656
result = count_words "one,\ntwo,\nthree"
5757
expected = {
58-
two: 1,
5958
one: 1,
59+
two: 1,
6060
three: 1,
6161
}
62-
assert.has.same_kv expected, result
62+
assert.has.same_kv result, expected
6363

6464
pending 'ignore punctuation', ->
6565
result = count_words "car: carpet as java: javascript!!&@$%^&"
6666
expected = {
67+
carpet: 1,
6768
car: 1,
69+
javascript: 1,
6870
as: 1,
6971
java: 1,
70-
javascript: 1,
71-
carpet: 1,
7272
}
73-
assert.has.same_kv expected, result
73+
assert.has.same_kv result, expected
7474

7575
pending 'include numbers', ->
7676
result = count_words "testing, 1, 2 testing"
7777
expected = {
78-
testing: 2,
79-
'2': 1,
8078
'1': 1,
79+
'2': 1,
80+
testing: 2,
8181
}
82-
assert.has.same_kv expected, result
82+
assert.has.same_kv result, expected
8383

8484
pending 'normalize case', ->
8585
result = count_words "go Go GO Stop stop"
8686
expected = {
87-
go: 3,
8887
stop: 2,
88+
go: 3,
8989
}
90-
assert.has.same_kv expected, result
90+
assert.has.same_kv result, expected
9191

9292
pending 'with apostrophes', ->
9393
result = count_words "'First: don't laugh. Then: don't cry. You're getting it.'"
9494
expected = {
9595
then: 1,
96-
laugh: 1,
96+
cry: 1,
97+
"you're": 1,
98+
getting: 1,
9799
"don't": 2,
98-
it: 1,
99100
first: 1,
100-
getting: 1,
101-
"you're": 1,
102-
cry: 1,
101+
laugh: 1,
102+
it: 1,
103103
}
104-
assert.has.same_kv expected, result
104+
assert.has.same_kv result, expected
105105

106106
pending 'with quotations', ->
107107
result = count_words "Joe can't tell between 'large' and large."
108108
expected = {
109-
joe: 1,
109+
between: 1,
110110
tell: 1,
111+
joe: 1,
111112
large: 2,
112-
and: 1,
113-
between: 1,
114113
"can't": 1,
114+
and: 1,
115115
}
116-
assert.has.same_kv expected, result
116+
assert.has.same_kv result, expected
117117

118118
pending 'substrings from the beginning', ->
119119
result = count_words "Joe can't tell between app, apple and a."
120120
expected = {
121+
between: 1,
121122
a: 1,
122-
and: 1,
123-
joe: 1,
124-
apple: 1,
125123
tell: 1,
126124
app: 1,
127-
between: 1,
125+
joe: 1,
126+
apple: 1,
128127
"can't": 1,
128+
and: 1,
129129
}
130-
assert.has.same_kv expected, result
130+
assert.has.same_kv result, expected
131131

132132
pending 'multiple spaces not detected as a word', ->
133133
result = count_words " multiple whitespaces"
134134
expected = {
135135
multiple: 1,
136136
whitespaces: 1,
137137
}
138-
assert.has.same_kv expected, result
138+
assert.has.same_kv result, expected
139139

140140
pending 'alternating word separators not detected as a word', ->
141141
result = count_words ",\n,one,\n ,two \n 'three'"
142142
expected = {
143-
two: 1,
144143
one: 1,
144+
two: 1,
145145
three: 1,
146146
}
147-
assert.has.same_kv expected, result
147+
assert.has.same_kv result, expected
148148

149149
pending 'quotation for word with apostrophe', ->
150150
result = count_words "can, can't, 'can't'"
151151
expected = {
152152
"can't": 2,
153153
can: 1,
154154
}
155-
assert.has.same_kv expected, result
155+
assert.has.same_kv result, expected

exercises/test_helpers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ Useful for generating pretty tables mostly.
123123
124124
- dnd-character: `assert.between value, min, max`
125125
- space-age: `assert.approx_equal #{case.expected}, result`
126-
- word-count: `assert.has.same_kv table1, table2`:x
126+
- word-count: `assert.has.same_kv result, expected`
127127

0 commit comments

Comments
 (0)