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

match command line prompt line by line



5
6
7
8
# File 'lib/cmd_matcher.rb', line 5

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

options

:names => hash keys
:at_most => has xxx lines at most
:at_least => has xxx lines at least
:no_newline => match line more than 1 times


15
16
17
# File 'lib/cmd_matcher.rb', line 15

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

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

options

:names => hash keys
:at_least => has xxx lines at least
:no_newline => match line more than 1 times


33
34
35
36
37
38
# File 'lib/cmd_matcher.rb', line 33

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

options

:names => hash keys
:no_newline => match line more than 1 times


22
23
24
25
26
27
# File 'lib/cmd_matcher.rb', line 22

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

#match(prompts) ⇒ Object



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
# File 'lib/cmd_matcher.rb', line 40

def match(prompts)
  lines = prompts.split("\n")
  @matchers.each {|expect, opt|
    if opt[:at_most] == nil && opt[:at_least] == nil
      if m = line_match(lines, expect, opt)
        if opt[:no_newline]
          line = lines[0]
          index = line.index(m)
          lines[0] = line[index + m.size..-1]
        else
          lines.shift
        end
      else
        return false
      end
    else
      times = 0
      while true
        if m = line_match(lines, expect, opt)
          if opt[:no_newline]
            line = lines[0]
            index = line.index(m)
            lines[0] = line[index + m.size..-1]
          else
            lines.shift 
          end
          
          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