snarl-snp 0.1.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/GUIDE.rdoc.ja +126 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +144 -0
- data/README.rdoc.ja +166 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/exsample/ping.rb +8 -0
- data/exsample/winamp_nowplaying.rb +227 -0
- data/exsample/yahoo_weather.rb +35 -0
- data/lib/snarl/autotest.rb +112 -0
- data/lib/snarl/snp.rb +11 -0
- data/lib/snarl/snp/action.rb +138 -0
- data/lib/snarl/snp/config.rb +32 -0
- data/lib/snarl/snp/error.rb +76 -0
- data/lib/snarl/snp/request.rb +83 -0
- data/lib/snarl/snp/response.rb +36 -0
- data/lib/snarl/snp/snp.rb +142 -0
- data/snarl-snp.gemspec +78 -0
- data/spec/exsample/data/weather_yahoo_co_jp.html +608 -0
- data/spec/exsample/yahoo_weather_spec.rb +22 -0
- data/spec/snp/action_spec.rb +198 -0
- data/spec/snp/config_spec.rb +53 -0
- data/spec/snp/request_spec.rb +72 -0
- data/spec/snp/response_sprc.rb +96 -0
- data/spec/snp/snp_spec.rb +265 -0
- data/spec/spec_helper.rb +20 -0
- metadata +119 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
require 'snarl/snp'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'webmock'
|
6
|
+
|
7
|
+
html = File.open(File.join(File.dirname(__FILE__), 'data', 'weather_yahoo_co_jp.html')){|f| f.read}
|
8
|
+
WebMock.stub_request(:get, 'http://weather.yahoo.co.jp/weather/jp/1b/1400.html').to_return(:body => html)
|
9
|
+
|
10
|
+
require 'open-uri'
|
11
|
+
#p open('http://weather.yahoo.co.jp/weather/jp/1b/1400.html').read
|
12
|
+
describe "Show weathercast of Sapporo, Japan" do
|
13
|
+
it "do" do
|
14
|
+
lambda{
|
15
|
+
argv = ARGV.dup
|
16
|
+
ARGV.clear # spec -fs -v yahoo_weather_spec.rb #=> ARGV[0] == '-fs'
|
17
|
+
ARGV[0] = Snarl::SNP::Config.host
|
18
|
+
load File.expand_path(File.dirname(__FILE__) + '../../../exsample/yahoo_weather.rb')
|
19
|
+
ARGV.concat(argv)
|
20
|
+
}.should_not raise_error
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe "SNP" do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@host = '192.168.0.2'
|
8
|
+
@port = 9887
|
9
|
+
@app = Snarl::SNP::DEFAULT_TITLE
|
10
|
+
@class = '1'
|
11
|
+
Snarl::SNP::Config.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#register" do
|
15
|
+
it "everything goes well" do
|
16
|
+
lambda{
|
17
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
18
|
+
snp.register(@app)
|
19
|
+
snp.unregister(@app)
|
20
|
+
end
|
21
|
+
}.should_not raise_error
|
22
|
+
end
|
23
|
+
it "twice raises error on verbose" do
|
24
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
25
|
+
snp.verbose = true
|
26
|
+
snp.register(@app)
|
27
|
+
lambda{snp.register(@app)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
|
28
|
+
snp.unregister(@app)
|
29
|
+
end
|
30
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
31
|
+
snp.register(@app)
|
32
|
+
lambda{snp.register(@app)}.should_not raise_error(Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED)
|
33
|
+
snp.unregister(@app)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#unregister" do
|
39
|
+
# it "everything goes well" is done in #register
|
40
|
+
it "unregister before register raises error on verbose" do
|
41
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
42
|
+
snp.verbose = true
|
43
|
+
lambda{snp.unregister(@app)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_NOT_REGISTERED)
|
44
|
+
end
|
45
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
46
|
+
lambda{snp.unregister(@app)}.should_not raise_error(Snarl::SNP::Error::SNP_ERROR_NOT_REGISTERED)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#add_class" do
|
52
|
+
it "everything goes well" do
|
53
|
+
lambda{
|
54
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
55
|
+
snp.register(@app)
|
56
|
+
snp.add_class(@class)
|
57
|
+
snp.unregister(@app)
|
58
|
+
end
|
59
|
+
}.should_not raise_error
|
60
|
+
end
|
61
|
+
it "add same class raises error on verbose" do
|
62
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
63
|
+
snp.verbose = true
|
64
|
+
snp.register(@app)
|
65
|
+
snp.add_class(@class)
|
66
|
+
lambda{snp.add_class(@class)}.should raise_error(Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS)
|
67
|
+
snp.unregister(@app)
|
68
|
+
end
|
69
|
+
Snarl::SNP.open(@host, @port) do |snp|
|
70
|
+
snp.register(@app)
|
71
|
+
snp.add_class(@class)
|
72
|
+
lambda{snp.add_class(@app)}.should_not raise_error(Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS)
|
73
|
+
snp.unregister(@app)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#notification" do
|
79
|
+
it "sends title, text, icon, timeout, class" do
|
80
|
+
Snarl::SNP.open(@host) do |snp|
|
81
|
+
snp.register(@app)
|
82
|
+
snp.add_class(@class)
|
83
|
+
res = snp.notification('tit', 'tex(4 no icon popups)', 'ico', 9, 'cls')
|
84
|
+
expected = "type=SNP#?version=1.0#?action=notification#?app=Ruby-Snarl#?class=cls#?title=tit#?text=tex(4 no icon popups)#?timeout=9#?icon=ico\r\n"
|
85
|
+
res.request.to_s.should eql(expected)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
it "sends keyword-hash, keys=[:title, :text, :icon, :timeout, :class]" do
|
89
|
+
Snarl::SNP.open(@host) do |snp|
|
90
|
+
snp.register(@app)
|
91
|
+
snp.add_class(@class)
|
92
|
+
res = snp.notification(:title => 'tit', :text => 'tex', :icon => 'ico', :timeout => 9, :class => 'cls')
|
93
|
+
expected = "type=SNP#?version=1.0#?action=notification#?app=Ruby-Snarl#?class=cls#?title=tit#?text=tex#?timeout=9#?icon=ico\r\n"
|
94
|
+
res.request.to_s.should eql(expected)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
it "sends 'anonymous' message if not registered yet" do
|
98
|
+
Snarl::SNP.open(@host) do |snp|
|
99
|
+
res = snp.notification('tit', 'tex', 'ico', 9)
|
100
|
+
expected = "type=SNP#?version=1.0#?action=notification#?title=tit#?text=tex#?timeout=9#?icon=ico\r\n"
|
101
|
+
res.request.to_s.should eql(expected)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
it "sends 'anonymous' message if keyhash has {:app => nil} pair" do
|
105
|
+
Snarl::SNP.open(@host) do |snp|
|
106
|
+
res = snp.notification(:app => nil, :title => 'tit', :text => 'tex', :icon => 'ico', :timeout => 9)
|
107
|
+
expected = "type=SNP#?version=1.0#?action=notification#?title=tit#?text=tex#?timeout=9#?icon=ico\r\n"
|
108
|
+
res.request.to_s.should eql(expected)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "#hello" do
|
114
|
+
it "returns Snarl release identifier" do
|
115
|
+
Snarl::SNP.open(@host) do |snp|
|
116
|
+
snp.hello.infomation.should match(/\ASnarl /)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "#version" do
|
122
|
+
it "returns Snarl (inner) version" do
|
123
|
+
Snarl::SNP.open(@host) do |snp|
|
124
|
+
snp.version.infomation.should match(/[\d\.]+/)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "#normalize_notification_params" do
|
130
|
+
before do
|
131
|
+
@snp = Snarl::SNP.new
|
132
|
+
@default_title = Snarl::SNP::DEFAULT_TITLE
|
133
|
+
@default_timeout = Snarl::SNP::DEFAULT_TIMEOUT
|
134
|
+
end
|
135
|
+
def normalize_notification_params(param)
|
136
|
+
@snp.__send__(:normalize_notification_params, param)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "['text'] returns {:title => DEFAULT_TITLE, :text => 'text', :timeout => DEFAULT_TIMEOUT}" do
|
140
|
+
params = ['text']
|
141
|
+
expected = {:action => 'notification', :title => @default_title, :text => "text", :timeout => @default_timeout}
|
142
|
+
normalize_notification_params(params).should eql(expected)
|
143
|
+
end
|
144
|
+
it "['text'] returns {:title => snp.title, :text => 'text', :timeout => DEFAULT_TIMEOUT}, when snp.title is set" do
|
145
|
+
params = ['text']
|
146
|
+
snp = Snarl::SNP.new
|
147
|
+
snp.title = 'snp.title'
|
148
|
+
expected = {:action => 'notification', :title => 'snp.title', :text => "text", :timeout => @default_timeout}
|
149
|
+
snp.__send__(:normalize_notification_params, params).should eql(expected)
|
150
|
+
end
|
151
|
+
it "['text', 9] returns {:title => DEFAULT_TITLE, :text => 'text', :timeout => 9}" do
|
152
|
+
params = ['text', 9]
|
153
|
+
expected = {:action => 'notification', :title => @default_title, :text => "text", :timeout => 9}
|
154
|
+
normalize_notification_params(params).should eql(expected)
|
155
|
+
end
|
156
|
+
it "['text', 9] returns {:title => snp.title, :text => 'text', :timeout => 9}, when snp.title is set" do
|
157
|
+
params = ['text', 9]
|
158
|
+
snp = Snarl::SNP.new
|
159
|
+
snp.title = 'snp.title'
|
160
|
+
expected = {:action => 'notification', :title => 'snp.title', :text => "text", :timeout => 9}
|
161
|
+
snp.__send__(:normalize_notification_params, params).should eql(expected)
|
162
|
+
end
|
163
|
+
it "['title', 'text', 9] returns {:title => 'title', :text => 'text', :timeout => 9}" do
|
164
|
+
params = ['title', 'text', 9]
|
165
|
+
expected = {:action => 'notification', :title => 'title', :text => 'text', :timeout => 9}
|
166
|
+
normalize_notification_params(params).should eql(expected)
|
167
|
+
end
|
168
|
+
it "[1,2,3,4,5,6,7] returns {:title, :text, :icon, :timeout, :class, :action, :app} Hash" do
|
169
|
+
timeout = 9
|
170
|
+
params = ['title', 'text', 'icon', timeout, 'class', 'action', 'app']
|
171
|
+
expected = {:action => 'notification', :title => 'title', :text => 'text', :icon => 'icon', :timeout => timeout,
|
172
|
+
:class => 'class', :app => 'app'}
|
173
|
+
normalize_notification_params(params).should eql(expected)
|
174
|
+
end
|
175
|
+
it "[1,2,3,4,nil,nil,nil] returns {:title, :text, :icon, :timeout} Hash. nil-value key is omitted" do
|
176
|
+
timeout = 9
|
177
|
+
params = ['title', 'text', 'icon', timeout, nil, nil, nil]
|
178
|
+
expected = {:action => 'notification', :title => 'title', :text => 'text', :icon => 'icon', :timeout => timeout}
|
179
|
+
normalize_notification_params(params).should eql(expected)
|
180
|
+
end
|
181
|
+
it "when already registered, app is @app" do
|
182
|
+
params = [{:text => 'text'}]
|
183
|
+
snp = Snarl::SNP.new
|
184
|
+
snp.instance_variable_set(:@app, 'app')
|
185
|
+
expected = {:action => 'notification', :app => 'app', :title => @default_title, :text => "text", :timeout => @default_timeout}
|
186
|
+
snp.__send__(:normalize_notification_params, params).should eql(expected)
|
187
|
+
end
|
188
|
+
it "if @app is :anonymous, app is nil" do
|
189
|
+
params = [{:text => 'text'}]
|
190
|
+
snp = Snarl::SNP.new
|
191
|
+
snp.instance_variable_set(:@app, :anonymous)
|
192
|
+
expected = {:action => 'notification', :app => nil, :title => @default_title, :text => "text", :timeout => @default_timeout}
|
193
|
+
snp.__send__(:normalize_notification_params, params).should eql(expected)
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe "Config" do
|
5
|
+
|
6
|
+
before do
|
7
|
+
Snarl::SNP::Config.reset
|
8
|
+
end
|
9
|
+
|
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')
|
14
|
+
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)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
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)
|
26
|
+
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)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
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
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe "Request" do
|
5
|
+
|
6
|
+
describe "normalize_action" do
|
7
|
+
def normalize_action(action)
|
8
|
+
Snarl::SNP::Request.new.__send__(:normalize_action, action)
|
9
|
+
end
|
10
|
+
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')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#[]=" do
|
17
|
+
it "set commands with []=" do
|
18
|
+
req = Snarl::SNP::Request.new
|
19
|
+
req[:action] = 'register'
|
20
|
+
req['app'] = 'Just Testing...'
|
21
|
+
expected = {"app"=>"Just Testing...", :action =>"register"}
|
22
|
+
req.commands.should eql(expected)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#normalize" do
|
27
|
+
it "normalize" do
|
28
|
+
cmd_hash = {
|
29
|
+
:action => 'add-class',
|
30
|
+
:app => "Just \r\nTesting\r\n...",
|
31
|
+
'tiTLe' => nil,
|
32
|
+
:cLASS => '1'
|
33
|
+
}
|
34
|
+
req = Snarl::SNP::Request.new(cmd_hash)
|
35
|
+
req.__send__(:normalize)
|
36
|
+
expected = {"app"=>"Just \nTesting\n...", "class"=>"1", "action"=>"add_class", "type"=>"SNP", "version"=>"1.0"}
|
37
|
+
req.commands.should eql(expected)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#to_s" do
|
42
|
+
it "is sendable string" do
|
43
|
+
action = :notification
|
44
|
+
cmds = {
|
45
|
+
:action => action,
|
46
|
+
:app => 'Just Testing...',
|
47
|
+
:class => '1',
|
48
|
+
:title => 'Hello',
|
49
|
+
:text => 'World!',
|
50
|
+
:timeout => 10
|
51
|
+
}
|
52
|
+
req = Snarl::SNP::Request.new(cmds)
|
53
|
+
extepted = "type=SNP#?version=1.0#?action=notification#?app=Just Testing...#?class=1#?title=Hello#?text=World!#?timeout=10\r\n"
|
54
|
+
req.to_s.should eql(extepted)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".build" do
|
59
|
+
it "makes Request Object" do
|
60
|
+
action = :add_class
|
61
|
+
cmds = {
|
62
|
+
:action => action,
|
63
|
+
:app => 'Just Testing...',
|
64
|
+
:class => '1',
|
65
|
+
:title => '1 is one'
|
66
|
+
}
|
67
|
+
req = Snarl::SNP::Request.new(cmds)
|
68
|
+
extepted = "type=SNP#?version=1.0#?action=add_class#?app=Just Testing...#?class=1#?title=1 is one\r\n"
|
69
|
+
req.to_s.should eql(extepted)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe "Response" do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@res = 'SNP/1.0/0/OK/1234'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "initialize" do
|
11
|
+
it "parse string" do
|
12
|
+
Snarl::SNP::Response.new(@res).instance_variable_get(:@response).should eql(@res)
|
13
|
+
end
|
14
|
+
it "parse getable obj" do
|
15
|
+
socket = Object.new
|
16
|
+
socket.stub!(:get).and_return(@res)
|
17
|
+
Snarl::SNP::Response.new(socket).instance_variable_get(:@response).should eql(@res)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#parse_response" do
|
22
|
+
it "raise error when unparsable response" do
|
23
|
+
lambda{
|
24
|
+
Snarl::SNP::Response.new("SNP/1.0/zerozero/Oops!\r\n")
|
25
|
+
}.should raise_error(Snarl::SNP::Error::RUBYSNARL_UNKNOWN_RESPONSE)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#code" do
|
30
|
+
it "SNP1.1 response code(3rd block) on non-error-response is String '0'" do
|
31
|
+
Snarl::SNP::Response.new(@res).code.should eql('0')
|
32
|
+
end
|
33
|
+
it "other response code" do
|
34
|
+
begin
|
35
|
+
Snarl::SNP::Response.new("SNP/1.1/203/Application is already registered\r\n")
|
36
|
+
rescue Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED => ex
|
37
|
+
ex.response.code.should eql('203')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#message" do
|
43
|
+
it "SNP1.1 respnse message(4th block) on non-error-response is 'OK'" do
|
44
|
+
Snarl::SNP::Response.new(@res).message.should eql('OK')
|
45
|
+
end
|
46
|
+
it "no 5th block response" do
|
47
|
+
begin
|
48
|
+
Snarl::SNP::Response.new("SNP/1.1/203/Application is already registered\r\n")
|
49
|
+
rescue Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED => ex
|
50
|
+
ex.response.message.should eql('Application is already registered')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#infomation" do
|
56
|
+
it "SNP1.1 response infomation(5th opptional block) on non-error-notification-response is integer str" do
|
57
|
+
Snarl::SNP::Response.new(@res).infomation.should eql('1234')
|
58
|
+
end
|
59
|
+
it "no 5th block response, returns nil" do
|
60
|
+
begin
|
61
|
+
Snarl::SNP::Response.new("SNP/1.1/203/Application is already registered\r\n")
|
62
|
+
rescue Snarl::SNP::Error::SNP_ERROR_ALREADY_REGISTERED => ex
|
63
|
+
ex.response.infomation.should be_nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#error" do
|
69
|
+
it "returns Snarl::SNP::Error class" do
|
70
|
+
Snarl::SNP::Response.new(@res).error.should eql(Snarl::SNP::Error::SNP_OK)
|
71
|
+
begin
|
72
|
+
Snarl::SNP::Response.new("SNP/1.0/204/Class is already registered\r\n")
|
73
|
+
rescue Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS => ex
|
74
|
+
ex.response.error.should eql(Snarl::SNP::Error::SNP_ERROR_CLASS_ALREADY_EXISTS)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#ok?" do
|
80
|
+
it "returns true when OK response" do
|
81
|
+
Snarl::SNP::Response.new(@res).should be_ok
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#to_s" do
|
86
|
+
it "to_s is response code" do
|
87
|
+
Snarl::SNP::Response.new(@res).to_s.should eql('0')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#inspect" do
|
92
|
+
it "inspect is response string itself" do
|
93
|
+
Snarl::SNP::Response.new(@res).inspect.should eql(@res)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,265 @@
|
|
1
|
+
# -*- coding:utf-8 -*-
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe "SNP" do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@host = '192.168.0.2'
|
8
|
+
@port = 9887
|
9
|
+
@app = Snarl::SNP::DEFAULT_TITLE
|
10
|
+
@class = '1'
|
11
|
+
Snarl::SNP::Config.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "initialize" do
|
15
|
+
it "new('192.168.0.2', 9887). (snarl_working_machine_host, snarl_port_9887)" do
|
16
|
+
host, port, verbose = '192.168.0.2', 9887, true
|
17
|
+
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)
|
20
|
+
snp.verbose.should eql(verbose)
|
21
|
+
end
|
22
|
+
it "default host is 127.0.0.1, default port is 9887. when SNP.new()" do
|
23
|
+
snp = Snarl::SNP.new
|
24
|
+
snp.instance_variable_get(:@host).should eql(nil)
|
25
|
+
snp.instance_variable_get(:@port).should eql(nil)
|
26
|
+
snp.verbose.should eql(false)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
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"
|
37
|
+
end
|
38
|
+
|
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
|
53
|
+
end
|
54
|
+
|
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
|
70
|
+
end
|
71
|
+
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
|
+
}
|
95
|
+
end
|
96
|
+
|
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)
|
103
|
+
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)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
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
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
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)
|
142
|
+
end
|
143
|
+
}.should_not raise_error
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
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 /)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
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\.]+/)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
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/)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
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
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
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
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
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
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
describe "SNP1.1 feature" do
|
205
|
+
# NOTE: With "type=SNP#?version=1.0" also works on Snarl R2.21. wow.
|
206
|
+
|
207
|
+
before :all do
|
208
|
+
@host = '192.168.0.2'
|
209
|
+
@port = 9887
|
210
|
+
@app = Snarl::SNP::DEFAULT_TITLE
|
211
|
+
@class = '1'
|
212
|
+
end
|
213
|
+
|
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
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
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
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
describe "notification returns notification token value." do
|
240
|
+
it "counter?" do
|
241
|
+
Snarl::SNP.open(@host) do |snp|
|
242
|
+
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)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
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
|
+
end
|
265
|
+
|