Class: Assert::Context

Inherits:
Object
  • Object
show all
Includes:
Assertions, Macros::Methods
Defined in:
lib/assert/context.rb

Constant Summary

Constants included from Assertions

Assertions::IGNORED_ASSERTION_HELPERS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Macros::Methods

included

Methods included from Assertions

#assert_block, #assert_empty, #assert_equal, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_not_block, #assert_not_empty, #assert_not_equal, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_match, #assert_not_nil, #assert_not_respond_to, #assert_not_same, #assert_nothing_raised, #assert_raises, #assert_respond_to, #assert_same, #method_missing

Constructor Details

#initialize(running_test = nil) ⇒ Context

Returns a new instance of Context.



147
148
149
# File 'lib/assert/context.rb', line 147

def initialize(running_test = nil)
  @__running_test__ = running_test
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Assert::Assertions

Class Method Details

.description(text = nil) ⇒ Object Also known as: desc

Add a piece of description text or return the full description for the context



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/assert/context.rb', line 68

def description(text=nil)
  if text
    self.descriptions << text.to_s
  else
    parent = self.superclass.desc if self.superclass.respond_to?(:desc)
    own = self.descriptions
    [parent, *own].compact.reject do |p|
      p.to_s.empty?
    end.join(" ")
  end
end

.inherited(klass) ⇒ Object

if a class subclasses Context, add it to the suite



18
19
20
# File 'lib/assert/context.rb', line 18

def self.inherited(klass)
  Assert.suite << klass
end

.setup(scope_or_method_name = nil, &block) ⇒ Object Also known as: before

Add a setup block to run before each test or run the list of teardown blocks in given scope



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

def setup(scope_or_method_name = nil, &block)
  is_method = scope_or_method_name.kind_of?(String) || scope_or_method_name.kind_of?(Symbol)
  if block_given? || is_method
    self.setups << (block || scope_or_method_name)
  elsif !is_method
    scope = scope_or_method_name
    # setup parent before child
    self.superclass.setup(scope) if self.superclass.respond_to?(:setup)
    self.setups.each do |setup|
      setup.kind_of?(::Proc) ? scope.instance_eval(&setup) : scope.send(setup)
    end
  end
end

.setup_once(&block) ⇒ Object Also known as: before_once



25
26
27
# File 'lib/assert/context.rb', line 25

def setup_once(&block)
  Assert.suite.setup(&block)
end

.should(desc_or_macro, &block) ⇒ Object



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

def should(desc_or_macro, &block)
  if !desc_or_macro.kind_of?(Macro)
    desc_or_macro = "should #{desc_or_macro}"
  end
  test(desc_or_macro, &block)
end

.should_eventually(desc, &block) ⇒ Object Also known as: should_skip



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

def should_eventually(desc, &block)
  should(desc)
end

.subject(&block) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/assert/context.rb', line 81

def subject(&block)
  if block_given?
    @subject = block
  else
    @subject || if superclass.respond_to?(:subject)
      superclass.subject
    end
  end
end

.teardown(scope_or_method_name = nil, &block) ⇒ Object Also known as: after

Add a teardown block to run after each test or run the list of teardown blocks in given scope



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/assert/context.rb', line 52

def teardown(scope_or_method_name = nil, &block)
  is_method = scope_or_method_name.kind_of?(String) || scope_or_method_name.kind_of?(Symbol)
  if block_given? || is_method
    self.teardowns << (block || scope_or_method_name)
  elsif !is_method
    scope = scope_or_method_name
    # teardown child before parent
    self.teardowns.each do |teardown|
      teardown.kind_of?(::Proc) ? scope.instance_eval(&teardown) : scope.send(teardown)
    end
    self.superclass.teardown(scope) if self.superclass.respond_to?(:teardown)
  end
end

.teardown_once(&block) ⇒ Object Also known as: after_once



30
31
32
# File 'lib/assert/context.rb', line 30

def teardown_once(&block)
  Assert.suite.teardown(&block)
end

.test(desc_or_macro, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/assert/context.rb', line 91

def test(desc_or_macro, &block)
  if desc_or_macro.kind_of?(Macro)
    instance_eval(&desc_or_macro)
  else
    method_name = "test: #{desc_or_macro}"

    # if no block given, create a test that just skips
    method_block = block_given? ? block : (Proc.new { skip })

    # instead of using the typical 'method_defined?' pattern (which) checks
    # all parent contexts, we really just need to make sure the method_name
    # is not part of self's local_pulic_test_methods for this check
    if Assert.suite.send(:local_public_test_methods, self).include?(method_name)
      from = caller.first
      puts "WARNING: should #{desc_or_macro.inspect} is redefining #{method_name}!"
      puts "  from: #{from}"
      puts "  self: #{self.inspect}"
    end

    define_method(method_name, &method_block)
  end
end

.test_eventually(desc, &block) ⇒ Object Also known as: test_skip



114
115
116
# File 'lib/assert/context.rb', line 114

def test_eventually(desc, &block)
  test(desc)
end

Instance Method Details

#assert(assertion, fail_desc = nil, what_failed_msg = nil) ⇒ Object

check if the assertion is a truthy value, if so create a new pass result, otherwise create a new fail result with the desc and what failed msg. all other assertion helpers use this one in the end



154
155
156
157
158
# File 'lib/assert/context.rb', line 154

def assert(assertion, fail_desc=nil, what_failed_msg=nil)
  what_failed_msg ||= "Failed assert: assertion was <#{assertion.inspect}>."
  msg = fail_message(fail_desc) { what_failed_msg }
  assertion ? pass : fail(msg)
end

#assert_not(assertion, fail_desc = nil) ⇒ Object Also known as: refute

the opposite of assert, check if the assertion is a false value, if so create a new pass result, otherwise create a new fail result with the desc and it’s what failed msg



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

def assert_not(assertion, fail_desc=nil)
  what_failed_msg = "Failed assert_not: assertion was <#{assertion.inspect}>."
  assert(!assertion, fail_desc, what_failed_msg)
end

#fail(fail_msg = nil) ⇒ Object Also known as: flunk

adds a Fail result to the end of the test’s results does not break test execution



183
184
185
186
187
188
# File 'lib/assert/context.rb', line 183

def fail(fail_msg=nil)
  capture_result do |test_name, backtrace|
    message = (fail_message(fail_msg) { }).call
    Assert::Result::Fail.new(test_name, message, backtrace)
  end
end

#ignore(ignore_msg = nil) ⇒ Object

adds an Ignore result to the end of the test’s results does not break test execution



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

def ignore(ignore_msg=nil)
  capture_result do |test_name, backtrace|
    Assert::Result::Ignore.new(test_name, ignore_msg, backtrace)
  end
end

#inspectObject



205
206
207
# File 'lib/assert/context.rb', line 205

def inspect
  "#<#{self.class}>"
end

#pass(pass_msg = nil) ⇒ Object

adds a Pass result to the end of the test’s results does not break test execution



175
176
177
178
179
# File 'lib/assert/context.rb', line 175

def pass(pass_msg=nil)
  capture_result do |test_name, backtrace|
    Assert::Result::Pass.new(test_name, pass_msg, backtrace)
  end
end

#skip(skip_msg = nil) ⇒ Object

adds a Skip result to the end of the test’s results and breaks test execution



169
170
171
# File 'lib/assert/context.rb', line 169

def skip(skip_msg=nil)
  raise(Result::TestSkipped, skip_msg || "")
end

#subjectObject



199
200
201
202
203
# File 'lib/assert/context.rb', line 199

def subject
  if subj = self.class.subject
    instance_eval(&subj)
  end
end