tuomas-tweetwine 0.2.2 → 0.2.4

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.
@@ -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
- context "An IO, with colorization disabled" do
48
- setup do
49
- @input = mock()
50
- @output = mock()
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
- should "output a record as user info when no status is given" do
55
- record = { :user => "fooman" }
56
- @output.expects(:puts).with(<<-END
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
- END
60
- )
61
- @io.show_record(record)
62
- end
63
-
64
- should "output a record as status info when status is given, without in-reply info" do
65
- status = "Hi, @barman! Lulz woo! #hellos"
66
- record = {
67
- :user => "fooman",
68
- :status => {
69
- :created_at => Time.at(1),
70
- :text => status
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
79
- )
80
- @io.show_record(record)
81
- end
82
-
83
- should "output a record as status info when status is given, with in-reply info" do
84
- status = "Hi, @fooman! How are you doing?"
85
- record = {
86
- :user => "barman",
87
- :status => {
88
- :created_at => Time.at(1),
89
- :text => status,
90
- :in_reply_to => "fooman"
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
99
- )
100
- @io.show_record(record)
101
- end
96
+ END
97
+ )
98
+ @io.show_record(record)
99
+ end
102
100
 
103
- should "output a preview of a status" do
104
- status = "@nick, check http://bit.ly/18rU_Vx"
105
- @output.expects(:puts).with(<<-END
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
- END
110
- )
111
- @io.show_status_preview(status)
107
+ END
108
+ )
109
+ @io.show_status_preview(status)
110
+ end
112
111
  end
113
- end
114
112
 
115
- context "An IO, with colorization enabled" do
116
- setup do
117
- @input = mock()
118
- @output = mock()
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
- should "output a record as user info when no status is given" do
123
- record = { :user => "fooman" }
124
- @output.expects(:puts).with(<<-END
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
- END
128
- )
129
- @io.show_record(record)
130
- end
131
-
132
- should "output a record as status info when status is given, without in-reply info" do
133
- record = {
134
- :user => "fooman",
135
- :status => {
136
- :created_at => Time.at(1),
137
- :text => "Wondering the meaning of life."
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
146
- )
147
- @io.show_record(record)
148
- end
149
-
150
- should "output a record as status info when status is given, with in-reply info" do
151
- record = {
152
- :user => "barman",
153
- :status => {
154
- :created_at => Time.at(1),
155
- :text => "Hi, @fooman! How are you doing? #hellos",
156
- :in_reply_to => "fooman"
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
165
- )
166
- @io.show_record(record)
167
- end
160
+ END
161
+ )
162
+ @io.show_record(record)
163
+ end
168
164
 
169
- should "output a preview of a status" do
170
- status = "@nick, check http://bit.ly/18rU_Vx"
171
- @output.expects(:puts).with(<<-END
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
- END
176
- )
177
- @io.show_status_preview(status)
178
- end
179
-
180
- should "highlight HTTP and HTTPS URLs in a status" do
181
- record = {
182
- :user => "barman",
183
- :status => {
184
- :created_at => Time.at(1),
185
- :text => "Three links: http://bit.ly/18rU_Vx http://is.gd/1qLk3 and https://is.gd/2rLk4",
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
194
- )
195
- @io.show_record(record)
196
- end
197
-
198
- should "highlight HTTP and HTTPS URLs in a status, even if duplicates" do
199
- record = {
200
- :user => "barman",
201
- :status => {
202
- :created_at => Time.at(1),
203
- :text => "Duplicate links: http://is.gd/1qLk3 and http://is.gd/1qLk3",
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
212
- )
213
- @io.show_record(record)
214
- end
215
-
216
- should "highlight usernames in a status" do
217
- record = {
218
- :user => "barman",
219
- :status => {
220
- :created_at => Time.at(1),
221
- :text => "I salute you @fooman, @barbaz, and @spoonman!",
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
- Util.expects(:humanize_time_diff).returns([2, "secs"])
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
- END
230
- )
231
- @io.show_record(record)
225
+ END
226
+ )
227
+ @io.show_record(record)
228
+ end
232
229
  end
233
230
  end
234
231
 
@@ -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 rest client wrapper" do
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) { RestClientWrapper.get("https://secret:agent@hushhush.net") }
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::ECONNRESET)
19
- assert_raise(ClientError) { RestClientWrapper.get("http://www.invalid.net") }
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) { RestClientWrapper.get("http://unknown.net") }
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
@@ -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 "To initialize a StartupConfig" do
9
- should "require at least one supported command" do
10
- assert_raise(ArgumentError) { StartupConfig.new([]) }
11
- assert_nothing_raised { StartupConfig.new([:default_action]) }
12
- end
13
- end
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
- should "use the first supported command as a default command when given no command as a cmdline argument" do
21
- @config.parse
22
- assert_equal :default_action, @config.command
23
- end
16
+ context "at runtime" do
17
+ setup do
18
+ @config = StartupConfig.new([:default_action, :another_action])
19
+ end
24
20
 
25
- should "check that given command is supported" do
26
- @config.parse(%w{default_action}) { |args| {} }
27
- assert_equal :default_action, @config.command
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
- @config.parse(%w{another_action}) { |args| {} }
30
- assert_equal :another_action, @config.command
31
- end
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
- should "parse cmdline args, command, and leftover args" do
34
- @config.parse(%w{--opt foo another_action left overs}) do |args|
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
- context "when given no cmdline args and a config file" do
44
- setup do
45
- @config.parse([], TEST_CONFIG_FILE)
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
- should "have the parsed option defined" do
49
- assert_equal false, @config.options[:colorize]
50
- end
51
- end
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
- context "when given cmdline args and no config file" do
54
- setup do
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
- should "have the parsed option defined" do
62
- assert_equal "foo", @config.options[:opt]
63
- end
64
- end
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
- context "when given an option both as a cmdline option and in a config file" do
67
- setup do
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
- should "the command line option should override the config file option" do
75
- assert_equal true, @config.options[:colorize]
76
- end
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
- should "have nil for an undefined option" do
79
- assert_nil @config.options[:num_statuses]
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