tweetwine 0.4.1 → 0.4.2
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/CHANGELOG.rdoc +6 -0
- data/Rakefile +22 -23
- data/lib/tweetwine/cli.rb +167 -165
- data/lib/tweetwine/config.rb +3 -2
- data/lib/tweetwine/exceptions.rb +1 -0
- data/lib/tweetwine/version.rb +1 -1
- data/project.rb +16 -16
- data/test/fixture/{config_example.yaml → config_integration.yaml} +0 -0
- data/test/fixture/oauth.rb +11 -11
- data/test/{test_helper.rb → helper.rb} +16 -7
- data/test/{example/authorization_example.rb → integration/authorization_test.rb} +17 -17
- data/test/integration/global_options_test.rb +67 -0
- data/test/integration/helper.rb +70 -0
- data/test/integration/invalid_config_file_test.rb +28 -0
- data/test/integration/search_statuses_test.rb +81 -0
- data/test/integration/show_followers_test.rb +24 -0
- data/test/integration/show_friends_test.rb +24 -0
- data/test/integration/show_home_test.rb +47 -0
- data/test/integration/show_mentions_test.rb +24 -0
- data/test/integration/show_user_test.rb +48 -0
- data/test/integration/update_status_test.rb +199 -0
- data/test/integration/use_http_proxy_test.rb +71 -0
- data/test/{example/user_help_example.rb → integration/user_help_test.rb} +36 -36
- data/test/unit/character_encoding_test.rb +19 -15
- data/test/unit/cli_test.rb +9 -10
- data/test/unit/config_test.rb +73 -71
- data/test/unit/helper.rb +108 -0
- data/test/unit/http_test.rb +39 -39
- data/test/unit/oauth_test.rb +15 -16
- data/test/unit/obfuscate_test.rb +4 -4
- data/test/unit/option_parser_test.rb +12 -12
- data/test/unit/promise_test.rb +10 -10
- data/test/unit/support_test.rb +44 -45
- data/test/unit/tweet_helper.rb +1 -1
- data/test/unit/tweet_test.rb +42 -42
- data/test/unit/twitter_test.rb +300 -303
- data/test/unit/ui_test.rb +310 -312
- data/test/unit/uri_test.rb +7 -7
- data/test/unit/url_shortener_test.rb +77 -79
- data/tweetwine.gemspec +6 -15
- metadata +55 -145
- data/test/example/example_helper.rb +0 -58
- data/test/example/global_options_example.rb +0 -64
- data/test/example/search_statuses_example.rb +0 -76
- data/test/example/show_followers_example.rb +0 -24
- data/test/example/show_friends_example.rb +0 -24
- data/test/example/show_home_example.rb +0 -44
- data/test/example/show_mentions_example.rb +0 -24
- data/test/example/show_user_example.rb +0 -44
- data/test/example/update_status_example.rb +0 -183
- data/test/example/use_http_proxy_example.rb +0 -68
- data/test/unit/unit_helper.rb +0 -111
@@ -0,0 +1,199 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'integration/helper'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Tweetwine::Test::Integration
|
7
|
+
|
8
|
+
class UpdateStatusTest < TestCase
|
9
|
+
RUBYGEMS_FIXTURE = fixture_file 'shorten_rubygems.html'
|
10
|
+
RUBYGEMS_FULL_URL = 'http://rubygems.org/'
|
11
|
+
RUBYGEMS_FULL_URL_ENC = 'http%3a%2f%2frubygems.org%2f'
|
12
|
+
RUBYGEMS_SHORT_URL = 'http://is.gd/gGazV'
|
13
|
+
RUBYGEMS_SHORT_URL_ENC = 'http%3a%2f%2fis.gd%2fgGazV'
|
14
|
+
RUBYLANG_FIXTURE = fixture_file 'shorten_rubylang.html'
|
15
|
+
RUBYLANG_FULL_URL = 'http://ruby-lang.org/'
|
16
|
+
RUBYLANG_FULL_URL_ENC = 'http%3a%2f%2fruby-lang.org%2f'
|
17
|
+
RUBYLANG_SHORT_URL = 'http://is.gd/gGaM3'
|
18
|
+
RUBYLANG_SHORT_URL_ENC = 'http%3a%2f%2fis.gd%2fgGaM3'
|
19
|
+
SHORTEN_CONFIG = read_shorten_config
|
20
|
+
SHORTEN_METHOD = SHORTEN_CONFIG[:method].to_sym
|
21
|
+
STATUS_WITH_FULL_URLS = "ruby links: #{RUBYGEMS_FULL_URL} #{RUBYLANG_FULL_URL}"
|
22
|
+
STATUS_WITH_SHORT_URLS = "ruby links: #{RUBYGEMS_SHORT_URL} #{RUBYLANG_SHORT_URL}"
|
23
|
+
STATUS_WITHOUT_URLS = "bored. going to sleep."
|
24
|
+
UPDATE_FIXTURE_WITH_URLS = fixture_file 'update_with_urls.json'
|
25
|
+
UPDATE_FIXTURE_WITHOUT_URLS = fixture_file 'update_without_urls.json'
|
26
|
+
UPDATE_FIXTURE_UTF8 = fixture_file 'update_utf8.json'
|
27
|
+
UPDATE_URL = "https://api.twitter.com/1/statuses/update.json"
|
28
|
+
|
29
|
+
BODY_WITH_SHORT_URLS = "status=ruby%20links%3a%20#{RUBYGEMS_SHORT_URL_ENC}%20#{RUBYLANG_SHORT_URL_ENC}"
|
30
|
+
BODY_WITHOUT_URLS = "status=bored.%20going%20to%20sleep."
|
31
|
+
|
32
|
+
describe "update my status from command line with colorization disabled" do
|
33
|
+
before do
|
34
|
+
stub_http_request(:post, UPDATE_URL).with(:body => BODY_WITHOUT_URLS).to_return(:body => UPDATE_FIXTURE_WITHOUT_URLS)
|
35
|
+
at_snapshot do
|
36
|
+
@output = start_cli %W{--no-colors update #{STATUS_WITHOUT_URLS}}, %w{y}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "sends and shows the status" do
|
41
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
42
|
+
@output[6].must_equal STATUS_WITHOUT_URLS
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "update my status from command line with colorization enabled" do
|
47
|
+
before do
|
48
|
+
stub_http_request(:post, UPDATE_URL).with(:body => BODY_WITHOUT_URLS).to_return(:body => UPDATE_FIXTURE_WITHOUT_URLS)
|
49
|
+
at_snapshot do
|
50
|
+
@output = start_cli %W{--colors update #{STATUS_WITHOUT_URLS}}, %w{y}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sends and shows the status" do
|
55
|
+
@output[5].must_equal "\e[32m#{USER}\e[0m, 9 hours ago:"
|
56
|
+
@output[6].must_equal STATUS_WITHOUT_URLS
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "update my status from command line when message is spread over multiple arguments" do
|
61
|
+
before do
|
62
|
+
stub_http_request(:post, UPDATE_URL).with(:body => BODY_WITHOUT_URLS).to_return(:body => UPDATE_FIXTURE_WITHOUT_URLS)
|
63
|
+
at_snapshot do
|
64
|
+
@output = start_cli(%w{--no-colors update} + STATUS_WITHOUT_URLS.split, %w{y})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "sends and shows the status" do
|
69
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
70
|
+
@output[6].must_equal STATUS_WITHOUT_URLS
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "cancel status update from command line" do
|
75
|
+
before do
|
76
|
+
@output = start_cli %W{--no-colors update #{STATUS_WITHOUT_URLS}}, %w{n}
|
77
|
+
end
|
78
|
+
|
79
|
+
it "shows cancellation message" do
|
80
|
+
@output[3].must_match(/Cancelled./)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "update my status from STDIN" do
|
85
|
+
before do
|
86
|
+
stub_http_request(:post, UPDATE_URL).with(:body => BODY_WITHOUT_URLS).to_return(:body => UPDATE_FIXTURE_WITHOUT_URLS)
|
87
|
+
at_snapshot do
|
88
|
+
@output = start_cli %w{update}, [STATUS_WITHOUT_URLS, 'y']
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it "sends and shows the status" do
|
93
|
+
@output[0].must_equal "Status update: "
|
94
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
95
|
+
@output[6].must_equal STATUS_WITHOUT_URLS
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "cancel a status update from STDIN" do
|
100
|
+
before do
|
101
|
+
@output = start_cli %w{update}, [STATUS_WITHOUT_URLS, 'n']
|
102
|
+
end
|
103
|
+
|
104
|
+
it "shows a cancellation message" do
|
105
|
+
@output[3].must_match(/Cancelled./)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
if defined? Encoding
|
110
|
+
describe "encode status in UTF-8 (String supports encoding)" do
|
111
|
+
before do
|
112
|
+
@status_utf8 = "résumé"
|
113
|
+
@status_latin1 = @status_utf8.encode('ISO-8859-1')
|
114
|
+
url_encoded_body = "status=r%c3%a9sum%c3%a9"
|
115
|
+
stub_http_request(:post, UPDATE_URL).with(:body => url_encoded_body).to_return(:body => UPDATE_FIXTURE_UTF8)
|
116
|
+
at_snapshot do
|
117
|
+
@output = start_cli %W{--no-colors update #{@status_latin1}}, %w{y}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
it "sends and shows the status" do
|
122
|
+
# NOTE: Should be in latin-1, but StringIO converts it to UTF-8. At
|
123
|
+
# least on tty Ruby 1.9.2 outputs it in latin-1.
|
124
|
+
#@output[1].should == @status_latin1 # preview
|
125
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
126
|
+
@output[6].must_equal @status_utf8
|
127
|
+
end
|
128
|
+
end
|
129
|
+
else
|
130
|
+
describe "encode status in UTF-8 (String does not support encoding)" do
|
131
|
+
before do
|
132
|
+
@status_latin1 = "r\xe9sum\xe9"
|
133
|
+
@status_utf8 = "r\xc3\xa9sum\xc3\xa9"
|
134
|
+
url_encoded_body = "status=r%c3%a9sum%c3%a9"
|
135
|
+
stub_http_request(:post, UPDATE_URL).with(:body => url_encoded_body).to_return(:body => UPDATE_FIXTURE_UTF8)
|
136
|
+
tmp_kcode('NONE') do
|
137
|
+
tmp_env(:LANG => 'latin1') do
|
138
|
+
Tweetwine::CharacterEncoding.forget_guess
|
139
|
+
at_snapshot do
|
140
|
+
@output = start_cli %W{--no-colors update #{@status_latin1}}, %w{y}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
it "sends and shows the status" do
|
147
|
+
@output[1].must_equal @status_latin1 # preview
|
148
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
149
|
+
@output[6].must_equal @status_utf8
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "shorten URLs in status update" do
|
155
|
+
before do
|
156
|
+
@shorten_rubygems_body = "#{SHORTEN_CONFIG[:url_param_name]}=#{RUBYGEMS_FULL_URL_ENC}"
|
157
|
+
@shorten_rubylang_body = "#{SHORTEN_CONFIG[:url_param_name]}=#{RUBYLANG_FULL_URL_ENC}"
|
158
|
+
stub_http_request(SHORTEN_METHOD, SHORTEN_CONFIG[:service_url]).
|
159
|
+
with(:body => @shorten_rubygems_body).
|
160
|
+
to_return(:body => RUBYGEMS_FIXTURE)
|
161
|
+
stub_http_request(SHORTEN_METHOD, SHORTEN_CONFIG[:service_url]).
|
162
|
+
with(:body => @shorten_rubylang_body).
|
163
|
+
to_return(:body => RUBYLANG_FIXTURE)
|
164
|
+
stub_http_request(:post, UPDATE_URL).
|
165
|
+
with(:body => BODY_WITH_SHORT_URLS).
|
166
|
+
to_return(:body => UPDATE_FIXTURE_WITH_URLS)
|
167
|
+
at_snapshot do
|
168
|
+
@output = start_cli %W{--no-colors update #{STATUS_WITH_FULL_URLS}}, %w{y}
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
it "shortens the URLs in the status before sending it" do
|
173
|
+
assert_requested(SHORTEN_METHOD, SHORTEN_CONFIG[:service_url], :body => @shorten_rubygems_body)
|
174
|
+
assert_requested(SHORTEN_METHOD, SHORTEN_CONFIG[:service_url], :body => @shorten_rubylang_body)
|
175
|
+
@output[1].must_equal STATUS_WITH_SHORT_URLS
|
176
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
177
|
+
@output[6].must_equal STATUS_WITH_SHORT_URLS
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "disable URL shortening for status updates" do
|
182
|
+
before do
|
183
|
+
stub_http_request(:post, UPDATE_URL).
|
184
|
+
with(:body => BODY_WITH_SHORT_URLS).
|
185
|
+
to_return(:body => UPDATE_FIXTURE_WITH_URLS)
|
186
|
+
at_snapshot do
|
187
|
+
@output = start_cli %W{--no-colors --no-url-shorten update #{STATUS_WITH_SHORT_URLS}}, %w{y}
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
it "passes URLs as is in the status" do
|
192
|
+
@output[1].must_equal STATUS_WITH_SHORT_URLS
|
193
|
+
@output[5].must_equal "#{USER}, 9 hours ago:"
|
194
|
+
@output[6].must_equal STATUS_WITH_SHORT_URLS
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'integration/helper'
|
4
|
+
|
5
|
+
module Tweetwine::Test::Integration
|
6
|
+
|
7
|
+
class UseHttpProxyTest < TestCase
|
8
|
+
HOME_URL = "https://api.twitter.com/1/statuses/home_timeline.json?count=20&page=1"
|
9
|
+
|
10
|
+
before do
|
11
|
+
stub_http_request(:get, HOME_URL).to_return(:body => fixture_file('home.json'))
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
ENV.delete 'http_proxy'
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "enable proxy via environment variable" do
|
19
|
+
before do
|
20
|
+
ENV['http_proxy'] = PROXY_URL
|
21
|
+
@output = start_cli %w{home}
|
22
|
+
end
|
23
|
+
|
24
|
+
it "uses the proxy to fetch my home timeline" do
|
25
|
+
must_use_proxy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "enable proxy via command line option" do
|
30
|
+
before do
|
31
|
+
ENV.delete 'http_proxy'
|
32
|
+
@output = start_cli %W{--http-proxy #{PROXY_URL} home}
|
33
|
+
end
|
34
|
+
|
35
|
+
it "uses the proxy to fetch my home timeline" do
|
36
|
+
must_use_proxy
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "disable proxy via command line option" do
|
41
|
+
before do
|
42
|
+
ENV['http_proxy'] = PROXY_URL
|
43
|
+
@output = start_cli %w{--no-http-proxy home}
|
44
|
+
end
|
45
|
+
|
46
|
+
it "the application does not use the proxy to fetch my home timeline" do
|
47
|
+
refute_use_proxy
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def must_use_proxy
|
54
|
+
nh = net_http
|
55
|
+
nh.proxy_class?.must_equal true
|
56
|
+
nh.instance_variable_get(:@proxy_address).must_equal PROXY_HOST
|
57
|
+
nh.instance_variable_get(:@proxy_port).must_equal PROXY_PORT
|
58
|
+
assert_requested(:get, HOME_URL)
|
59
|
+
end
|
60
|
+
|
61
|
+
def refute_use_proxy
|
62
|
+
net_http.proxy_class?.must_equal false
|
63
|
+
assert_requested(:get, HOME_URL)
|
64
|
+
end
|
65
|
+
|
66
|
+
def net_http
|
67
|
+
CLI.http.instance_variable_get(:@http)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -1,37 +1,35 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'integration/helper'
|
4
4
|
|
5
|
-
|
6
|
-
as_a "user"
|
7
|
-
i_want_to "see help and error messages"
|
8
|
-
in_order_to "know how to use the application"
|
5
|
+
module Tweetwine::Test::Integration
|
9
6
|
|
7
|
+
class UserHelpTest < TestCase
|
10
8
|
%w{-v version ver v}.each do |arg|
|
11
|
-
|
12
|
-
|
9
|
+
describe "show version with '#{arg}'" do
|
10
|
+
before do
|
13
11
|
@status = start_app [arg] do |_, _, stdout|
|
14
12
|
@output = stdout.readlines.join
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
|
19
|
-
@output.
|
20
|
-
@status.exitstatus.
|
16
|
+
it "shows version and exists with success status" do
|
17
|
+
@output.must_match(/\d+\.\d+\.\d+$/)
|
18
|
+
@status.exitstatus.must_equal 0
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
23
|
%w{-h help}.each do |arg|
|
26
|
-
|
27
|
-
|
24
|
+
describe "show general help with '#{arg}'" do
|
25
|
+
before do
|
28
26
|
@status = start_app [arg] do |_, _, stdout|
|
29
27
|
@output = stdout.readlines.join
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
|
-
|
34
|
-
@output.
|
31
|
+
it "shows help and exists with success status" do
|
32
|
+
@output.must_equal <<-END
|
35
33
|
#{Tweetwine.summary}
|
36
34
|
|
37
35
|
Usage: #{CLI::EXEC_NAME} [global_options...] [<command>] [command_options...]
|
@@ -63,43 +61,43 @@ Usage: #{CLI::EXEC_NAME} [global_options...] [<command>] [command_options...]
|
|
63
61
|
user Show user's timeline.
|
64
62
|
version Show program version and exit.
|
65
63
|
END
|
66
|
-
@status.exitstatus.
|
64
|
+
@status.exitstatus.must_equal 0
|
67
65
|
end
|
68
66
|
end
|
69
67
|
|
70
68
|
%w{followers friends help home mentions search update user version}.each do |command|
|
71
|
-
|
72
|
-
|
69
|
+
describe "show command specific help with '#{arg} #{command}'" do
|
70
|
+
before do
|
73
71
|
@status = start_app [arg, command] do |_, _, stdout|
|
74
72
|
@output = stdout.readlines.join
|
75
73
|
end
|
76
74
|
end
|
77
75
|
|
78
|
-
|
76
|
+
it "shows help about the command and exits with success status" do
|
79
77
|
cmd_class = Tweetwine::CLI.const_get("#{command.capitalize}Command")
|
80
78
|
expected_about = cmd_class.about
|
81
79
|
expected_usage = "Usage: tweetwine #{command} #{cmd_class.usage}".strip
|
82
|
-
@output.
|
80
|
+
@output.must_equal <<-END
|
83
81
|
#{expected_about}
|
84
82
|
|
85
83
|
#{expected_usage}
|
86
84
|
END
|
87
|
-
@status.exitstatus.
|
85
|
+
@status.exitstatus.must_equal 0
|
88
86
|
end
|
89
87
|
end
|
90
88
|
end
|
91
89
|
|
92
|
-
|
93
|
-
|
90
|
+
describe "show help command's help with '#{arg} <invalid_command>'" do
|
91
|
+
before do
|
94
92
|
@status = start_app [arg, 'invalid'] do |_, _, stdout, stderr|
|
95
93
|
@stdout = stdout.readlines.join
|
96
94
|
@stderr = stderr.readlines.join
|
97
95
|
end
|
98
96
|
end
|
99
97
|
|
100
|
-
|
101
|
-
@stderr.
|
102
|
-
@stdout.
|
98
|
+
it "shows help about help command and exits with failure status" do
|
99
|
+
@stderr.must_equal "ERROR: unknown command: invalid\n\n"
|
100
|
+
@stdout.must_equal <<-END
|
103
101
|
Show help and exit. Try it with <command> argument.
|
104
102
|
|
105
103
|
Usage: tweetwine help [<command>]
|
@@ -107,34 +105,36 @@ Usage: tweetwine help [<command>]
|
|
107
105
|
If <command> is given, show specific help about that command. If no
|
108
106
|
<command> is given, show general help.
|
109
107
|
END
|
110
|
-
@status.exitstatus.
|
108
|
+
@status.exitstatus.must_equal CommandLineError.status_code
|
111
109
|
end
|
112
110
|
end
|
113
111
|
end
|
114
112
|
|
115
|
-
|
116
|
-
|
113
|
+
describe "show error and exit with failure status when invalid option" do
|
114
|
+
before do
|
117
115
|
@status = start_app %w{-X} do |_, _, _, stderr|
|
118
116
|
@output = stderr.readlines.join.chomp
|
119
117
|
end
|
120
118
|
end
|
121
119
|
|
122
|
-
|
123
|
-
@output.
|
124
|
-
@status.exitstatus.
|
120
|
+
it "exists with failure status" do
|
121
|
+
@output.must_equal 'ERROR: invalid option: -X'
|
122
|
+
@status.exitstatus.must_equal CommandLineError.status_code
|
125
123
|
end
|
126
124
|
end
|
127
125
|
|
128
|
-
|
129
|
-
|
126
|
+
describe "show error and exit with failure status when invalid command" do
|
127
|
+
before do
|
130
128
|
@status = start_app %w{invalid} do |_, _, _, stderr|
|
131
129
|
@output = stderr.readlines.join.chomp
|
132
130
|
end
|
133
131
|
end
|
134
132
|
|
135
|
-
|
136
|
-
@output.
|
137
|
-
@status.exitstatus.
|
133
|
+
it "exists with failure status" do
|
134
|
+
@output.must_equal 'ERROR: unknown command: invalid'
|
135
|
+
@status.exitstatus.must_equal UnknownCommandError.status_code
|
138
136
|
end
|
139
137
|
end
|
140
138
|
end
|
139
|
+
|
140
|
+
end
|
@@ -1,24 +1,24 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'unit/helper'
|
4
4
|
|
5
|
-
module Tweetwine::Test
|
5
|
+
module Tweetwine::Test::Unit
|
6
6
|
|
7
|
-
class CharacterEncodingTest <
|
7
|
+
class CharacterEncodingTest < TestCase
|
8
8
|
if defined? Encoding
|
9
|
-
|
10
|
-
|
9
|
+
describe "when transcoding to UTF-8 when String supports encoding" do
|
10
|
+
it "transcodes string to UTF-8" do
|
11
11
|
str_utf8 = "groß résumé"
|
12
12
|
str_latin1 = str_utf8.encode('ISO-8859-1')
|
13
13
|
assert_equal str_utf8, CharacterEncoding.to_utf8(str_latin1)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
it "raises exception if result is invalid UTF-8" do
|
17
|
+
assert_raises(TranscodeError) { CharacterEncoding.to_utf8("\xa4") }
|
18
18
|
end
|
19
19
|
end
|
20
20
|
else
|
21
|
-
|
21
|
+
describe "when transcoding to UTF-8 when String does not support encoding" do
|
22
22
|
# résumé
|
23
23
|
RESUME_EUC = "r\x8F\xAB\xB1sum\x8F\xAB\xB1"
|
24
24
|
RESUME_LATIN1 = "r\xe9sum\xe9"
|
@@ -28,7 +28,11 @@ class CharacterEncodingTest < UnitTestCase
|
|
28
28
|
HOME_SJIS = "\x83\x7a\x81\x5b\x83\x80"
|
29
29
|
HOME_UTF8 = "\xe3\x83\x9b\xe3\x83\xbc\xe3\x83\xa0"
|
30
30
|
|
31
|
-
|
31
|
+
before do
|
32
|
+
Tweetwine::CharacterEncoding.forget_guess
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
32
36
|
Tweetwine::CharacterEncoding.forget_guess
|
33
37
|
end
|
34
38
|
|
@@ -36,7 +40,7 @@ class CharacterEncodingTest < UnitTestCase
|
|
36
40
|
['EUC', RESUME_EUC, RESUME_UTF8],
|
37
41
|
['SJIS', HOME_SJIS, HOME_UTF8]
|
38
42
|
].each do |(kcode, original, expected)|
|
39
|
-
|
43
|
+
it "transcodes with Iconv, guessing first from $KCODE, case #{kcode}" do
|
40
44
|
tmp_kcode(kcode) do
|
41
45
|
assert_equal expected, CharacterEncoding.to_utf8(original)
|
42
46
|
end
|
@@ -48,7 +52,7 @@ class CharacterEncodingTest < UnitTestCase
|
|
48
52
|
['EUC-JP', RESUME_EUC, RESUME_UTF8],
|
49
53
|
['SHIFT_JIS', HOME_SJIS, HOME_UTF8]
|
50
54
|
].each do |(lang, original, expected)|
|
51
|
-
|
55
|
+
it "transcodes with Iconv, guessing second from envar $LANG, case #{lang}" do
|
52
56
|
tmp_kcode('NONE') do
|
53
57
|
tmp_env(:LANG => lang) do
|
54
58
|
assert_equal expected, CharacterEncoding.to_utf8(original)
|
@@ -57,14 +61,14 @@ class CharacterEncodingTest < UnitTestCase
|
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
|
-
|
64
|
+
it "passes string as is, if guess is UTF-8, case $KCODE is UTF-8" do
|
61
65
|
tmp_kcode('UTF8') do
|
62
66
|
assert_same RESUME_UTF8, CharacterEncoding.to_utf8(RESUME_UTF8)
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
66
70
|
%w{utf8 UTF-8 en_US.UTF-8 fi_FI.utf-8 fi_FI.utf8}.each do |lang|
|
67
|
-
|
71
|
+
it "passes string as is, if guess is UTF-8, case envar $LANG is '#{lang}'" do
|
68
72
|
tmp_kcode('NONE') do
|
69
73
|
tmp_env(:LANG => lang) do
|
70
74
|
assert_same RESUME_UTF8, CharacterEncoding.to_utf8(RESUME_UTF8)
|
@@ -73,10 +77,10 @@ class CharacterEncodingTest < UnitTestCase
|
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
76
|
-
|
80
|
+
it "raises exception if conversion cannot be done because we couldn't guess external encoding" do
|
77
81
|
tmp_kcode('NONE') do
|
78
82
|
tmp_env(:LANG => nil) do
|
79
|
-
|
83
|
+
assert_raises(Tweetwine::TranscodeError) { CharacterEncoding.to_utf8(RESUME_LATIN1) }
|
80
84
|
end
|
81
85
|
end
|
82
86
|
end
|
data/test/unit/cli_test.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'unit/helper'
|
4
|
+
require 'stringio'
|
4
5
|
|
5
|
-
|
6
|
+
module Tweetwine::Test::Unit
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
should "disallow using #new to create a new instance" do
|
13
|
-
assert_raise(NoMethodError) { CLI.new }
|
8
|
+
# See `test/integration` directory for integration tests.
|
9
|
+
class CLITest < TestCase
|
10
|
+
describe "for initialization" do
|
11
|
+
it "disallows using #new to create a new instance" do
|
12
|
+
assert_raises(NoMethodError) { CLI.new }
|
14
13
|
end
|
15
14
|
|
16
|
-
|
15
|
+
it "allows defining same option multiple times, last value winning" do
|
17
16
|
winning_option_value = 'second'
|
18
17
|
start_cli %W{-f first -f #{winning_option_value} -v}
|
19
18
|
assert_equal winning_option_value, CLI.config[:config_file]
|