awesome_print 1.2.0 → 1.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -2
- data/Appraisals +62 -0
- data/CHANGELOG.md +180 -0
- data/CONTRIBUTING.md +82 -0
- data/Gemfile +3 -2
- data/LICENSE +16 -19
- data/README.md +112 -87
- data/Rakefile +17 -4
- data/awesome_print.gemspec +33 -0
- data/init.rb +1 -0
- data/lib/ap.rb +2 -2
- data/lib/awesome_print.rb +22 -16
- data/lib/awesome_print/colorize.rb +24 -0
- data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
- data/lib/awesome_print/core_ext/class.rb +3 -2
- data/lib/awesome_print/core_ext/kernel.rb +1 -1
- data/lib/awesome_print/core_ext/logger.rb +1 -1
- data/lib/awesome_print/core_ext/method.rb +2 -2
- data/lib/awesome_print/core_ext/object.rb +3 -2
- data/lib/awesome_print/core_ext/string.rb +3 -3
- data/lib/awesome_print/custom_defaults.rb +57 -0
- data/lib/awesome_print/ext/action_view.rb +8 -4
- data/lib/awesome_print/ext/active_record.rb +47 -13
- data/lib/awesome_print/ext/active_support.rb +1 -1
- data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
- data/lib/awesome_print/ext/mongoid.rb +9 -7
- data/lib/awesome_print/ext/nobrainer.rb +52 -0
- data/lib/awesome_print/ext/nokogiri.rb +4 -4
- data/lib/awesome_print/ext/ostruct.rb +27 -0
- data/lib/awesome_print/ext/ripple.rb +6 -7
- data/lib/awesome_print/ext/sequel.rb +7 -6
- data/lib/awesome_print/formatter.rb +54 -327
- data/lib/awesome_print/formatters.rb +15 -0
- data/lib/awesome_print/formatters/array_formatter.rb +139 -0
- data/lib/awesome_print/formatters/base_formatter.rb +140 -0
- data/lib/awesome_print/formatters/class_formatter.rb +25 -0
- data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
- data/lib/awesome_print/formatters/file_formatter.rb +22 -0
- data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
- data/lib/awesome_print/formatters/method_formatter.rb +22 -0
- data/lib/awesome_print/formatters/object_formatter.rb +78 -0
- data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
- data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
- data/lib/awesome_print/indentator.rb +18 -0
- data/lib/awesome_print/inspector.rb +92 -94
- data/lib/awesome_print/version.rb +2 -2
- data/spec/active_record_helper.rb +30 -0
- data/spec/colors_spec.rb +31 -31
- data/spec/core_ext/logger_spec.rb +43 -0
- data/spec/core_ext/string_spec.rb +20 -0
- data/spec/ext/action_view_spec.rb +21 -0
- data/spec/ext/active_record_spec.rb +260 -0
- data/spec/ext/active_support_spec.rb +30 -0
- data/spec/ext/mongo_mapper_spec.rb +261 -0
- data/spec/ext/mongoid_spec.rb +67 -0
- data/spec/ext/nobrainer_spec.rb +59 -0
- data/spec/ext/nokogiri_spec.rb +46 -0
- data/spec/ext/ostruct_spec.rb +22 -0
- data/spec/ext/ripple_spec.rb +48 -0
- data/spec/formats_spec.rb +272 -205
- data/spec/methods_spec.rb +173 -177
- data/spec/misc_spec.rb +129 -105
- data/spec/objects_spec.rb +162 -27
- data/spec/spec_helper.rb +75 -37
- data/spec/support/active_record_data.rb +20 -0
- data/spec/support/active_record_data/3_2_diana.txt +24 -0
- data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
- data/spec/support/active_record_data/3_2_multi.txt +50 -0
- data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
- data/spec/support/active_record_data/4_0_diana.txt +98 -0
- data/spec/support/active_record_data/4_0_multi.txt +198 -0
- data/spec/support/active_record_data/4_1_diana.txt +97 -0
- data/spec/support/active_record_data/4_1_multi.txt +196 -0
- data/spec/support/active_record_data/4_2_diana.txt +109 -0
- data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
- data/spec/support/active_record_data/4_2_multi.txt +220 -0
- data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
- data/spec/support/active_record_data/5_0_diana.txt +105 -0
- data/spec/support/active_record_data/5_0_multi.txt +212 -0
- data/spec/support/active_record_data/5_1_diana.txt +104 -0
- data/spec/support/active_record_data/5_1_multi.txt +210 -0
- data/spec/support/active_record_data/5_2_diana.txt +104 -0
- data/spec/support/active_record_data/5_2_multi.txt +210 -0
- data/spec/support/active_record_data/6_0_diana.txt +104 -0
- data/spec/support/active_record_data/6_0_multi.txt +210 -0
- data/spec/support/active_record_data/6_1_diana.txt +109 -0
- data/spec/support/active_record_data/6_1_multi.txt +220 -0
- data/spec/support/ext_verifier.rb +42 -0
- data/spec/support/mongoid_versions.rb +26 -0
- data/spec/support/rails_versions.rb +55 -0
- metadata +163 -32
- data/CHANGELOG +0 -96
- data/lib/awesome_print/ext/no_brainer.rb +0 -58
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'AwesomePrint Ostruct extension' do
|
4
|
+
before do
|
5
|
+
@ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'empty hash' do
|
9
|
+
struct = OpenStruct.new
|
10
|
+
expect(@ap.send(:awesome, struct)).to eq('OpenStruct {}')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'plain multiline' do
|
14
|
+
struct = OpenStruct.new name: 'Foo', address: 'Bar'
|
15
|
+
expect(@ap.send(:awesome, struct)).to eq <<-EOS.strip
|
16
|
+
OpenStruct {
|
17
|
+
:address => "Bar",
|
18
|
+
:name => "Foo"
|
19
|
+
}
|
20
|
+
EOS
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'AwesomePrint/Ripple', skip: -> { !ExtVerifier.has_ripple? }.call do
|
4
|
+
|
5
|
+
if ExtVerifier.has_ripple?
|
6
|
+
before :all do
|
7
|
+
class RippleUser
|
8
|
+
include Ripple::Document
|
9
|
+
|
10
|
+
key_on :_id
|
11
|
+
property :_id, String
|
12
|
+
property :first_name, String
|
13
|
+
property :last_name, String
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after :all do
|
18
|
+
Object.instance_eval { remove_const :RippleUser }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
before do
|
23
|
+
@ap = AwesomePrint::Inspector.new plain: true, sort_keys: true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should print class instance' do
|
27
|
+
user = RippleUser.new _id: '12345', first_name: 'Al', last_name: 'Capone'
|
28
|
+
out = @ap.send :awesome, user
|
29
|
+
|
30
|
+
expect(out).to be_similar_to <<-EOS.strip
|
31
|
+
#<RippleUser:placeholder_id> {
|
32
|
+
:_id => "12345",
|
33
|
+
:first_name => "Al",
|
34
|
+
:last_name => "Capone"
|
35
|
+
}
|
36
|
+
EOS
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should print the class' do
|
40
|
+
expect(@ap.send(:awesome, RippleUser)).to eq <<-EOS.strip
|
41
|
+
class RippleUser < Object {
|
42
|
+
:_id => :string,
|
43
|
+
:first_name => :string,
|
44
|
+
:last_name => :string
|
45
|
+
}
|
46
|
+
EOS
|
47
|
+
end
|
48
|
+
end
|
data/spec/formats_spec.rb
CHANGED
@@ -1,24 +1,20 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
describe "AwesomePrint" do
|
7
|
-
before do
|
8
|
-
stub_dotfile!
|
9
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'rational'
|
4
|
+
require 'set'
|
10
5
|
|
11
|
-
|
6
|
+
RSpec.describe 'AwesomePrint' do
|
7
|
+
describe 'Array' do
|
12
8
|
before do
|
13
|
-
@arr = [
|
9
|
+
@arr = [1, :two, 'three', [nil, [true, false]]]
|
14
10
|
end
|
15
11
|
|
16
|
-
it
|
17
|
-
[].ai.
|
12
|
+
it 'empty array' do
|
13
|
+
expect([].ai).to eq('[]')
|
18
14
|
end
|
19
15
|
|
20
|
-
it
|
21
|
-
@arr.ai(:
|
16
|
+
it 'plain multiline' do
|
17
|
+
expect(@arr.ai(plain: true)).to eq <<-EOS.strip
|
22
18
|
[
|
23
19
|
[0] 1,
|
24
20
|
[1] :two,
|
@@ -34,8 +30,8 @@ describe "AwesomePrint" do
|
|
34
30
|
EOS
|
35
31
|
end
|
36
32
|
|
37
|
-
it
|
38
|
-
@arr.ai(:
|
33
|
+
it 'plain multiline without index' do
|
34
|
+
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
39
35
|
[
|
40
36
|
1,
|
41
37
|
:two,
|
@@ -51,8 +47,8 @@ EOS
|
|
51
47
|
EOS
|
52
48
|
end
|
53
49
|
|
54
|
-
it
|
55
|
-
@arr.ai(:
|
50
|
+
it 'plain multiline indented' do
|
51
|
+
expect(@arr.ai(plain: true, indent: 2)).to eq <<-EOS.strip
|
56
52
|
[
|
57
53
|
[0] 1,
|
58
54
|
[1] :two,
|
@@ -68,8 +64,8 @@ EOS
|
|
68
64
|
EOS
|
69
65
|
end
|
70
66
|
|
71
|
-
it
|
72
|
-
@arr.ai(:
|
67
|
+
it 'plain multiline indented without index' do
|
68
|
+
expect(@arr.ai(plain: true, indent: 2, index: false)).to eq <<-EOS.strip
|
73
69
|
[
|
74
70
|
1,
|
75
71
|
:two,
|
@@ -85,12 +81,12 @@ EOS
|
|
85
81
|
EOS
|
86
82
|
end
|
87
83
|
|
88
|
-
it
|
89
|
-
@arr.ai(:
|
84
|
+
it 'plain single line' do
|
85
|
+
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, :two, "three", [ nil, [ true, false ] ] ]')
|
90
86
|
end
|
91
87
|
|
92
|
-
it
|
93
|
-
@arr.ai.
|
88
|
+
it 'colored multiline (default)' do
|
89
|
+
expect(@arr.ai).to eq <<-EOS.strip
|
94
90
|
[
|
95
91
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
96
92
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
@@ -106,8 +102,8 @@ EOS
|
|
106
102
|
EOS
|
107
103
|
end
|
108
104
|
|
109
|
-
it
|
110
|
-
@arr.ai(:
|
105
|
+
it 'colored multiline indented' do
|
106
|
+
expect(@arr.ai(indent: 8)).to eq <<-EOS.strip
|
111
107
|
[
|
112
108
|
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
113
109
|
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
@@ -123,20 +119,20 @@ EOS
|
|
123
119
|
EOS
|
124
120
|
end
|
125
121
|
|
126
|
-
it
|
127
|
-
@arr.ai(:
|
122
|
+
it 'colored single line' do
|
123
|
+
expect(@arr.ai(multiline: false)).to eq("[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]")
|
128
124
|
end
|
129
125
|
end
|
130
126
|
|
131
127
|
#------------------------------------------------------------------------------
|
132
|
-
describe
|
128
|
+
describe 'Nested Array' do
|
133
129
|
before do
|
134
|
-
@arr = [
|
130
|
+
@arr = [1, 2]
|
135
131
|
@arr << @arr
|
136
132
|
end
|
137
133
|
|
138
|
-
it
|
139
|
-
@arr.ai(:
|
134
|
+
it 'plain multiline' do
|
135
|
+
expect(@arr.ai(plain: true)).to eq <<-EOS.strip
|
140
136
|
[
|
141
137
|
[0] 1,
|
142
138
|
[1] 2,
|
@@ -145,8 +141,8 @@ EOS
|
|
145
141
|
EOS
|
146
142
|
end
|
147
143
|
|
148
|
-
it
|
149
|
-
@arr.ai(:
|
144
|
+
it 'plain multiline without index' do
|
145
|
+
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
150
146
|
[
|
151
147
|
1,
|
152
148
|
2,
|
@@ -155,19 +151,19 @@ EOS
|
|
155
151
|
EOS
|
156
152
|
end
|
157
153
|
|
158
|
-
it
|
159
|
-
@arr.ai(:
|
154
|
+
it 'plain single line' do
|
155
|
+
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, 2, [...] ]')
|
160
156
|
end
|
161
157
|
end
|
162
158
|
|
163
159
|
#------------------------------------------------------------------------------
|
164
|
-
describe
|
160
|
+
describe 'Limited Output Array' do
|
165
161
|
before(:each) do
|
166
162
|
@arr = (1..1000).to_a
|
167
163
|
end
|
168
164
|
|
169
|
-
it
|
170
|
-
@arr.ai(:
|
165
|
+
it 'plain limited output large' do
|
166
|
+
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
171
167
|
[
|
172
168
|
[ 0] 1,
|
173
169
|
[ 1] 2,
|
@@ -180,9 +176,9 @@ EOS
|
|
180
176
|
EOS
|
181
177
|
end
|
182
178
|
|
183
|
-
it
|
179
|
+
it 'plain limited output small' do
|
184
180
|
@arr = @arr[0..3]
|
185
|
-
@arr.ai(:
|
181
|
+
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
186
182
|
[
|
187
183
|
[0] 1,
|
188
184
|
[1] 2,
|
@@ -192,8 +188,8 @@ EOS
|
|
192
188
|
EOS
|
193
189
|
end
|
194
190
|
|
195
|
-
it
|
196
|
-
@arr.ai(:
|
191
|
+
it 'plain limited output with 10 lines' do
|
192
|
+
expect(@arr.ai(plain: true, limit: 10)).to eq <<-EOS.strip
|
197
193
|
[
|
198
194
|
[ 0] 1,
|
199
195
|
[ 1] 2,
|
@@ -209,8 +205,8 @@ EOS
|
|
209
205
|
EOS
|
210
206
|
end
|
211
207
|
|
212
|
-
it
|
213
|
-
@arr.ai(:
|
208
|
+
it 'plain limited output with 11 lines' do
|
209
|
+
expect(@arr.ai(plain: true, limit: 11)).to eq <<-EOS.strip
|
214
210
|
[
|
215
211
|
[ 0] 1,
|
216
212
|
[ 1] 2,
|
@@ -229,13 +225,13 @@ EOS
|
|
229
225
|
end
|
230
226
|
|
231
227
|
#------------------------------------------------------------------------------
|
232
|
-
describe
|
228
|
+
describe 'Limited Output Hash' do
|
233
229
|
before(:each) do
|
234
|
-
@hash = (
|
230
|
+
@hash = ('a'..'z').inject({}) { |h, v| h.merge({ v => v.to_sym }) }
|
235
231
|
end
|
236
232
|
|
237
|
-
it
|
238
|
-
@hash.ai(:
|
233
|
+
it 'plain limited output' do
|
234
|
+
expect(@hash.ai(sort_keys: true, plain: true, limit: true)).to eq <<-EOS.strip
|
239
235
|
{
|
240
236
|
"a" => :a,
|
241
237
|
"b" => :b,
|
@@ -250,17 +246,17 @@ EOS
|
|
250
246
|
end
|
251
247
|
|
252
248
|
#------------------------------------------------------------------------------
|
253
|
-
describe
|
249
|
+
describe 'Hash' do
|
254
250
|
before do
|
255
|
-
@hash = { 1 => { :
|
251
|
+
@hash = { 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }
|
256
252
|
end
|
257
|
-
|
258
|
-
it
|
259
|
-
{}.ai.
|
253
|
+
|
254
|
+
it 'empty hash' do
|
255
|
+
expect({}.ai).to eq('{}')
|
260
256
|
end
|
261
|
-
|
262
|
-
it
|
263
|
-
@hash.ai(:
|
257
|
+
|
258
|
+
it 'plain multiline' do
|
259
|
+
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
264
260
|
{
|
265
261
|
1 => {
|
266
262
|
:sym => {
|
@@ -275,8 +271,24 @@ EOS
|
|
275
271
|
EOS
|
276
272
|
end
|
277
273
|
|
278
|
-
it
|
279
|
-
@hash.ai(:
|
274
|
+
it 'new hash syntax' do
|
275
|
+
expect(@hash.ai(plain: true, ruby19_syntax: true)).to eq <<-EOS.strip
|
276
|
+
{
|
277
|
+
1 => {
|
278
|
+
sym: {
|
279
|
+
"str" => {
|
280
|
+
[ 1, 2, 3 ] => {
|
281
|
+
{ k: :v } => Hash < Object
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
}
|
286
|
+
}
|
287
|
+
EOS
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'plain multiline indented' do
|
291
|
+
expect(@hash.ai(plain: true, indent: 1)).to eq <<-EOS.strip
|
280
292
|
{
|
281
293
|
1 => {
|
282
294
|
:sym => {
|
@@ -291,12 +303,12 @@ EOS
|
|
291
303
|
EOS
|
292
304
|
end
|
293
305
|
|
294
|
-
it
|
295
|
-
@hash.ai(:
|
306
|
+
it 'plain single line' do
|
307
|
+
expect(@hash.ai(plain: true, multiline: false)).to eq('{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }')
|
296
308
|
end
|
297
309
|
|
298
|
-
it
|
299
|
-
@hash.ai.
|
310
|
+
it 'colored multiline (default)' do
|
311
|
+
expect(@hash.ai).to eq <<-EOS.strip
|
300
312
|
{
|
301
313
|
1\e[0;37m => \e[0m{
|
302
314
|
:sym\e[0;37m => \e[0m{
|
@@ -311,8 +323,24 @@ EOS
|
|
311
323
|
EOS
|
312
324
|
end
|
313
325
|
|
314
|
-
it
|
315
|
-
@hash.ai(:
|
326
|
+
it 'colored with new hash syntax' do
|
327
|
+
expect(@hash.ai(ruby19_syntax: true)).to eq <<-EOS.strip
|
328
|
+
{
|
329
|
+
1\e[0;37m => \e[0m{
|
330
|
+
sym\e[0;37m: \e[0m{
|
331
|
+
\"str\"\e[0;37m => \e[0m{
|
332
|
+
[ 1, 2, 3 ]\e[0;37m => \e[0m{
|
333
|
+
{ k: :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
|
334
|
+
}
|
335
|
+
}
|
336
|
+
}
|
337
|
+
}
|
338
|
+
}
|
339
|
+
EOS
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'colored multiline indented' do
|
343
|
+
expect(@hash.ai(indent: 2)).to eq <<-EOS.strip
|
316
344
|
{
|
317
345
|
1\e[0;37m => \e[0m{
|
318
346
|
:sym\e[0;37m => \e[0m{
|
@@ -327,48 +355,48 @@ EOS
|
|
327
355
|
EOS
|
328
356
|
end
|
329
357
|
|
330
|
-
it
|
331
|
-
@hash.ai(:
|
358
|
+
it 'colored single line' do
|
359
|
+
expect(@hash.ai(multiline: false)).to eq("{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }")
|
332
360
|
end
|
333
361
|
|
334
362
|
end
|
335
363
|
|
336
364
|
#------------------------------------------------------------------------------
|
337
|
-
describe
|
365
|
+
describe 'Nested Hash' do
|
338
366
|
before do
|
339
367
|
@hash = {}
|
340
368
|
@hash[:a] = @hash
|
341
369
|
end
|
342
370
|
|
343
|
-
it
|
344
|
-
@hash.ai(:
|
371
|
+
it 'plain multiline' do
|
372
|
+
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
345
373
|
{
|
346
374
|
:a => {...}
|
347
375
|
}
|
348
376
|
EOS
|
349
377
|
end
|
350
378
|
|
351
|
-
it
|
352
|
-
@hash.ai(:
|
379
|
+
it 'plain single line' do
|
380
|
+
expect(@hash.ai(plain: true, multiline: false)).to eq('{ :a => {...} }')
|
353
381
|
end
|
354
382
|
end
|
355
383
|
|
356
384
|
#------------------------------------------------------------------------------
|
357
|
-
describe
|
385
|
+
describe 'Hash with several keys' do
|
358
386
|
before do
|
359
|
-
@hash = {
|
387
|
+
@hash = { 'b' => 'b', :a => 'a', :z => 'z', 'alpha' => 'alpha' }
|
360
388
|
end
|
361
389
|
|
362
|
-
it
|
363
|
-
out = @hash.ai(:
|
390
|
+
it 'plain multiline' do
|
391
|
+
out = @hash.ai(plain: true)
|
364
392
|
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
|
365
|
-
out.
|
366
|
-
out.
|
367
|
-
out.
|
368
|
-
out.
|
369
|
-
out.
|
393
|
+
expect(out).to match(/^\{[^\}]+\}/m)
|
394
|
+
expect(out).to match(/ "b" => "b",?/)
|
395
|
+
expect(out).to match(/ :a => "a",?/)
|
396
|
+
expect(out).to match(/ :z => "z",?/)
|
397
|
+
expect(out).to match(/ "alpha" => "alpha",?$/)
|
370
398
|
else
|
371
|
-
out.
|
399
|
+
expect(out).to eq <<-EOS.strip
|
372
400
|
{
|
373
401
|
"b" => "b",
|
374
402
|
:a => "a",
|
@@ -378,9 +406,9 @@ EOS
|
|
378
406
|
EOS
|
379
407
|
end
|
380
408
|
end
|
381
|
-
|
382
|
-
it
|
383
|
-
@hash.ai(:
|
409
|
+
|
410
|
+
it 'plain multiline with sorted keys' do
|
411
|
+
expect(@hash.ai(plain: true, sort_keys: true)).to eq <<-EOS.strip
|
384
412
|
{
|
385
413
|
:a => "a",
|
386
414
|
"alpha" => "alpha",
|
@@ -393,15 +421,15 @@ EOS
|
|
393
421
|
end
|
394
422
|
|
395
423
|
#------------------------------------------------------------------------------
|
396
|
-
describe
|
424
|
+
describe 'Negative options[:indent]' do
|
397
425
|
#
|
398
426
|
# With Ruby < 1.9 the order of hash keys is not defined so we can't
|
399
427
|
# reliably compare the output string.
|
400
428
|
#
|
401
|
-
it
|
402
|
-
hash = { [0, 0, 255] => :yellow, :red =>
|
403
|
-
out = hash.ai(:
|
404
|
-
out.
|
429
|
+
it 'hash keys must be left aligned' do
|
430
|
+
hash = { [0, 0, 255] => :yellow, :red => 'rgb(255, 0, 0)', 'magenta' => 'rgb(255, 0, 255)' }
|
431
|
+
out = hash.ai(plain: true, indent: -4, sort_keys: true)
|
432
|
+
expect(out).to eq <<-EOS.strip
|
405
433
|
{
|
406
434
|
[ 0, 0, 255 ] => :yellow,
|
407
435
|
"magenta" => "rgb(255, 0, 255)",
|
@@ -410,10 +438,10 @@ EOS
|
|
410
438
|
EOS
|
411
439
|
end
|
412
440
|
|
413
|
-
it
|
414
|
-
arr = [
|
415
|
-
out = arr.ai(:
|
416
|
-
out.
|
441
|
+
it 'nested hash keys should be indented (array of hashes)' do
|
442
|
+
arr = [{ a: 1, bb: 22, ccc: 333 }, { 1 => :a, 22 => :bb, 333 => :ccc }]
|
443
|
+
out = arr.ai(plain: true, indent: -4, sort_keys: true)
|
444
|
+
expect(out).to eq <<-EOS.strip
|
417
445
|
[
|
418
446
|
[0] {
|
419
447
|
:a => 1,
|
@@ -429,10 +457,10 @@ EOS
|
|
429
457
|
EOS
|
430
458
|
end
|
431
459
|
|
432
|
-
it
|
433
|
-
arr = { :
|
434
|
-
out = arr.ai(:
|
435
|
-
out.
|
460
|
+
it 'nested hash keys should be indented (hash of hashes)' do
|
461
|
+
arr = { first: { a: 1, bb: 22, ccc: 333 }, second: { 1 => :a, 22 => :bb, 333 => :ccc } }
|
462
|
+
out = arr.ai(plain: true, indent: -4, sort_keys: true)
|
463
|
+
expect(out).to eq <<-EOS.strip
|
436
464
|
{
|
437
465
|
:first => {
|
438
466
|
:a => 1,
|
@@ -450,196 +478,184 @@ EOS
|
|
450
478
|
end
|
451
479
|
|
452
480
|
#------------------------------------------------------------------------------
|
453
|
-
describe
|
454
|
-
it
|
455
|
-
self.class.ai(:
|
481
|
+
describe 'Class' do
|
482
|
+
it 'should show superclass (plain)' do
|
483
|
+
expect(self.class.ai(plain: true)).to eq("#{self.class} < #{self.class.superclass}")
|
456
484
|
end
|
457
485
|
|
458
|
-
it
|
459
|
-
self.class.ai.
|
486
|
+
it 'should show superclass (color)' do
|
487
|
+
expect(self.class.ai).to eq("#{self.class} < #{self.class.superclass}".yellow)
|
460
488
|
end
|
461
489
|
end
|
462
490
|
|
463
491
|
#------------------------------------------------------------------------------
|
464
|
-
describe
|
465
|
-
it
|
466
|
-
File.open(__FILE__,
|
467
|
-
f.ai(:
|
492
|
+
describe 'File' do
|
493
|
+
it 'should display a file (plain)' do
|
494
|
+
File.open(__FILE__, 'r') do |f|
|
495
|
+
expect(f.ai(plain: true)).to eq("#{f.inspect}\n#{`ls -alF #{f.path}`.chop}")
|
468
496
|
end
|
469
497
|
end
|
470
498
|
end
|
471
499
|
|
472
500
|
#------------------------------------------------------------------------------
|
473
|
-
describe
|
474
|
-
it
|
501
|
+
describe 'Dir' do
|
502
|
+
it 'should display a direcory (plain)' do
|
475
503
|
Dir.open(File.dirname(__FILE__)) do |d|
|
476
|
-
d.ai(:
|
504
|
+
expect(d.ai(plain: true)).to eq("#{d.inspect}\n#{`ls -alF #{d.path}`.chop}")
|
477
505
|
end
|
478
506
|
end
|
479
507
|
end
|
480
508
|
|
481
509
|
#------------------------------------------------------------------------------
|
482
|
-
describe
|
483
|
-
it
|
484
|
-
big = BigDecimal(
|
485
|
-
big.ai(:
|
510
|
+
describe 'BigDecimal and Rational' do
|
511
|
+
it 'should present BigDecimal object with arbitrary precision' do
|
512
|
+
big = BigDecimal('201020102010201020102010201020102010.4')
|
513
|
+
expect(big.ai(plain: true)).to eq('201020102010201020102010201020102010.4')
|
486
514
|
end
|
487
515
|
|
488
|
-
it
|
516
|
+
it 'should present Rational object with arbitrary precision' do
|
489
517
|
rat = Rational(201020102010201020102010201020102010, 2)
|
490
|
-
out = rat.ai(:
|
518
|
+
out = rat.ai(plain: true)
|
491
519
|
#
|
492
520
|
# Ruby 1.9 slightly changed the format of Rational#to_s, see
|
493
521
|
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
|
494
522
|
# http://www.ruby-forum.com/topic/189397
|
495
523
|
#
|
496
|
-
if RUBY_VERSION <
|
497
|
-
out.
|
524
|
+
if RUBY_VERSION < '1.9'
|
525
|
+
expect(out).to eq('100510051005100510051005100510051005')
|
498
526
|
else
|
499
|
-
out.
|
527
|
+
expect(out).to eq('100510051005100510051005100510051005/1')
|
500
528
|
end
|
501
529
|
end
|
502
530
|
end
|
503
531
|
|
504
532
|
#------------------------------------------------------------------------------
|
505
|
-
describe
|
506
|
-
it
|
533
|
+
describe 'Utility methods' do
|
534
|
+
it 'should merge options' do
|
507
535
|
ap = AwesomePrint::Inspector.new
|
508
|
-
ap.send(:merge_options!, { :
|
509
|
-
options = ap.instance_variable_get(
|
510
|
-
options[:color][:array].
|
511
|
-
options[:indent].
|
536
|
+
ap.send(:merge_options!, { color: { array: :black }, indent: 0 })
|
537
|
+
options = ap.instance_variable_get('@options')
|
538
|
+
expect(options[:color][:array]).to eq(:black)
|
539
|
+
expect(options[:indent]).to eq(0)
|
512
540
|
end
|
513
541
|
end
|
514
542
|
|
515
543
|
#------------------------------------------------------------------------------
|
516
|
-
describe
|
544
|
+
describe 'Set' do
|
517
545
|
before do
|
518
|
-
@arr = [1, :two,
|
546
|
+
@arr = [1, :two, 'three']
|
519
547
|
@set = Set.new(@arr)
|
520
548
|
end
|
521
549
|
|
522
|
-
it
|
523
|
-
Set.new.ai.
|
550
|
+
it 'empty set' do
|
551
|
+
expect(Set.new.ai).to eq([].ai)
|
524
552
|
end
|
525
553
|
|
526
|
-
if RUBY_VERSION >
|
527
|
-
it
|
528
|
-
@set.ai(:
|
554
|
+
if RUBY_VERSION > '1.9'
|
555
|
+
it 'plain multiline' do
|
556
|
+
expect(@set.ai(plain: true)).to eq(@arr.ai(plain: true))
|
529
557
|
end
|
530
558
|
|
531
|
-
it
|
532
|
-
@set.ai(:
|
559
|
+
it 'plain multiline indented' do
|
560
|
+
expect(@set.ai(plain: true, indent: 1)).to eq(@arr.ai(plain: true, indent: 1))
|
533
561
|
end
|
534
562
|
|
535
|
-
it
|
536
|
-
@set.ai(:
|
563
|
+
it 'plain single line' do
|
564
|
+
expect(@set.ai(plain: true, multiline: false)).to eq(@arr.ai(plain: true, multiline: false))
|
537
565
|
end
|
538
566
|
|
539
|
-
it
|
540
|
-
@set.ai.
|
567
|
+
it 'colored multiline (default)' do
|
568
|
+
expect(@set.ai).to eq(@arr.ai)
|
541
569
|
end
|
542
570
|
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
|
543
|
-
it
|
544
|
-
@set.sort_by{ |x| x.to_s }.ai(:
|
571
|
+
it 'plain multiline' do
|
572
|
+
expect(@set.sort_by { |x| x.to_s }.ai(plain: true)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true))
|
545
573
|
end
|
546
574
|
|
547
|
-
it
|
548
|
-
@set.sort_by{ |x| x.to_s }.ai(:
|
575
|
+
it 'plain multiline indented' do
|
576
|
+
expect(@set.sort_by { |x| x.to_s }.ai(plain: true, indent: 1)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true, indent: 1))
|
549
577
|
end
|
550
578
|
|
551
|
-
it
|
552
|
-
@set.sort_by{ |x| x.to_s }.ai(:
|
579
|
+
it 'plain single line' do
|
580
|
+
expect(@set.sort_by { |x| x.to_s }.ai(plain: true, multiline: false)).to eq(@arr.sort_by { |x| x.to_s }.ai(plain: true, multiline: false))
|
553
581
|
end
|
554
582
|
|
555
|
-
it
|
556
|
-
@set.sort_by{ |x| x.to_s }.ai.
|
583
|
+
it 'colored multiline (default)' do
|
584
|
+
expect(@set.sort_by { |x| x.to_s }.ai).to eq(@arr.sort_by { |x| x.to_s }.ai)
|
557
585
|
end
|
558
586
|
end
|
559
587
|
end
|
560
588
|
|
561
589
|
#------------------------------------------------------------------------------
|
562
|
-
describe
|
590
|
+
describe 'Struct' do
|
563
591
|
before do
|
564
592
|
@struct = unless defined?(Struct::SimpleStruct)
|
565
|
-
Struct.new(
|
593
|
+
Struct.new('SimpleStruct', :name, :address).new
|
566
594
|
else
|
567
595
|
Struct::SimpleStruct.new
|
568
596
|
end
|
569
|
-
@struct.name =
|
570
|
-
@struct.address =
|
597
|
+
@struct.name = 'Herman Munster'
|
598
|
+
@struct.address = '1313 Mockingbird Lane'
|
571
599
|
end
|
572
|
-
|
573
|
-
it
|
574
|
-
Struct.new(
|
600
|
+
|
601
|
+
it 'empty struct' do
|
602
|
+
expect(Struct.new('EmptyStruct').ai).to eq("\e[1;33mStruct::EmptyStruct < Struct\e[0m")
|
575
603
|
end
|
576
|
-
|
577
|
-
it
|
604
|
+
|
605
|
+
it 'plain multiline' do
|
578
606
|
s1 = <<-EOS.strip
|
579
|
-
|
580
|
-
|
581
|
-
:name => "Herman Munster"
|
582
|
-
}
|
607
|
+
address = \"1313 Mockingbird Lane\",
|
608
|
+
name = \"Herman Munster\"
|
583
609
|
EOS
|
584
610
|
s2 = <<-EOS.strip
|
585
|
-
|
586
|
-
|
587
|
-
:address => "1313 Mockingbird Lane"
|
588
|
-
}
|
611
|
+
name = \"Herman Munster\",
|
612
|
+
address = \"1313 Mockingbird Lane\"
|
589
613
|
EOS
|
590
|
-
@struct.ai(:
|
614
|
+
expect(@struct.ai(plain: true)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
591
615
|
end
|
592
616
|
|
593
|
-
it
|
617
|
+
it 'plain multiline indented' do
|
594
618
|
s1 = <<-EOS.strip
|
595
|
-
|
596
|
-
|
597
|
-
:name => "Herman Munster"
|
598
|
-
}
|
619
|
+
address = "1313 Mockingbird Lane",
|
620
|
+
name = "Herman Munster"
|
599
621
|
EOS
|
600
622
|
s2 = <<-EOS.strip
|
601
|
-
|
602
|
-
|
603
|
-
:address => "1313 Mockingbird Lane"
|
604
|
-
}
|
623
|
+
name = "Herman Munster",
|
624
|
+
address = "1313 Mockingbird Lane"
|
605
625
|
EOS
|
606
|
-
@struct.ai(:
|
626
|
+
expect(@struct.ai(plain: true, indent: 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
607
627
|
end
|
608
628
|
|
609
|
-
it
|
610
|
-
s1 =
|
611
|
-
s2 =
|
612
|
-
@struct.ai(:
|
629
|
+
it 'plain single line' do
|
630
|
+
s1 = 'address = "1313 Mockingbird Lane", name = "Herman Munster"'
|
631
|
+
s2 = 'name = "Herman Munster", address = "1313 Mockingbird Lane"'
|
632
|
+
expect(@struct.ai(plain: true, multiline: false)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
613
633
|
end
|
614
634
|
|
615
|
-
it
|
635
|
+
it 'colored multiline (default)' do
|
616
636
|
s1 = <<-EOS.strip
|
617
|
-
|
618
|
-
|
619
|
-
:name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
620
|
-
}
|
637
|
+
address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
|
638
|
+
name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m
|
621
639
|
EOS
|
622
640
|
s2 = <<-EOS.strip
|
623
|
-
|
624
|
-
|
625
|
-
:address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
626
|
-
}
|
641
|
+
name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m,
|
642
|
+
address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
|
627
643
|
EOS
|
628
|
-
@struct.ai.
|
644
|
+
expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
|
629
645
|
end
|
630
646
|
end
|
631
647
|
|
632
648
|
#------------------------------------------------------------------------------
|
633
|
-
describe
|
649
|
+
describe 'Inherited from standard Ruby classes' do
|
634
650
|
after do
|
635
|
-
Object.instance_eval{ remove_const :My } if defined?(My)
|
651
|
+
Object.instance_eval { remove_const :My } if defined?(My)
|
636
652
|
end
|
637
653
|
|
638
|
-
it
|
654
|
+
it 'inherited from Array should be displayed as Array' do
|
639
655
|
class My < Array; end
|
640
656
|
|
641
|
-
my = My.new([
|
642
|
-
my.ai(:
|
657
|
+
my = My.new([1, :two, 'three', [nil, [true, false]]])
|
658
|
+
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
643
659
|
[
|
644
660
|
[0] 1,
|
645
661
|
[1] :two,
|
@@ -655,11 +671,11 @@ EOS
|
|
655
671
|
EOS
|
656
672
|
end
|
657
673
|
|
658
|
-
it
|
674
|
+
it 'inherited from Hash should be displayed as Hash' do
|
659
675
|
class My < Hash; end
|
660
676
|
|
661
|
-
my = My[
|
662
|
-
my.ai(:
|
677
|
+
my = My[{ 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }]
|
678
|
+
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
663
679
|
{
|
664
680
|
1 => {
|
665
681
|
:sym => {
|
@@ -674,31 +690,31 @@ EOS
|
|
674
690
|
EOS
|
675
691
|
end
|
676
692
|
|
677
|
-
it
|
693
|
+
it 'inherited from File should be displayed as File' do
|
678
694
|
class My < File; end
|
679
695
|
|
680
696
|
my = File.new('/dev/null') rescue File.new('nul')
|
681
|
-
my.ai(:
|
697
|
+
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
682
698
|
end
|
683
699
|
|
684
|
-
it
|
700
|
+
it 'inherited from Dir should be displayed as Dir' do
|
685
701
|
class My < Dir; end
|
686
702
|
|
687
703
|
require 'tmpdir'
|
688
704
|
my = My.new(Dir.tmpdir)
|
689
|
-
my.ai(:
|
705
|
+
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
690
706
|
end
|
691
707
|
|
692
|
-
it
|
708
|
+
it 'should handle a class that defines its own #send method' do
|
693
709
|
class My
|
694
710
|
def send(arg1, arg2, arg3); end
|
695
711
|
end
|
696
712
|
|
697
713
|
my = My.new
|
698
|
-
my.methods.ai(:
|
714
|
+
expect { my.methods.ai(plain: true) }.not_to raise_error
|
699
715
|
end
|
700
716
|
|
701
|
-
it
|
717
|
+
it 'should handle a class defines its own #method method (ex. request.method)' do
|
702
718
|
class My
|
703
719
|
def method
|
704
720
|
'POST'
|
@@ -706,7 +722,58 @@ EOS
|
|
706
722
|
end
|
707
723
|
|
708
724
|
my = My.new
|
709
|
-
my.methods.ai(:
|
725
|
+
expect { my.methods.ai(plain: true) }.not_to raise_error
|
726
|
+
end
|
727
|
+
|
728
|
+
describe 'should handle a class that defines its own #to_hash method' do
|
729
|
+
it 'that takes arguments' do
|
730
|
+
class My
|
731
|
+
def to_hash(a, b)
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
my = My.new
|
736
|
+
expect { my.ai(plain: true) }.not_to raise_error
|
737
|
+
end
|
738
|
+
|
739
|
+
it 'that returns nil' do
|
740
|
+
class My
|
741
|
+
def to_hash()
|
742
|
+
return nil
|
743
|
+
end
|
744
|
+
end
|
745
|
+
|
746
|
+
my = My.new
|
747
|
+
expect { my.ai(plain: true) }.not_to raise_error
|
748
|
+
end
|
749
|
+
|
750
|
+
it "that returns an object that doesn't support #keys" do
|
751
|
+
class My
|
752
|
+
def to_hash()
|
753
|
+
object = Object.new
|
754
|
+
object.define_singleton_method('[]') { return nil }
|
755
|
+
|
756
|
+
return object
|
757
|
+
end
|
758
|
+
end
|
759
|
+
|
760
|
+
my = My.new
|
761
|
+
expect { my.ai(plain: true) }.not_to raise_error
|
762
|
+
end
|
763
|
+
|
764
|
+
it "that returns an object that doesn't support subscripting" do
|
765
|
+
class My
|
766
|
+
def to_hash()
|
767
|
+
object = Object.new
|
768
|
+
object.define_singleton_method(:keys) { return [:foo] }
|
769
|
+
|
770
|
+
return object
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|
774
|
+
my = My.new
|
775
|
+
expect { my.ai(plain: true) }.not_to raise_error
|
776
|
+
end
|
710
777
|
end
|
711
778
|
end
|
712
779
|
end
|