Module: RSpec::Matchers::BuiltIn::CountExpectation Private
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb |
Overview
Instance Attribute Summary
- #count_expectation_type readonly protected Internal use only
- #expected_count readonly protected Internal use only
- #has_expected_count? ⇒ Boolean readonly private Internal use only
Instance Method Summary
-
#at_least(number)
Specifies the minimum number of times the method is expected to match.
-
#at_most(number)
Specifies the maximum number of times the method is expected to match.
-
#exactly(number)
Specifies that the method is expected to match the given number of times.
-
#once
Specifies that the method is expected to match once.
-
#thrice
Specifies that the method is expected to match thrice.
-
#times
No-op.
-
#twice
Specifies that the method is expected to match twice.
- #count_constraint_to_number(n) private Internal use only
- #count_expectation_description private Internal use only
- #count_failure_reason(action) private Internal use only
-
#cover?(count, number) ⇒ Boolean
private
Internal use only
:nocov:
- #expected_count_matches?(actual_count) ⇒ Boolean private Internal use only
- #human_readable_count(count) private Internal use only
- #human_readable_expectation_type private Internal use only
- #raise_impossible_count_expectation(count) private Internal use only
- #raise_unsupported_count_expectation private Internal use only
- #set_expected_count(relativity, n) private Internal use only
- #unsupported_count_expectation?(relativity) ⇒ Boolean private Internal use only
Instance Attribute Details
#count_expectation_type (readonly, protected)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 55
attr_reader :count_expectation_type, :expected_count
#expected_count (readonly, protected)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 55
attr_reader :count_expectation_type, :expected_count
#has_expected_count? ⇒ Boolean
(readonly, private)
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 79
def has_expected_count? !!count_expectation_type end
Instance Method Details
#at_least(number)
Specifies the minimum number of times the method is expected to match
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 42
def at_least(number) set_expected_count(:>=, number) self end
#at_most(number)
Specifies the maximum number of times the method is expected to match
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 35
def at_most(number) set_expected_count(:<=, number) self end
#count_constraint_to_number(n) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 122
def count_constraint_to_number(n) case n when Numeric then n when :once then 1 when :twice then 2 when :thrice then 3 else raise ArgumentError, "Expected a number, :once, :twice or :thrice," \ " but got #{n}" end end
#count_expectation_description (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 141
def count_expectation_description "#{human_readable_expectation_type}#{human_readable_count(expected_count)}" end
#count_failure_reason(action) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 145
def count_failure_reason(action) "#{count_expectation_description}" \ " but #{action}#{human_readable_count(@actual_count)}" end
#cover?(count, number) ⇒ Boolean
(private)
:nocov:
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 65
def cover?(count, number) count.cover?(number) end
#exactly(number)
Specifies that the method is expected to match the given number of times.
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 28
def exactly(number) set_expected_count(:==, number) self end
#expected_count_matches?(actual_count) ⇒ Boolean
(private)
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 71
def expected_count_matches?(actual_count) @actual_count = actual_count return @actual_count > 0 unless count_expectation_type return cover?(expected_count, actual_count) if count_expectation_type == :<=> @actual_count.__send__(count_expectation_type, expected_count) end
#human_readable_count(count) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 159
def human_readable_count(count) case count when Range then " #{count.first} and #{count.last} times" when nil then '' when 1 then ' once' when 2 then ' twice' else " #{count} times" end end
#human_readable_expectation_type (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 150
def human_readable_expectation_type case count_expectation_type when :<= then ' at most' when :>= then ' at least' when :<=> then ' between' else '' end end
#once
Specifies that the method is expected to match once.
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 10
def once exactly(1) end
#raise_impossible_count_expectation(count) (private)
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 102
def raise_impossible_count_expectation(count) text = case count_expectation_type when :<= then "at_least(#{count}).at_most(#{expected_count})" when :>= then "at_least(#{expected_count}).at_most(#{count})" end raise ArgumentError, "The constraint #{text} is not possible" end
#raise_unsupported_count_expectation (private)
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 111
def raise_unsupported_count_expectation text = case count_expectation_type when :<= then "at_least" when :>= then "at_most" when :<=> then "at_least/at_most combination" else "count" end raise ArgumentError, "Multiple #{text} constraints are not supported" end
#set_expected_count(relativity, n) (private)
[ GitHub ]# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 83
def set_expected_count(relativity, n) raise_unsupported_count_expectation if unsupported_count_expectation?(relativity) count = count_constraint_to_number(n) if count_expectation_type == :<= && relativity == :>= raise_impossible_count_expectation(count) if count > expected_count @count_expectation_type = :<=> @expected_count = count..expected_count elsif count_expectation_type == :>= && relativity == :<= raise_impossible_count_expectation(count) if count < expected_count @count_expectation_type = :<=> @expected_count = expected_count..count else @count_expectation_type = relativity @expected_count = count end end
#thrice
Specifies that the method is expected to match thrice.
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 22
def thrice exactly(3) end
#times
No-op. Provides syntactic sugar.
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 49
def times self end
#twice
Specifies that the method is expected to match twice.
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 16
def twice exactly(2) end
#unsupported_count_expectation?(relativity) ⇒ Boolean
(private)
# File 'rspec-expectations/lib/rspec/matchers/built_in/count_expectation.rb', line 134
def unsupported_count_expectation?(relativity) return true if count_expectation_type == :== return true if count_expectation_type == :<=> (count_expectation_type == :<= && relativity == :<=) || (count_expectation_type == :>= && relativity == :>=) end