-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtesting.rb
196 lines (160 loc) · 4.29 KB
/
testing.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
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
187
188
189
190
191
192
193
194
195
196
require 'minitest/autorun'
testdir = __dir__
rootdir = File.dirname(testdir)
libdir = File.join(rootdir, 'lib')
STDOUT.sync = true
$LOAD_PATH.unshift(testdir) unless $LOAD_PATH.include?(testdir)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
$LOAD_PATH.unshift(rootdir) unless $LOAD_PATH.include?(rootdir)
class Testing
class Slug < ::String
def self.for(*args)
string = args.flatten.compact.join('-')
words = string.to_s.scan(/\w+/)
words.map! { |word| word.gsub(/[^0-9a-zA-Z_-]/, '') }
words.delete_if { |word| word.nil? || word.strip.empty? }
new(words.join('-').downcase)
end
end
class Context
attr_accessor :name
def initialize(name, *_args)
@name = name
end
def to_s
Slug.for(name)
end
end
end
def _testing(*args, &block)
Class.new(::Minitest::Test) do
i_suck_and_my_tests_are_order_dependent!
## class methods
#
class << self
def contexts
@contexts ||= []
end
def context(*args, &block)
return contexts.last if args.empty? && block.nil?
context = Testing::Context.new(*args)
contexts.push(context)
begin
yield(context)
ensure
contexts.pop
end
end
def slug_for(*args)
string = [context, args].flatten.compact.join('-')
words = string.to_s.scan(/\w+/)
words.map! { |word| word.gsub(/[^0-9a-zA-Z_-]/, '') }
words.delete_if { |word| word.nil? || word.strip.empty? }
words.join('-').downcase.sub(/_$/, '')
end
def name
const_get(:Name)
end
def testno
format('%05d', (@testno ||= 0))
ensure
@testno += 1
end
def testing(*args, &block)
method = ['test', testno, slug_for(*args)].delete_if(&:empty?).join('_')
define_method(method, &block)
end
def test(*args, &block)
testing(*args, &block)
end
def setup(&block)
define_method(:setup, &block) if block
end
def teardown(&block)
define_method(:teardown, &block) if block
end
def prepare(&block)
@prepare ||= []
@prepare.push(block) if block
@prepare
end
def cleanup(&block)
@cleanup ||= []
@cleanup.push(block) if block
@cleanup
end
end
## configure the subclass!
#
const_set(:Testno, '0')
slug = slug_for(*args).tr('-', '_')
name = ['TESTING', format('%03d', const_get(:Testno)), slug].delete_if(&:empty?).join('_')
name = name.upcase!
const_set(:Name, name)
const_set(:Missing, Object.new.freeze)
## instance methods
#
alias_method('__assert__', 'assert')
def assert(*args, &block)
if (args.size == 1) && args.first.is_a?(Hash)
options = args.first
expected = getopt(:expected, options) { missing }
actual = getopt(:actual, options) { missing }
actual, expected, = options.to_a.flatten if (expected == missing) && (actual == missing)
expected = expected.call if expected.respond_to?(:call)
actual = actual.call if actual.respond_to?(:call)
assert_equal(expected, actual)
end
result = if block
label = "assert(#{args.join(' ')})"
yield
else
result = args.shift
label = "assert(#{args.join(' ')})"
result
end
__assert__(result, label)
result
end
def missing
self.class.const_get(:Missing)
end
def getopt(opt, hash, options = nil, &block)
[opt.to_s, opt.to_s.to_sym].each do |key|
return hash[key] if hash.key?(key)
end
default =
if block
yield
else
options.is_a?(Hash) ? options[:default] : nil
end
default
end
def subclass_of(exception)
class << exception
def ==(other)
super || self > other
end
end
exception
end
##
#
module_eval(&block)
setup
prepare.each(&:call)
at_exit do
teardown
cleanup.each(&:call)
end
self
end
end
if $PROGRAM_NAME == __FILE__
_testing 'Testing' do
testing('foo') { assert true }
test { assert true }
p instance_methods.grep(/test/)
end
end