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

Instance Method Summary collapse

Class Method Details

.enrich(mod) ⇒ Object

Use this in your main test helper to enrich Minitest::Expectation, or something akin to it

Parameters:

  • (Module(Minitest))


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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?

Returns:

  • (Minitest::Expectation)


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)

Returns:

  • (Minitest::Expectation)


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?

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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)

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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)

Returns:

  • (Minitest::Expectation)


88
89
90
91
92
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 88

def not_within?(other, msg = nil)
  msg = ctx.message(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

Parameters:

  • stdout (String, Regexp) (defaults to: nil)
  • stderr (String, Regexp) (defaults to: nil)

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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

Returns:

  • (Minitest::Expectation)


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)

Returns:

  • (Minitest::Expectation)


79
80
81
82
83
# File 'bridgetown-foundation/lib/bridgetown/foundation/intuitive_expectations.rb', line 79

def within?(other, msg = nil)
  msg = ctx.message(msg) { "Expected #{ctx.mu_pp target} to be within #{ctx.mu_pp other}" }
  ctx.assert target.within?(other), msg
  self
end