snarl-snp 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,54 @@
1
- # -*- coding:utf-8 -*-
2
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
2
 
4
3
  describe "Request" do
5
4
 
6
- describe "normalize_action" do
7
- def normalize_action(action)
8
- Snarl::SNP::Request.new.__send__(:normalize_action, action)
5
+ describe "#normalize_command" do
6
+ def normalize_cmdkey(command)
7
+ Snarl::SNP::Request.new.__send__(:normalize_cmdkey, command)
8
+ end
9
+ it "returns downcased string" do
10
+ normalize_cmdkey('action').should eql('action')
11
+ normalize_cmdkey('Action').should eql('action')
12
+ normalize_cmdkey(:action).should eql('action')
13
+ end
14
+ end
15
+
16
+ describe "#normalize_value" do
17
+ def normalize_value(value)
18
+ Snarl::SNP::Request.new.__send__(:normalize_value, value)
19
+ end
20
+ it 'converts one "\r\n" to one "\n"' do
21
+ normalize_value("1\r\n2\r\n").should eql("1\n2\n")
22
+ end
23
+ it 'converts one "\r" to one "\n"' do
24
+ normalize_value("1\r2\r").should eql("1\n2\n")
25
+ end
26
+ it 'never change multiple "\n\n\n\n\n"' do
27
+ many_newline = "1\n\n\n\n\n2"
28
+ normalize_value(many_newline).should eql(many_newline)
29
+ end
30
+ end
31
+
32
+ describe "#normalize_action_value" do
33
+ def normalize_action_value(cmdhash)
34
+ Snarl::SNP::Request.new.__send__(:normalize_action_value, cmdhash)
9
35
  end
10
36
  it "returns action String, add-class is add_class" do
11
- normalize_action('register').should eql('register')
12
- normalize_action('add-class').should eql('add_class')
37
+ normalize_action_value('register').should eql('register')
38
+ normalize_action_value(:register).should eql('register')
39
+ normalize_action_value('Register').should eql('register')
40
+ normalize_action_value('add-class').should eql('add_class')
41
+ normalize_action_value(:add_class).should eql('add_class')
13
42
  end
14
43
  end
15
44
 
45
+
16
46
  describe "#[]=" do
17
- it "set commands with []=" do
47
+ it "set command pair with normalization" do
18
48
  req = Snarl::SNP::Request.new
19
- req[:action] = 'register'
20
- req['app'] = 'Just Testing...'
21
- expected = {"app"=>"Just Testing...", :action =>"register"}
49
+ req[:action] = 'add-class'
50
+ req['Class'] = 'Just Testing...'
51
+ expected = {"class"=>"Just Testing...", 'action' =>"add_class"}
22
52
  req.commands.should eql(expected)
23
53
  end
24
54
  end
