-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathassertion.rb
55 lines (53 loc) · 1.35 KB
/
assertion.rb
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
module Generator
class ExerciseCase
module Assertion
# generates assertions of the form
#
# assert whatever
# refute whatever
#
# depending on whether 'expected' is true or false
#
# Example:
#
# assert_or_refute(expected, "Luhn.valid?(#{input.inspect})")
#
def assert_or_refute(expected, actual)
assertion = expected ? 'assert' : 'refute'
"#{assertion} #{actual}\n"
end
# generates assertions of the form
#
# assert_nil whatever
# assert_equal expected, whatever
#
# depending on whether 'expected' is nil or not
#
# Example:
#
# assert_equal(expected, "PigLatin.translate(#{input.inspect})")
#
def assert_equal(expected, actual)
assertion = expected.nil? ? 'assert_nil' : "assert_equal #{expected.inspect},"
"#{assertion} #{actual}\n"
end
# generates assertions of the form
#
# assert_raises(SomeError) do
# whatever
# end
#
# Example
#
# assert_raises(ArgumentError, 'Say.new(number).in_english')
#
def assert_raises(error, actual)
[
"assert_raises(#{error}) do\n",
"#{actual}\n".gsub(/^/, ' '), # indent by 2
"end\n"
].join
end
end
end
end