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)
Add a piece of description text or return the full description for the context.
-
.inherited(klass) ⇒ Object
if a class subclasses Context, add it to the suite.
-
.setup(scope = 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)
- .should(desc_or_macro, &block) ⇒ Object
- .should_eventually(desc, &block) ⇒ Object (also: should_skip)
- .subject(&block) ⇒ Object
-
.teardown(scope = 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)
- .test(desc_or_macro, &block) ⇒ Object
- .test_eventually(desc, &block) ⇒ Object (also: test_skip)
Instance Method Summary collapse
-
#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.
-
#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(fail_msg = nil) ⇒ Object
(also: #flunk)
adds a Fail result to the end of the test’s results does not break test execution.
-
#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
Methods included from Macros::Methods
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.
137 138 139 |
# File 'lib/assert/context.rb', line 137 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/assert/context.rb', line 60 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 = 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 |
# File 'lib/assert/context.rb', line 36 def setup(scope=nil, &block) if block_given? self.setups << block elsif scope # setup parent before child self.superclass.setup(scope) if self.superclass.respond_to?(:setup) self.setups.each{|setup| scope.instance_eval(&setup)} 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
104 105 106 107 108 109 |
# File 'lib/assert/context.rb', line 104 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
116 117 118 |
# File 'lib/assert/context.rb', line 116 def should_eventually(desc, &block) should(desc) { skip } end |
.subject(&block) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/assert/context.rb', line 73 def subject(&block) if block_given? @subject = block else @subject || if superclass.respond_to?(:subject) superclass.subject end end end |
.teardown(scope = 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
48 49 50 51 52 53 54 55 56 |
# File 'lib/assert/context.rb', line 48 def teardown(scope=nil, &block) if block_given? self.teardowns << block elsif scope # teardown child before parent self.teardowns.each{|teardown| scope.instance_eval(&teardown)} 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/assert/context.rb', line 83 def test(desc_or_macro, &block) if desc_or_macro.kind_of?(Macro) instance_eval(&desc_or_macro) else raise ArgumentError, "please provide a test block" unless block_given? method_name = "test: #{desc_or_macro}" # 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, &block) end end |
.test_eventually(desc, &block) ⇒ Object Also known as: test_skip
111 112 113 |
# File 'lib/assert/context.rb', line 111 def test_eventually(desc, &block) test(desc) { skip } 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
144 145 146 147 148 |
# File 'lib/assert/context.rb', line 144 def assert(assertion, fail_desc=nil, what_failed_msg=nil) what_failed_msg ||= "Failed assert: assertion was <#{assertion.inspect}>." msg = (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
152 153 154 155 |
# File 'lib/assert/context.rb', line 152 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
173 174 175 176 177 178 |
# File 'lib/assert/context.rb', line 173 def fail(fail_msg=nil) capture_result do |test_name, backtrace| = ((fail_msg) { }).call Assert::Result::Fail.new(test_name, , 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
183 184 185 186 187 |
# File 'lib/assert/context.rb', line 183 def ignore(ignore_msg=nil) capture_result do |test_name, backtrace| Assert::Result::Ignore.new(test_name, ignore_msg, backtrace) end end |
#inspect ⇒ Object
195 196 197 |
# File 'lib/assert/context.rb', line 195 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
165 166 167 168 169 |
# File 'lib/assert/context.rb', line 165 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
159 160 161 |
# File 'lib/assert/context.rb', line 159 def skip(skip_msg=nil) raise(Result::TestSkipped, skip_msg || "") end |
#subject ⇒ Object
189 190 191 192 193 |
# File 'lib/assert/context.rb', line 189 def subject if subj = self.class.subject instance_eval(&subj) end end |