-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
exercise_case.rb
76 lines (61 loc) · 1.63 KB
/
exercise_case.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'ostruct'
require 'generator/exercise_case/assertion'
require 'generator/exercise_case/case_helpers'
module Generator
class ExerciseCase
include CaseHelpers
include Assertion
attr_reader :canonical
def initialize(canonical:)
@canonical = canonical
end
def to_s(comment_out_skip: false)
body = [
"#{skip(comment_out_skip)}\n",
format_workload(workload)
].join
indent_by(2, test_method(body))
end
def test_name
"test_#{clean_description}"
end
def skip(comment_out)
comment_out ? '# skip' : 'skip'
end
def workload
raise StandardError, "You need to subclass and implement the 'workload' method"
end
def method_missing(sym, *args, &block)
return canonical.send(sym) if canonical.respond_to?(sym)
super(sym, *args, &block)
end
def respond_to_missing?(sym, include_private = false)
canonical.respond_to?(sym) || super
end
def error_expected?
canonical.respond_to?(:expected_error)
end
def format_workload(workload)
case workload
when String
"#{workload.chomp}\n"
when Array
workload.map { |line| "#{line.chomp}\n" }.join
end
end
private
def test_method(body)
[
"def #{test_name}\n",
indent_by(2, body),
"end\n"
].join
end
def clean_description
description = self.description.downcase.strip
description.gsub!(/\W/, '_') # no non-word characters
description.gsub!(/__*/, '_') # no multiple underscores
description.sub!(/_*$/, '') # no trailing underscores
end
end
end