Module: Kintama::Context::ClassMethods
- Defined in:
- lib/kintama/context.rb
Instance Method Summary
collapse
-
#[](name) ⇒ Object
-
#all_runnables ⇒ Object
-
#children ⇒ Object
-
#context(name = nil, parent = self, &block) ⇒ Object
(also: #testcase, #describe)
-
#failures ⇒ Object
Returns an array of tests in this and all subcontexts which failed in the previous run.
-
#find_definition(&block) ⇒ Object
-
#find_definition_1_8 ⇒ Object
-
#find_definition_1_9(&block) ⇒ Object
-
#find_definition_rbx(&block) ⇒ Object
-
#given(name, parent = self, &block) ⇒ Object
Create a new context starting with “given ”.
-
#inherited(child) ⇒ Object
-
#it(name, &block) ⇒ Object
Define a test to run in this context.
-
#method_missing(name, *args, &block) ⇒ Object
-
#on_finish(&block) ⇒ Object
(also: #after_all)
-
#on_finish_blocks ⇒ Object
-
#on_start(&block) ⇒ Object
(also: #before_all)
-
#on_start_blocks ⇒ Object
-
#passed? ⇒ Boolean
Returns true if this context has no known failed tests.
-
#pending ⇒ Object
-
#respond_to?(name) ⇒ Boolean
-
#run(reporter = nil) ⇒ Object
Runs all tests in this context and any subcontexts.
-
#run_tests(test_set, run_subcontexts, reporter) ⇒ Object
Run a specific set of tests using the given the reporter.
-
#runnable_on_line(line) ⇒ Object
-
#setup(&block) ⇒ Object
Define the setup for this context.
-
#setup_blocks ⇒ Object
-
#should(name_or_matcher, &block) ⇒ Object
Define a test to run in this context.
-
#should_not(matcher) ⇒ Object
Define a test using a negated matcher, e.g.
-
#subcontexts ⇒ Object
-
#subject(&block) ⇒ Object
Defines the subject of any matcher-based tests.
-
#teardown(&block) ⇒ Object
Define the teardown for this context.
-
#teardown_blocks ⇒ Object
-
#test(name, &block) ⇒ Object
Define a test to run in this context.
-
#tests ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/kintama/context.rb', line 202
def method_missing(name, *args, &block)
if self[de_methodize(name)]
self[de_methodize(name)]
else
begin
super
rescue NameError, NoMethodError => e
if parent
parent.send(name, *args, &block)
else
raise e
end
end
end
end
|
Instance Method Details
#[](name) ⇒ Object
198
199
200
|
# File 'lib/kintama/context.rb', line 198
def [](name)
subcontexts.find { |s| s.name == name } || tests.find { |t| t.name == name }
end
|
#all_runnables ⇒ Object
179
180
181
|
# File 'lib/kintama/context.rb', line 179
def all_runnables
tests + subcontexts + subcontexts.map { |s| s.all_runnables }.flatten
end
|
#children ⇒ Object
167
168
169
|
# File 'lib/kintama/context.rb', line 167
def children
@children ||= []
end
|
#context(name = nil, parent = self, &block) ⇒ Object
Also known as:
testcase, describe
Create a new context. If this is called within a context, a new subcontext will be created. Aliases are ‘testcase’ and ‘describe’
50
51
52
53
54
55
56
57
|
# File 'lib/kintama/context.rb', line 50
def context(name=nil, parent=self, &block)
c = Class.new(parent)
c.send(:include, Kintama::Context)
c.name = name.to_s if name
c.definition = find_definition(&block)
c.class_eval(&block) if block
c
end
|
#failures ⇒ Object
Returns an array of tests in this and all subcontexts which failed in the previous run
190
191
192
|
# File 'lib/kintama/context.rb', line 190
def failures
ran_tests.select { |t| !t.passed? } + subcontexts.map { |s| s.failures }.flatten
end
|
#find_definition(&block) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/kintama/context.rb', line 35
def find_definition(&block)
if defined? RUBY_ENGINE
case RUBY_ENGINE
when "ruby"
RUBY_VERSION =~ /^1.9/ ? find_definition_1_9(&block) : find_definition_1_8
when "rbx"
find_definition_rbx(&block)
end
else
find_definition_1_8
end
end
|
#find_definition_1_8 ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/kintama/context.rb', line 15
def find_definition_1_8
line = caller.find { |line| line =~ /^[^:]+:(\d+)$/ }
if line
parts = line.split(":")
parts[1] = parts[1].to_i
parts
end
end
|
#find_definition_1_9(&block) ⇒ Object
24
25
26
|
# File 'lib/kintama/context.rb', line 24
def find_definition_1_9(&block)
block.source_location if block
end
|
#find_definition_rbx(&block) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/kintama/context.rb', line 28
def find_definition_rbx(&block)
if block
m = block.block.code
[m.file, m.first_line]
end
end
|
#given(name, parent = self, &block) ⇒ Object
Create a new context starting with “given ”
62
63
64
|
# File 'lib/kintama/context.rb', line 62
def given(name, parent=self, &block)
context("given " + name, parent, &block)
end
|
#inherited(child) ⇒ Object
163
164
165
|
# File 'lib/kintama/context.rb', line 163
def inherited(child)
children << child
end
|
#it(name, &block) ⇒ Object
Define a test to run in this context. The test name will start with “it ”
159
160
161
|
# File 'lib/kintama/context.rb', line 159
def it(name, &block)
test("it " + name, &block)
end
|
#on_finish(&block) ⇒ Object
Also known as:
after_all
113
114
115
|
# File 'lib/kintama/context.rb', line 113
def on_finish(&block)
self.on_finish_blocks << block
end
|
#on_finish_blocks ⇒ Object
109
110
111
|
# File 'lib/kintama/context.rb', line 109
def on_finish_blocks
@on_finish_blocks ||= []
end
|
#on_start(&block) ⇒ Object
Also known as:
before_all
104
105
106
|
# File 'lib/kintama/context.rb', line 104
def on_start(&block)
self.on_start_blocks << block
end
|
#on_start_blocks ⇒ Object
100
101
102
|
# File 'lib/kintama/context.rb', line 100
def on_start_blocks
@on_start_blocks ||= []
end
|
#passed? ⇒ Boolean
Returns true if this context has no known failed tests.
184
185
186
|
# File 'lib/kintama/context.rb', line 184
def passed?
failures.empty?
end
|
#pending ⇒ Object
194
195
196
|
# File 'lib/kintama/context.rb', line 194
def pending
ran_tests.select { |t| t.pending? } + subcontexts.map { |s| s.pending }.flatten
end
|
#respond_to?(name) ⇒ Boolean
218
219
220
221
222
|
# File 'lib/kintama/context.rb', line 218
def respond_to?(name)
self[name] ||
super ||
(parent ? parent.respond_to?(name) : false)
end
|
#run(reporter = nil) ⇒ Object
Runs all tests in this context and any subcontexts. Returns true if all tests passed; otherwise false
226
227
228
|
# File 'lib/kintama/context.rb', line 226
def run(reporter=nil)
run_tests(tests, true, reporter)
end
|
#run_tests(test_set, run_subcontexts, reporter) ⇒ Object
Run a specific set of tests using the given the reporter
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/kintama/context.rb', line 231
def run_tests(test_set, run_subcontexts, reporter)
@ran_tests = []
reporter.context_started(self) if reporter
on_start_blocks.each { |b| instance_eval(&b) }
test_set.each { |t| instance = t.new; instance.run(reporter); ran_tests << instance }
subcontexts.each { |s| s.run(reporter) } if run_subcontexts
on_finish_blocks.each { |b| instance_eval(&b) }
reporter.context_finished(self) if reporter
passed?
end
|
#runnable_on_line(line) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/kintama/context.rb', line 242
def runnable_on_line(line)
known_runnables = all_runnables.delete_if { |r| r.line_defined.nil? }
sorted_runnables = known_runnables.sort_by { |r| r.line_defined }
if sorted_runnables.first && line >= sorted_runnables.first.line_defined
next_runnable = sorted_runnables.find { |r| r.line_defined > line }
index = sorted_runnables.index(next_runnable)
if index != nil && index > 0
sorted_runnables[index-1]
else
sorted_runnables.last
end
else
nil
end
end
|
#setup(&block) ⇒ Object
Define the setup for this context. It will also be run for any subcontexts, before their own setup blocks
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/kintama/context.rb', line 76
def setup(&block)
self.setup_blocks << block
blocks = self.setup_blocks
define_method(:setup) do
super()
blocks.each { |b| instance_eval(&b) }
end
end
|
#setup_blocks ⇒ Object
66
67
68
|
# File 'lib/kintama/context.rb', line 66
def setup_blocks
@setup_blocks ||= []
end
|
#should(name_or_matcher, &block) ⇒ Object
Define a test to run in this context. The test name will start with “should ” You can either supply a name and block, or a matcher. In the latter case, a test will be generated using that matcher.
137
138
139
140
141
142
143
144
145
|
# File 'lib/kintama/context.rb', line 137
def should(name_or_matcher, &block)
if name_or_matcher.respond_to?(:matches?)
test("should " + name_or_matcher.description) do
assert name_or_matcher.matches?(subject), name_or_matcher.failure_message
end
else
test("should " + name_or_matcher, &block)
end
end
|
#should_not(matcher) ⇒ Object
Define a test using a negated matcher, e.g.
subject { 'a' }
should_not equal('b')
152
153
154
155
156
|
# File 'lib/kintama/context.rb', line 152
def should_not(matcher)
test("should not " + matcher.description) do
assert !matcher.matches?(subject), matcher.negative_failure_message
end
end
|
#subcontexts ⇒ Object
175
176
177
|
# File 'lib/kintama/context.rb', line 175
def subcontexts
children.select { |c| c.is_a_context? }.sort_by { |s| s.name }
end
|
#subject(&block) ⇒ Object
Defines the subject of any matcher-based tests.
119
120
121
122
123
|
# File 'lib/kintama/context.rb', line 119
def subject(&block)
define_method(:subject) do
@__subject ||= yield
end
end
|
#teardown(&block) ⇒ Object
Define the teardown for this context. It will also be run for any subcontexts, after their own teardown blocks
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/kintama/context.rb', line 89
def teardown(&block)
self.teardown_blocks << block
blocks = self.teardown_blocks
define_method(:teardown) do
blocks.each { |b| instance_eval(&b) }
super()
end
end
|
#teardown_blocks ⇒ Object
70
71
72
|
# File 'lib/kintama/context.rb', line 70
def teardown_blocks
@teardown_blocks ||= []
end
|
#test(name, &block) ⇒ Object
Define a test to run in this context.
126
127
128
129
130
131
132
|
# File 'lib/kintama/context.rb', line 126
def test(name, &block)
c = Class.new(self)
c.send(:include, Kintama::Test)
c.name = name
c.definition = find_definition(&block)
c.block = block if block_given?
end
|
#tests ⇒ Object
171
172
173
|
# File 'lib/kintama/context.rb', line 171
def tests
children.select { |c| c.is_a_test? }.sort_by { |t| t.name }
end
|