cmd-matcher 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,3 +1,24 @@
1
- == cmd-matcher
1
+ == cmd_matcher
2
+ command line response to hash
3
+ usage:
2
4
 
3
- You should document your project here.
5
+ cm = CmdMatcher.new
6
+ cm.has_one "++++++++++++++++++++++++++++++++++"
7
+ cm.has_one "sn type id"
8
+ cm.has_one "++++++++++++++++++++++++++++++++++"
9
+ cm.has_many /(\d+)\s+(trunk|access)\s+(\d+)/, :names => [:sn, :type, :id]
10
+ cm.has "++++++++++++++++++++++++++++++++++", :at_most => 1
11
+ cm.has_one /total\s*(\d+)/, :names => [:total]
12
+
13
+ cm.match(%Q{++++++++++++++++++++++++++++++++++
14
+ sn type id
15
+ ++++++++++++++++++++++++++++++++++
16
+ 1 trunk 100
17
+ 2 access 200
18
+ ++++++++++++++++++++++++++++++++++
19
+ total 2
20
+ })
21
+ puts cm.matches[:total]
22
+ puts cm.matches[:type][0]
23
+ puts cm.matches[:type][1]
24
+
data/Rakefile CHANGED
@@ -13,13 +13,13 @@ require 'spec/rake/spectask'
13
13
 
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.name = 'cmd-matcher'
16
- s.version = '0.0.1'
16
+ s.version = '0.0.2'
17
17
  s.has_rdoc = true
18
18
  s.extra_rdoc_files = ['README', 'LICENSE']
19
- s.summary = 'Your summary here'
19
+ s.summary = 'command line response to hash'
20
20
  s.description = s.summary
21
- s.author = ''
22
- s.email = ''
21
+ s.author = 'YuZhenpin'
22
+ s.email = 'yuzhenpin@126.com'
23
23
  # s.executables = ['your_executable_here']
24
24
  s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
25
  s.require_path = "lib"
data/lib/cmd_matcher.rb CHANGED
@@ -1,26 +1,90 @@
1
1
  class CmdMatcher
2
+ attr_reader :matches
3
+
2
4
  def initialize
3
5
  @matchers = []
6
+ @matches = {}
4
7
  end
5
8
 
6
9
  def has(line, opt = {})
7
-
8
- end
9
-
10
- def head(line, opt = {})
11
-
10
+ @matchers << [line, opt]
12
11
  end
13
12
 
14
13
  def has_one(line, opt = {})
15
-
14
+ arg = opt.clone
15
+ arg[:at_most] = nil
16
+ arg[:at_least] = nil
17
+ has(line, arg)
16
18
  end
17
19
 
18
20
  def has_many(line, opt = {})
19
-
21
+ arg = opt.clone
22
+ arg[:at_most] = nil
23
+ arg[:at_least] = 1
24
+ has(line, arg)
20
25
  end
21
26
 
22
27
  def match(prompts)
23
-
28
+ lines = prompts.split("\n")
29
+ @matchers.each {|expect, opt|
30
+ if opt[:at_most] == nil && opt[:at_least] == nil
31
+ if line_match(lines, expect, opt)
32
+ lines.shift
33
+ else
34
+ return false
35
+ end
36
+ else
37
+ times = 0
38
+ while true
39
+ if line_match(lines, expect, opt)
40
+ lines.shift
41
+ times += 1
42
+ else
43
+ if opt[:at_least] && opt[:at_least] > times
44
+ return false
45
+ end
46
+
47
+ if opt[:at_most] && opt[:at_most] < times
48
+ return false
49
+ end
50
+
51
+ break
52
+ end
53
+ end
54
+ end
55
+ }
56
+ return true
57
+ end
58
+
59
+ def line_match(lines, expect, opt = {})
60
+ if expect.is_a?(Regexp)
61
+ line_regex(lines, expect, opt)
62
+ else
63
+ line_equal(lines, expect, opt)
64
+ end
65
+ end
66
+
67
+ def line_regex(lines, expect, opt = {})
68
+ line = lines[0]
69
+ line_matcher = LineMatcher.new(expect, opt)
70
+ if line_matcher.match(line)
71
+ line_matcher.append_to(@matches)
72
+ return true
73
+ else
74
+ puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
75
+ return false
76
+ end
77
+ end
78
+
79
+ def line_equal(lines, expect, opt = {})
80
+ line = lines[0]
81
+ line_equaler = LineEqualer.new(expect, opt)
82
+ if line_equaler.match(line)
83
+ return true
84
+ else
85
+ puts "<expect>#{expect},\n <but got>#{line}" if $DEBUG
86
+ return false
87
+ end
24
88
  end
25
89
 
26
90
  class LineMatcher
@@ -29,6 +93,11 @@ class CmdMatcher
29
93
  @names = opt[:names] || []
30
94
  @matcher = matcher
31
95
  @matches = nil
96
+ if opt[:at_most] && opt[:at_most] > 1 or opt[:at_least]
97
+ @has_many = true
98
+ else
99
+ @has_many = false
100
+ end
32
101
  end
33
102
 
34
103
  def match(line)
@@ -36,10 +105,24 @@ class CmdMatcher
36
105
  return false unless m
37
106
  @matches = {}
38
107
  @names.each_with_index {|name, index|
39
- @matches[name] = m[index + 1]
108
+ if @has_many
109
+ @matches[name] = [m[index + 1]]
110
+ else
111
+ @matches[name] = m[index + 1]
112
+ end
40
113
  }
41
114
  return true
42
115
  end
