urban 1.0.0 → 2.0.0
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.
- checksums.yaml +7 -0
- data/.travis.yml +10 -2
- data/HISTORY.rdoc +33 -18
- data/Manifest.txt +25 -0
- data/README.rdoc +16 -15
- data/Rakefile +14 -31
- data/bin/urban +1 -1
- data/lib/urban.rb +4 -1
- data/lib/urban/cli.rb +64 -74
- data/lib/urban/dictionary.rb +11 -11
- data/lib/urban/web.rb +27 -15
- data/script/test +10 -0
- data/test/fixtures/impromptu.html +1039 -0
- data/test/{data → fixtures}/missing.html +0 -0
- data/test/fixtures/screens/definition.txt +5 -0
- data/test/fixtures/screens/definition_with_url.txt +7 -0
- data/test/fixtures/screens/definitions.txt +9 -0
- data/test/fixtures/screens/help.txt +19 -0
- data/test/fixtures/screens/invalid_option_error.txt +2 -0
- data/test/fixtures/screens/missing_phrase_error.txt +1 -0
- data/test/fixtures/screens/no_internet_error.txt +1 -0
- data/test/test_helper.rb +27 -20
- data/test/urban/cli_test.rb +77 -135
- data/test/urban/dictionary_test.rb +29 -19
- data/test/urban/web_test.rb +27 -45
- metadata +113 -102
- data/.gitignore +0 -26
- data/Gemfile +0 -4
- data/lib/urban/version.rb +0 -3
- data/test/data/impromptu.html +0 -463
- data/test/minitest/stop_light.rb +0 -57
- data/urban.gemspec +0 -25
File without changes
|
@@ -0,0 +1,9 @@
|
|
1
|
+
|
2
|
+
IMPROMPTU
|
3
|
+
|
4
|
+
Something that is made up on the spot and given little time to gather and present. Usually referring to speeches that are given only a few minutes to prepare for.
|
5
|
+
|
6
|
+
On the spot
|
7
|
+
|
8
|
+
Something that is made up on the spot. Can also mean a speech that was made with little or no preparation.
|
9
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Usage: urban [OPTION]... [PHRASE]
|
2
|
+
Search http://urbandictionary.com for definitions of phrases
|
3
|
+
|
4
|
+
Options:
|
5
|
+
-a, --all List all definitions
|
6
|
+
-r, --random Return a random phrase and definition
|
7
|
+
-u, --url Print the definition's url after the definition
|
8
|
+
-h, --help Show this message
|
9
|
+
-v, --version Show version information
|
10
|
+
|
11
|
+
Examples:
|
12
|
+
urban cookie monster Search for "cookie monster" and print its
|
13
|
+
first definition
|
14
|
+
urban -a cookie monster Search for "cookie monster" and print all of
|
15
|
+
its available definitions
|
16
|
+
urban -r Print a random phrase and its first definition
|
17
|
+
urban -ra Print a random phrase and all of its available
|
18
|
+
definitions
|
19
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
urban: no definitions found for GUBBLE.
|
@@ -0,0 +1 @@
|
|
1
|
+
urban: no internet connection available.
|
data/test/test_helper.rb
CHANGED
@@ -1,28 +1,35 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
2
|
|
3
|
-
require
|
4
|
-
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require 'ostruct'
|
3
|
+
require "rubygems"
|
4
|
+
require "minitest/autorun"
|
5
|
+
require "minitest/pride"
|
6
|
+
require "minitest/hell"
|
7
|
+
require "urban"
|
8
|
+
require "ostruct"
|
10
9
|
|
11
|
-
|
12
|
-
[ 'Something that is made up on the spot and given little time to gather and present. Usually referring to speeches that are given only a few minutes to prepare for.',
|
13
|
-
'On the spot',
|
14
|
-
'Something that is made up on the spot. Can also mean a speech that was made with little or no preparation.' ],
|
15
|
-
'http://www.urbandictionary.com/define.php?term=impromptu')
|
10
|
+
class Urban::Test < Minitest::Test
|
16
11
|
|
17
|
-
|
12
|
+
def load_fixture(filename)
|
13
|
+
IO.read(File.expand_path("../fixtures/#{filename}", __FILE__))
|
14
|
+
end
|
18
15
|
|
19
|
-
def
|
20
|
-
|
21
|
-
end
|
16
|
+
def empty_entry
|
17
|
+
@empty_entry ||= Urban::Dictionary::Entry.new('gubble', nil, nil)
|
18
|
+
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
def test_entry
|
21
|
+
@test_entry ||= Urban::Dictionary::Entry.new(
|
22
|
+
"impromptu",
|
23
|
+
[
|
24
|
+
"Something that is made up on the spot and given little time to " +
|
25
|
+
"gather and present. Usually referring to speeches that are given " +
|
26
|
+
"only a few minutes to prepare for.",
|
27
|
+
"On the spot",
|
28
|
+
"Something that is made up on the spot. " +
|
29
|
+
"Can also mean a speech that was made with little or no preparation."
|
30
|
+
],
|
31
|
+
"http://www.urbandictionary.com/define.php?term=impromptu"
|
32
|
+
)
|
27
33
|
end
|
34
|
+
|
28
35
|
end
|
data/test/urban/cli_test.rb
CHANGED
@@ -1,212 +1,154 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require 'shellwords'
|
5
|
-
|
6
|
-
class CLITest < MiniTest::Unit::TestCase
|
7
|
-
|
8
|
-
HELP_SCREEN = <<-EOS
|
9
|
-
Usage: urban [OPTION]... [PHRASE]
|
10
|
-
Search http://urbandictionary.com for definitions of phrases
|
11
|
-
|
12
|
-
Options:
|
13
|
-
-a, --all List all definitions
|
14
|
-
-r, --random Return a random phrase and definition
|
15
|
-
-u, --url Print the definition's url after the definition
|
16
|
-
-h, --help Show this message
|
17
|
-
-v, --version Show version information
|
18
|
-
-l, --list DEPRECATED please use --all or -a instead
|
19
|
-
|
20
|
-
Examples:
|
21
|
-
urban cookie monster Search for "cookie monster" and print its
|
22
|
-
first definition
|
23
|
-
urban -a cookie monster Search for "cookie monster" and print all of
|
24
|
-
its available definitions
|
25
|
-
urban -r Print a random phrase and its first definition
|
26
|
-
urban -ra Print a random phrase and all of its available
|
27
|
-
definitions
|
28
|
-
|
29
|
-
EOS
|
1
|
+
require "test_helper"
|
2
|
+
require "shellwords"
|
3
|
+
require "urban/cli"
|
30
4
|
|
31
|
-
|
32
|
-
|
33
|
-
|
5
|
+
class CLITest < Urban::Test
|
6
|
+
|
7
|
+
attr_accessor :program, :dictionary
|
34
8
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
9
|
+
def nothing; "" end
|
10
|
+
|
11
|
+
def assert_program_output(stdout, stderr, args)
|
12
|
+
assert_output stdout, stderr do
|
13
|
+
program.run(Shellwords.shellwords(args))
|
39
14
|
end
|
40
15
|
end
|
41
16
|
|
17
|
+
def setup
|
18
|
+
self.program = Urban::CLI.new
|
19
|
+
self.dictionary = Urban::Dictionary.new
|
20
|
+
|
21
|
+
program.dictionary = dictionary
|
22
|
+
end
|
23
|
+
|
42
24
|
class CLIArgumentParsingTest < CLITest
|
43
25
|
|
44
|
-
# Helpers
|
45
26
|
def assert_flag_is_set(name)
|
46
27
|
["-#{name.chars.first}", "--#{name}"].each do |args|
|
47
|
-
options =
|
48
|
-
|
28
|
+
options = program.send(:parse, [args])
|
29
|
+
assert options.send(name)
|
49
30
|
end
|
50
31
|
end
|
51
32
|
|
52
|
-
# Tests
|
53
33
|
def test_defaults
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
assert_equal('', options.phrase)
|
61
|
-
end
|
34
|
+
options = program.send(:parse, [])
|
35
|
+
refute options.help
|
36
|
+
refute options.version
|
37
|
+
refute options.random
|
38
|
+
refute options.all
|
39
|
+
assert_empty options.phrase
|
62
40
|
end
|
63
41
|
|
64
42
|
def test_phrase
|
65
|
-
|
66
|
-
|
67
|
-
assert_equal('foo bar', options.phrase)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_help_flag
|
72
|
-
assert_silent do
|
73
|
-
assert_flag_is_set('help')
|
74
|
-
end
|
43
|
+
options = program.send :parse, ["foo bar"]
|
44
|
+
assert_equal "foo bar", options.phrase
|
75
45
|
end
|
76
46
|
|
77
47
|
def test_version_flag
|
78
|
-
|
79
|
-
assert_flag_is_set('version')
|
80
|
-
end
|
48
|
+
assert_flag_is_set "version"
|
81
49
|
end
|
82
50
|
|
83
51
|
def test_random_flag
|
84
|
-
|
85
|
-
assert_flag_is_set('random')
|
86
|
-
end
|
52
|
+
assert_flag_is_set "random"
|
87
53
|
end
|
88
54
|
|
89
55
|
def test_all_flag
|
90
|
-
|
91
|
-
assert_flag_is_set('all')
|
92
|
-
end
|
56
|
+
assert_flag_is_set "all"
|
93
57
|
end
|
94
58
|
|
95
|
-
def
|
96
|
-
|
97
|
-
assert_flag_is_set('url')
|
98
|
-
end
|
59
|
+
def test_url_flag
|
60
|
+
assert_flag_is_set "url"
|
99
61
|
end
|
100
62
|
end
|
101
63
|
|
102
64
|
class CLIRunnerStandardOutputTest < CLITest
|
103
65
|
|
104
|
-
SINGLE_DEFINITION = "\n#{TEST_ENTRY.phrase.upcase}\n\n#{TEST_ENTRY.definitions.first}\n\n"
|
105
|
-
MULTIPLE_DEFINITIONS = "\n#{TEST_ENTRY.phrase.upcase}\n\n#{TEST_ENTRY.definitions.join("\n\n")}\n\n"
|
106
|
-
DEFINITION_WITH_URL = "\n#{TEST_ENTRY.phrase.upcase}\n\n#{TEST_ENTRY.definitions.first}\n\nURL: #{TEST_ENTRY.url}\n\n"
|
107
|
-
|
108
66
|
def setup
|
109
67
|
super
|
110
|
-
@dictionary = MiniTest::Mock.new
|
111
68
|
end
|
112
69
|
|
113
|
-
# Tests
|
114
70
|
def test_help_flag_prints_help
|
115
|
-
|
71
|
+
help_screen = load_fixture "screens/help.txt"
|
72
|
+
assert_program_output help_screen, nothing, nothing
|
116
73
|
end
|
117
74
|
|
118
75
|
def test_version_flag_prints_version
|
119
|
-
|
120
|
-
|
121
|
-
end
|
76
|
+
version_screen = "Urban #{Urban::VERSION} (c) Thomas Miller\n"
|
77
|
+
assert_program_output version_screen, nothing, "--version"
|
122
78
|
end
|
123
79
|
|
124
80
|
def test_random_flag_prints_single_definition
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
81
|
+
single_definition = load_fixture "screens/definition.txt"
|
82
|
+
|
83
|
+
dictionary.stub(:random, test_entry) do
|
84
|
+
assert_program_output single_definition, nothing, "--random"
|
85
|
+
end
|
129
86
|
end
|
130
87
|
|
131
88
|
def test_phrase_prints_single_definition
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
89
|
+
single_definition = load_fixture "screens/definition.txt"
|
90
|
+
dictionary.stub(:search, test_entry) do
|
91
|
+
assert_program_output single_definition, nothing, "impromptu"
|
92
|
+
end
|
136
93
|
end
|
137
94
|
|
138
95
|
def test_random_and_all_flag_prints_multiple_definitions
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
96
|
+
multiple_definitions = load_fixture "screens/definitions.txt"
|
97
|
+
dictionary.stub(:random, test_entry) do
|
98
|
+
assert_program_output multiple_definitions, nothing, "--all --random"
|
99
|
+
end
|
143
100
|
end
|
144
101
|
|
145
102
|
def test_phrase_and_all_flag_prints_multiple_definitions
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
103
|
+
multiple_definitions = load_fixture "screens/definitions.txt"
|
104
|
+
dictionary.stub(:search, test_entry) do
|
105
|
+
assert_program_output multiple_definitions, nothing, "--all impromptu"
|
106
|
+
end
|
150
107
|
end
|
151
108
|
|
152
109
|
def test_random_and_url_flag_prints_definition_with_url
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
110
|
+
definition_with_url = load_fixture "screens/definition_with_url.txt"
|
111
|
+
dictionary.stub(:random, test_entry) do
|
112
|
+
assert_program_output definition_with_url, nothing, "--url --random"
|
113
|
+
end
|
157
114
|
end
|
158
115
|
|
159
116
|
def test_phrase_and_url_flag_prints_definition_with_url
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
117
|
+
definition_with_url = load_fixture "screens/definition_with_url.txt"
|
118
|
+
dictionary.stub(:search, test_entry) do
|
119
|
+
assert_program_output definition_with_url, nothing, "--url impromptu"
|
120
|
+
end
|
164
121
|
end
|
165
122
|
|
166
|
-
def test_list_flag_prints_deprecation_warning
|
167
|
-
expected = /WARNING: --list and -l are deprecated please use --all or -a instead/
|
168
|
-
@program.dictionary = @dictionary.expect(:search, TEST_ENTRY, ['impromptu'])
|
169
|
-
@program.dictionary = @dictionary.expect(:random, TEST_ENTRY)
|
170
|
-
stdout, stederr = capture_io { @program.run(Shellwords.shellwords('--list impromptu')) }
|
171
|
-
assert_match expected, stdout
|
172
|
-
stdou, stederr = capture_io { @program.run(Shellwords.shellwords('-rl')) }
|
173
|
-
assert_match expected, stdout
|
174
|
-
end
|
175
123
|
end
|
176
124
|
|
177
125
|
class CLIRunnerErrorOutputTest < CLITest
|
178
126
|
|
179
|
-
ERROR_MISSING_PHRASE = "urban: no definitions found for #{EMPTY_ENTRY.phrase.upcase}.\n"
|
180
|
-
ERROR_NO_INTERNET = "urban: no internet connection available.\n"
|
181
|
-
ERROR_INVALID_OPTION = <<-EOE
|
182
|
-
urban: invalid option: -b
|
183
|
-
Try `urban --help' for more information.
|
184
|
-
EOE
|
185
|
-
|
186
127
|
def setup
|
187
128
|
super
|
188
129
|
end
|
189
130
|
|
190
|
-
# Tests
|
191
131
|
def test_search_missing_phrase_prints_error
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
132
|
+
missing_phrase_error = load_fixture "screens/missing_phrase_error.txt"
|
133
|
+
|
134
|
+
dictionary.stub :search, empty_entry do
|
135
|
+
assert_program_output nothing, missing_phrase_error, "gubble"
|
136
|
+
end
|
196
137
|
end
|
197
138
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
139
|
+
|
140
|
+
def test_search_with_no_internet_prints_error
|
141
|
+
no_internet_error = load_fixture "screens/no_internet_error.txt"
|
142
|
+
raise_socket_error = Proc.new { raise SocketError }
|
143
|
+
|
144
|
+
dictionary.stub :search, raise_socket_error do
|
145
|
+
assert_program_output nothing, no_internet_error, "gubble"
|
146
|
+
end
|
203
147
|
end
|
204
148
|
|
205
149
|
def test_invalid_option_prints_help
|
206
|
-
|
207
|
-
|
208
|
-
@program.dictionary = dictionary
|
209
|
-
assert_program_output(['-b'], nil, ERROR_INVALID_OPTION)
|
150
|
+
invalid_option_error = load_fixture "screens/invalid_option_error.txt"
|
151
|
+
assert_program_output nothing, invalid_option_error, "-b"
|
210
152
|
end
|
211
153
|
end
|
212
154
|
end
|
@@ -1,37 +1,47 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "urban/dictionary"
|
2
3
|
|
3
|
-
class DictionaryTest <
|
4
|
+
class DictionaryTest < Urban::Test
|
5
|
+
|
6
|
+
attr_accessor :web_service, :dictionary, :response
|
4
7
|
|
5
8
|
def setup
|
6
|
-
|
7
|
-
|
9
|
+
self.web_service = MiniTest::Mock.new
|
10
|
+
self.dictionary = Urban::Dictionary.new
|
11
|
+
|
12
|
+
dictionary.web_service = web_service
|
8
13
|
|
9
|
-
|
10
|
-
|
11
|
-
|
14
|
+
self.response = OpenStruct.new(
|
15
|
+
:url => "http://www.urbandictionary.com/define.php?term=impromptu",
|
16
|
+
:stream => load_fixture("impromptu.html")
|
17
|
+
)
|
12
18
|
end
|
13
19
|
|
14
20
|
def test_process_extracts_elements_from_html
|
15
|
-
entry =
|
16
|
-
assert_equal(
|
21
|
+
entry = dictionary.send(:process, response )
|
22
|
+
assert_equal(test_entry, entry)
|
17
23
|
end
|
18
24
|
|
19
25
|
def test_dictionary_calls_random
|
20
|
-
|
21
|
-
|
22
|
-
|
26
|
+
web_service.expect(:random, response)
|
27
|
+
|
28
|
+
assert_equal(test_entry, dictionary.random)
|
29
|
+
web_service.verify
|
23
30
|
end
|
24
31
|
|
25
32
|
def test_dictionary_calls_search
|
26
|
-
|
27
|
-
|
28
|
-
|
33
|
+
web_service.expect(:search, response, ['impromptu'])
|
34
|
+
|
35
|
+
assert_equal(test_entry, dictionary.search('impromptu'))
|
36
|
+
web_service.verify
|
29
37
|
end
|
30
38
|
|
31
39
|
def test_dictionary_returns_empty_for_missing_phrases
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
40
|
+
response.stream = load_fixture 'missing.html'
|
41
|
+
web_service.expect(:search, response, ['gubble'])
|
42
|
+
|
43
|
+
assert_equal(empty_entry, dictionary.search('gubble'))
|
44
|
+
web_service.verify
|
36
45
|
end
|
46
|
+
|
37
47
|
end
|