Class: Philiprehberger::Assert::Assertion
- Inherits:
-
Object
- Object
- Philiprehberger::Assert::Assertion
- Defined in:
- lib/philiprehberger/assert/assertion.rb
Overview
Chainable assertion object returned by Assert.that.
Instance Method Summary collapse
- #between(min, max) ⇒ Object
- #ends_with(suffix) ⇒ Object
- #gt(num) ⇒ Object
- #gte(num) ⇒ Object
- #includes_key(key) ⇒ Object
-
#initialize(value, message: nil, failures: nil) ⇒ Assertion
constructor
A new instance of Assertion.
- #is_a(type) ⇒ Object
- #lt(num) ⇒ Object
- #lte(num) ⇒ Object
- #matches(pattern) ⇒ Object
- #not_blank ⇒ Object
- #not_empty ⇒ Object
- #one_of(*values) ⇒ Object
- #responds_to(*methods) ⇒ Object
- #satisfies(description = nil, &block) ⇒ Object
- #starts_with(prefix) ⇒ Object
Constructor Details
#initialize(value, message: nil, failures: nil) ⇒ Assertion
Returns a new instance of Assertion.
7 8 9 10 11 |
# File 'lib/philiprehberger/assert/assertion.rb', line 7 def initialize(value, message: nil, failures: nil) @value = value @message = @failures = failures end |
Instance Method Details
#between(min, max) ⇒ Object
57 58 59 |
# File 'lib/philiprehberger/assert/assertion.rb', line 57 def between(min, max) check(@value.between?(min, max), "Expected #{@value.inspect} to be between #{min} and #{max}") end |
#ends_with(suffix) ⇒ Object
41 42 43 |
# File 'lib/philiprehberger/assert/assertion.rb', line 41 def ends_with(suffix) check(@value.end_with?(suffix), "expected to end with #{suffix.inspect}") end |
#gt(num) ⇒ Object
25 26 27 |
# File 'lib/philiprehberger/assert/assertion.rb', line 25 def gt(num) check(@value > num, "Expected #{@value.inspect} to be > #{num}") end |
#gte(num) ⇒ Object
17 18 19 |
# File 'lib/philiprehberger/assert/assertion.rb', line 17 def gte(num) check(@value >= num, "Expected #{@value.inspect} to be >= #{num}") end |
#includes_key(key) ⇒ Object
53 54 55 |
# File 'lib/philiprehberger/assert/assertion.rb', line 53 def includes_key(key) check(@value.respond_to?(:key?) && @value.key?(key), "Expected #{@value.inspect} to include key #{key.inspect}") end |
#is_a(type) ⇒ Object
13 14 15 |
# File 'lib/philiprehberger/assert/assertion.rb', line 13 def is_a(type) check(@value.is_a?(type), "Expected #{@value.inspect} to be a #{type}") end |
#lt(num) ⇒ Object
29 30 31 |
# File 'lib/philiprehberger/assert/assertion.rb', line 29 def lt(num) check(@value < num, "Expected #{@value.inspect} to be < #{num}") end |
#lte(num) ⇒ Object
21 22 23 |
# File 'lib/philiprehberger/assert/assertion.rb', line 21 def lte(num) check(@value <= num, "Expected #{@value.inspect} to be <= #{num}") end |
#matches(pattern) ⇒ Object
33 34 35 |
# File 'lib/philiprehberger/assert/assertion.rb', line 33 def matches(pattern) check(pattern.match?(@value.to_s), "Expected #{@value.inspect} to match #{pattern.inspect}") end |
#not_blank ⇒ Object
45 46 47 |
# File 'lib/philiprehberger/assert/assertion.rb', line 45 def not_blank check(!@value.nil? && !@value.to_s.strip.empty?, 'Expected value to not be blank') end |
#not_empty ⇒ Object
49 50 51 |
# File 'lib/philiprehberger/assert/assertion.rb', line 49 def not_empty check(@value.respond_to?(:empty?) && !@value.empty?, 'Expected value to not be empty') end |
#one_of(*values) ⇒ Object
61 62 63 |
# File 'lib/philiprehberger/assert/assertion.rb', line 61 def one_of(*values) check(values.include?(@value), "Expected #{@value.inspect} to be one of #{values.inspect}") end |
#responds_to(*methods) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/philiprehberger/assert/assertion.rb', line 65 def responds_to(*methods) missing = methods.reject { |m| @value.respond_to?(m) } if missing.empty? self else msg = @message || "Expected #{@value.inspect} to respond to #{missing.join(', ')}" raise AssertionError, msg unless @failures @failures << msg self end end |
#satisfies(description = nil, &block) ⇒ Object
79 80 81 |
# File 'lib/philiprehberger/assert/assertion.rb', line 79 def satisfies(description = nil, &block) check(block.call(@value), "Expected #{@value.inspect} to satisfy #{description || 'custom condition'}") end |
#starts_with(prefix) ⇒ Object
37 38 39 |
# File 'lib/philiprehberger/assert/assertion.rb', line 37 def starts_with(prefix) check(@value.start_with?(prefix), "expected to start with #{prefix.inspect}") end |