tuomas-tweetwine 0.2.2 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/Rakefile +1 -0
- data/bin/tweetwine +50 -38
- data/lib/tweetwine/client.rb +6 -5
- data/lib/tweetwine/io.rb +1 -1
- data/lib/tweetwine/meta.rb +1 -1
- data/lib/tweetwine/rest_client_wrapper.rb +24 -4
- data/lib/tweetwine/url_shortener.rb +3 -2
- data/lib/tweetwine/util.rb +1 -1
- data/test/client_test.rb +340 -332
- data/test/io_test.rb +140 -143
- data/test/options_test.rb +1 -1
- data/test/rest_client_wrapper_test.rb +42 -5
- data/test/startup_config_test.rb +56 -54
- data/test/url_shortener_test.rb +135 -102
- data/test/util_test.rb +16 -18
- metadata +5 -4
data/test/io_test.rb
CHANGED
@@ -3,12 +3,13 @@ require "test_helper"
|
|
3
3
|
module Tweetwine
|
4
4
|
|
5
5
|
class IOTest < Test::Unit::TestCase
|
6
|
-
context "An IO" do
|
6
|
+
context "An IO instance" do
|
7
7
|
setup do
|
8
8
|
@input = mock()
|
9
9
|
@output = mock()
|
10
10
|
@io = IO.new({ :input => @input, :output => @output })
|
11
11
|
end
|
12
|
+
|
12
13
|
should "output prompt and return input as trimmed" do
|
13
14
|
@output.expects(:print).with("The answer: ")
|
14
15
|
@input.expects(:gets).returns(" 42 ")
|
@@ -42,193 +43,189 @@ class IOTest < Test::Unit::TestCase
|
|
42
43
|
@input.expects(:gets).returns("")
|
43
44
|
assert_equal false, @io.confirm("Fire nukes?")
|
44
45
|
end
|
45
|
-
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
@io = IO.new({ :input => @input, :output => @output, :colorize => false })
|
52
|
-
end
|
47
|
+
context "with colorization disabled" do
|
48
|
+
setup do
|
49
|
+
@io = IO.new({ :input => @input, :output => @output, :colorize => false })
|
50
|
+
end
|
53
51
|
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
should "output a record as user info when no status is given" do
|
53
|
+
record = { :user => "fooman" }
|
54
|
+
@output.expects(:puts).with(<<-END
|
57
55
|
fooman
|
58
56
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
57
|
+
END
|
58
|
+
)
|
59
|
+
@io.show_record(record)
|
60
|
+
end
|
61
|
+
|
62
|
+
should "output a record as status info when status is given, without in-reply info" do
|
63
|
+
status = "Hi, @barman! Lulz woo! #hellos"
|
64
|
+
record = {
|
65
|
+
:user => "fooman",
|
66
|
+
:status => {
|
67
|
+
:created_at => Time.at(1),
|
68
|
+
:text => status
|
69
|
+
}
|
71
70
|
}
|
72
|
-
|
73
|
-
|
74
|
-
@output.expects(:puts).with(<<-END
|
71
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
72
|
+
@output.expects(:puts).with(<<-END
|
75
73
|
fooman, 2 secs ago:
|
76
74
|
#{status}
|
77
75
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
76
|
+
END
|
77
|
+
)
|
78
|
+
@io.show_record(record)
|
79
|
+
end
|
80
|
+
|
81
|
+
should "output a record as status info when status is given, with in-reply info" do
|
82
|
+
status = "Hi, @fooman! How are you doing?"
|
83
|
+
record = {
|
84
|
+
:user => "barman",
|
85
|
+
:status => {
|
86
|
+
:created_at => Time.at(1),
|
87
|
+
:text => status,
|
88
|
+
:in_reply_to => "fooman"
|
89
|
+
}
|
91
90
|
}
|
92
|
-
|
93
|
-
|
94
|
-
@output.expects(:puts).with(<<-END
|
91
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
92
|
+
@output.expects(:puts).with(<<-END
|
95
93
|
barman, in reply to fooman, 2 secs ago:
|
96
94
|
#{status}
|
97
95
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
96
|
+
END
|
97
|
+
)
|
98
|
+
@io.show_record(record)
|
99
|
+
end
|
102
100
|
|
103
|
-
|
104
|
-
|
105
|
-
|
101
|
+
should "output a preview of a status" do
|
102
|
+
status = "@nick, check http://bit.ly/18rU_Vx"
|
103
|
+
@output.expects(:puts).with(<<-END
|
106
104
|
|
107
105
|
#{status}
|
108
106
|
|
109
|
-
|
110
|
-
|
111
|
-
|
107
|
+
END
|
108
|
+
)
|
109
|
+
@io.show_status_preview(status)
|
110
|
+
end
|
112
111
|
end
|
113
|
-
end
|
114
112
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
@io = IO.new({ :input => @input, :output => @output, :colorize => true })
|
120
|
-
end
|
113
|
+
context "with colorization enabled" do
|
114
|
+
setup do
|
115
|
+
@io = IO.new({ :input => @input, :output => @output, :colorize => true })
|
116
|
+
end
|
121
117
|
|
122
|
-
|
123
|
-
|
124
|
-
|
118
|
+
should "output a record as user info when no status is given" do
|
119
|
+
record = { :user => "fooman" }
|
120
|
+
@output.expects(:puts).with(<<-END
|
125
121
|
\033[32mfooman\033[0m
|
126
122
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
123
|
+
END
|
124
|
+
)
|
125
|
+
@io.show_record(record)
|
126
|
+
end
|
127
|
+
|
128
|
+
should "output a record as status info when status is given, without in-reply info" do
|
129
|
+
record = {
|
130
|
+
:user => "fooman",
|
131
|
+
:status => {
|
132
|
+
:created_at => Time.at(1),
|
133
|
+
:text => "Wondering the meaning of life."
|
134
|
+
}
|
138
135
|
}
|
139
|
-
|
140
|
-
|
141
|
-
@output.expects(:puts).with(<<-END
|
136
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
137
|
+
@output.expects(:puts).with(<<-END
|
142
138
|
\033[32mfooman\033[0m, 2 secs ago:
|
143
139
|
Wondering the meaning of life.
|
144
140
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
141
|
+
END
|
142
|
+
)
|
143
|
+
@io.show_record(record)
|
144
|
+
end
|
145
|
+
|
146
|
+
should "output a record as status info when status is given, with in-reply info" do
|
147
|
+
record = {
|
148
|
+
:user => "barman",
|
149
|
+
:status => {
|
150
|
+
:created_at => Time.at(1),
|
151
|
+
:text => "Hi, @fooman! How are you doing? #hellos",
|
152
|
+
:in_reply_to => "fooman"
|
153
|
+
}
|
157
154
|
}
|
158
|
-
|
159
|
-
|
160
|
-
@output.expects(:puts).with(<<-END
|
155
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
156
|
+
@output.expects(:puts).with(<<-END
|
161
157
|
\033[32mbarman\033[0m, in reply to \033[32mfooman\033[0m, 2 secs ago:
|
162
158
|
Hi, \033[33m@fooman\033[0m! How are you doing? \033[35m#hellos\033[0m
|
163
159
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
160
|
+
END
|
161
|
+
)
|
162
|
+
@io.show_record(record)
|
163
|
+
end
|
168
164
|
|
169
|
-
|
170
|
-
|
171
|
-
|
165
|
+
should "output a preview of a status" do
|
166
|
+
status = "@nick, check http://bit.ly/18rU_Vx"
|
167
|
+
@output.expects(:puts).with(<<-END
|
172
168
|
|
173
169
|
\033[33m@nick\033[0m, check \033[36mhttp://bit.ly/18rU_Vx\033[0m
|
174
170
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
171
|
+
END
|
172
|
+
)
|
173
|
+
@io.show_status_preview(status)
|
174
|
+
end
|
175
|
+
|
176
|
+
should "highlight HTTP and HTTPS URLs in a status" do
|
177
|
+
record = {
|
178
|
+
:user => "barman",
|
179
|
+
:status => {
|
180
|
+
:created_at => Time.at(1),
|
181
|
+
:text => "Three links: http://bit.ly/18rU_Vx http://is.gd/1qLk3 and https://is.gd/2rLk4",
|
182
|
+
}
|
186
183
|
}
|
187
|
-
|
188
|
-
|
189
|
-
@output.expects(:puts).with(<<-END
|
184
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
185
|
+
@output.expects(:puts).with(<<-END
|
190
186
|
\033[32mbarman\033[0m, 2 secs ago:
|
191
187
|
Three links: \033[36mhttp://bit.ly/18rU_Vx\033[0m \033[36mhttp://is.gd/1qLk3\033[0m and \033[36mhttps://is.gd/2rLk4\033[0m
|
192
188
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
189
|
+
END
|
190
|
+
)
|
191
|
+
@io.show_record(record)
|
192
|
+
end
|
193
|
+
|
194
|
+
should "highlight HTTP and HTTPS URLs in a status, even if duplicates" do
|
195
|
+
record = {
|
196
|
+
:user => "barman",
|
197
|
+
:status => {
|
198
|
+
:created_at => Time.at(1),
|
199
|
+
:text => "Duplicate links: http://is.gd/1qLk3 and http://is.gd/1qLk3",
|
200
|
+
}
|
204
201
|
}
|
205
|
-
|
206
|
-
|
207
|
-
@output.expects(:puts).with(<<-END
|
202
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
203
|
+
@output.expects(:puts).with(<<-END
|
208
204
|
\033[32mbarman\033[0m, 2 secs ago:
|
209
205
|
Duplicate links: \033[36mhttp://is.gd/1qLk3\033[0m and \033[36mhttp://is.gd/1qLk3\033[0m
|
210
206
|
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
207
|
+
END
|
208
|
+
)
|
209
|
+
@io.show_record(record)
|
210
|
+
end
|
211
|
+
|
212
|
+
should "highlight usernames in a status" do
|
213
|
+
record = {
|
214
|
+
:user => "barman",
|
215
|
+
:status => {
|
216
|
+
:created_at => Time.at(1),
|
217
|
+
:text => "I salute you @fooman, @barbaz, and @spoonman!",
|
218
|
+
}
|
222
219
|
}
|
223
|
-
|
224
|
-
|
225
|
-
@output.expects(:puts).with(<<-END
|
220
|
+
Util.expects(:humanize_time_diff).returns([2, "secs"])
|
221
|
+
@output.expects(:puts).with(<<-END
|
226
222
|
\033[32mbarman\033[0m, 2 secs ago:
|
227
223
|
I salute you \033[33m@fooman\033[0m, \033[33m@barbaz\033[0m, and \033[33m@spoonman\033[0m!
|
228
224
|
|
229
|
-
|
230
|
-
|
231
|
-
|
225
|
+
END
|
226
|
+
)
|
227
|
+
@io.show_record(record)
|
228
|
+
end
|
232
229
|
end
|
233
230
|
end
|
234
231
|
|
data/test/options_test.rb
CHANGED
@@ -3,7 +3,7 @@ require "test_helper"
|
|
3
3
|
module Tweetwine
|
4
4
|
|
5
5
|
class OptionsTest < Test::Unit::TestCase
|
6
|
-
context "Options" do
|
6
|
+
context "An Options instance" do
|
7
7
|
should "get the value corresponding to a key or nil (the default value)" do
|
8
8
|
assert_equal "alpha", Options.new({:a => "alpha"})[:a]
|
9
9
|
assert_equal nil, Options.new({})[:a]
|
@@ -1,29 +1,66 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
require "rest_client"
|
3
3
|
|
4
|
+
class Object
|
5
|
+
def sleep(timeout); end # speed up tests
|
6
|
+
end
|
7
|
+
|
4
8
|
module Tweetwine
|
5
9
|
|
6
10
|
class RestClientWrapperTest < Test::Unit::TestCase
|
7
|
-
context "A
|
11
|
+
context "A RestClientWrapper instance" do
|
12
|
+
setup do
|
13
|
+
@io = mock()
|
14
|
+
@rest_client = RestClientWrapper.new(@io)
|
15
|
+
end
|
16
|
+
|
8
17
|
should "raise ClientError for an invalid request" do
|
9
18
|
RestClient.expects(:get) \
|
10
19
|
.with("https://secret:agent@hushhush.net") \
|
11
20
|
.raises(RestClient::Unauthorized)
|
12
|
-
assert_raise(ClientError) {
|
21
|
+
assert_raise(ClientError) { @rest_client.get("https://secret:agent@hushhush.net") }
|
13
22
|
end
|
14
23
|
|
15
24
|
should "raise ClientError when connection cannot be established" do
|
16
25
|
RestClient.expects(:get) \
|
17
26
|
.with("http://www.invalid.net") \
|
18
|
-
.raises(Errno::
|
19
|
-
assert_raise(ClientError) {
|
27
|
+
.raises(Errno::ECONNABORTED)
|
28
|
+
assert_raise(ClientError) { @rest_client.get("http://www.invalid.net") }
|
20
29
|
end
|
21
30
|
|
22
31
|
should "raise ClientError when host cannot be resolved" do
|
23
32
|
RestClient.expects(:get) \
|
24
33
|
.with("http://unknown.net") \
|
25
34
|
.raises(SocketError)
|
26
|
-
assert_raise(ClientError) {
|
35
|
+
assert_raise(ClientError) { @rest_client.get("http://unknown.net") }
|
36
|
+
end
|
37
|
+
|
38
|
+
should "retry connection upon connection reset" do
|
39
|
+
rest_client_calls = sequence("RestClient")
|
40
|
+
RestClient.expects(:get) \
|
41
|
+
.with("http://www.heavilyloaded.net") \
|
42
|
+
.in_sequence(rest_client_calls) \
|
43
|
+
.raises(Errno::ECONNRESET)
|
44
|
+
RestClient.expects(:get) \
|
45
|
+
.with("http://www.heavilyloaded.net") \
|
46
|
+
.in_sequence(rest_client_calls)
|
47
|
+
@io.expects(:warn).with("Could not connect -- retrying in 4 seconds")
|
48
|
+
@rest_client.get("http://www.heavilyloaded.net")
|
49
|
+
end
|
50
|
+
|
51
|
+
should "retry connection a maximum of certain number of times" do
|
52
|
+
rest_client_calls = sequence("RestClient")
|
53
|
+
io_calls = sequence("IO")
|
54
|
+
RestClientWrapper::MAX_RETRIES.times do
|
55
|
+
RestClient.expects(:get) \
|
56
|
+
.with("http://www.heavilyloaded.net") \
|
57
|
+
.in_sequence(rest_client_calls) \
|
58
|
+
.raises(Errno::ECONNRESET)
|
59
|
+
end
|
60
|
+
(RestClientWrapper::MAX_RETRIES - 1).times do
|
61
|
+
@io.expects(:warn).in_sequence(io_calls)
|
62
|
+
end
|
63
|
+
assert_raise(ClientError) { @rest_client.get("http://www.heavilyloaded.net") }
|
27
64
|
end
|
28
65
|
end
|
29
66
|
end
|
data/test/startup_config_test.rb
CHANGED
@@ -5,78 +5,80 @@ module Tweetwine
|
|
5
5
|
class StartupConfigTest < Test::Unit::TestCase
|
6
6
|
TEST_CONFIG_FILE = File.dirname(__FILE__) << "/test_config.yaml"
|
7
7
|
|
8
|
-
context "
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
context "An initialized StartupConfig" do
|
16
|
-
setup do
|
17
|
-
@config = StartupConfig.new([:default_action, :another_action])
|
8
|
+
context "A StartupConfig instance" do
|
9
|
+
context "upon initialization" do
|
10
|
+
should "require at least one supported command" do
|
11
|
+
assert_raise(ArgumentError) { StartupConfig.new([]) }
|
12
|
+
assert_nothing_raised { StartupConfig.new([:default_action]) }
|
13
|
+
end
|
18
14
|
end
|
19
15
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
context "at runtime" do
|
17
|
+
setup do
|
18
|
+
@config = StartupConfig.new([:default_action, :another_action])
|
19
|
+
end
|
24
20
|
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
should "use the first supported command as a default command when given no command as a cmdline argument" do
|
22
|
+
@config.parse
|
23
|
+
assert_equal :default_action, @config.command
|
24
|
+
end
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
should "check that given command is supported" do
|
27
|
+
@config.parse(%w{default_action}) { |args| {} }
|
28
|
+
assert_equal :default_action, @config.command
|
32
29
|
|
33
|
-
|
34
|
-
|
35
|
-
args.slice!(0..1)
|
36
|
-
{:opt => "foo"}
|
30
|
+
@config.parse(%w{another_action}) { |args| {} }
|
31
|
+
assert_equal :another_action, @config.command
|
37
32
|
end
|
38
|
-
assert_equal({:opt => "foo"}, @config.options)
|
39
|
-
assert_equal :another_action, @config.command
|
40
|
-
assert_equal %w{left overs}, @config.args
|
41
|
-
end
|
42
33
|
|
43
|
-
|
44
|
-
|
45
|
-
|
34
|
+
should "parse cmdline args, command, and leftover args" do
|
35
|
+
@config.parse(%w{--opt foo another_action left overs}) do |args|
|
36
|
+
args.slice!(0..1)
|
37
|
+
{:opt => "foo"}
|
38
|
+
end
|
39
|
+
assert_equal({:opt => "foo"}, @config.options)
|
40
|
+
assert_equal :another_action, @config.command
|
41
|
+
assert_equal %w{left overs}, @config.args
|
46
42
|
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
context "when given no cmdline args and a config file" do
|
45
|
+
setup do
|
46
|
+
@config.parse([], TEST_CONFIG_FILE)
|
47
|
+
end
|
52
48
|
|
53
|
-
|
54
|
-
|
55
|
-
@config.parse(%w{--opt foo}) do |args|
|
56
|
-
args.clear
|
57
|
-
{:opt => "foo"}
|
49
|
+
should "have the parsed option defined" do
|
50
|
+
assert_equal false, @config.options[:colorize]
|
58
51
|
end
|
59
52
|
end
|
60
53
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
54
|
+
context "when given cmdline args and no config file" do
|
55
|
+
setup do
|
56
|
+
@config.parse(%w{--opt foo}) do |args|
|
57
|
+
args.clear
|
58
|
+
{:opt => "foo"}
|
59
|
+
end
|
60
|
+
end
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
@config.parse(%w{--colorize}, TEST_CONFIG_FILE) do |args|
|
69
|
-
args.clear
|
70
|
-
{:colorize => true}
|
62
|
+
should "have the parsed option defined" do
|
63
|
+
assert_equal "foo", @config.options[:opt]
|
71
64
|
end
|
72
65
|
end
|
73
66
|
|
74
|
-
|
75
|
-
|
76
|
-
|
67
|
+
context "when given an option both as a cmdline option and in a config file" do
|
68
|
+
setup do
|
69
|
+
@config.parse(%w{--colorize}, TEST_CONFIG_FILE) do |args|
|
70
|
+
args.clear
|
71
|
+
{:colorize => true}
|
72
|
+
end
|
73
|
+
end
|
77
74
|
|
78
|
-
|
79
|
-
|
75
|
+
should "the command line option should override the config file option" do
|
76
|
+
assert_equal true, @config.options[:colorize]
|
77
|
+
end
|
78
|
+
|
79
|
+
should "have nil for an undefined option" do
|
80
|
+
assert_nil @config.options[:num_statuses]
|
81
|
+
end
|
80
82
|
end
|
81
83
|
end
|
82
84
|
end
|