Module: Kintama::Context::ClassMethods

Defined in:
lib/kintama/context.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



239
240
241
# File 'lib/kintama/context.rb', line 239

def [](name)
  subcontexts.find { |s| s.name == name } || tests.find { |t| t.name == name }
end

#after_setup(&block) ⇒ Object Also known as: action



121
122
123
# File 'lib/kintama/context.rb', line 121

def after_setup(&block)
  self.after_setup_blocks << block
end

#after_setup_blocksObject



107
108
109
# File 'lib/kintama/context.rb', line 107

def after_setup_blocks
  @after_setup_blocks ||= []
end

#all_runnablesObject



220
221
222
# File 'lib/kintama/context.rb', line 220

def all_runnables
  tests + subcontexts + subcontexts.map { |s| s.all_runnables }.flatten
end

#childrenObject



208
209
210
# File 'lib/kintama/context.rb', line 208

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’



87
88
89
90
91
92
93
94
# File 'lib/kintama/context.rb', line 87

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

#failuresObject

Returns an array of tests in this and all subcontexts which failed in the previous run



231
232
233
# File 'lib/kintama/context.rb', line 231

def failures
  ran_tests.select { |t| !t.passed? } + subcontexts.map { |s| s.failures }.flatten
end

#find_definition(&block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kintama/context.rb', line 72

def find_definition(&block)
  if defined? RUBY_ENGINE
    case RUBY_ENGINE
    when "ruby", "jruby"
      Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9') ? find_definition_yarv(&block) : find_definition_1_8
    when "rbx"
      find_definition_rbx(&block)
    end
  else
    find_definition_1_8
  end
end

#find_definition_1_8Object



52
53
54
55
56
57
58
59
# File 'lib/kintama/context.rb', line 52

def find_definition_1_8
  line = caller.find { |l| l =~ /^[^:]+:(\d+)$/ }
  if line
    parts = line.split(":")
    parts[1] = parts[1].to_i
    parts
  end
end

#find_definition_rbx(&block) ⇒ Object



65
66
67
68
69
70
# File 'lib/kintama/context.rb', line 65

def find_definition_rbx(&block)
  if block
    block_environment = block.block
    [block_environment.file, block_environment.line]
  end
end

#find_definition_yarv(&block) ⇒ Object



61
62
63
# File 'lib/kintama/context.rb', line 61

def find_definition_yarv(&block)
  block.source_location if block
end

#given(name, parent = self, &block) ⇒ Object

Create a new context starting with “given ”



99
100
101
# File 'lib/kintama/context.rb', line 99

def given(name, parent=self, &block)
  context("given " + name, parent, &block)
end

#inherited(child) ⇒ Object



204
205
206
# File 'lib/kintama/context.rb', line 204

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 ”



200
201
202
# File 'lib/kintama/context.rb', line 200

def it(name, &block)
  test("it " + name, &block)
end

#let(name, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/kintama/context.rb', line 150

def let(name, &block)
  define_method(name) do
    memo = "@__#{name}"
    if instance_variable_defined?(memo)
      instance_variable_get(memo)
    else
      instance_variable_set(memo, instance_eval(&block))
    end
  end
end

#on_finish(&block) ⇒ Object Also known as: after_all



145
146
147
# File 'lib/kintama/context.rb', line 145

def on_finish(&block)
  self.on_finish_blocks << block
end

#on_finish_blocksObject



141
142
143
# File 'lib/kintama/context.rb', line 141

def on_finish_blocks
  @on_finish_blocks ||= []
end

#on_start(&block) ⇒ Object Also known as: before_all



136
137
138
# File 'lib/kintama/context.rb', line 136

def on_start(&block)
  self.on_start_blocks << block
end

#on_start_blocksObject



132
133
134
# File 'lib/kintama/context.rb', line 132

def on_start_blocks
  @on_start_blocks ||= []
end

#passed?Boolean

Returns true if this context has no known failed tests.

Returns:

  • (Boolean)


225
226
227
# File 'lib/kintama/context.rb', line 225

def passed?
  failures.empty?
end

#pendingObject



235
236
237
# File 'lib/kintama/context.rb', line 235

def pending
  ran_tests.select { |t| t.pending? } + subcontexts.map { |s| s.pending }.flatten
end

#run(reporter = nil) ⇒ Object

Runs all tests in this context and any subcontexts. Returns true if all tests passed; otherwise false



245
246
247
# File 'lib/kintama/context.rb', line 245

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



250
251
252
253
254
255
256
257
258
259
# File 'lib/kintama/context.rb', line 250

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/kintama/context.rb', line 261

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



117
118
119
# File 'lib/kintama/context.rb', line 117

def setup(&block)
  self.setup_blocks << block
end

#setup_blocksObject



103
104
105
# File 'lib/kintama/context.rb', line 103

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.



178
179
180
181
182
183
184
185
186
# File 'lib/kintama/context.rb', line 178

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')


193
194
195
196
197
# File 'lib/kintama/context.rb', line 193

def should_not(matcher)
  test("should not " + matcher.description) do
    assert !matcher.matches?(subject), matcher.negative_failure_message
  end
end

#subcontextsObject



216
217
218
# File 'lib/kintama/context.rb', line 216

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.



162
163
164
# File 'lib/kintama/context.rb', line 162

def subject(&block)
  let("subject", &block)
end

#teardown(&block) ⇒ Object

Define the teardown for this context. It will also be run for any subcontexts, after their own teardown blocks



128
129
130
# File 'lib/kintama/context.rb', line 128

def teardown(&block)
  self.teardown_blocks << block
end

#teardown_blocksObject



111
112
113
# File 'lib/kintama/context.rb', line 111

def teardown_blocks
  @teardown_blocks ||= []
end

#test(name, &block) ⇒ Object

Define a test to run in this context.



167
168
169
170
171
172
173
# File 'lib/kintama/context.rb', line 167

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

#testsObject



212
213
214
# File 'lib/kintama/context.rb', line 212

def tests
  children.select { |c| c.is_a_test? }.sort_by { |t| t.name }
end