Class: CmdMatcher

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

Defined Under Namespace

Classes: LineEqualer, LineMatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCmdMatcher

Returns a new instance of CmdMatcher.



4
5
6
7
# File 'lib/cmd-matcher.rb', line 4

def initialize
  @matchers = []
  @matches = {}
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



2
3
4
# File 'lib/cmd-matcher.rb', line 2

def matches
  @matches
end

Instance Method Details

#has(line, opt = {}) ⇒ Object



9
10
11
# File 'lib/cmd-matcher.rb', line 9

def has(line, opt = {})
  @matchers << [line, opt]
end

#has_many(line, opt = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/cmd-matcher.rb', line 20

def has_many(line, opt = {})
  arg = opt.clone
  arg[:at_most] = nil
  arg[:at_least] = 1
  has(line, arg)
end

#has_one(line, opt = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/cmd-matcher.rb', line 13

def has_one(line, opt = {})
  arg = opt.clone
  arg[:at_most] = nil
  arg[:at_least] = nil
  has(line, arg)
end

#line_equal(lines, expect, opt = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/cmd-matcher.rb', line 79

def line_equal(lines, expect, opt = {})
  line = lines[0]
  line_equaler = LineEqualer.new(expect, opt)
  if line_equaler.match(line)
    return true
  else
    puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
    return false
  end
end

#line_match(lines, expect, opt = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/cmd-matcher.rb', line 59

def line_match(lines, expect, opt = {})
  if expect.is_a?(Regexp)
    line_regex(lines, expect, opt)
  else
    line_equal(lines, expect, opt)
  end
end

#line_regex(lines, expect, opt = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cmd-matcher.rb', line 67

def line_regex(lines, expect, opt = {})
  line = lines[0]
  line_matcher = LineMatcher.new(expect, opt)
  if line_matcher.match(line)
    line_matcher.append_to(@matches)
    return true
  else
    puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
    return false
  end
end

#match(prompts) ⇒ Object



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

def match(prompts)
  lines = prompts.split("\n")
  @matchers.each {|expect, opt|
    if opt[:at_most] == nil && opt[:at_least] == nil
      if line_match(lines, expect, opt)
        lines.shift
      else
        return false
      end
    else
      times = 0
      while true
        if line_match(lines, expect, opt)
          lines.shift
          times += 1
        else
          if opt[:at_least] && opt[:at_least] > times
            return false
          end
          
          if opt[:at_most] && opt[:at_most] < times
            return false
          end
         
          break
        end
      end
    end
  }
  return true
end