Module: Bridgetown::Foundation::IntuitiveExpectations
- Defined in:
- bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb
Overview
This module provides a set of Ruby syntax-inspired mechanism for writing expectations
which wrap the native statements within Minitest::Expectation.
Generally the methods here return self, so you can chain multiple expectations together for a single object.
Class Method Summary collapse
-
.enrich(mod) ⇒ Object
Use this in your main test helper to enrich
Minitest::Expectation, or something akin to it.
Instance Method Summary collapse
-
#empty?(msg = nil) ⇒ Minitest::Expectation
Expect the object (like a string, array, etc.) to be empty.
-
#equal?(other, msg = nil) ⇒ Minitest::Expectation
(also: #==)
Expect the object to equal a value.
-
#exclude?(other, msg = nil) ⇒ Minitest::Expectation
(also: #not_include?)
Expect the object (like a string, array, etc.) not to include another value.
-
#falsy?(msg = nil) ⇒ Minitest::Expectation
(also: #falsey?, #false?)
Expect the object to be a falsy value.
-
#include?(other, msg = nil) ⇒ Minitest::Expectation
(also: #<<)
Expect the object (like a string, array, etc.) to include another value.
-
#is?(sym, msg = nil) ⇒ Minitest::Expectation
Expect the object to return a truthy value from a predicate, e.g.
-
#is_a?(klass, msg = nil) ⇒ Minitest::Expectation
(also: #kind_of?)
Expect the object to be an instance of a class (or a subclass).
-
#isnt?(sym, msg = nil) ⇒ Minitest::Expectation
Expect the object to return a falsy value from a predicate, e.g.
-
#match?(other, msg = nil) ⇒ Minitest::Expectation
(also: #=~)
Expect the object to match a regular expression.
-
#nil?(msg = nil) ⇒ Minitest::Expectation
Expect the object to be nil.
-
#not_a?(klass, msg = nil) ⇒ Minitest::Expectation
(also: #isnt_a?, #is_not_a?, #not_kind_of?)
Expect the object not to be an instance of a class (or a subclass).
-
#not_empty?(msg = nil) ⇒ Minitest::Expectation
(also: #filled?)
Expect the object (like a string, array, etc.) not to be empty.
-
#not_equal?(other, msg = nil) ⇒ Minitest::Expectation
(also: #!=)
Expect the object not to equal a value.
-
#not_match?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object not to match a regular expression.
-
#not_nil?(msg = nil) ⇒ Minitest::Expectation
Expect the object not to be nil.
-
#not_output? ⇒ Minitest::Expectation
(also: #silent?)
Expect the block not to send output to stdout and stderr.
-
#not_within?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object not to be within another value (as defined by the
within?object refinement). -
#output?(stdout = nil, stderr = nil) ⇒ Minitest::Expectation
Expect the block to send output to stdout and/or stderr.
-
#raise?(exception, msg = nil) ⇒ Minitest::Expectation
Expect the block not to raise the exception.
-
#truthy?(msg = nil) ⇒ Minitest::Expectation
(also: #true?)
Expect the object to be a truthy value.
-
#within?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object to be within another value (as defined by the
within?object refinement).
Class Method Details
.enrich(mod) ⇒ Object
Use this in your main test helper to enrich Minitest::Expectation, or something akin to it
17 18 19 20 21 22 23 24 25 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 17 def self.enrich(mod) mod::Expectation.include self filter_exp = %r!bridgetown/foundation/intuitive_expectations\.rb! if mod.backtrace_filter.respond_to?(:add_filter) mod.backtrace_filter.add_filter filter_exp else mod.backtrace_filter = mod::BacktraceFilter.new(%r!#{mod::BacktraceFilter::MT_RE}|#{filter_exp}!) end end |
Instance Method Details
#empty?(msg = nil) ⇒ Minitest::Expectation
Expect the object (like a string, array, etc.) to be empty
110 111 112 113 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 110 def empty?(msg = nil) must_be_empty(msg) self end |
#equal?(other, msg = nil) ⇒ Minitest::Expectation Also known as: ==
Expect the object to equal a value
62 63 64 65 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 62 def equal?(other, msg = nil) must_equal(other, msg) self end |
#exclude?(other, msg = nil) ⇒ Minitest::Expectation Also known as: not_include?
Expect the object (like a string, array, etc.) not to include another value
133 134 135 136 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 133 def exclude?(other, msg = nil) wont_include(other, msg) self end |
#falsy?(msg = nil) ⇒ Minitest::Expectation Also known as: falsey?, false?
Expect the object to be a falsy value
37 38 39 40 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 37 def falsy?(msg = nil) ctx.refute target, msg self end |
#include?(other, msg = nil) ⇒ Minitest::Expectation Also known as: <<
Expect the object (like a string, array, etc.) to include another value
125 126 127 128 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 125 def include?(other, msg = nil) must_include(other, msg) self end |
#is?(sym, msg = nil) ⇒ Minitest::Expectation
Expect the object to return a truthy value from a predicate, e.g.
expect(new_user).is? :new?
47 48 49 50 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 47 def is?(sym, msg = nil) must_be(sym, Minitest::Assertions::UNDEFINED, msg) self end |
#is_a?(klass, msg = nil) ⇒ Minitest::Expectation Also known as: kind_of?
Expect the object to be an instance of a class (or a subclass)
156 157 158 159 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 156 def is_a?(klass, msg = nil) must_be_kind_of(klass, msg) self end |
#isnt?(sym, msg = nil) ⇒ Minitest::Expectation
Expect the object to return a falsy value from a predicate, e.g.
expect(saved_user).isnt? :new?
55 56 57 58 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 55 def isnt?(sym, msg = nil) wont_be(sym, Minitest::Assertions::UNDEFINED, msg) self end |
#match?(other, msg = nil) ⇒ Minitest::Expectation Also known as: =~
Expect the object to match a regular expression
141 142 143 144 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 141 def match?(other, msg = nil) must_match(other, msg) self end |
#nil?(msg = nil) ⇒ Minitest::Expectation
Expect the object to be nil
96 97 98 99 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 96 def nil?(msg = nil) must_be_nil(msg) self end |
#not_a?(klass, msg = nil) ⇒ Minitest::Expectation Also known as: isnt_a?, is_not_a?, not_kind_of?
Expect the object not to be an instance of a class (or a subclass)
164 165 166 167 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 164 def not_a?(klass, msg = nil) wont_be_kind_of(klass, msg) self end |
#not_empty?(msg = nil) ⇒ Minitest::Expectation Also known as: filled?
Expect the object (like a string, array, etc.) not to be empty
117 118 119 120 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 117 def not_empty?(msg = nil) wont_be_empty(msg) self end |
#not_equal?(other, msg = nil) ⇒ Minitest::Expectation Also known as: !=
Expect the object not to equal a value
70 71 72 73 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 70 def not_equal?(other, msg = nil) wont_equal(other, msg) self end |
#not_match?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object not to match a regular expression
149 150 151 152 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 149 def not_match?(other, msg = nil) wont_match(other, msg) self end |
#not_nil?(msg = nil) ⇒ Minitest::Expectation
Expect the object not to be nil
103 104 105 106 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 103 def not_nil?(msg = nil) wont_be_nil(msg) self end |
#not_output? ⇒ Minitest::Expectation Also known as: silent?
Expect the block not to send output to stdout and stderr
195 196 197 198 199 200 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 195 def not_output? Warning.warn "Calling `#{__callee__}` for the same block re-executes the block" if @block_ran @block_ran ||= true must_be_silent self end |
#not_within?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object not to be within another value (as defined by the within?
object refinement)
88 89 90 91 92 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 88 def not_within?(other, msg = nil) msg = ctx.(msg) { "Expected #{ctx.mu_pp target} to not be within #{ctx.mu_pp other}" } ctx.refute target.within?(other), msg self end |
#output?(stdout = nil, stderr = nil) ⇒ Minitest::Expectation
Expect the block to send output to stdout and/or stderr
186 187 188 189 190 191 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 186 def output?(stdout = nil, stderr = nil) Warning.warn "Calling `#{__callee__}` for the same block re-executes the block" if @block_ran @block_ran ||= true must_output stdout, stderr self end |
#raise?(exception, msg = nil) ⇒ Minitest::Expectation
Expect the block not to raise the exception
174 175 176 177 178 179 180 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 174 def raise?(exception, msg = nil) Warning.warn "Calling `#{__callee__}` for the same block re-executes the block" if @block_ran @block_ran ||= true # we need this ternary operator because `must_raise` takes a variable number of arguments msg ? must_raise(exception, msg) : must_raise(exception) self end |
#truthy?(msg = nil) ⇒ Minitest::Expectation Also known as: true?
Expect the object to be a truthy value
29 30 31 32 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 29 def truthy?(msg = nil) ctx.assert target, msg self end |
#within?(other, msg = nil) ⇒ Minitest::Expectation
Expect the object to be within another value (as defined by the within?
object refinement)
79 80 81 82 83 |
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 79 def within?(other, msg = nil) msg = ctx.(msg) { "Expected #{ctx.mu_pp target} to be within #{ctx.mu_pp other}" } ctx.assert target.within?(other), msg self end |