@@ -0,0 +1,226 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Snarl::SNP" do
4
+
5
+ before :all do
6
+ @host = '192.168.0.2'
7
+ @port = 9887
8
+ @app = Snarl::SNP::DEFAULT_TITLE
9
+ @class = '1'
10
+ end
11
+
12
+ def snp_open(&block)
13
+ snp = Snarl::SNP.new(@host, @port)
14
+ snp.stub!(:send).and_return('SNP/1.1/0/OK/1234/')
15
+ block.call(snp)
16
+ end
17
+
18
+ describe "#auto_register" do
19
+ it "do register when snp['app'] exists" do
20
+ expected = "type=SNP#?version=1.0#?action=register#?app=#{@app}\r\n"
21
+ snp_open do |snp|
22
+ snp['app'] = @app
23
+ snp.auto_register.request_str.should eql(expected)
24
+ end
25
+ end
26
+ it "do nothing when snp['app'] is nil/unset/empty" do
27
+ snp_open do |snp|
28
+ snp['app'] = nil
29
+ snp.auto_register.should be_nil
30
+ end
31
+ snp_open do |snp|
32
+ snp.instance_variable_get(:@config).config.delete('app')
33
+ snp.auto_register.should be_nil
34
+ end
35
+ snp_open do |snp|
36
+ snp['app'] = ""
37
+ snp.auto_register.should be_nil
38
+ end
39
+ end
40
+
41
+ end
42
+ describe "#auto_add_class" do
43
+ before :all do
44
+ classname1, classtitle1 = 'classname1', 'classtitle1'
45
+ classname2 = 'classname2'
46
+ @res1 = "type=SNP#?version=1.0#?action=add_class#?app=#{@app}#?class=#{classname1}#?title=#{classtitle1}\r\n"
47
+ @res2 = "type=SNP#?version=1.0#?action=add_class#?app=#{@app}#?class=#{classname2}\r\n"
48
+ @classes = [[classname1, classtitle1], [classname2, nil]]
49
+ end
50
+
51
+ it "do register when snp['class'] exists" do
52
+ expected = [@res1, @res2]
53
+ snp_open do |snp|
54
+ snp['app'] = @app
55
+ snp['class'] = @classes
56
+ snp.auto_add_class.map{|e| e.request_str}.should eql(expected)
57
+ end
58
+ end
59
+ it "do nothing when snp['class'] is nil/unset/empty" do
60
+ snp_open do |snp|
61
+ snp['app'] = @app
62
+ snp['class'] = nil
63
+ snp.auto_add_class.should be_nil
64
+ end
65
+ snp_open do |snp|
66
+ snp['app'] = @app
67
+ snp.instance_variable_get(:@config).config.delete('class')
68
+ snp.auto_add_class.should be_nil
69
+ end
70
+ snp_open do |snp|
71
+ snp['app'] = @app
72
+ snp['class'] = []
73
+ snp.auto_add_class.should be_nil
74
+ end
75
+ end
76
+ it "do nothing when snp['app'] is unset" do
77
+ snp_open do |snp|
78
+ snp.instance_variable_get(:@config).config.delete('app')
79
+ snp['class'] = @classes
80
+ snp.auto_add_class.should be_nil
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#auto_notification" do
86
+ it "snp['notification'] == {notify_arg_hash} with snp['app']" do
87
+ expected = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=title#?text=text#?timeout=10\r\n"
88
+ snp_open do |snp|
89
+ snp['app'] = @app
90
+ snp['notification'] = {'title' => 'title', 'text' => 'text'}
91
+ snp.auto_notification.request_str.should eql(expected)
92
+ end
93
+ end
94
+ it "snp['notification'] == {notify_arg_hash} without snp['app'] but hash has {'app' => app}" do
95
+ expected = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=title#?text=text#?timeout=10\r\n"
96
+ snp_open do |snp|
97
+ snp['notification'] = {'app' => @app, 'title' => 'title', 'text' => 'text'}
98
+ snp.auto_notification.request_str.should eql(expected)
99
+ end
100
+ end
101
+ it "snp['notification'] == [notify_arg_array]" do
102
+ expected = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=title#?text=text#?timeout=9\r\n"
103
+ snp_open do |snp|
104
+ snp['app'] = @app
105
+ snp['notification'] = ['title', 'text', 9]
106
+ snp.auto_notification.request_str.should eql(expected)
107
+ end
108
+ end
109
+ it "snp['notification'] == [{notify_arg_hash1}, {notify_arg_hash2}, ...]" do
110
+ arg1 = {'title' => 'title1', 'text' => 'text1', 'timeout' => 8}
111
+ arg2 = {'title' => 'title2', 'text' => 'text2', 'timeout' => 7}
112
+ notifications = [arg1, arg2]
113
+ res1 = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=title1#?text=text1#?timeout=8\r\n"
114
+ res2 = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=title2#?text=text2#?timeout=7\r\n"
115
+ expected = [res1, res2]
116
+ snp_open do |snp|
117
+ snp['app'] = @app
118
+ snp['notification'] = notifications
119
+ snp.auto_notification.map{|res| res.request_str}.should eql(expected)
120
+ end
121
+ end
122
+ it "snp['notification'] == nil, build notify_hash from app,text,title,..." do
123
+ expected = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?class=class#?title=title#?text=text#?timeout=6\r\n"
124
+ snp_open do |snp|
125
+ snp['app'] = @app
126
+ snp.instance_variable_get(:@config).config.delete('notification')
127
+ snp['title'] = 'title'
128
+ snp['text'] = 'text'
129
+ snp['timeout'] = '6'
130
+ snp['class'] = 'class'
131
+ snp.auto_notification.request_str.should eql(expected)
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "#auto_unregister" do
137
+ it "do unregister when snp['unregister'] and snp['app'] exist" do
138
+ expected = "type=SNP#?version=1.0#?action=unregister#?app=#{@app}\r\n"
139
+ snp_open do |snp|
140
+ snp['app'] = @app
141
+ snp['unregister'] = true
142
+ snp.auto_unregister.request_str.should eql(expected)
143
+ end
144
+ end
145
+ it "do nothing when snp['unregister'] is nil/unset/false" do
146
+ snp_open do |snp|
147
+ snp['app'] = @app
148
+ snp['unregister'] = nil
149
+ snp.auto_unregister.should be_nil
150
+ end
151
+ snp_open do |snp|
152
+ snp['app'] = @app
153
+ snp.instance_variable_get(:@config).config.delete('unregister')
154
+ snp.auto_unregister.should be_nil
155
+ end
156
+ snp_open do |snp|
157
+ snp['app'] = @app
158
+ snp['unregister'] = false
159
+ snp.auto_unregister.should be_nil
160
+ end
161
+ end
162
+ it "do nothing when snp['app'] is nil/unset/empty" do
163
+ snp_open do |snp|
164
+ snp['app'] = nil
165
+ snp['unregister'] = true
166
+ snp.auto_unregister.should be_nil
167
+ end
168
+ snp_open do |snp|
169
+ snp.instance_variable_get(:@config).config.delete('app')
170
+ snp['unregister'] = true
171
+ snp.auto_unregister.should be_nil
172
+ end
173
+ snp_open do |snp|
174
+ snp['app'] = ""
175
+ snp['unregister'] = true
176
+ snp.auto_unregister.should be_nil
177
+ end
178
+ end
179
+ end
180
+
181
+ describe "#autoexecute_with_config!" do
182
+ before do
183
+ @classname, @classtitle = 'classname', 'classtitle'
184
+ @classes = [[@classname, @classtitle]]
185
+ @title, @text = 'title', 'text'
186
+ @notifications = {'title' => @title, 'text' => @text}
187
+ end
188
+
189
+ it "do register and add_class and notification automatically" do
190
+ res_register = "type=SNP#?version=1.0#?action=register#?app=#{@app}\r\n"
191
+ res_add_class = "type=SNP#?version=1.0#?action=add_class#?app=#{@app}#?class=#{@classname}#?title=#{@classtitle}\r\n"
192
+ res_notification = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=#{@title}#?text=#{@text}#?timeout=10\r\n"
193
+ expected = [res_register, res_add_class, res_notification]
194
+ snp_open do |snp|
195
+ snp['app'] = @app
196
+ snp['add_class'] = @classes
197
+ snp['notification'] = @notifications
198
+ snp.autoexecute_with_config!.flatten.map{|res| res.request_str}.should eql(expected)
199
+ end
200
+ end
201
+ it "do nothing if no snp['app'] and snp['add_class'] and snp['notification']" do
202
+ snp_open do |snp|
203
+ snp.instance_variable_get(:@config).config.delete('app')
204
+ snp.instance_variable_get(:@config).config.delete('add_class')
205
+ snp.instance_variable_get(:@config).config.delete('notification')
206
+ snp.autoexecute_with_config!.flatten.should eql([nil, nil, nil])
207
+ end
208
+ end
209
+ end
210
+
211
+ describe "#apply_yaml" do
212
+ it "apply yamldata to snp" do
213
+ app, classname = 'app', 'class'
214
+ yamldata = "app : #{app}\nclass : #{classname}\n"
215
+ snp_open do |snp|
216
+ snp['app'].should be_nil
217
+ snp['class'].should be_nil
218
+ snp.apply_yaml(yamldata)
219
+ snp['app'].should eql(app)
220
+ snp['class'].should eql([[classname, nil]])
221
+ end
222
+ end
223
+ end
224
+
225
+ # describe "#set_logger" #=> real_connection_spec.rb
226
+ end
@@ -1,5 +1,5 @@
1
- # -*- coding:utf-8 -*-
2
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'stringio'
3
3
 
