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.
File without changes
@@ -0,0 +1,5 @@
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
+
@@ -0,0 +1,7 @@
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
+ URL: http://www.urbandictionary.com/define.php?term=impromptu
7
+
@@ -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,2 @@
1
+ urban: invalid option: -b
2
+ Try `urban --help' for more information.
@@ -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 'rubygems'
4
- gem 'minitest' if RUBY_VERSION > '1.9'
5
- require 'minitest/autorun'
6
- require 'urban'
7
- require 'urban/cli'
8
- require 'minitest/stop_light'
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
- TEST_ENTRY = Urban::Dictionary::Entry.new('impromptu',
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
- EMPTY_ENTRY = Urban::Dictionary::Entry.new('gubble', nil, nil)
12
+ def load_fixture(filename)
13
+ IO.read(File.expand_path("../fixtures/#{filename}", __FILE__))
14
+ end
18
15
 
19
- def load_file(filename)
20
- IO.read(File.expand_path("../data/#{filename}", __FILE__))
21
- end
16
+ def empty_entry
17
+ @empty_entry ||= Urban::Dictionary::Entry.new('gubble', nil, nil)
18
+ end
22
19
 
23
- module Stub
24
- def stub(name, &block)
25
- singleton_class = class << self; self; end
26
- singleton_class.send(:define_method, name, &block)
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
@@ -1,212 +1,154 @@
1
- require 'test_helper'
2
- require 'open-uri'
3
- require 'socket'
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
- def setup
32
- @program = Urban::CLI.new
33
- end
5
+ class CLITest < Urban::Test
6
+
7
+ attr_accessor :program, :dictionary
34
8
 
35
- # Helpers
36
- def assert_program_output(argument_variations, stdout=nil, stderr=nil)
37
- argument_variations.each do |args|
38
- assert_output(stdout, stderr) { @program.run(Shellwords.shellwords(args)) }
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 = @program.send(:parse, [args])
48
- assert_equal(true, options.send(name))
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
- assert_silent do
55
- options = @program.send(:parse, [])
56
- assert_equal(false, options.help)
57
- assert_equal(false, options.version)
58
- assert_equal(false, options.random)
59
- assert_equal(false, options.all)
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
- assert_silent do
66
- options = @program.send(:parse, ['foo bar'])
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
- assert_silent do
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
- assert_silent do
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
- assert_silent do
91
- assert_flag_is_set('all')
92
- end
56
+ assert_flag_is_set "all"
93
57
  end
94
58
 
95
- def test_all_flag
96
- assert_silent do
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
- assert_output(HELP_SCREEN) { @program.run([]) }
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
- ['-v', '--v'].each do |args|
120
- assert_output("Urban #{Urban::VERSION} (c) Thomas Miller\n") { @program.run([args]) }
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
- @program.dictionary = @dictionary.expect(:random, TEST_ENTRY)
126
- argument_variations = ['-r', '--random']
127
- assert_program_output(argument_variations, SINGLE_DEFINITION)
128
- @dictionary.verify
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
- @program.dictionary = @dictionary.expect(:search, TEST_ENTRY, ['impromptu'])
133
- argument_variations = ['impromptu']
134
- assert_program_output(argument_variations, SINGLE_DEFINITION)
135
- @dictionary.verify
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
- @program.dictionary = @dictionary.expect(:random, TEST_ENTRY)
140
- argument_variations = ['-ra', '-r -a', '--random -a', '-r --all', '--all --random']
141
- assert_program_output(argument_variations, MULTIPLE_DEFINITIONS)
142
- @dictionary.verify
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
- @program.dictionary = @dictionary.expect(:search, TEST_ENTRY, ['impromptu'])
147
- argument_variations = ['impromptu -a', '--all impromptu']
148
- assert_program_output(argument_variations, MULTIPLE_DEFINITIONS)
149
- @dictionary.verify
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
- @program.dictionary = @dictionary.expect(:random, TEST_ENTRY)
154
- argument_variations = ['-ru', '-r -u', '--random -u', '-r --url', '--url --random']
155
- assert_program_output(argument_variations, DEFINITION_WITH_URL)
156
- @dictionary.verify
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
- @program.dictionary = @dictionary.expect(:search, TEST_ENTRY, ['impromptu'])
161
- argument_variations = ['impromptu -u', '--url impromptu']
162
- assert_program_output(argument_variations, DEFINITION_WITH_URL)
163
- @dictionary.verify
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
- dictionary = MiniTest::Mock.new
193
- @program.dictionary = dictionary.expect(:search, EMPTY_ENTRY, ['gubble'])
194
- assert_program_output(['gubble'], nil, ERROR_MISSING_PHRASE)
195
- dictionary.verify
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
- def test_search_missing_phrase_prints_error
199
- dictionary = (Object.new).extend Stub
200
- dictionary.stub(:search) { |phrase| raise SocketError }
201
- @program.dictionary = dictionary
202
- assert_program_output(['gubble'], nil, ERROR_NO_INTERNET)
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
- dictionary = (Object.new).extend Stub
207
- dictionary.stub(:search) { |phrase| raise OptionParser::InvalidOption }
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 'test_helper'
1
+ require "test_helper"
2
+ require "urban/dictionary"
2
3
 
3
- class DictionaryTest < MiniTest::Unit::TestCase
4
+ class DictionaryTest < Urban::Test
5
+
6
+ attr_accessor :web_service, :dictionary, :response
4
7
 
5
8
  def setup
6
- @web_service = MiniTest::Mock.new
7
- @dictionary = Urban::Dictionary
9
+ self.web_service = MiniTest::Mock.new
10
+ self.dictionary = Urban::Dictionary.new
11
+
12
+ dictionary.web_service = web_service
8
13
 
9
- @response = OpenStruct.new
10
- @response.url = 'http://www.urbandictionary.com/define.php?term=impromptu'
11
- @response.stream = load_file('impromptu.html')
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 = @dictionary.send(:process, @response )
16
- assert_equal(TEST_ENTRY, entry)
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
- @dictionary.web_service = @web_service.expect(:random, @response)
21
- assert_equal(TEST_ENTRY, @dictionary.random)
22
- @web_service.verify
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
- @dictionary.web_service = @web_service.expect(:search, @response, ['impromptu'])
27
- assert_equal(TEST_ENTRY, @dictionary.search('impromptu'))
28
- @web_service.verify
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
- @response.stream = load_file('missing.html')
33
- @dictionary.web_service = @web_service.expect(:search, @response, ['gubble'])
34
- assert_equal(EMPTY_ENTRY, @dictionary.search('gubble'))
35
- @web_service.verify
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