Class: FlexMock::CountValidator

Inherits:
Object
  • Object
show all
Includes:
SpyDescribers
Defined in:
lib/flexmock/validators.rb

Overview

Base class for all the count validators.

Defined Under Namespace

Classes: ValidationFailed

Instance Method Summary collapse

Methods included from SpyDescribers

#append_call_record, #block_description, #describe_calls, #describe_spy, #describe_spy_expectation, #describe_spy_negative_expectation, #spy_description, #times_description

Constructor Details

#initialize(expectation, limit) ⇒ CountValidator

Returns a new instance of CountValidator.



24
25
26
27
# File 'lib/flexmock/validators.rb', line 24

def initialize(expectation, limit)
  @exp = expectation
  @limit = limit
end

Instance Method Details

#describeObject

Human readable description of the validator



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flexmock/validators.rb', line 37

def describe
  case @limit
  when 0
    ".never"
  when 1
    ".once"
  when 2
    ".twice"
  else
    ".times(#{@limit})"
  end
end

#describe_limitObject



50
51
52
# File 'lib/flexmock/validators.rb', line 50

def describe_limit
  @limit.to_s
end

#eligible?(n) ⇒ Boolean

If the expectation has been called n times, is it still eligible to be called again? The default answer compares n to the established limit.

Returns:

  • (Boolean)


32
33
34
# File 'lib/flexmock/validators.rb', line 32

def eligible?(n)
  n < @limit
end

#validate_count(n, &block) ⇒ Object



57
58
59
60
61
# File 'lib/flexmock/validators.rb', line 57

def validate_count(n, &block)
  unless yield
    raise ValidationFailed, construct_validation_count_error_message(n)
  end
end