4
4
  describe "SNP" do
5
5
 
@@ -8,258 +8,281 @@ describe "SNP" do
8
8
  @port = 9887
9
9
  @app = Snarl::SNP::DEFAULT_TITLE
10
10
  @class = '1'
11
- Snarl::SNP::Config.reset
11
+ end
12
+
13
+ def snp_open(&block)
14
+ snp = Snarl::SNP.new(@host, @port)
15
+ snp.stub!(:send).and_return('SNP/1.1/0/OK/1234/')
16
+ block.call(snp)
12
17
  end
13
18
 
14
19
  describe "initialize" do
15
20
  it "new('192.168.0.2', 9887). (snarl_working_machine_host, snarl_port_9887)" do
16
21
  host, port, verbose = '192.168.0.2', 9887, true
17
22
  snp = Snarl::SNP.new(host, port, verbose)
18
- snp.instance_variable_get(:@host).should eql(host)
19
- snp.instance_variable_get(:@port).should eql(port)
23
+ snp['host'].should eql(host)
24
+ snp['port'].should eql(port)
20
25
  snp.verbose.should eql(verbose)
21
26
  end
22
27
  it "default host is 127.0.0.1, default port is 9887. when SNP.new()" do
28
+ bk_host = ENV['SNARL_HOST']
29
+ ENV['SNARL_HOST'] = nil
30
+ bk_port = ENV['SNARL_PORT']
31
+ ENV['SNARL_PORT'] = nil
23
32
  snp = Snarl::SNP.new
