Class: Assert::Context

Inherits:
Object
  • Object
show all
Extended by:
LetDSL, SetupDSL, SubjectDSL, SuiteDSL, TestDSL
Includes:
Assertions, MethodMissing, Macros::Methods
Defined in:
lib/assert.rb,
lib/assert/context.rb,
lib/assert/context/let_dsl.rb,
lib/assert/context/test_dsl.rb,
lib/assert/context/setup_dsl.rb,
lib/assert/context/suite_dsl.rb,
lib/assert/context/subject_dsl.rb,
lib/assert/context/method_missing.rb

Overview

A Context is a scope for tests to run in. Contexts have setup and teardown blocks, subjects, and descriptions. Tests are run in the scope of a Context instance. Therefore, a Context should have minimal base logic/methods/instance_vars. The instance should remain pure to not pollute test scopes.

Defined Under Namespace

Modules: LetDSL, MethodMissing, SetupDSL, SubjectDSL, SuiteDSL, TestDSL

Constant Summary

Constants included from Assertions

Assertions::IGNORED_ASSERTION_HELPERS

Instance Method Summary collapse

Methods included from SetupDSL

around, arounds, run_arounds, run_setups, run_teardowns, setup, setup_once, setups, teardown, teardown_once, teardowns

Methods included from SubjectDSL

description

Methods included from SuiteDSL

suite

Methods included from TestDSL

should, should_eventually, test, test_eventually

Methods included from LetDSL

let

Methods included from Macros::Methods

included

Methods included from Assertions

#assert_block, #assert_changes, #assert_empty, #assert_equal, #assert_false, #assert_file_exists, #assert_includes, #assert_instance_of, #assert_is_a, #assert_is_not_a, #assert_match, #assert_nil, #assert_not_block, #assert_not_changes, #assert_not_empty, #assert_not_equal, #assert_not_false, #assert_not_file_exists, #assert_not_includes, #assert_not_instance_of, #assert_not_match, #assert_not_nil, #assert_not_respond_to, #assert_not_same, #assert_not_true, #assert_nothing_raised, #assert_raises, #assert_respond_to, #assert_same, #assert_true

Methods included from MethodMissing

#method_missing, #respond_to_missing?

Constructor Details

#initialize(running_test, config, result_callback) ⇒ Context

Returns a new instance of Context.


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

def initialize(running_test, config, result_callback)
  @__assert_running_test__ = running_test
  @__assert_config__       = config
  @__assert_with_bt__      = []
  @__assert_pending__      = 0

  @__assert_result_callback__ =
    proc do |result|
      unless @__assert_with_bt__.empty?
        result.set_with_bt(@__assert_with_bt__.dup)
      end
      result_callback.call(result)
      result
    end
end

Dynamic Method Handling

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

Instance Method Details

#assert(assertion, desc = nil) ⇒ Object

Check if the result is true. If so, create a new pass result, Otherwise create a new fail result with the desc and fail msg.


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

def assert(assertion, desc = nil)
  if assertion
    pass
  else
    what =
      if block_given?
        yield
      else
        "Failed assert: assertion was "\
        "`#{Assert::U.show(assertion, __assert_config__)}`."
      end
    fail(fail_message(desc, what))
  end
end

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

The opposite of assert. Check if the result is false. If so, create a new pass result. Otherwise create a new fail result with the desc and fail msg.


70
71
72
73
74
75
# File 'lib/assert/context.rb', line 70

def assert_not(assertion, fail_desc = nil)
  assert(!assertion, fail_desc) do
    "Failed assert_not: assertion was "\
    "`#{Assert::U.show(assertion, __assert_config__)}`."
  end
end

#assert_that(actual_value = Assert::ActualValue.not_given, &actual_value_block) ⇒ Object


78
79
80
81
82
# File 'lib/assert/context.rb', line 78

def assert_that(
      actual_value = Assert::ActualValue.not_given,
      &actual_value_block)
  Assert::ActualValue.new(actual_value, context: self, &actual_value_block)
end

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

adds a Fail result to the end of the test's results break test execution if assert is configured to halt on failures


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/assert/context.rb', line 105

def fail(message = nil)
  if @__assert_pending__ == 0
    if halt_on_fail?
      raise Result::TestFailure, message || ""
    else
      capture_result(Assert::Result::Fail, message || "")
    end
  elsif halt_on_fail?
    raise Result::TestSkipped, "Pending fail: #{message || ""}"
  else
    capture_result(Assert::Result::Skip, "Pending fail: #{message || ""}")
  end
end

#ignore(ignore_msg = nil) ⇒ Object

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


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

def ignore(ignore_msg = nil)
  capture_result(Assert::Result::Ignore, ignore_msg)
end

#inspectObject


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

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


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

def pass(pass_msg = nil)
  if @__assert_pending__ == 0
    capture_result(Assert::Result::Pass, pass_msg)
  else
    capture_result(
      Assert::Result::Fail,
      "Pending pass (make it not pending)",
    )
  end
end

#pending(&block) ⇒ Object

runs block and any fails are skips and any passes are fails


127
128
129
130
131
132
133
134
# File 'lib/assert/context.rb', line 127

def pending(&block)
  begin
    @__assert_pending__ += 1
    instance_eval(&block)
  ensure
    @__assert_pending__ -= 1
  end
end

#skip(skip_msg = nil, called_from = nil) ⇒ Object

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


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

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

#subjectObject


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

def subject
  unless instance_variable_defined?("@__assert_subject__")
    @__assert_subject__ =
      instance_eval(&self.class.subject) if self.class.subject
  end

  @__assert_subject__
end

#with_backtrace(bt, &block) ⇒ Object

alter the backtraces of fail/skip results generated in the given block


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/assert/context.rb', line 137

def with_backtrace(bt, &block)
  bt ||= []
  begin
    @__assert_with_bt__.push(bt.first)
    instance_eval(&block)
  rescue Result::TestSkipped, Result::TestFailure => ex
    if ex.assert_with_bt.nil? && !@__assert_with_bt__.empty?
      ex.assert_with_bt = @__assert_with_bt__.dup
    end
    raise(ex)
  ensure
    @__assert_with_bt__.pop
  end
end