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
|
# File 'lib/testable_examples/runner.rb', line 17
def run_examples
$: << @include_path if @include_path
Array(@requires).each {|r| require r} if @requires
in_example = false
examples = []
STDOUT.sync = true
current_example = ''
matchers = []
rb_files = File.exist?(@base) ? [@base] : Dir[File.join(@base, 'lib/**/*.rb')]
puts "Scanning #{rb_files * ', '}"
rb_files.each do |file|
lines = File.read(file).split(/\n/)
lines.each do |line|
if line[/^\s*#(.*)/] line = $1.strip
case line
when /^example:/i then in_example = true
when /^(?:# )?=+> (.*)/
if in_example
expected = $1.strip
matchers << Matcher.new(expected)
if matchers.last.match_exception?
current_example << " rescue e = $!; nil"
end
current_example << "\nmatchers[#{matchers.size - 1}].match(__example_runner, $!)"
end
when ''
unless current_example.empty?
examples << current_example
current_example = ''
end
in_example = false
else
current_example << "\n__example_runner = (" << line << ")" if in_example
end
else
unless current_example.empty?
examples << current_example
current_example = ''
end
in_example = false
end
end
end
print "Running #{examples.size} example#{'s' if examples.size != 1} "
examples.each do |example|
print "."
eval(example)
end
puts " ✔"
end
|