/
dsl.cr
186 lines (152 loc) · 3.81 KB
/
dsl.cr
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require "colorize"
require "option_parser"
module Spec
private COLORS = {
success: :green,
fail: :red,
error: :red,
pending: :yellow,
comment: :cyan,
}
private LETTERS = {
success: '.',
fail: 'F',
error: 'E',
pending: '*',
}
@@use_colors = true
# :nodoc:
def self.color(str, status)
if use_colors?
str.colorize(COLORS[status])
else
str
end
end
# :nodoc:
def self.use_colors?
@@use_colors
end
# :nodoc:
def self.use_colors=(@@use_colors)
end
# :nodoc:
class SpecError < Exception
getter file : String
getter line : Int32
def initialize(message, @file, @line)
super(message)
end
end
# :nodoc:
class AssertionFailed < SpecError
end
# :nodoc:
class NestingSpecError < SpecError
end
@@aborted = false
# :nodoc:
def self.abort!
@@aborted = true
exit
end
# :nodoc:
def self.pattern=(pattern)
@@pattern = Regex.new(Regex.escape(pattern))
end
# :nodoc:
def self.line=(@@line : Int32)
end
# :nodoc:
def self.slowest=(@@slowest : Int32)
end
# :nodoc:
def self.slowest
@@slowest
end
# :nodoc:
def self.to_human(span : Time::Span)
total_milliseconds = span.total_milliseconds
if total_milliseconds < 1
return "#{(span.total_milliseconds * 1000).round.to_i} microseconds"
end
total_seconds = span.total_seconds
if total_seconds < 1
return "#{span.total_milliseconds.round(2)} milliseconds"
end
if total_seconds < 60
return "#{total_seconds.round(2)} seconds"
end
minutes = span.minutes
seconds = span.seconds
"#{minutes}:#{seconds < 10 ? "0" : ""}#{seconds} minutes"
end
# :nodoc:
def self.add_location(file, line)
locations = @@locations ||= {} of String => Array(Int32)
lines = locations[File.expand_path(file)] ||= [] of Int32
lines << line
end
# :nodoc:
def self.matches?(description, file, line, end_line = line)
spec_pattern = @@pattern
spec_line = @@line
locations = @@locations
# When a method invokes `it` and only forwards line information,
# not end_line information (this can happen in code before we
# introduced the end_line feature) then running a spec by giving
# a line won't work because end_line might be located before line.
# So, we also check `line == spec_line` to somehow preserve
# backwards compatibility.
if spec_line && (line == spec_line || line <= spec_line <= end_line)
return true
end
if locations
lines = locations[file]?
return true if lines && lines.any? { |l| line == l || line <= l <= end_line }
end
if spec_pattern || spec_line || locations
Spec::RootContext.matches?(description, spec_pattern, spec_line, locations)
else
true
end
end
@@fail_fast = false
# :nodoc:
def self.fail_fast=(@@fail_fast)
end
# :nodoc:
def self.fail_fast?
@@fail_fast
end
# Instructs the spec runner to execute the given block
# before each spec, regardless of where this method is invoked.
def self.before_each(&block)
before_each = @@before_each ||= [] of ->
before_each << block
end
# Instructs the spec runner to execute the given block
# after each spec, regardless of where this method is invoked.
def self.after_each(&block)
after_each = @@after_each ||= [] of ->
after_each << block
end
# :nodoc:
def self.run_before_each_hooks
@@before_each.try &.each &.call
end
# :nodoc:
def self.run_after_each_hooks
@@after_each.try &.each &.call
end
# :nodoc:
def self.run
start_time = Time.monotonic
at_exit do
elapsed_time = Time.monotonic - start_time
Spec::RootContext.finish(elapsed_time, @@aborted)
exit 1 unless Spec::RootContext.succeeded && !@@aborted
end
end
end
require "./*"