~ubuntu-branches/ubuntu/oneiric/rake/oneiric

« back to all changes in this revision

Viewing changes to flexmock-0.6.0/lib/flexmock/validators.rb

  • Committer: Bazaar Package Importer
  • Author(s): Adam Majer
  • Date: 2007-05-06 17:15:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506171536-qy40vbx5a248gsak
Tags: 0.7.3-1
* New upstream release
* Updated flexmock from 0.1.7 to 0.6.0
* Updated Standards to 0.7.2 - no changes
* Fixed Build-Depends on debhelper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
#---
 
4
# Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
 
5
# All rights reserved.
 
6
 
 
7
# Permission is granted for use, copying, modification, distribution,
 
8
# and distribution of modified versions of this work as long as the
 
9
# above copyright notice is included.
 
10
#+++
 
11
 
 
12
require 'flexmock/noop'
 
13
 
 
14
class FlexMock
 
15
 
 
16
  ####################################################################
 
17
  # Base class for all the count validators.
 
18
  #
 
19
  class CountValidator
 
20
    def initialize(expectation, limit)
 
21
      @exp = expectation
 
22
      @limit = limit
 
23
    end
 
24
 
 
25
    # If the expectation has been called +n+ times, is it still
 
26
    # eligible to be called again?  The default answer compares n to
 
27
    # the established limit.
 
28
    def eligible?(n)
 
29
      n < @limit
 
30
    end
 
31
  end
 
32
 
 
33
  ####################################################################
 
34
  # Validator for exact call counts.
 
35
  #
 
36
  class ExactCountValidator < CountValidator
 
37
    # Validate that the method expectation was called exactly +n+
 
38
    # times.
 
39
    def validate(n)
 
40
      FlexMock.framework_adapter.assert_equal @limit, n,
 
41
        "method '#{@exp}' called incorrect number of times"
 
42
    end
 
43
  end
 
44
 
 
45
  ####################################################################
 
46
  # Validator for call counts greater than or equal to a limit.
 
47
  #
 
48
  class AtLeastCountValidator < CountValidator
 
49
    # Validate the method expectation was called no more than +n+
 
50
    # times.
 
51
    def validate(n)
 
52
      FlexMock.framework_adapter.assert_block(
 
53
        "Method '#{@exp}' should be called at least #{@limit} times,\n" +
 
54
        "only called #{n} times") { n >= @limit }
 
55
    end
 
56
 
 
57
    # If the expectation has been called +n+ times, is it still
 
58
    # eligible to be called again?  Since this validator only
 
59
    # establishes a lower limit, not an upper limit, then the answer
 
60
    # is always true.
 
61
    def eligible?(n)
 
62
      true
 
63
    end
 
64
  end
 
65
 
 
66
  ####################################################################
 
67
  # Validator for call counts less than or equal to a limit.
 
68
  #
 
69
  class AtMostCountValidator < CountValidator
 
70
    # Validate the method expectation was called at least +n+ times.
 
71
    def validate(n)
 
72
      FlexMock.framework_adapter.assert_block(
 
73
        "Method '#{@exp}' should be called at most #{@limit} times,\n" +
 
74
        "only called #{n} times") { n <= @limit }
 
75
    end
 
76
  end  
 
77
end