Class: CommandTest::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/command_test/matcher.rb

Instance Method Summary collapse

Instance Method Details

#match?(expected, actual, e = 0, a = 0) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/command_test/matcher.rb', line 3

def match?(expected, actual, e=0, a=0)
  while e < expected.length
    case (specifier = expected[e])
    when nil
      return false  # passed end of actual
    when String, Regexp
      if specifier === actual[a]
        a += 1
      else
        return false
      end
    when Integer
      specifier >= 0 or
        raise ArgumentError, "negative integer matcher: #{specifier}"
      a += specifier
    when Range, :*, :+
      case specifier
      when :*
        specifier = 0...(actual.length - a)
      when :+
        specifier = 1...(actual.length - a)
      end
      specifier.end >= specifier.begin or
        raise ArgumentError, "descending range matcher: #{specifier}"
      specifier.begin >= 0 or
        raise ArgumentError, "negative range bounds: #{specifier}"
      specifier.each do |n|
        if match?(expected, actual, e + 1, a + n)
          return true
        end
      end
      return false
    else
      raise ArgumentError, "invalid matcher: #{specifier}"
    end
    e += 1
  end
  a == actual.length
end