24
- snp.instance_variable_get(:@host).should eql(nil)
25
- snp.instance_variable_get(:@port).should eql(nil)
33
+ snp['host'].should eql('127.0.0.1')
34
+ snp['port'].should eql(9887)
26
35
  snp.verbose.should eql(false)
36
+ ENV['SNARL_HOST'] = bk_host
37
+ ENV['SNARL_PORT'] = bk_port
27
38
  end
28
39
  end
29
40
 
30
41
  describe "#request" do
31
- describe "send raw string" do
32
- before :all do
33
- @register ="type=SNP#?version=1.0#?action=register#?app=#{@app}\r\n"
34
- @add_class = "type=SNP#?version=1.0#?action=add_class#?app=#{@app}#?class=#{@class}\r\n"
35
- @notification = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=Raw!#?text=raw message!#?timeout=10\r\n"
36
- @unregister = "type=SNP#?version=1.0#?action=unregister#?app=#{@app}\r\n"
42
+
43
+ module ErrorPair
44
+ def self.casual_errors
45
+ {
46
+ 'SNP/1.1/202/Not Registered' => Snarl::SNP::Error::SNP_ERROR_NOT_REGISTERED,
47
+ 'SNP/1.1/203/Already Registered' => Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED,
48
+ 'SNP/1.1/204/Class Already Exists' => Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS,
49
+ 'SNP/1.1/201/Not Running' => Snarl::SNP::Error::SNP_ERROR_NOT_RUNNING,
50
+ 'SNP/1.1/101/Failed' => Snarl::SNP::Error::SNP_ERROR_FAILED,
51
+ }
37
52
  end
38
53
 
39
- it "raises error according to SNP#verbose" do
40
- Snarl::SNP.open(@host, @port) do |snp|
41
- lambda{
42
- snp.request(@register)
43
- snp.request(@register)
44
- }.should_not raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
45
- end
46
- Snarl::SNP.open(@host, @port) do |snp|
47
- snp.verbose = true
48
- lambda{
49
- snp.request(@register)
50
- snp.request(@register)
51
- }.should raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
52
- end
54
+ def self.fatal_errors
55
+ {
56
+ 'SNP/1.1/102/Unknown Connamd' => Snarl::SNP::Error::SNP_ERROR_UNKNOWN_COMMAND,
57
+ 'SNP/1.1/103/Timed Out' => Snarl::SNP::Error::SNP_ERROR_TIMED_OUT,
58
+ 'SNP/1.1/107/Bad packet' => Snarl::SNP::Error::SNP_ERROR_BAD_PACKET,
59
+ 'SNP/1.1/998/!Snarl::SNP\'s original error(unknown response code)' => Snarl::SNP::Error::RUBYSNARL_UNKNOWN_RESPONSE
60
+ }
53
61
  end
