Module: FlexMock::ArgumentMatching

Defined in:
lib/flexmock/argument_matching.rb

Constant Summary collapse

MISSING_ARG =
Object.new

Class Method Summary collapse

Class Method Details

.all_match?(expected_args, expected_kw, expected_block, actual_args, actual_kw, actual_block) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/flexmock/argument_matching.rb', line 7

def all_match?(expected_args, expected_kw, expected_block, actual_args, actual_kw, actual_block)
  all_match_args?(expected_args, actual_args) &&
    all_match_kw?(expected_kw, actual_kw) &&
    all_match_block?(expected_block, actual_block)
end

.all_match_args?(expected_args, actual_args) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flexmock/argument_matching.rb', line 13

def all_match_args?(expected_args, actual_args)
  return true if expected_args.nil?
  return false if actual_args.size > expected_args.size
  i = 0
  while i < actual_args.size
    return false unless match?(expected_args[i], actual_args[i])
    i += 1
  end
  while i < expected_args.size
    return false unless match?(expected_args[i], MISSING_ARG)
    i += 1
  end
  true
end

.all_match_block?(expected_block, actual_block) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/flexmock/argument_matching.rb', line 46

def all_match_block?(expected_block, actual_block)
  return true if expected_block.nil?

  !(expected_block ^ actual_block)
end

.all_match_kw?(expected_kw, actual_kw) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flexmock/argument_matching.rb', line 28

def all_match_kw?(expected_kw, actual_kw)
  return true if expected_kw.nil?
  return expected_kw === actual_kw if expected_kw.kind_of? HashMatcher

  matched_expected_k = Set.new
  actual_kw.each do |actual_k, actual_v|
    found_match = expected_kw.find do |k, v|
      match?(k, actual_k) && match?(v, actual_v)
    end
    return false unless found_match
    matched_expected_k << found_match
  end

  return false unless matched_expected_k.size == expected_kw.keys.size

  true
end

.match?(expected, actual) ⇒ Boolean

Does the expected argument match the corresponding actual value.

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/flexmock/argument_matching.rb', line 53

def match?(expected, actual)
  expected === actual ||
  expected == actual ||
  ( Regexp === expected && expected === actual.to_s )
end

.missing?(arg) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/flexmock/argument_matching.rb', line 59

def missing?(arg)
  arg == MISSING_ARG
end