Module: Kintama::Context::ClassMethods

Defined in:
lib/kintama/context.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/kintama/context.rb', line 169

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



165
166
167
# File 'lib/kintama/context.rb', line 165

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

#all_runnablesObject



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

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

#childrenObject



134
135
136
# File 'lib/kintama/context.rb', line 134

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’



17
18
19
20
21
22
23
24
# File 'lib/kintama/context.rb', line 17

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 = caller.find { |line| line =~ /^#{block.__file__}:(\d+)$/ }
  c.class_eval(&block)
  c
end

#failuresObject

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



157
158
159
# File 'lib/kintama/context.rb', line 157

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

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

Create a new context starting with “given ”



29
30
31
# File 'lib/kintama/context.rb', line 29

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

#inherited(child) ⇒ Object



130
131
132
# File 'lib/kintama/context.rb', line 130

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 ”



126
127
128
# File 'lib/kintama/context.rb', line 126

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

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



80
81
82
# File 'lib/kintama/context.rb', line 80

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

#on_finish_blocksObject



76
77
78
# File 'lib/kintama/context.rb', line 76

def on_finish_blocks
  @on_finish_blocks ||= []
end

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



71
72
73
# File 'lib/kintama/context.rb', line 71

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

#on_start_blocksObject



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

def on_start_blocks
  @on_start_blocks ||= []
end

#passed?Boolean

Returns true if this context has no known failed tests.

Returns:

  • (Boolean)


151
152
153
# File 'lib/kintama/context.rb', line 151

def passed?
  failures.empty?
end

#pendingObject



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

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
# File 'lib/kintama/context.rb', line 185

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



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

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



198
199
200
201
202
203
204
205
206
207
# File 'lib/kintama/context.rb', line 198

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kintama/context.rb', line 209

def runnable_on_line(line)
  sorted_runnables = all_runnables.delete_if { |r| r.line_defined.nil? }.sort_by { |r| r.line_defined }
  if 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



43
44
45
46
47
48
49
50
51
52
# File 'lib/kintama/context.rb', line 43

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

  # redefine setup for the current set of blocks
  blocks = self.setup_blocks
  define_method(:setup) do
    super()
    blocks.each { |b| instance_eval(&b) }
  end
end

#setup_blocksObject



33
34
35
# File 'lib/kintama/context.rb', line 33

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.



104
105
106
107
108
109
110
111
112
# File 'lib/kintama/context.rb', line 104

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


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

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

#subcontextsObject



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

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.



86
87
88
89
90
# File 'lib/kintama/context.rb', line 86

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/kintama/context.rb', line 56

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

  # redefine teardown for the current set of blocks
  blocks = self.teardown_blocks
  define_method(:teardown) do
    blocks.each { |b| instance_eval(&b) }
    super()
  end
end

#teardown_blocksObject



37
38
39
# File 'lib/kintama/context.rb', line 37

def teardown_blocks
  @teardown_blocks ||= []
end

#test(name, &block) ⇒ Object

Define a test to run in this context.



93
94
95
96
97
98
99
# File 'lib/kintama/context.rb', line 93

def test(name, &block)
  c = Class.new(self)
  c.send(:include, Kintama::Test)
  c.name = name
  c.definition = caller.find { |line| line =~ /^[^:]+:(\d+)$/ }
  c.block = block if block_given?
end

#testsObject



138
139
140
# File 'lib/kintama/context.rb', line 138

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