62
+ end
54
63
 
55
- it "sends command text" do
56
- lambda{
57
- Snarl::SNP.open(@host, @port) do |snp|
58
- begin
59
- snp.request(@register)
60
- rescue Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED
61
- end
62
- begin
63
- snp.request(@add_class)
64
- rescue Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS
65
- end
66
- snp.request(@notification)
67
- snp.request(@unregister)
68
- end
69
- }.should_not raise_error
64
+ before :all do
65
+ @register ="type=SNP#?version=1.0#?action=register#?app=#{@app}\r\n"
66
+ @add_class = "type=SNP#?version=1.0#?action=add_class#?app=#{@app}#?class=#{@class}\r\n"
67
+ @notification = "type=SNP#?version=1.0#?action=notification#?app=#{@app}#?title=Raw!#?text=text!#?timeout=10\r\n"
68
+ @unregister = "type=SNP#?version=1.0#?action=unregister#?app=#{@app}\r\n"
69
+ end
70
+
71
+ it "returns SNP::Response object" do
72
+ snp_open do |snp|
73
+ snp.request("fake").should be_kind_of(Snarl::SNP::Response)
70
74
  end
71
75
  end
72
- describe "send hash" do
73
- before :each do
74
- @register = {
75
- :action => 'register',
76
- :app => @app
77
- }
78
- @add_class = {
79
- :app => @app,
80
- :action => 'add_class',
81
- :class => @class
82
- }
83
- @notification = {
84
- :app => @app,
85
- :action => 'notification',
86
- :class => @class,
87
- :title => 'command!',
88
- :text => 'Hashed message!',
89
- :timeout => 10
90
- }
91
- @unregister = {
92
- :action => 'unregister',
93
- :app => @app
94
- }
76
+
77
+ it "if argument is String, it is used as-is" do
78
+ req_str = "fake"
79
+ snp_open do |snp|
80
+ snp.request(req_str).request_str.should eql(req_str)
81
+ end
82
+ end
83
+
84
+ it "Hash argument is converted in SNP::Request" do
85
+ notification = {
86
+ :app => @app,
87
+ :action => action = 'notification',
88
+ :class => @class,
89
+ :title => title = 'title',
90
+ :text => text = 'text',
91
+ :timeout => timeout = 9
92
+ }
93
+ expected = "type=SNP#?version=1.0#?action=#{action}#?app=#{@app}#?class=#{@class}#?title=#{title}#?text=#{text}#?timeout=#{timeout}\r\n"
94
+ snp_open do |snp|
95
+ snp.request(notification).request_str.should eql(expected)
96
+ end
97
+ end
98
+
99
+ describe "if snp.logger is set, put send/get logs to logger" do
100
+
101
+ def sio_logger(sio)
102
+ logger = Logger.new(sio)
103
+ logger.datetime_format = ""
104
+ logger
105
+ end
106
+
107
+ def expected_log_output(str, level='DEBUG')
108
+ /#{level[0,1]},\s+\[#\d+\]\s+#{Regexp.quote("#{level} -- : #{str}")}/
95
109
  end
96
110
 
97
- it "raises error according to SNP#verbose" do
98
- Snarl::SNP.open(@host, @port) do |snp|
99
- lambda{
100
- snp.request(@register)
101
- snp.request(@register)
102
- }.should_not raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
111
+ it "request string is put to log" do
112
+ log = StringIO.new
113
+ req_query = "type=SNP#?version=1.0#?action=notification#?title=title#?text=text#?timeout=10"
114
+ expected = "notification: \"#{req_query}\""
115
+ snp_open do |snp|
116
+ snp.logger = sio_logger(log)
117
+ snp.notification('title', 'text')
118
+ log.rewind
119
+ log.read.should match(expected_log_output(expected, 'DEBUG'))
103
120
  end
