File: expectation_list_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 (82 lines) | stat: -rw-r--r-- 3,885 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
73
74
75
76
77
78
79
80
81
82
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/expectation_list'
require 'mocha/expectation'
require 'mocha/invocation'
require 'set'
require 'method_definer'

class ExpectationListTest < Mocha::TestCase
  include MethodDefiner
  include Mocha

  def test_should_return_added_expectation
    expectation_list = ExpectationList.new
    expectation = Expectation.new(nil, :my_method)
    assert_same expectation, expectation_list.add(expectation)
  end

  def test_should_find_matching_expectation
    expectation_list = ExpectationList.new
    expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
    expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
    expectation_list.add(expectation1)
    expectation_list.add(expectation2)
    assert_same expectation1, expectation_list.match(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
  end

  def test_should_remove_all_expectations_matching_method_name
    expectation_list = ExpectationList.new
    expectation1 = Expectation.new(nil, :method_one).with(:argument1, :argument2)
    expectation2 = Expectation.new(nil, :method_one).with(:argument3, :argument4)
    expectation3 = Expectation.new(nil, :method_two)
    expectation_list.add(expectation1)
    expectation_list.add(expectation2)
    expectation_list.add(expectation3)
    expectation_list.remove_all_matching_method(:method_one)
    assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument1, :argument2))
    assert_nil expectation_list.match(Invocation.new(:irrelevant, :method_one, :argument3, :argument4))
    assert_same expectation3, expectation_list.match(Invocation.new(:irrelevant, :method_two))
  end

  def test_should_find_most_recent_matching_expectation
    expectation_list = ExpectationList.new
    expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
    expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
    expectation_list.add(expectation1)
    expectation_list.add(expectation2)
    assert_same expectation2, expectation_list.match(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
  end

  def test_should_find_matching_expectation_allowing_invocation
    expectation_list = ExpectationList.new
    expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
    expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
    define_instance_method(expectation1, :invocations_allowed?) { true }
    define_instance_method(expectation2, :invocations_allowed?) { true }
    expectation_list.add(expectation1)
    expectation_list.add(expectation2)
    assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method, [:argument1, :argument2]))
  end

  def test_should_find_most_recent_matching_expectation_allowing_invocation
    expectation_list = ExpectationList.new
    expectation1 = Expectation.new(nil, :my_method)
    expectation2 = Expectation.new(nil, :my_method)
    define_instance_method(expectation1, :invocations_allowed?) { true }
    define_instance_method(expectation2, :invocations_allowed?) { false }
    expectation_list.add(expectation1)
    expectation_list.add(expectation2)
    assert_same expectation1, expectation_list.match_allowing_invocation(Invocation.new(:irrelevant, :my_method))
  end

  def test_should_combine_two_expectation_lists_into_one
    expectation_list1 = ExpectationList.new
    expectation_list2 = ExpectationList.new
    expectation1 = Expectation.new(nil, :my_method)
    expectation2 = Expectation.new(nil, :my_method)
    expectation_list1.add(expectation1)
    expectation_list2.add(expectation2)
    expectation_list = expectation_list1 + expectation_list2
    assert_equal [expectation1, expectation2], expectation_list.to_a
  end
end