Skip to content

Commit 6de11dc

Browse files
authored
All the exercises use the test_helpers library (#149)
[no important files changed]
1 parent 7f2fa3e commit 6de11dc

63 files changed

Lines changed: 406 additions & 577 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/generate-spec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ included_tests_from_toml = (path) ->
5050
-- functions marked as global so spec_generators can see them
5151
export indent, quote, is_json_null, is_empty, contains
5252

53-
indent = (text, level) -> string.rep(' ', level) .. text
53+
indent = (text, level) ->
54+
error 'Provide a level for `indent`', 2 if not level
55+
string.rep(' ', level) .. text
5456

5557
quote = (str) ->
5658
if str\find "'"

exercises/practice/all-your-base/.meta/spec_generator.moon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
list_of_digits = (digits) -> "{#{table.concat digits, ', '}}"
1+
import int_list from require 'test_helpers'
22

33
{
44
module_imports: {'rebase'},
@@ -7,13 +7,13 @@ list_of_digits = (digits) -> "{#{table.concat digits, ', '}}"
77
local lines
88
if not case.expected.error
99
lines = {
10-
"result = rebase in_base: #{case.input.inputBase}, out_base: #{case.input.outputBase}, digits: #{list_of_digits case.input.digits}",
11-
"expected = #{list_of_digits case.expected}",
10+
"result = rebase in_base: #{case.input.inputBase}, out_base: #{case.input.outputBase}, digits: #{int_list case.input.digits}",
11+
"expected = #{int_list case.expected}",
1212
"assert.are.same expected, result"
1313
}
1414
else
1515
lines = {
16-
"f = -> rebase in_base: #{case.input.inputBase}, out_base: #{case.input.outputBase}, digits: #{list_of_digits case.input.digits}",
16+
"f = -> rebase in_base: #{case.input.inputBase}, out_base: #{case.input.outputBase}, digits: #{int_list case.input.digits}",
1717
"assert.has.errors f, #{quote case.expected.error}"
1818
}
1919

exercises/practice/allergies/.meta/spec_generator.moon

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
str_list = (list) ->
2-
"{#{table.concat [quote word for word in *list], ', '}}"
1+
import word_list from require 'test_helpers'
32

43
{
54
module_name: 'Allergies',
@@ -15,7 +14,7 @@ str_list = (list) ->
1514
when 'list'
1615
lines = {
1716
"result = Allergies.list #{case.input.score}",
18-
"expected = #{str_list case.expected}",
17+
"expected = #{word_list case.expected}",
1918
"assert.is.same expected, result",
2019
}
2120
table.concat [indent line, level for line in *lines], '\n'

exercises/practice/anagram/.meta/spec_generator.moon

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
list_of_words = (list) ->
2-
"{#{table.concat [quote word for word in *list], ', '}}"
1+
import word_list from require 'test_helpers'
32

43
{
54
module_name: 'find_anagrams',
65
generate_test: (case, level) ->
76
lines = {
8-
"result = find_anagrams #{quote case.input.subject}, #{list_of_words case.input.candidates}",
9-
"expected = #{list_of_words case.expected}",
7+
"result = find_anagrams #{quote case.input.subject}, #{word_list case.input.candidates}",
8+
"expected = #{word_list case.expected}",
109
"assert.are.same expected, result"
1110
}
1211
table.concat [indent line, level for line in *lines], '\n'

exercises/practice/baffling-birthdays/.meta/spec_generator.moon

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
tablex = require 'pl.tablex'
2-
3-
int_list = (list) -> "{#{table.concat list, ', '}}"
4-
5-
string_list = (list) ->
6-
"{#{table.concat [quote word for word in *list], ', '}}"
2+
import int_list, word_list from require 'test_helpers'
73

84
{
95
module_name: 'BafflingBirthdays',
@@ -13,7 +9,7 @@ string_list = (list) ->
139
switch case.property
1410
when 'sharedBirthday'
1511
lines = {
16-
"birthdates = #{string_list case.input.birthdates}",
12+
"birthdates = #{word_list case.input.birthdates}",
1713
"assert.is.#{case.expected} BafflingBirthdays.#{case.property} birthdates",
1814
}
1915
when 'estimatedProbabilityOfSharedBirthday'
@@ -68,7 +64,7 @@ string_list = (list) ->
6864
tablex = require 'pl.tablex'
6965
7066
--
71-
epsilon = 0.5
67+
epsilon = 1.0
7268
is_close_to = (state, arguments) ->
7369
{a, b} = arguments
7470
math.abs(a - b) <= epsilon

exercises/practice/baffling-birthdays/baffling_birthdays_spec.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe 'baffling-birthdays', ->
66
tablex = require 'pl.tablex'
77

88
--
9-
epsilon = 0.5
9+
epsilon = 1.0
1010
is_close_to = (state, arguments) ->
1111
{a, b} = arguments
1212
math.abs(a - b) <= epsilon

exercises/practice/binary-search-tree/.meta/spec_generator.moon

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
stringx = require 'pl.stringx'
22
import dump from require 'moon'
3+
import word_list from require 'test_helpers'
34

45
bst = (t, level) ->
56
dumped = dump t
67
dumped = dumped\gsub('%[(.-)%] =', '%1:')
78
lines = [indent line, level for line in *(stringx.splitlines dumped)]
89
table.concat(lines, "\n")\gsub('{%s+}', 'nil')\gsub('^%s+', '')
910

10-
string_list = (list) ->
11-
"{#{table.concat [quote word for word in *list], ', '}}"
12-
13-
1411
{
1512
module_name: 'BinarySearchTree',
1613

@@ -19,16 +16,16 @@ string_list = (list) ->
1916
switch case.property
2017
when 'data'
2118
lines = {
22-
"tree = BinarySearchTree #{string_list case.input.treeData}"
19+
"tree = BinarySearchTree #{word_list case.input.treeData}"
2320
"result = tree\\data!"
2421
"expected = #{bst case.expected, level}",
2522
"assert.are.same expected, result"
2623
}
2724
when 'sortedData'
2825
lines = {
29-
"tree = BinarySearchTree #{string_list case.input.treeData}"
26+
"tree = BinarySearchTree #{word_list case.input.treeData}"
3027
"result = tree\\sorted!"
31-
"expected = #{string_list case.expected}",
28+
"expected = #{word_list case.expected}",
3229
"assert.are.same expected, result"
3330
}
3431
table.concat [indent line, level for line in *lines], '\n'

exercises/practice/binary-search-tree/binary_search_tree_spec.moon

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ describe 'binary-search-tree', ->
55
tree = BinarySearchTree {'4'}
66
result = tree\data!
77
expected = {
8+
left: nil
89
right: nil
910
data: "4"
10-
left: nil
1111
}
1212
assert.are.same expected, result
1313

@@ -16,75 +16,75 @@ describe 'binary-search-tree', ->
1616
tree = BinarySearchTree {'4', '2'}
1717
result = tree\data!
1818
expected = {
19-
right: nil
20-
data: "4"
2119
left: {
20+
left: nil
2221
right: nil
2322
data: "2"
24-
left: nil
2523
}
24+
right: nil
25+
data: "4"
2626
}
2727
assert.are.same expected, result
2828

2929
pending 'same number at left node', ->
3030
tree = BinarySearchTree {'4', '4'}
3131
result = tree\data!
3232
expected = {
33-
right: nil
34-
data: "4"
3533
left: {
34+
left: nil
3635
right: nil
3736
data: "4"
38-
left: nil
3937
}
38+
right: nil
39+
data: "4"
4040
}
4141
assert.are.same expected, result
4242

4343
pending 'greater number at right node', ->
4444
tree = BinarySearchTree {'4', '5'}
4545
result = tree\data!
4646
expected = {
47+
left: nil
4748
right: {
49+
left: nil
4850
right: nil
4951
data: "5"
50-
left: nil
5152
}
5253
data: "4"
53-
left: nil
5454
}
5555
assert.are.same expected, result
5656

5757
pending 'can create complex tree', ->
5858
tree = BinarySearchTree {'4', '2', '6', '1', '3', '5', '7'}
5959
result = tree\data!
6060
expected = {
61-
right: {
62-
right: {
63-
right: nil
64-
data: "7"
65-
left: nil
66-
}
67-
data: "6"
61+
left: {
6862
left: {
69-
right: nil
70-
data: "5"
7163
left: nil
64+
right: nil
65+
data: "1"
7266
}
73-
}
74-
data: "4"
75-
left: {
7667
right: {
68+
left: nil
7769
right: nil
7870
data: "3"
79-
left: nil
8071
}
8172
data: "2"
73+
}
74+
right: {
8275
left: {
76+
left: nil
8377
right: nil
84-
data: "1"
78+
data: "5"
79+
}
80+
right: {
8581
left: nil
82+
right: nil
83+
data: "7"
8684
}
85+
data: "6"
8786
}
87+
data: "4"
8888
}
8989
assert.are.same expected, result
9090

exercises/practice/book-store/.meta/spec_generator.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
int_list = (list) -> "{#{table.concat list, ', '}}"
1+
import int_list from require 'test_helpers'
22

33
{
44
module_name: 'BookStore',

exercises/practice/camicia/.meta/spec_generator.moon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
string_list = (list) -> "{#{table.concat [quote(v) for v in *list], ', '}}"
1+
import word_list from require 'test_helpers'
22

33
{
44
module_name: 'Camicia'
55

66
generate_test: (case, level) ->
77
lines = {
8-
"playerA = #{string_list case.input.playerA}"
9-
"playerB = #{string_list case.input.playerB}"
8+
"playerA = #{word_list case.input.playerA}"
9+
"playerB = #{word_list case.input.playerB}"
1010
"expected = {"
1111
" status: #{quote case.expected.status}"
1212
" cards: #{case.expected.cards}"

0 commit comments

Comments
 (0)