cmd_matcher 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == cmd_matcher
2
+
3
+ Put appropriate LICENSE for your project here.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == cmd_matcher
2
+
3
+ You should document your project here.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+ require 'spec/rake/spectask'
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = 'cmd_matcher'
16
+ s.version = '0.0.1'
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ['README', 'LICENSE']
19
+ s.summary = 'command line matcher'
20
+ s.description = s.summary
21
+ s.author = ''
22
+ s.email = ''
23
+ # s.executables = ['your_executable_here']
24
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
+ s.require_path = "lib"
26
+ s.bindir = "bin"
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |p|
30
+ p.gem_spec = spec
31
+ p.need_tar = true
32
+ p.need_zip = true
33
+ end
34
+
35
+ Rake::RDocTask.new do |rdoc|
36
+ files =['README', 'LICENSE', 'lib/**/*.rb']
37
+ rdoc.rdoc_files.add(files)
38
+ rdoc.main = "README" # page to start on
39
+ rdoc.title = "cmd_matcher Docs"
40
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
41
+ rdoc.options << '--line-numbers'
42
+ end
43
+
44
+ Rake::TestTask.new do |t|
45
+ t.test_files = FileList['test/**/*.rb']
46
+ end
47
+
48
+ Spec::Rake::SpecTask.new do |t|
49
+ t.spec_files = FileList['spec/**/*.rb']
50
+ t.libs << Dir["lib"]
51
+ end
@@ -0,0 +1,146 @@
1
+ class CmdMatcher
2
+ attr_reader :matches
3
+
4
+ def initialize
5
+ @matchers = []
6
+ @matches = {}
7
+ end
8
+
9
+ def has(line, opt = {})
10
+ @matchers << [line, opt]
11
+ end
12
+
13
+ def has_one(line, opt = {})
14
+ arg = opt.clone
15
+ arg[:at_most] = nil
16
+ arg[:at_least] = nil
17
+ has(line, arg)
18
+ end
19
+
20
+ def has_many(line, opt = {})
21
+ arg = opt.clone
22
+ arg[:at_most] = nil
23
+ arg[:at_least] = 1
24
+ has(line, arg)
25
+ end
26
+
27
+ def match(prompts)
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
+ puts "times=#{times}"
44
+ if opt[:at_least] && opt[:at_least] > times
45
+ return false
46
+ end
47
+
48
+ if opt[:at_most] && opt[:at_most] < times
49
+ return false
50
+ end
51
+
52
+ break
53
+ end
54
+ end
55
+ end
56
+ }
57
+ return true
58
+ end
59
+
60
+ def line_match(lines, expect, opt = {})
61
+ if expect.is_a?(Regexp)
62
+ line_regex(lines, expect, opt)
63
+ else
64
+ line_equal(lines, expect, opt)
65
+ end
66
+ end
67
+
68
+ def line_regex(lines, expect, opt = {})
69
+ line = lines[0]
70
+ puts "line=#{line}"
71
+ line_matcher = LineMatcher.new(expect, opt)
72
+ if line_matcher.match(line)
73
+ line_matcher.append_to(@matches)
74
+ return true
75
+ else
76
+ puts "unequal: <expect>#{expect},\n <but got>#{line}"
77
+ return false
78
+ end
79
+ end
80
+
81
+ def line_equal(lines, expect, opt = {})
82
+ line = lines[0]
83
+ puts "line=#{line}"
84
+ line_equaler = LineEqualer.new(expect, opt)
85
+ if line_equaler.match(line)
86
+ return true
87
+ else
88
+ puts "unequal: <expect>#{expect},\n <but got>#{line}"
89
+ return false
90
+ end
91
+ end
92
+
93
+ class LineMatcher
94
+ attr_reader :matches
95
+ def initialize(matcher, opt = {})
96
+ @names = opt[:names] || []
97
+ @matcher = matcher
98
+ @matches = nil
99
+ if opt[:at_most] && opt[:at_most] > 1 or opt[:at_least]
100
+ @has_many = true
101
+ else
102
+ @has_many = false
103
+ end
104
+ end
105
+
106
+ def match(line)
107
+ m = line.match(@matcher)
108
+ return false unless m
109
+ @matches = {}
110
+ @names.each_with_index {|name, index|
111
+ if @has_many
112
+ @matches[name] = [m[index + 1]]
113
+ else
114
+ @matches[name] = m[index + 1]
115
+ end
116
+ }
117
+ return true
118
+ end
119
+
120
+ def append_to(matches)
121
+ @names.each {|name|
122
+ if matches[name].is_a?(Array)
123
+ matches[name] += @matches[name]
124
+ else
125
+ matches[name] = @matches[name]
126
+ end
127
+ }
128
+ end
129
+ end
130
+
131
+ class LineEqualer < LineMatcher
132
+ def match(line)
133
+ @matcher.strip == line.strip
134
+ end
135
+ end
136
+ end
137
+
138
+ if __FILE__ == $0
139
+ cm = CmdMatcher.new
140
+ ao.reply :has_one => "++++++++++++++++++++++++++++++++++"
141
+ ao.reply :has_one => "sn type id"
142
+ ao.reply :has_one => "++++++++++++++++++++++++++++++++++"
143
+ ao.reply :has_many => /(\d)\s*(trunk|access)(\d+)/, :names => [:sn, :type, :id]
144
+ ao.reply :has => "++++++++++++++++++++++++++++++++++", :at_least => 1
145
+ ao.reply :has => "++++++++++++++++++++++++++++++++++", :at_most => 1
146
+ 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
+
@@ -0,0 +1,50 @@
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 "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"
12
+ end
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
48
+ end
49
+ end
50
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmd_matcher
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - ""
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-30 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: command line matcher
22
+ email: ""
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - LICENSE
30
+ files:
31
+ - LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/cmd_matcher.rb
35
+ - spec/cmd_matcher_spec.rb
36
+ - spec/line_matcher_spec.rb
37
+ homepage:
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.22
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: command line matcher
70
+ test_files: []
71
+