Class: Assert::Context
- Inherits:
-
Object
- Object
- Assert::Context
- Includes:
- Assertions, Macros::Methods
- Defined in:
- lib/assert/context.rb
Constant Summary
Constants included from Assertions
Assertions::IGNORED_ASSERTION_HELPERS
Class Method Summary collapse
-
.description(text = nil) ⇒ Object
(also: desc, describe)
Add a piece of description text or return the full description for the context.
-
.method_added(method_name) ⇒ Object
if a test method is added to a context manually (not using a context helper): capture any context info, build a test obj, and add it to the suite.
-
.setup(scope_or_method_name = nil, &block) ⇒ Object
(also: before)
Add a setup block to run before each test or run the list of teardown blocks in given scope.
- .setup_once(&block) ⇒ Object (also: before_once, startup)
- .should(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object
- .should_eventually(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object (also: should_skip)
- .subject(&block) ⇒ Object
-
.teardown(scope_or_method_name = nil, &block) ⇒ Object
(also: after)
Add a teardown block to run after each test or run the list of teardown blocks in given scope.
- .teardown_once(&block) ⇒ Object (also: after_once, shutdown)
- .test(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object
- .test_eventually(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object (also: test_skip)
Instance Method Summary collapse
-
#assert(assertion, fail_desc = 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.
-
#assert_not(assertion, fail_desc = nil) ⇒ Object
(also: #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.
-
#fail(message = nil) ⇒ Object
(also: #flunk)
adds a Fail result to the end of the test’s results break test execution if Assert.config.halt_on_fail.
-
#ignore(ignore_msg = nil) ⇒ Object
adds an Ignore result to the end of the test’s results does not break test execution.
-
#initialize(running_test = nil) ⇒ Context
constructor
A new instance of Context.
- #inspect ⇒ Object
-
#pass(pass_msg = nil) ⇒ Object
adds a Pass result to the end of the test’s results does not break test execution.
-
#skip(skip_msg = nil) ⇒ Object
adds a Skip result to the end of the test’s results and breaks test execution.
- #subject ⇒ Object
-
#with_backtrace(bt, &block) ⇒ Object
alter the backtraces of fail results generated in the given block.
Methods included from Macros::Methods
Methods included from Assertions
#assert_block, #assert_empty, #assert_equal, #assert_file_exists, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_not_block, #assert_not_empty, #assert_not_equal, #assert_not_file_exists, #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.
170 171 172 |
# File 'lib/assert/context.rb', line 170 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, describe
Add a piece of description text or return the full description for the context
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/assert/context.rb', line 91 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 |
.method_added(method_name) ⇒ Object
if a test method is added to a context manually (not using a context helper): capture any context info, build a test obj, and add it to the suite
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/assert/context.rb', line 19 def self.method_added(method_name) if method_name.to_s =~ Suite::TEST_METHOD_REGEX klass_method_name = "#{self}##{method_name}" if Assert.suite.test_methods.include?(klass_method_name) puts "WARNING: redefining '#{klass_method_name}'" puts " from: #{called_from}" else Assert.suite.test_methods << klass_method_name end ci = Suite::ContextInfo.new(self, nil, caller.first) Assert.suite.tests << Test.new(method_name.to_s, ci, method_name) end 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/assert/context.rb', line 51 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 # arg is a block or method that needs to be stored as a setup self.setups << (block || scope_or_method_name) elsif !is_method # arg is an instance of this class (the scope for a test), # run the setups for this context in the scope scope = scope_or_method_name # setup parent... self.superclass.setup(scope) if self.superclass.respond_to?(:setup) # ... before child 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, startup
38 39 40 |
# File 'lib/assert/context.rb', line 38 def setup_once(&block) Assert.suite.setup(&block) end |
.should(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/assert/context.rb', line 139 def should(desc_or_macro, called_from=nil, first_caller=nil, &block) if !desc_or_macro.kind_of?(Macro) desc_or_macro = "should #{desc_or_macro}" end test(desc_or_macro, called_from, first_caller || caller.first, &block) end |
.should_eventually(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object Also known as: should_skip
146 147 148 149 150 151 |
# File 'lib/assert/context.rb', line 146 def should_eventually(desc_or_macro, called_from=nil, first_caller=nil, &block) if !desc_or_macro.kind_of?(Macro) desc_or_macro = "should #{desc_or_macro}" end test_eventually(desc_or_macro, called_from, first_caller || caller.first, &block) end |
.subject(&block) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/assert/context.rb', line 105 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/assert/context.rb', line 71 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 # arg is a block or method that needs to be stored as a teardown self.teardowns << (block || scope_or_method_name) elsif !is_method # arg is an instance of this class (the scope for a test), # run the setups for this context in the scope scope = scope_or_method_name # teardown child... self.teardowns.each do |teardown| teardown.kind_of?(::Proc) ? scope.instance_eval(&teardown) : scope.send(teardown) end # ... before parent self.superclass.teardown(scope) if self.superclass.respond_to?(:teardown) end end |
.teardown_once(&block) ⇒ Object Also known as: after_once, shutdown
44 45 46 |
# File 'lib/assert/context.rb', line 44 def teardown_once(&block) Assert.suite.teardown(&block) end |
.test(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/assert/context.rb', line 115 def test(desc_or_macro, called_from=nil, first_caller=nil, &block) if desc_or_macro.kind_of?(Macro) instance_eval(&desc_or_macro) elsif block_given? ci = Suite::ContextInfo.new(self, called_from, first_caller || caller.first) test_name = desc_or_macro # create a test from the given code block Assert.suite.tests << Test.new(test_name, ci, &block) else test_eventually(desc_or_macro, called_from, first_caller || caller.first, &block) end end |
.test_eventually(desc_or_macro, called_from = nil, first_caller = nil, &block) ⇒ Object Also known as: test_skip
129 130 131 132 133 134 135 136 |
# File 'lib/assert/context.rb', line 129 def test_eventually(desc_or_macro, called_from=nil, first_caller=nil, &block) ci = Suite::ContextInfo.new(self, called_from, first_caller || caller.first) test_name = desc_or_macro.kind_of?(Macro) ? desc_or_macro.name : desc_or_macro skip_block = block.nil? ? Proc.new { skip 'TODO' } : Proc.new { skip } # create a test from a proc that just skips Assert.suite.tests << Test.new(test_name, ci, &skip_block) end |
Instance Method Details
#assert(assertion, fail_desc = 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
177 178 179 180 181 182 183 184 |
# File 'lib/assert/context.rb', line 177 def assert(assertion, fail_desc = nil) if assertion pass else what_failed_msg = block_given? ? yield : "Failed assert: assertion was <#{assertion.inspect}>." fail((fail_desc, what_failed_msg)) end 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
188 189 190 |
# File 'lib/assert/context.rb', line 188 def assert_not(assertion, fail_desc = nil) assert(!assertion, fail_desc){ "Failed assert_not: assertion was <#{assertion.inspect}>." } 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.config.halt_on_fail
211 212 213 214 215 216 217 218 219 |
# File 'lib/assert/context.rb', line 211 def fail( = nil) if Assert.config.halt_on_fail raise Result::TestFailure, || '' else capture_result do |test, backtrace| Assert::Result::Fail.new(test, || '', backtrace) end end end |
#ignore(ignore_msg = nil) ⇒ Object
adds an Ignore result to the end of the test’s results does not break test execution
203 204 205 206 207 |
# File 'lib/assert/context.rb', line 203 def ignore(ignore_msg=nil) capture_result do |test, backtrace| Assert::Result::Ignore.new(test, ignore_msg, backtrace) end end |
#inspect ⇒ Object
247 248 249 |
# File 'lib/assert/context.rb', line 247 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
195 196 197 198 199 |
# File 'lib/assert/context.rb', line 195 def pass(pass_msg=nil) capture_result do |test, backtrace| Assert::Result::Pass.new(test, 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
223 224 225 |
# File 'lib/assert/context.rb', line 223 def skip(skip_msg=nil) raise(Result::TestSkipped, skip_msg || '') end |
#subject ⇒ Object
241 242 243 244 245 |
# File 'lib/assert/context.rb', line 241 def subject if subj = self.class.subject instance_eval(&subj) end end |
#with_backtrace(bt, &block) ⇒ Object
alter the backtraces of fail results generated in the given block
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/assert/context.rb', line 228 def with_backtrace(bt, &block) bt ||= [] current_results.count.tap do |count| begin instance_eval(&block) rescue Result::TestSkipped, Result::TestFailure => e e.set_backtrace(bt); raise(e) ensure current_results[count..-1].each{ |r| r.set_backtrace(bt) } end end end |