104
- Snarl::SNP.open(@host, @port) do |snp|
105
- snp.verbose = true
106
- lambda{
107
- snp.request(@register)
108
- snp.request(@register)
109
- }.should raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
121
+ end
122
+
123
+ it "response string is put to log" do
124
+ log = StringIO.new
125
+ expected = "notification: SNP/1.1/0/OK/1234/"
126
+ snp_open do |snp|
127
+ snp.logger = sio_logger(log)
128
+ snp.notification('title', 'text')
129
+ log.rewind
130
+ log.read.should match(expected_log_output(expected, 'INFO'))
110
131
  end
111
132
  end
112
133
 
113
- it "sends command Hash" do
114
- lambda{
115
- Snarl::SNP.open(@host, @port) do |snp|
116
- begin
117
- snp.request(@register)
118
- rescue Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED
119
- end
120
- begin
121
- snp.request(@add_class)
122
- rescue Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS
123
- end
124
- snp.request(@notification)
125
- snp.request(@unregister)
126
- end
127
- }.should_not raise_error
134
+ it "if no casual errors raise, log has 2 lines(reqstr, response)" do
135
+ log = StringIO.new
136
+ snp_open do |snp|
137
+ snp.logger = sio_logger(log)
138
+ snp.notification('title', 'text')
139
+ log.rewind
140
+ log.readlines.size.should eql(2)
141
+ end
128
142
  end
129
- end
130
- end
131
143
 
132
- describe "#add_classes" do
133
- it "adds two class pair like [['class1', 'one'], ['class2', 'two']]" do
134
- classes = [['1', 'one'], ['2', 'two']]
135
- lambda{
136
- Snarl::SNP.open(@host, @port) do |snp|
137
- snp.register(@app)
138
- snp.add_classes(classes)
139
- snp.notification('First class', '1!', nil, 4, '1')
140
- snp.notification('Second class', '2!', nil, 4, '2')
141
- snp.unregister(@app)
144
+ it "casual error is put regardress of snp.verbose" do
145
+ log = StringIO.new
146
+ expected = "register: (ignored) (203) Application is already registered"
147
+ snp_open do |snp|
148
+ snp.logger = sio_logger(log)
149
+ snp.stub!(:send).and_return('SNP/1.1/203/Application is already registered')
150
+ snp.register('Ruby-Snarl')
151
+ log.rewind
152
+ log.read.should match(expected_log_output(expected, 'INFO'))
142
153
  end
143
- }.should_not raise_error
154
+ end
144
155
  end
145
- end
146
156
 
