File: cardinality_test.rb

package info (click to toggle)
ruby-mocha 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,540 kB
  • sloc: ruby: 11,899; javascript: 477; makefile: 14
file content (72 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/cardinality'
require 'mocha/invocation'
require 'mocha/return_values'
require 'mocha/yield_parameters'

class CardinalityTest < Mocha::TestCase
  include Mocha

  def new_invocation
    Invocation.new(:irrelevant, :irrelevant)
  end

  def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
    cardinality = Cardinality.new(2, 3)
    assert cardinality.invocations_allowed?
    cardinality << new_invocation
    assert cardinality.invocations_allowed?
    cardinality << new_invocation
    assert cardinality.invocations_allowed?
    cardinality << new_invocation
    assert !cardinality.invocations_allowed?
  end

  def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold
    cardinality = Cardinality.new(2, 3)
    assert !cardinality.satisfied?
    cardinality << new_invocation
    assert !cardinality.satisfied?
    cardinality << new_invocation
    assert cardinality.satisfied?
    cardinality << new_invocation
    assert cardinality.satisfied?
  end

  def test_should_describe_cardinality_defined_using_at_least
    assert_equal 'allowed any number of times', Cardinality.new.at_least(0).anticipated_times
    assert_equal 'expected at least once', Cardinality.new.at_least(1).anticipated_times
    assert_equal 'expected at least twice', Cardinality.new.at_least(2).anticipated_times
    assert_equal 'expected at least 3 times', Cardinality.new.at_least(3).anticipated_times
  end

  def test_should_describe_cardinality_defined_using_at_most
    assert_equal 'expected at most once', Cardinality.new.at_most(1).anticipated_times
    assert_equal 'expected at most twice', Cardinality.new.at_most(2).anticipated_times
    assert_equal 'expected at most 3 times', Cardinality.new.at_most(3).anticipated_times
  end

  def test_should_describe_cardinality_defined_using_exactly
    assert_equal 'expected never', Cardinality.new.exactly(0).anticipated_times
    assert_equal 'expected exactly once', Cardinality.new.exactly(1).anticipated_times
    assert_equal 'expected exactly twice', Cardinality.new.exactly(2).anticipated_times
    assert_equal 'expected exactly 3 times', Cardinality.new.exactly(3).anticipated_times
  end

  def test_should_describe_cardinality_defined_using_times_with_range
    assert_equal 'expected between 2 and 4 times', Cardinality.new.times(2..4).anticipated_times
    assert_equal 'expected between 1 and 3 times', Cardinality.new.times(1..3).anticipated_times
  end

  def test_should_need_verifying
    assert Cardinality.new.exactly(2).needs_verifying?
    assert Cardinality.new.at_least(3).needs_verifying?
    assert Cardinality.new.at_most(2).needs_verifying?
    assert Cardinality.new.times(4).needs_verifying?
    assert Cardinality.new.times(2..4).needs_verifying?
  end

  def test_should_not_need_verifying
    assert_equal false, Cardinality.new.at_least(0).needs_verifying?
  end
end