cmd_matcher 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/cmd_matcher.rb +41 -9
- data/spec/cmd_matcher_benchmark.rb +28 -0
- data/spec/cmd_matcher_spec.rb +12 -1
- metadata +4 -3
data/Rakefile
CHANGED
data/lib/cmd_matcher.rb
CHANGED
@@ -1,15 +1,24 @@
|
|
1
1
|
class CmdMatcher
|
2
2
|
attr_reader :matches
|
3
3
|
|
4
|
+
# match command line prompt line by line
|
4
5
|
def initialize
|
5
6
|
@matchers = []
|
6
7
|
@matches = {}
|
7
8
|
end
|
8
9
|
|
10
|
+
## options
|
11
|
+
# :names => hash keys
|
12
|
+
# :at_most => has xxx lines at most
|
13
|
+
# :at_least => has xxx lines at least
|
14
|
+
# :no_newline => match line more than 1 times
|
9
15
|
def has(line, opt = {})
|
10
16
|
@matchers << [line, opt]
|
11
17
|
end
|
12
18
|
|
19
|
+
## options
|
20
|
+
# :names => hash keys
|
21
|
+
# :no_newline => match line more than 1 times
|
13
22
|
def has_one(line, opt = {})
|
14
23
|
arg = opt.clone
|
15
24
|
arg[:at_most] = nil
|
@@ -17,10 +26,14 @@ class CmdMatcher
|
|
17
26
|
has(line, arg)
|
18
27
|
end
|
19
28
|
|
29
|
+
## options
|
30
|
+
# :names => hash keys
|
31
|
+
# :at_least => has xxx lines at least
|
32
|
+
# :no_newline => match line more than 1 times
|
20
33
|
def has_many(line, opt = {})
|
21
34
|
arg = opt.clone
|
22
35
|
arg[:at_most] = nil
|
23
|
-
arg[:at_least]
|
36
|
+
arg[:at_least] ||= 1
|
24
37
|
has(line, arg)
|
25
38
|
end
|
26
39
|
|
@@ -28,16 +41,29 @@ class CmdMatcher
|
|
28
41
|
lines = prompts.split("\n")
|
29
42
|
@matchers.each {|expect, opt|
|
30
43
|
if opt[:at_most] == nil && opt[:at_least] == nil
|
31
|
-
if line_match(lines, expect, opt)
|
32
|
-
|
44
|
+
if m = line_match(lines, expect, opt)
|
45
|
+
if opt[:no_newline]
|
46
|
+
line = lines[0]
|
47
|
+
index = line.index(m)
|
48
|
+
lines[0] = line[index + m.size..-1]
|
49
|
+
else
|
50
|
+
lines.shift
|
51
|
+
end
|
33
52
|
else
|
34
53
|
return false
|
35
54
|
end
|
36
55
|
else
|
37
56
|
times = 0
|
38
57
|
while true
|
39
|
-
if line_match(lines, expect, opt)
|
40
|
-
|
58
|
+
if m = line_match(lines, expect, opt)
|
59
|
+
if opt[:no_newline]
|
60
|
+
line = lines[0]
|
61
|
+
index = line.index(m)
|
62
|
+
lines[0] = line[index + m.size..-1]
|
63
|
+
else
|
64
|
+
lines.shift
|
65
|
+
end
|
66
|
+
|
41
67
|
times += 1
|
42
68
|
else
|
43
69
|
if opt[:at_least] && opt[:at_least] > times
|
@@ -56,6 +82,7 @@ class CmdMatcher
|
|
56
82
|
return true
|
57
83
|
end
|
58
84
|
|
85
|
+
private
|
59
86
|
def line_match(lines, expect, opt = {})
|
60
87
|
if expect.is_a?(Regexp)
|
61
88
|
line_regex(lines, expect, opt)
|
@@ -66,10 +93,14 @@ class CmdMatcher
|
|
66
93
|
|
67
94
|
def line_regex(lines, expect, opt = {})
|
68
95
|
line = lines[0]
|
96
|
+
unless line
|
97
|
+
puts lines.inspect
|
98
|
+
end
|
99
|
+
|
69
100
|
line_matcher = LineMatcher.new(expect, opt)
|
70
|
-
if line_matcher.match(line)
|
101
|
+
if m = line_matcher.match(line)
|
71
102
|
line_matcher.append_to(@matches)
|
72
|
-
return
|
103
|
+
return m[0]
|
73
104
|
else
|
74
105
|
puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
|
75
106
|
return false
|
@@ -80,7 +111,7 @@ class CmdMatcher
|
|
80
111
|
line = lines[0]
|
81
112
|
line_equaler = LineEqualer.new(expect, opt)
|
82
113
|
if line_equaler.match(line)
|
83
|
-
return
|
114
|
+
return line
|
84
115
|
else
|
85
116
|
puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
|
86
117
|
return false
|
@@ -101,6 +132,7 @@ class CmdMatcher
|
|
101
132
|
end
|
102
133
|
|
103
134
|
def match(line)
|
135
|
+
return false unless line
|
104
136
|
m = line.match(@matcher)
|
105
137
|
return false unless m
|
106
138
|
@matches = {}
|
@@ -111,7 +143,7 @@ class CmdMatcher
|
|
111
143
|
@matches[name] = m[index + 1]
|
112
144
|
end
|
113
145
|
}
|
114
|
-
return
|
146
|
+
return m
|
115
147
|
end
|
116
148
|
|
117
149
|
def append_to(matches)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "benchmark"
|
3
|
+
require 'cmd_matcher'
|
4
|
+
|
5
|
+
n = 10000
|
6
|
+
|
7
|
+
Benchmark.bm do |x|
|
8
|
+
x.report {
|
9
|
+
n.times { |i|
|
10
|
+
cm = CmdMatcher.new
|
11
|
+
cm.has_one "++++++++++++++++++++++++++++++++++"
|
12
|
+
cm.has_one "sn type id"
|
13
|
+
cm.has_one "++++++++++++++++++++++++++++++++++"
|
14
|
+
cm.has_many /(\d+)\s+(trunk|access)\s+(\d+)/, :names => [:sn, :type, :id]
|
15
|
+
cm.has "++++++++++++++++++++++++++++++++++", :at_most => 1
|
16
|
+
cm.has_one /total\s*(\d+)/, :names => [:total]
|
17
|
+
|
18
|
+
cm.match(%Q{++++++++++++++++++++++++++++++++++
|
19
|
+
sn type id
|
20
|
+
++++++++++++++++++++++++++++++++++
|
21
|
+
1 trunk 100
|
22
|
+
2 access 200
|
23
|
+
++++++++++++++++++++++++++++++++++
|
24
|
+
total 2
|
25
|
+
})
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
data/spec/cmd_matcher_spec.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require "rubygems"
|
5
5
|
require 'cmd_matcher'
|
6
|
-
|
6
|
+
$DEBUG = true
|
7
7
|
describe "CmdMatcher" do
|
8
8
|
it "should desc" do
|
9
9
|
cm = CmdMatcher.new
|
@@ -26,5 +26,16 @@ total 2
|
|
26
26
|
cm.matches[:type][0].should == "trunk"
|
27
27
|
cm.matches[:type][1].should == "access"
|
28
28
|
end
|
29
|
+
|
30
|
+
it "should no newline" do
|
31
|
+
cm = CmdMatcher.new
|
32
|
+
cm.has_one /hello (\w+), and/, :no_newline => true, :names => [:name]
|
33
|
+
cm.has_many /(\w+)/, :names => [:other], :no_newline => true
|
34
|
+
|
35
|
+
cm.match(%Q{hello world, and a b c d e}).should == true
|
36
|
+
cm.matches[:name].should == "world"
|
37
|
+
cm.matches[:other][0].should == "a"
|
38
|
+
cm.matches[:other][1].should == "b"
|
39
|
+
end
|
29
40
|
end
|
30
41
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmd_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- YuZhenpin
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- README
|
33
33
|
- Rakefile
|
34
34
|
- lib/cmd_matcher.rb
|
35
|
+
- spec/cmd_matcher_benchmark.rb
|
35
36
|
- spec/cmd_matcher_spec.rb
|
36
37
|
- spec/line_matcher_spec.rb
|
37
38
|
homepage:
|