147
- describe "#snarl_hello" do
148
- it "returns Snarl release identifier string without popup" do
149
- Snarl::SNP.open(@host) do |snp|
150
- snp.snarl_hello.should match(/\ASnarl /)
157
+ describe "raises no Error::Casual when respnse is casual error and setting is default(SNP#verbose is unset, false)" do
158
+ ErrorPair.casual_errors.each_pair do |res, err|
159
+ e = <<E
160
+ it "not raise #{err}" do
161
+ snp_open do |snp|
162
+ snp.stub!(:send).and_return("#{res}")
163
+ lambda{snp.request("fake")}.should_not raise_error(#{err})
164
+ end
165
+ end
166
+ E
167
+ instance_eval(e)
151
168
  end
152
169
  end
153
- end
154
170
 
155
- describe "#snarl_version" do
156
- it "returns Snarl (inner) version string without popup" do
157
- Snarl::SNP.open(@host) do |snp|
158
- snp.snarl_version.should match(/[\d\.]+/)
171
+ describe "raises Error::Casual when respnse is casual error and SNP#verbose is set to true" do
172
+ ErrorPair.casual_errors.each_pair do |res, err|
173
+ e = <<E
174
+ it "raise #{err}" do
175
+ snp_open do |snp|
176
+ snp.verbose = true
177
+ snp.stub!(:send).and_return("#{res}")
178
+ lambda{snp.request("fake")}.should raise_error(#{err})
179
+ end
180
+ end
181
+ E
182
+ instance_eval(e)
159
183
  end
160
184
  end
161
- end
162
185
 
163
- describe "#iconset" do
164
- it "set icon using" do
165
- iconset = {:red => 'red.jpg', :blue => 'blue.jpg'}
166
- Snarl::SNP.open(@host, @port) do |snp|
167
- snp.register(@app)
168
- snp.iconset(iconset)
169
- snp.notification('icon!', 'icon1!', :red, 1).request.to_s.should match(/icon=red\.jpg/)
170
- snp.notification('icon!', 'icon2!', :blue, 1).request.to_s.should match(/icon=blue\.jpg/)
186
+ describe "raises Error::Fatal when response is fatal error" do
187
+ ErrorPair.fatal_errors.each_pair do |res, err|
188
+ e = <<E
189
+ it "raise #{err}" do
190
+ snp_open do |snp|
191
+ snp.stub!(:send).and_return("#{res}")
192
+ lambda{snp.request("fake")}.should raise_error(#{err})
193
+ end
194
+ end
195
+ E
196
+ instance_eval(e)
171
197
  end
172
198
  end
173
- end
174
199
 
175
- describe ".ping" do
176
- it "ping!" do
177
- lambda{Snarl::SNP.ping(@host)}.should_not raise_error
178
- end
179
- it "no host ping!" do
180
- Snarl::SNP::Config.host = @host
181
- lambda{Snarl::SNP.ping(nil)}.should_not raise_error
200
+ describe "raises Error::Fatal when response is fatal error regardless of SNP#verbose" do
201
+ ErrorPair.fatal_errors.each_pair do |res, err|
202
+ e = <<E
203
+ it "raise #{err}" do
204
+ snp_open do |snp|
205
+ snp.stub!(:send).and_return("#{res}")
206
+ lambda{snp.request("fake")}.should raise_error(#{err})
207
+ end
208
+ end
209
+ E
210
+ instance_eval(e)
211
+ end
182
212
  end
183
- end
184
213
 
185
- describe ".open{|snp| ...}" do
186
- it "supplies Snarl::SNP object block" do
187
- Snarl::SNP.open(@host, @port) do |snp|
188
- snp.notification('hello!', 10) # TODO: default timeout
214
+ describe "returns SNP::Response when error does not raise" do
215
+ it "when SNP/1.1/0/OK, returns its SNP::Response object" do
216
+ snp_open do |snp|
217
+ snp.stub!(:send).and_return("SNP/1.1/0/OK")
218
+ snp.request("fake").inspect.should eql("SNP/1.1/0/OK")
219
+ end
220
+ end
221
+ ErrorPair.casual_errors.keys.each do |res|
222
+ e = <<E
223
+ it "when #{res}, returns its SNP::Response object" do
224
+ snp_open do |snp|
225
+ snp.stub!(:send).and_return("#{res}")
226
+ snp.request("fake").inspect.should eql("#{res}")
227
+ end
228
+ end
229
+ E
230
+ instance_eval(e)
189
231
  end
190
232
  end
191
- end
192
233
 
193
- describe ".show_message" do
194
- it "show_message(host, port, title, text, timeout, icon) shows popup message" do
195
- lambda{Snarl::SNP.show_message(@host, @port, 'snp_spec', 'test mesage', 2, nil)}.should_not raise_error
196
- end
197
- it "show_message(host, text) shows popup message with ease" do
198
- lambda{Snarl::SNP.show_message(@host, 'short mesage')}.should_not raise_error
199
- end
200
234
  end
201
235
 
202
- end
203
-
204
- describe "SNP1.1 feature" do
205
- # NOTE: With "type=SNP#?version=1.0" also works on Snarl R2.21. wow.
236
+ describe "#add_classes" do
206
237
 
207
- before :all do
208
- @host = '192.168.0.2'
209
- @port = 9887
210
- @app = Snarl::SNP::DEFAULT_TITLE
211
- @class = '1'
238
+ it "runs add_class for each argument (if Array, use add_classes(*arr))" do
239
+ classes = [['1', 'one'], ['2', 'two']]
240
+ snp_open do |snp|
241
+ snp.register(@app)
242
+ snp.stub!(:send).and_return{|req| "SNP/1.1/0/OK/#{req.to_s.scan(/class=(\d)/).flatten[0]}"}
243
+ snp.add_classes(*classes).map{|res| res.infomation}.should eql(['1', '2'])
244
+ snp.unregister(@app)
245
+ end
246
+ end
247
+ it "runs add_class for multi_class_str each argument" do
248
+ snp_open do |snp|
249
+ snp.register(@app)
250
+ snp.stub!(:send).and_return{|req| "SNP/1.1/0/OK/#{req.to_s.scan(/class=(\d)/).flatten[0]}"}
251
+ snp.add_classes('1', '2').map{|res| res.infomation}.should eql(['1', '2'])
252
+ snp.unregister(@app)
253
+ end
254
+ end
212
255
  end
213
256
 
214
- # Supports notification feedback forwarding in the form of 3xx codes sent to the remote application's port.
215
- ## How test?
216
-
217
- describe "new action" do
218
-
219
- describe "action=hello receives Snarl release identifier (i.e. Snarl R2.21)." do
220
- it "type=SNP#?version=1.1#?action=hello\\r\\n" do
221
- Snarl::SNP.open(@host) do |snp|
222
- res = snp.request("type=SNP#?version=1.1#?action=hello\r\n")
223
- res.inspect.should match(/\ASNP\/1.1\/0\/OK\/Snarl /)
224
- end
257
+ describe "#snarl_hello" do
258
+ it "returns Snarl release identifier string without popup" do
259
+ snp_open do |snp|
260
+ snp.stub!(:send).and_return("SNP/1.1/0/OK/Snarl R2.21")
261
+ expected = "Snarl R2.21"
262
+ snp.snarl_hello.should eql(expected)
225
263
  end
226
264
  end
265
+ end
227
266
 
228
- describe "action=version receives Snarl (inner) version (i.e. 40.15)" do
229
- it "type=SNP#?version=1.1#?action=version\\r\\n" do
230
- Snarl::SNP.open(@host) do |snp|
231
- res = snp.request("type=SNP#?version=1.1#?action=version\r\n")
232
- res.inspect.should match(/\ASNP\/1.1\/0\/OK\/[\d\.]+\Z/)
233
- end
267
+ describe "#snarl_version" do
268
+ it "returns Snarl (inner) version string without popup" do
269
+ snp_open do |snp|
270
+ snp.stub!(:send).and_return("SNP/1.1/0/OK/40.15")
271
+ expected = "40.15"
272
+ snp.snarl_version.should eql(expected)
234
273
  end
235
274
  end
236
-
237
275
  end
238
276
 
239
- describe "notification returns notification token value." do
240
- it "counter?" do
241
- Snarl::SNP.open(@host) do |snp|
277
+ describe "#iconset" do
278
+ it "set icon using" do
279
+ iconset = {:red => 'red.jpg', :blue => 'blue.jpg'}
280
+ snp_open do |snp|
242
281
  snp.register(@app)
243
- snp.add_class(@class)
244
- res = snp.request("type=SNP#?version=1.1#?action=notification#?app=#{@app}#?title=1.1!#?text=val!#?timeout=2\r\n")
245
- res.inspect.should match(/\ASNP\/1.1\/0\/OK\/\d+\Z/)
246
- snp.unregister(@class)
282
+ snp.iconset(iconset)
283
+ snp.notification('icon!', 'icon1!', :red, 1).request_str.should match(/icon=red\.jpg/)
284
+ snp.notification('icon!', 'icon2!', :blue, 1).request_str.should match(/icon=blue\.jpg/)
247
285
  end
248
286
  end
249
287
  end
250
-
251
- # describe "notification icon path can be URL" do
252
- # it "use Google News Icon" do
253
- # lambda{
254
- # Snarl::SNP.open(@host) do |snp|
255
- # snp.register(@app)
256
- # snp.add_class(@class)
257
- # icon = 'http://www.google.com/images/newspaper.gif'
258
- # res = snp.request("type=SNP#?version=1.1#?action=notification#?app=#{@app}#?title=1.1!#?text=Google News!!#?timeout=4#?icon=#{icon}\r\n")
259
- # snp.unregister(@class)
260
- # end
261
- # }.should_not raise_error
262
- # end
263
- # end
264
288
  end
265
-