snarl-snp 0.1.1 → 0.2.0

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.
@@ -1,53 +1,197 @@
1
- # -*- coding:utf-8 -*-
2
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
2
 
4
- describe "Config" do
3
+ describe "Snarl::SNP::Config" do
4
+
5
+ before :all do
6
+ @host = '0.0.0.0'
7
+ @port = 9876
8
+ @app = 'app'
9
+ @class1 = 'class1'
10
+ @classtitle1 = @class1 + 'title'
11
+ @class2 = 'class2'
12
+ @class3 = 'class3'
13
+ @title = 'title'
14
+ @timeout = 9
15
+ @icon = 'icon.jpg'
16
+ @text = 'text'
17
+
18
+ # just only for bypassing emacs ruby-mode indent bug, class is "class"
19
+ @yaml = <<YAML
20
+ host : #{@host}
21
+ port : #{@port}
22
+ app : #{@app}
23
+ "class" :
24
+ - [#{@class1}, #{@classtitle1}]
25
+ - [#{@class2}]
26
+ - #{@class3}
27
+ notification :
28
+ title : #{@title}
29
+ text : #{@text}
30
+ timeout : #{@timeout}
31
+ icon : #{@icon}
32
+ "class" : #{@class1}
33
+ unregister : false
34
+ logfile : $stdout
35
+ loglevel : 0
36
+ YAML
37
+ end
5
38
 
6
39
  before do
7
- Snarl::SNP::Config.reset
8
40
  end
9
41
 
10
- describe ".host=" do
11
- it "set host into String" do
12
- Snarl::SNP::Config.host = 1234
13
- Snarl::SNP::Config.host.should eql('1234')
42
+ describe ".initialize" do
43
+ before :all do
44
+ @backup_host = ENV['SNARL_HOST']
45
+ @backup_port = ENV['SNARL_PORT']
14
46
  end
15
- it "set nil, returns default" do
16
- config = Snarl::SNP::Config.new
17
- Snarl::SNP::Config.host = nil
18
- Snarl::SNP::Config.host.should eql(Snarl::SNP::Config::DEFAULT_HOST)
47
+ after :all do
48
+ ENV['SNARL_HOST'] = @backup_host
49
+ ENV['SNARL_PORT'] = @backup_port
50
+ end
51
+
52
+ it "When ENV['SNARL_PORT'] is nil, default host is 127.0.0.1" do
53
+ ENV['SNARL_HOST'] = nil
54
+ Snarl::SNP::Config.new['host'].should eql('127.0.0.1')
55
+ end
56
+ it "When ENV['SNARL_PORT'] is set, default host is SNARL_HOST" do
57
+ ENV['SNARL_HOST'] = '192.168.0.2'
58
+ Snarl::SNP::Config.new['host'].should eql('192.168.0.2')
59
+ end
60
+
61
+ it "When ENV['SNARL_PORT'] is nil, default port is 9887.to_i" do
62
+ ENV['SNARL_PORT'] = nil
63
+ Snarl::SNP::Config.new['port'].should eql(9887)
64
+ end
65
+ it "When ENV['SNARL_PORT'] is set, default port is SNARL_PORT.to_i" do
66
+ ENV['SNARL_PORT'] = '12345'
67
+ Snarl::SNP::Config.new['port'].should eql(12345)
19
68
  end
20
69
  end
21
70
 
22
- describe ".port=" do
23
- it "set port into Integer" do
24
- Snarl::SNP::Config.port = '9887'
25
- Snarl::SNP::Config.port.should eql(9887)
71
+ describe "#[]=" do
72
+ it "config['app'] = 'appname' works" do
73
+ appname = 'appname'
74
+ config = Snarl::SNP::Config.new
75
+ config['app'] = appname
76
+ config.instance_variable_get(:@config)['app'].should eql(appname)
26
77
  end
27
- it "set nil, returns default" do
28
- Snarl::SNP::Config.port = nil
29
- Snarl::SNP::Config.port.should eql(Snarl::SNP::Config::DEFAULT_PORT)
78
+ it "Symbol key converts to String. config[':app'] = 'appname' works" do
79
+ appname = 'appname'
80
+ config = Snarl::SNP::Config.new
81
+ config[:app] = appname
82
+ config.instance_variable_get(:@config)['app'].should eql(appname)
30
83
  end
31
84
  end
32
85
 
33
- # describe ".load_snp_config" do
34
-
35
- # before do
36
- # @yaml = Tempfile.new('rubysnarlsnpconfigspec')
37
- # end
38
- # after do
39
- # @yaml.close
40
- # end
41
-
42
- # it "read yaml from argument path" do
43
- # host = '192.168.0.2'
44
- # port = 9887
45
- # @yaml.write("host : #{host}\nport : #{port}")
46
- # @yaml.close
47
- # config = Snarl::SNP::Config.load_snp_config(@yaml.path)
48
- # config.host.should eql(host)
49
- # config.port.should eql(port) # YAML.load('9887') #=> Integer
50
- # end
51
- # end
86
+ describe "#[]" do
87
+ it "returns @config[k]" do
88
+ appname = 'appname'
89
+ config = Snarl::SNP::Config.new
90
+ config.instance_variable_get(:@config)['app'] = appname
91
+ config['app'].should eql(appname)
92
+ end
93
+ end
52
94
 
95
+ describe "Config" do
96
+
97
+ describe ".store" do
98
+ it "normalize pair and store to hash" do
99
+ expected = {
100
+ "host" => "0.0.0.0",
101
+ "port" => 9876,
102
+ "app" => "app",
103
+ "class" => [["class1", "class1title"], ["class2", nil], ["class3", nil]],
104
+ "notification" => {
105
+ "title" => "title",
106
+ "text" => "text",
107
+ "timeout" => 9,
108
+ "icon" => "icon.jpg",
109
+ "class" => "class1",
110
+ },
111
+ "unregister" => false,
112
+ "logfile" => $stdout,
113
+ "loglevel" => 0
114
+ }
115
+ actual = {}
116
+ YAML.load(@yaml).each do |k, v|
117
+ Snarl::SNP::Config::Normalize.store(k, v, actual)
118
+ end
119
+ actual.should eql(expected)
120
+ end
121
+ end
122
+
123
+ describe "#extract_classes" do
124
+ def extract_classes(v)
125
+ Snarl::SNP::Config::Normalize.new('dum','my').__send__(:extract_classes, v)
126
+ end
127
+
128
+ it "if [[class1, title1]], return as is" do
129
+ actual = [['class1', 'title1']]
130
+ expected = actual
131
+ extract_classes(actual).should eql(expected)
132
+ end
133
+ it "if [classonly], returns [[classonly, nil]]" do
134
+ actual = ['class1']
135
+ expected = [['class1', nil]]
136
+ extract_classes(actual).should eql(expected)
137
+ end
138
+ it "if is String, returns [[string, nil]]" do
139
+ actual = 'class1'
140
+ expected = [['class1', nil]]
141
+ extract_classes(actual).should eql(expected)
142
+ end
143
+ it "if is nil, returns nil" do
144
+ actual = nil
145
+ expected = nil
146
+ extract_classes(actual).should eql(expected)
147
+ end
148
+ end
149
+
150
+ describe "#extract_loglevel" do
151
+ def extract_loglevel(v)
152
+ Snarl::SNP::Config::Normalize.new('dum','my').__send__(:extract_loglevel, v)
153
+ end
154
+
155
+ it "if integer string, returns s.to_i" do
156
+ extract_loglevel('1').should eql(1)
157
+ end
158
+ it "if yaml_undef, returns 0" do
159
+ extract_loglevel(nil).should eql(0)
160
+ extract_loglevel('').should eql(0)
161
+ end
162
+ it "if .to_s matches /\A(debug|info|warn|error|fatal)\Z/i, returns 0-4" do
163
+ extract_loglevel('debug').should eql(0)
164
+ extract_loglevel('INFO').should eql(1)
165
+ extract_loglevel('Warn').should eql(2)
166
+ extract_loglevel(:error).should eql(3)
167
+ extract_loglevel(:FATAL).should eql(4)
168
+ end
169
+ it "if unknown str, returns 0" do
170
+ extract_loglevel('foo').should eql(0)
171
+ end
172
+ end
173
+
174
+ describe "#extract_logfile" do
175
+ def extract_logfile(v)
176
+ Snarl::SNP::Config::Normalize.new('dum','my').__send__(:extract_logfile, v)
177
+ end
178
+
179
+ it "if '$stdout' or '$stderr', returns its IO object" do
180
+ extract_logfile('$stdout').should eql($stdout)
181
+ extract_logfile('$stderr').should eql($stderr)
182
+ end
183
+ it "returns it as is unless $stdout or $stderr" do
184
+ actual = expected = 'logfile'
185
+ extract_logfile(actual).should eql(expected)
186
+ end
187
+ end
188
+
189
+ describe "#normalize" do
190
+ def normalize(k, v)
191
+ Snarl::SNP::Config::Normalize.new(k, v).normalize
192
+ end
193
+ # ah...
194
+ # pending('I do not want to write now')
195
+ end
196
+ end
53
197
  end
@@ -0,0 +1,295 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Snarl::SNP" do
4
+
5
+ before :all do
6
+ @host = SNARL_HOST # from spec_helper
7
+ @port = SNARL_PORT # from spec_helper
8
+ @app = Snarl::SNP::DEFAULT_TITLE
9
+ @class = '1'
10
+ end
11
+
12
+ describe ".open{|snp| ...}" do
13
+ it "supplies Snarl::SNP object block" do
14
+ Snarl::SNP.open(@host, @port) do |snp|
15
+ snp.notification('hello Snarl::SNP test!', 10)
16
+ end
17
+ end
18
+ end
19
+
20
+ def snp_open(&block)
21
+ snp = Snarl::SNP.new(@host, @port)
22
+ block.call(snp)
23
+ end
24
+
25
+ it "full SNP popup procedure goes well" do
26
+ lambda{
27
+ snp_open do |snp|
28
+ snp.register(@app)
29
+ snp.add_class(@class, 'classtitle')
30
+ res = snp.notification('tit', 'tex(no icon popups)', 'ico', 9, @class)
31
+ snp.unregister(@app)
32
+ end
33
+ }.should_not raise_error
34
+ end
35
+
36
+ describe "#register" do
37
+ it "twice raises error SNP_ERROR_ALREADY_REGISTERED" do
38
+ snp_open do |snp|
39
+ snp.register(@app)
40
+ snp.verbose = true
41
+ lambda{snp.register(@app)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
42
+ snp.unregister(@app)
43
+ end
44
+ end
45
+ it "empty app raises error SNP_ERROR_BAD_PACKET" do
46
+ snp_open do |snp|
47
+ snp.verbose = true
48
+ lambda{snp.register('')}.should raise_error(Snarl::SNP::Error::SNP_ERROR_BAD_PACKET)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "#add_class" do
54
+ it "empty classtitle raises no error" do
55
+ snp_open do |snp|
56
+ snp.register(@app)
57
+ snp.verbose = true
58
+ lambda{snp.add_class(@class)}.should_not raise_error(Snarl::SNP::Error)
59
+ snp.unregister(@app)
60
+ end
61
+ end
62
+ it "add same class raises error SNP_ERROR_CLASS_ALREADY_EXISTS" do
63
+ snp_open do |snp|
64
+ snp.register(@app)
65
+ snp.add_class(@class)
66
+ snp.verbose = true
67
+ lambda{snp.add_class(@class)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS)
68
+ snp.unregister(@app)
69
+ end
70
+ end
71
+ it "empty class raises error SNP_ERROR_BAD_PACKET" do
72
+ snp_open do |snp|
73
+ snp.register(@app)
74
+ snp.verbose = true
75
+ lambda{snp.add_class('')}.should raise_error(Snarl::SNP::Error::SNP_ERROR_BAD_PACKET)
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#notification" do
81
+ it "'anonymous-class' notification is not considered as a bad packet" do
82
+ snp_open do |snp|
83
+ lambda{
84
+ snp.register(@app)
85
+ snp.verbose = true
86
+ snp.notification(:text => 'anonymous class test', :timeout => '2')
87
+ }.should_not raise_error(Snarl::SNP::Error)
88
+ end
89
+ end
90
+ it "'anonymous-app' notification is not considered as a bad packet" do
91
+ snp_open do |snp|
92
+ lambda{
93
+ snp.verbose = true
94
+ snp.notification(:text => 'anonymous app test', :timeout => '2')
95
+ }.should_not raise_error(Snarl::SNP::Error)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#unregister" do
101
+ it "unregister before registering raises error SNP_ERROR_NOT_REGISTERED" do
102
+ snp_open do |snp|
103
+ snp.unregister(@app)
104
+ snp.verbose = true
105
+ lambda{snp.unregister(@app)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_NOT_REGISTERED)
106
+ end
107
+ end
108
+ it "empty app raises error SNP_ERROR_BAD_PACKET" do
109
+ snp_open do |snp|
110
+ snp.verbose = true
111
+ lambda{snp.unregister('')}.should raise_error(Snarl::SNP::Error::SNP_ERROR_BAD_PACKET)
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "#hello" do
117
+ it "returns Snarl release identifier" do
118
+ snp_open do |snp|
119
+ snp.hello.infomation.should match(/\ASnarl /)
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "#version" do
125
+ it "returns Snarl (inner) version" do
126
+ snp_open do |snp|
127
+ snp.version.infomation.should match(/[\d\.]+/)
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "#request" do
133
+ it "misspelling action raises SNP_ERROR_BAD_PACKET" do
134
+ snp_open do |snp|
135
+ lambda{
136
+ snp.request("type=SNP#?version=1.0#?action=notification2#?app=#{@app}#?title=err!#?text=this never popup#?timeout=10\r\n")
137
+ }.should raise_error(Snarl::SNP::Error::SNP_ERROR_BAD_PACKET)
138
+ end
139
+ end
140
+ end
141
+
142
+ # FIXME: I have no idea to let class methods run under #stub!
143
+
144
+ describe ".ping" do
145
+ it "ping" do
146
+ expected = "type=SNP#?version=1.0#?action=notification#?title=Ruby-Snarl#?text=Ruby Snarl-SNP Ping Message#?timeout=10#?icon=3\r\n"
147
+ Snarl::SNP.ping(@host).request_str.should eql(expected)
148
+ end
149
+ end
150
+
151
+ describe ".show_message" do
152
+ it "show_message(host, port, title, text, timeout, icon) shows popup message" do
153
+ expected = "type=SNP#?version=1.0#?action=notification#?title=snp_spec#?text=test mesage#?timeout=2#?icon=icon\r\n"
154
+ Snarl::SNP.show_message(@host, @port, 'snp_spec', 'test mesage', 2, 'icon').request_str.should eql(expected)
155
+ end
156
+ it "if port Integer is omitted, (host, title, text, timeout, icon) is treated as (host, nil, title, text, timeout, icon)" do
157
+ expected = "type=SNP#?version=1.0#?action=notification#?title=snp_spec#?text=test mesage#?timeout=2#?icon=icon\r\n"
158
+ Snarl::SNP.show_message(@host, 'snp_spec', 'test mesage', 2, 'icon').request_str.should eql(expected)
159
+ end
160
+ end
161
+
162
+ # ============================================
163
+ # snp_procedure.rb
164
+
165
+ describe ".load" do
166
+ it "SNP.load(yaml){...} loads yaml and executes as SNP" do
167
+ yaml = <<EOY # for bypassing emacs ruby-mode indent bug, 'class : ...' is '"class" : ...'
168
+ host : #{@host}
169
+ port : #{@port}
170
+ app : Ruby-Snarl
171
+ "class" : class1
172
+ EOY
173
+ lambda{
174
+ Snarl::SNP.load(yaml) do |snp|
175
+ snp.notification('yaml load test')
176
+ end
177
+ }.should_not raise_error
178
+ end
179
+ it "SNP.load(yaml_with_notify) loads yaml and executes as SNP automatically" do
180
+ yaml = <<EOY # for bypassing emacs ruby-mode indent bug, 'class : ...' is '"class" : ...'
181
+ host : #{@host}
182
+ port : #{@port}
183
+ app : Ruby-Snarl
184
+ "class" : class1
185
+ title : yaml test
186
+ text : hello,yaml!
187
+ EOY
188
+ lambda{
189
+ Snarl::SNP.load(yaml) do |snp|
190
+ snp.notification('yaml load test')
191
+ end
192
+ }.should_not raise_error
193
+ end
194
+ it "SNP.load(yaml, Logger.new){...} puts SNPProcedure log to Logger" do
195
+ sio = StringIO.new
196
+ logger = Logger.new(sio)
197
+ yaml = <<EOY
198
+ host : #{@host}
199
+ port : #{@port}
200
+ app : Ruby-Snarl
201
+ EOY
202
+ expected = Regexp.quote('register: "type=SNP#?version=1.0#?action=register#?app=Ruby-Snarl')
203
+ Snarl::SNP.load(yaml, logger) do |snp|
204
+ end
205
+ sio.rewind
206
+ sio.read.should match(expected)
207
+ end
208
+ end
209
+
210
+ describe "#set_logger" do
211
+ before :all do
212
+ @yaml = <<EOY
213
+ host : #{@host}
214
+ port : #{@port}
215
+ app : Ruby-Snarl
216
+ EOY
217
+ end
218
+
219
+ before do
220
+ @logpath = Tempfile.new('snarlsnprealconnectionspecrb').path
221
+ end
222
+
223
+ it "set logger_obj as snp['logger'] when load(yaml, logger_obj)" do
224
+ logger = Logger.new(@logpath)
225
+ Snarl::SNP.load(@yaml, logger) do |snp|
226
+ snp.logger.should eql(logger)
227
+ end
228
+ end
229
+
230
+ it "set Logger.new(string) as snp['logger'] when load(yaml, string)" do
231
+ logger = @logpath
232
+ expected = Regexp.quote('register: "type=SNP#?version=1.0#?action=register#?app=Ruby-Snarl')
233
+ Snarl::SNP.load(@yaml, logger) do |snp|
234
+ end
235
+ File.open(logger){|f| f.read}.should match(expected)
236
+ end
237
+
238
+ it "set Logger.new(logpath) as snp['logger'] when load(\"logfile : logpath\")" do
239
+ yaml = "#{@yaml}\nlogfile : #{@logpath}"
240
+ expected = Regexp.quote('register: "type=SNP#?version=1.0#?action=register#?app=Ruby-Snarl')
241
+ Snarl::SNP.load(yaml) do |snp|
242
+ end
243
+ File.open(@logpath){|f| f.read}.should match(expected)
244
+ end
245
+
246
+ it "set lvl to Logger.new(file_y).level when load(\"logfile : file_y\\nloglevel : lvl\")" do
247
+ loglevel = 4
248
+ yaml = "#{@yaml}\nlogfile : #{@logpath}\nloglevel : #{loglevel}"
249
+ Snarl::SNP.load(yaml) do |snp|
250
+ snp.logger.level.should eql(loglevel)
251
+ end
252
+ end
253
+
254
+ it "does not set lvl to load_arg_logger.level when load(\"loglevel : lvl\", load_arg_logger)" do
255
+ loglevel = 4
256
+ logger = @logpath
257
+ yaml = "#{@yaml}\nloglevel : #{loglevel}"
258
+ Snarl::SNP.load(yaml, logger) do |snp|
259
+ snp.logger.level.should_not eql(loglevel)
260
+ snp.logger.level.should eql(0)
261
+ end
262
+ end
263
+ end
264
+
265
+ # =============================================
266
+ # bin/snarl_snp.rb
267
+ if defined?(TEST_SNARL_SNP_BIN_SPEC) then
268
+ class ::SNPBin
269
+ remove_const :"BINCMD_OPTION"
270
+ end
271
+ else
272
+ TEST_SNARL_SNP_BIN_SPEC=true
273
+ end
274
+ load File.expand_path(File.dirname(__FILE__) + '/../../bin/snarl_snp')
275
+
276
+ describe "SNPBin" do
277
+
278
+ before :all do
279
+ @argv_bak = ARGV.dup
280
+ end
281
+ after :all do
282
+ ARGV.replace(@argv_bak)
283
+ end
284
+
285
+ describe "run" do
286
+ it "#exec_snplib" do
287
+ arg = ['-H', SNARL_HOST, '-p', SNARL_PORT.to_s, '-a', 'Ruby-Snarl', '-c', 'class', '-m', 'exec text']
288
+ ARGV.replace([__FILE__] + arg)
289
+ lambda{
290
+ SNPBin.new.run
291
+ }.should_not raise_error
292
+ end
293
+ end
294
+ end
295
+ end