Class: Minicrest::Asserter
- Inherits:
-
Object
- Object
- Minicrest::Asserter
- Defined in:
- lib/minicrest/asserter.rb
Overview
Fluent assertion wrapper that enables comma-free assertion syntax.
Instance Method Summary collapse
-
#all_entries(entry_matcher) ⇒ true
Asserts that all entries match.
-
#all_items(item_matcher) ⇒ true
Asserts that all items match.
-
#between(min, max, exclusive: false) ⇒ true
Asserts that actual is between min and max.
-
#blank ⇒ true
Asserts that actual is blank.
-
#contains(*items) ⇒ true
Asserts that actual contains items in any order.
-
#contains_exactly(*items) ⇒ true
Asserts that actual contains items in order.
-
#descends_from(expected_type) ⇒ true
Asserts that actual is an instance of expected_type.
-
#empty ⇒ true
Asserts that actual is empty.
-
#ends_with(suffix) ⇒ true
Asserts that actual ends with suffix.
-
#equals(expected) ⇒ true
Asserts that actual equals expected by value (deep equality).
-
#falsy ⇒ true
Asserts that actual is falsy.
-
#has_attribute(name, value_matcher = nil) ⇒ true
Asserts that actual has attribute.
-
#has_key(*keys) ⇒ true
Asserts that actual has keys.
-
#has_size(expected) ⇒ true
Asserts that actual has expected size.
-
#has_value(*values) ⇒ true
Asserts that actual has values.
-
#includes(*items) ⇒ true
Asserts that actual includes items.
-
#initialize(actual, message = nil) { ... } ⇒ Asserter
constructor
Creates a new asserter for the given value or block.
-
#instance_of(expected_type) ⇒ true
Asserts that actual is an instance of expected_type.
-
#is(expected) ⇒ true
Asserts that actual is the same object as expected (reference equality).
-
#is_close_to(expected, delta) ⇒ true
Asserts that actual is close to expected.
-
#is_greater_than(expected) ⇒ true
Asserts that actual is greater than expected.
-
#is_greater_than_or_equal_to(expected) ⇒ true
Asserts that actual is greater than or equal to expected.
-
#is_in(collection) ⇒ true
Asserts that actual is in collection.
-
#is_less_than(expected) ⇒ true
Asserts that actual is less than expected.
-
#is_less_than_or_equal_to(expected) ⇒ true
Asserts that actual is less than or equal to expected.
-
#matches(matcher) ⇒ true
Asserts that actual matches the given matcher.
-
#matches_pattern(pattern) ⇒ true
Asserts that actual matches pattern.
-
#never(matcher) ⇒ true
(also: #is_not, #does_not)
Asserts that actual does NOT match the given matcher.
-
#nil_value ⇒ true
Asserts that actual is nil.
-
#no_entry(entry_matcher) ⇒ true
Asserts that no entries match.
-
#no_items(item_matcher) ⇒ true
Asserts that no items match.
-
#raises_error(expected_class = nil, message_matcher = nil) ⇒ true
Asserts that the block raises an error.
-
#raises_nothing ⇒ true
Asserts that the block does not raise any error.
-
#responds_to(*methods) ⇒ true
Asserts that actual responds to given methods.
-
#some_entry(entry_matcher) ⇒ true
Asserts that at least one entry matches.
-
#some_items(item_matcher) ⇒ true
Asserts that some items match.
-
#starts_with(prefix) ⇒ true
Asserts that actual starts with prefix.
-
#truthy ⇒ true
Asserts that actual is truthy.
Constructor Details
#initialize(actual, message = nil) { ... } ⇒ Asserter
Creates a new asserter for the given value or block.
27 28 29 30 31 |
# File 'lib/minicrest/asserter.rb', line 27 def initialize(actual, = nil, &block) @actual = actual @message = @block = block end |
Instance Method Details
#all_entries(entry_matcher) ⇒ true
Asserts that all entries match.
259 260 261 |
# File 'lib/minicrest/asserter.rb', line 259 def all_entries(entry_matcher) matches(AllEntries.new(entry_matcher)) end |
#all_items(item_matcher) ⇒ true
Asserts that all items match.
235 236 237 |
# File 'lib/minicrest/asserter.rb', line 235 def all_items(item_matcher) matches(AllItems.new(item_matcher)) end |
#between(min, max, exclusive: false) ⇒ true
Asserts that actual is between min and max.
178 179 180 |
# File 'lib/minicrest/asserter.rb', line 178 def between(min, max, exclusive: false) matches(Between.new(min, max, exclusive:)) end |
#blank ⇒ true
Asserts that actual is blank.
121 122 123 |
# File 'lib/minicrest/asserter.rb', line 121 def blank matches(Blank.new) end |
#contains(*items) ⇒ true
Asserts that actual contains items in any order.
219 220 221 |
# File 'lib/minicrest/asserter.rb', line 219 def contains(*items) matches(Contains.new(*items)) end |
#contains_exactly(*items) ⇒ true
Asserts that actual contains items in order.
227 228 229 |
# File 'lib/minicrest/asserter.rb', line 227 def contains_exactly(*items) matches(ContainsExactly.new(*items)) end |
#descends_from(expected_type) ⇒ true
Asserts that actual is an instance of expected_type.
53 54 55 |
# File 'lib/minicrest/asserter.rb', line 53 def descends_from(expected_type) matches(DescendsFrom.new(expected_type)) end |
#empty ⇒ true
Asserts that actual is empty.
128 129 130 |
# File 'lib/minicrest/asserter.rb', line 128 def empty matches(Empty.new) end |
#ends_with(suffix) ⇒ true
Asserts that actual ends with suffix.
106 107 108 |
# File 'lib/minicrest/asserter.rb', line 106 def ends_with(suffix) matches(EndsWith.new(suffix)) end |
#equals(expected) ⇒ true
Asserts that actual equals expected by value (deep equality).
37 38 39 |
# File 'lib/minicrest/asserter.rb', line 37 def equals(expected) matches(Equals.new(expected)) end |
#falsy ⇒ true
Asserts that actual is falsy.
82 83 84 |
# File 'lib/minicrest/asserter.rb', line 82 def falsy matches(Falsy.new) end |
#has_attribute(name, value_matcher = nil) ⇒ true
Asserts that actual has attribute.
292 293 294 |
# File 'lib/minicrest/asserter.rb', line 292 def has_attribute(name, value_matcher = nil) matches(HasAttribute.new(name, value_matcher)) end |
#has_key(*keys) ⇒ true
Asserts that actual has keys.
203 204 205 |
# File 'lib/minicrest/asserter.rb', line 203 def has_key(*keys) matches(HasKey.new(*keys)) end |
#has_size(expected) ⇒ true
Asserts that actual has expected size.
136 137 138 |
# File 'lib/minicrest/asserter.rb', line 136 def has_size(expected) matches(HasSize.new(expected)) end |
#has_value(*values) ⇒ true
Asserts that actual has values.
211 212 213 |
# File 'lib/minicrest/asserter.rb', line 211 def has_value(*values) matches(HasValue.new(*values)) end |
#includes(*items) ⇒ true
Asserts that actual includes items.
195 196 197 |
# File 'lib/minicrest/asserter.rb', line 195 def includes(*items) matches(Includes.new(*items)) end |
#instance_of(expected_type) ⇒ true
Asserts that actual is an instance of expected_type.
61 62 63 |
# File 'lib/minicrest/asserter.rb', line 61 def instance_of(expected_type) matches(InstanceOf.new(expected_type)) end |
#is(expected) ⇒ true
Asserts that actual is the same object as expected (reference equality).
45 46 47 |
# File 'lib/minicrest/asserter.rb', line 45 def is(expected) matches(Is.new(expected)) end |
#is_close_to(expected, delta) ⇒ true
Asserts that actual is close to expected.
187 188 189 |
# File 'lib/minicrest/asserter.rb', line 187 def is_close_to(expected, delta) matches(IsCloseTo.new(expected, delta)) end |
#is_greater_than(expected) ⇒ true
Asserts that actual is greater than expected.
144 145 146 |
# File 'lib/minicrest/asserter.rb', line 144 def is_greater_than(expected) matches(IsGreaterThan.new(expected)) end |
#is_greater_than_or_equal_to(expected) ⇒ true
Asserts that actual is greater than or equal to expected.
160 161 162 |
# File 'lib/minicrest/asserter.rb', line 160 def is_greater_than_or_equal_to(expected) matches(IsGreaterThanOrEqualTo.new(expected)) end |
#is_in(collection) ⇒ true
Asserts that actual is in collection.
283 284 285 |
# File 'lib/minicrest/asserter.rb', line 283 def is_in(collection) matches(IsIn.new(collection)) end |
#is_less_than(expected) ⇒ true
Asserts that actual is less than expected.
152 153 154 |
# File 'lib/minicrest/asserter.rb', line 152 def is_less_than(expected) matches(IsLessThan.new(expected)) end |
#is_less_than_or_equal_to(expected) ⇒ true
Asserts that actual is less than or equal to expected.
168 169 170 |
# File 'lib/minicrest/asserter.rb', line 168 def is_less_than_or_equal_to(expected) matches(IsLessThanOrEqualTo.new(expected)) end |
#matches(matcher) ⇒ true
Asserts that actual matches the given matcher.
Use this for combined matchers or custom matchers.
321 322 323 324 325 326 327 328 |
# File 'lib/minicrest/asserter.rb', line 321 def matches(matcher) return true if matcher.matches?(@actual) failure_msg = matcher.(@actual) failure_msg = "#{@message}: #{failure_msg}" if @message raise Minitest::Assertion, failure_msg end |
#matches_pattern(pattern) ⇒ true
Asserts that actual matches pattern.
114 115 116 |
# File 'lib/minicrest/asserter.rb', line 114 def matches_pattern(pattern) matches(MatchesPattern.new(pattern)) end |
#never(matcher) ⇒ true Also known as: is_not, does_not
Asserts that actual does NOT match the given matcher.
305 306 307 |
# File 'lib/minicrest/asserter.rb', line 305 def never(matcher) matches(Not.new(matcher)) end |
#nil_value ⇒ true
Asserts that actual is nil.
68 69 70 |
# File 'lib/minicrest/asserter.rb', line 68 def nil_value matches(NilValue.new) end |
#no_entry(entry_matcher) ⇒ true
Asserts that no entries match.
275 276 277 |
# File 'lib/minicrest/asserter.rb', line 275 def no_entry(entry_matcher) matches(NoEntry.new(entry_matcher)) end |
#no_items(item_matcher) ⇒ true
Asserts that no items match.
251 252 253 |
# File 'lib/minicrest/asserter.rb', line 251 def no_items(item_matcher) matches(NoItems.new(item_matcher)) end |
#raises_error(expected_class = nil, message_matcher = nil) ⇒ true
Asserts that the block raises an error.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/minicrest/asserter.rb', line 345 def raises_error(expected_class = nil, = nil) raise ArgumentError, 'raises_error requires a block' unless @block begin @block.call # Block didn't raise - this is a failure failure_msg = if expected_class "expected block to raise #{expected_class}, but no error was raised" else 'expected block to raise an error, but no error was raised' end failure_msg = "#{@message}: #{failure_msg}" if @message raise Minitest::Assertion, failure_msg rescue Minitest::Assertion raise rescue StandardError => e # Block raised an error - check if it matches expectations if expected_class && !e.is_a?(expected_class) failure_msg = "expected block to raise #{expected_class}, but raised #{e.class}: #{e.}" failure_msg = "#{@message}: #{failure_msg}" if @message raise Minitest::Assertion, failure_msg end if && !.matches?(e.) failure_msg = "expected block to raise #{expected_class || 'an error'} " \ "with message #{.description}, but message was #{e..inspect}" failure_msg = "#{@message}: #{failure_msg}" if @message raise Minitest::Assertion, failure_msg end true end end |
#raises_nothing ⇒ true
Asserts that the block does not raise any error.
386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/minicrest/asserter.rb', line 386 def raises_nothing raise ArgumentError, 'raises_nothing requires a block' unless @block begin @block.call true rescue StandardError => e failure_msg = "expected block not to raise an error, but raised #{e.class}: #{e.}" failure_msg = "#{@message}: #{failure_msg}" if @message raise Minitest::Assertion, failure_msg end end |
#responds_to(*methods) ⇒ true
Asserts that actual responds to given methods.
90 91 92 |
# File 'lib/minicrest/asserter.rb', line 90 def responds_to(*methods) matches(RespondsTo.new(*methods)) end |
#some_entry(entry_matcher) ⇒ true
Asserts that at least one entry matches.
267 268 269 |
# File 'lib/minicrest/asserter.rb', line 267 def some_entry(entry_matcher) matches(SomeEntry.new(entry_matcher)) end |
#some_items(item_matcher) ⇒ true
Asserts that some items match.
243 244 245 |
# File 'lib/minicrest/asserter.rb', line 243 def some_items(item_matcher) matches(SomeItems.new(item_matcher)) end |
#starts_with(prefix) ⇒ true
Asserts that actual starts with prefix.
98 99 100 |
# File 'lib/minicrest/asserter.rb', line 98 def starts_with(prefix) matches(StartsWith.new(prefix)) end |
#truthy ⇒ true
Asserts that actual is truthy.
75 76 77 |
# File 'lib/minicrest/asserter.rb', line 75 def truthy matches(Truthy.new) end |