File: parameters_matcher_test.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 4,885 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/parameters_matcher'

class ParametersMatcherTest < Test::Unit::TestCase

  include Mocha

  def test_should_match_any_actual_parameters_if_no_expected_parameters_specified
    parameters_matcher = ParametersMatcher.new
    assert parameters_matcher.match?([1, 2, 3])
  end

  def test_should_match_if_actual_parameters_are_same_as_expected_parameters
    parameters_matcher = ParametersMatcher.new([4, 5, 6])
    assert parameters_matcher.match?([4, 5, 6])
  end

  def test_should_not_match_if_actual_parameters_are_different_from_expected_parameters
    parameters_matcher = ParametersMatcher.new([4, 5, 6])
    assert !parameters_matcher.match?([1, 2, 3])
  end

  def test_should_not_match_if_there_are_less_actual_parameters_than_expected_parameters
    parameters_matcher = ParametersMatcher.new([4, 5, 6])
    assert !parameters_matcher.match?([4, 5])
  end

  def test_should_not_match_if_there_are_more_actual_parameters_than_expected_parameters
    parameters_matcher = ParametersMatcher.new([4, 5])
    assert !parameters_matcher.match?([4, 5, 6])
  end

  def test_should_not_match_if_not_all_required_parameters_are_supplied
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert !parameters_matcher.match?([4])
  end

  def test_should_match_if_all_required_parameters_match_and_no_optional_parameters_are_supplied
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert parameters_matcher.match?([4, 5])
  end

  def test_should_match_if_all_required_and_optional_parameters_match_and_some_optional_parameters_are_supplied
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert parameters_matcher.match?([4, 5, 6])
  end

  def test_should_match_if_all_required_and_optional_parameters_match_and_all_optional_parameters_are_supplied
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert parameters_matcher.match?([4, 5, 6, 7])
  end

  def test_should_not_match_if_all_required_and_optional_parameters_match_but_too_many_optional_parameters_are_supplied
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert !parameters_matcher.match?([4, 5, 6, 7, 8])
  end

  def test_should_not_match_if_all_required_parameters_match_but_some_optional_parameters_do_not_match
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert !parameters_matcher.match?([4, 5, 6, 0])
  end

  def test_should_not_match_if_some_required_parameters_do_not_match_although_all_optional_parameters_do_match
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert !parameters_matcher.match?([4, 0, 6])
  end

  def test_should_not_match_if_all_required_parameters_match_but_no_optional_parameters_match
    optionals = ParameterMatchers::Optionally.new(6, 7)
    parameters_matcher = ParametersMatcher.new([4, 5, optionals])
    assert !parameters_matcher.match?([4, 5, 0, 0])
  end

  def test_should_match_if_actual_parameters_satisfy_matching_block
    parameters_matcher = ParametersMatcher.new { |x, y| x + y == 3 }
    assert parameters_matcher.match?([1, 2])
  end

  def test_should_not_match_if_actual_parameters_do_not_satisfy_matching_block
    parameters_matcher = ParametersMatcher.new { |x, y| x + y == 3 }
    assert !parameters_matcher.match?([2, 3])
  end

  def test_should_remove_outer_array_braces
    params = [1, 2, [3, 4]]
    parameters_matcher = ParametersMatcher.new(params)
    assert_equal '(1, 2, [3, 4])', parameters_matcher.mocha_inspect
  end

  def test_should_display_numeric_arguments_as_is
    params = [1, 2, 3]
    parameters_matcher = ParametersMatcher.new(params)
    assert_equal '(1, 2, 3)', parameters_matcher.mocha_inspect
  end

  def test_should_remove_curly_braces_if_hash_is_only_argument
    params = [{:a => 1, :z => 2}]
    parameters_matcher = ParametersMatcher.new(params)
    assert_nil parameters_matcher.mocha_inspect.index('{')
    assert_nil parameters_matcher.mocha_inspect.index('}')
  end

  def test_should_not_remove_curly_braces_if_hash_is_not_the_only_argument
    params = [1, {:a => 1}]
    parameters_matcher = ParametersMatcher.new(params)
    assert_equal '(1, {:a => 1})', parameters_matcher.mocha_inspect
  end

  def test_should_indicate_that_matcher_will_match_any_actual_parameters
    parameters_matcher = ParametersMatcher.new
    assert_equal '(any_parameters)', parameters_matcher.mocha_inspect
  end

end