116
+
117
+ def append_to(matches)
118
+ @names.each {|name|
119
+ if matches[name].is_a?(Array)
120
+ matches[name] += @matches[name]
121
+ else
122
+ matches[name] = @matches[name]
123
+ end
124
+ }
125
+ end
43
126
  end
44
127
 
45
128
  class LineEqualer < LineMatcher
@@ -47,12 +130,4 @@ class CmdMatcher
47
130
  @matcher.strip == line.strip
48
131
  end
49
132
  end
50
- end
51
-
52
- cm = CmdMatcher.new
53
- ao.reply :head => "++++++++++++++++++++++++++++++++++"
54
- ao.reply :has_one => "sn type id"
55
- ao.reply :has_one => "++++++++++++++++++++++++++++++++++"
56
- ao.reply :has_many => /(\d)\s*(trunk|access)(\d+)/, :name => [:sn, :type, :id]
57
- ao.reply :has => "++++++++++++++++++++++++++++++++++", :at_last => 1
58
- ao.reply :has => "++++++++++++++++++++++++++++++++++", :at_most => 1
133
+ end
@@ -0,0 +1,30 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ require "rubygems"
5
+ require 'cmd-matcher'
6
+
7
+ describe "CmdMatcher" do
8
+ it "should desc" do
9
+ cm = CmdMatcher.new
10
+ cm.has_one "++++++++++++++++++++++++++++++++++"
11
+ cm.has_one "sn type id"
12
+ cm.has_one "++++++++++++++++++++++++++++++++++"
13
+ cm.has_many /(\d+)\s+(trunk|access)\s+(\d+)/, :names => [:sn, :type, :id]
14
+ cm.has "++++++++++++++++++++++++++++++++++", :at_most => 1
15
+ cm.has_one /total\s*(\d+)/, :names => [:total]
16
+
17
+ cm.match(%Q{++++++++++++++++++++++++++++++++++
18
+ sn type id
19
+ ++++++++++++++++++++++++++++++++++
20
+ 1 trunk 100
21
+ 2 access 200
22
+ ++++++++++++++++++++++++++++++++++
23
+ total 2
24
+ }).should == true
25
+ cm.matches[:total].should == "2"
26
+ cm.matches[:type][0].should == "trunk"
27
+ cm.matches[:type][1].should == "access"
28
+ end
29
+ end
30
+
@@ -1,17 +1,50 @@
1
1
  # To change this template, choose Tools | Templates
2
2
  # and open the template in the editor.
3
3
 
4
- $LOAD_PATH << 'I:\RailsInstaller\Ruby1.8.7\lib\ruby\gems\1.8\gems\cmd-matcher\lib'
4
+ require "rubygems"
5
+ require 'cmd-matcher'
5
6
 
6
- require 'cmd_matcher'
7
-
8
- describe LineMatcher do
9
- before(:each) do
10
- @line_matcher = LineMatcher.new
7
+ describe "LineMatcher" do
8
+ it "should match one" do
9
+ line_matcher = CmdMatcher::LineMatcher.new(/vlan\s*(\d+)/, :names => [:vlan])
10
+ line_matcher.match("vlan 10").should == true
11
+ line_matcher.matches[:vlan].should == "10"
11
12
  end
12
-
13
- it "should desc" do
14
- # TODO
13
+
14
+ it "should match at_most 1" do
15
+ line_matcher = CmdMatcher::LineMatcher.new(
16
+ /vlan\s*(\d+)/,
17
+ :names => [:vlan],
18
+ :at_most => 1
19
+ )
20
+ line_matcher.match("vlan 10").should == true
21
+ line_matcher.matches[:vlan].should == "10"
22
+ end
23
+
24
+ it "should match at_most 2" do
25
+ line_matcher = CmdMatcher::LineMatcher.new(
26
+ /vlan\s*(\d+)/,
27
+ :names => [:vlan],
28
+ :at_most => 2
29
+ )
30
+ line_matcher.match("vlan 10").should == true
31
+ line_matcher.matches[:vlan].should == ["10"]
32
+ end
33
+
34
+ it "should match at_least" do
35
+ line_matcher = CmdMatcher::LineMatcher.new(
36
+ /vlan\s*(\d+)/,
37
+ :names => [:vlan],
38
+ :at_least => 1
39
+ )
40
+ line_matcher.match("vlan 10").should == true
41
+ line_matcher.matches[:vlan].should == ["10"]
42
+ end
43
+
44
+ it "should equal" do
45
+ line_matcher = CmdMatcher::LineEqualer.new("hello world")
46
+ line_matcher.match("hello world").should == true
47
+ line_matcher.matches == nil
15
48
  end
16
49
  end
17
50
 
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmd-matcher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
- - ""
13
+ - YuZhenpin
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
@@ -18,8 +18,8 @@ cert_chain: []
18
18
  date: 2012-04-30 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
- description: Your summary here
22
- email: ""
21
+ description: command line response to hash
22
+ email: yuzhenpin@126.com
23
23
  executables: []
24
24
 
25
25
  extensions: []
@@ -32,6 +32,7 @@ files:
32
32
  - README
33
33
  - Rakefile
34
34
  - lib/cmd_matcher.rb
35
+ - spec/cmd_matcher_spec.rb
35
36
  - spec/line_matcher_spec.rb
36
37
  homepage:
37
38
  licenses: []
@@ -65,6 +66,6 @@ rubyforge_project:
65
66
  rubygems_version: 1.8.22
66
67
  signing_key:
67
68
  specification_version: 3
68
- summary: Your summary here
69
+ summary: command line response to hash
69
70
  test_files: []
70
71