Class: Tokyo::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/tokyo/unit.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.context(label, &block) ⇒ Object

define a context inside current spec/context.

Parameters:

  • label
  • &block


75
76
77
78
# File 'lib/tokyo/unit.rb', line 75

def context label, &block
  return unless block
  Tokyo.define_and_register_a_context(label, block, self)
end

.hooksObject



162
163
164
# File 'lib/tokyo/unit.rb', line 162

def hooks
  @__tokyo_hooks__ ||= {}
end

.inherited(base) ⇒ Object



62
63
64
# File 'lib/tokyo/unit.rb', line 62

def inherited base
  base.instance_variable_set(:@__tokyo_hooks__, Marshal.load(Marshal.dump(hooks)))
end

.run(test) ⇒ Object



179
180
181
182
183
184
# File 'lib/tokyo/unit.rb', line 179

def run test
  tests[test] || raise(StandardError, 'Undefined test %s at "%s"' % [test.inspect, __identity__])
  catch :__tokyo_status__ do
    allocate.__run__(tests[test], hooks[:before], hooks[:around], hooks[:after])
  end
end

.skip(reason = nil) ⇒ Object

skipping a whole spec/context

Examples:

spec Array do
  skip

  # code here wont be executed
end


175
176
177
# File 'lib/tokyo/unit.rb', line 175

def skip reason = nil
  throw(:__tokyo_skip__, Tokyo::Skip.new(reason, caller[0]))
end

.specObject

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/tokyo/unit.rb', line 66

def spec
  raise(NotImplementedError, 'Nested specs not supported. Please use a context instead')
end

.test(label, &block) ⇒ Object Also known as: it, should

define a test

Parameters:

  • label
  • &block


85
86
87
88
89
# File 'lib/tokyo/unit.rb', line 85

def test label, &block
  return unless block
  tests[label] = Tokyo.identity_string(:test, label, block)
  define_method(tests[label], &block)
end

.testsObject



93
94
95
# File 'lib/tokyo/unit.rb', line 93

def tests
  @__tokyo_tests__ ||= {}
end

Instance Method Details

#__run__(test, before, around, after) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tokyo/unit.rb', line 35

def __run__ test, before, around, after
  __send__(before) if before
  if around
    __send__(around, proc {__send__(test)})
  else
    __send__(test)
  end
  __send__(after) if after
  __assertions__.each(&:__validate_expectations__)
  __objects__.each_key {|o| Tokyo::Expectation.restore_object_status(o[:returned])}
  :__tokyo_passed__
rescue Exception => e
  throw(:__tokyo_status__, e)
end

#skip(reason = nil) ⇒ Object

stop executing current test and mark it as skipped

Examples:

test :something do
  skip "recheck this after fixing X"
  assert(x) == y # this wont run
end


31
32
33
# File 'lib/tokyo/unit.rb', line 31

def skip reason = nil
  throw(:__tokyo_status__, Tokyo::Skip.new(reason, caller[0]))
end