awesome_print 1.9.2 → 2.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/Appraisals +2 -33
- data/CHANGELOG.md +6 -15
- data/CONTRIBUTING.md +0 -1
- data/README.md +7 -49
- data/lib/ap.rb +1 -5
- data/lib/awesome_print.rb +7 -29
- data/lib/awesome_print/colorize.rb +2 -0
- data/lib/awesome_print/core_ext/active_support.rb +7 -0
- data/lib/awesome_print/core_ext/awesome_method_array.rb +0 -6
- data/lib/awesome_print/core_ext/class.rb +0 -5
- data/lib/awesome_print/core_ext/kernel.rb +0 -5
- data/lib/awesome_print/core_ext/logger.rb +0 -5
- data/lib/awesome_print/core_ext/object.rb +0 -5
- data/lib/awesome_print/core_ext/string.rb +0 -5
- data/lib/awesome_print/custom_defaults.rb +1 -1
- data/lib/awesome_print/formatter.rb +38 -106
- data/lib/awesome_print/formatters/array_formatter.rb +11 -8
- data/lib/awesome_print/formatters/base_formatter.rb +20 -53
- data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
- data/lib/awesome_print/formatters/class_formatter.rb +5 -6
- data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
- data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
- data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
- data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
- data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
- data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
- data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
- data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
- data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
- data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
- data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
- data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
- data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
- data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
- data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
- data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
- data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
- data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
- data/lib/awesome_print/formatters/file_formatter.rb +4 -6
- data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
- data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
- data/lib/awesome_print/formatters/method_formatter.rb +8 -6
- data/lib/awesome_print/formatters/module_formatter.rb +18 -0
- data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
- data/lib/awesome_print/formatters/object_formatter.rb +19 -9
- data/lib/awesome_print/formatters/open_struct.rb +19 -0
- data/lib/awesome_print/formatters/range_formatter.rb +11 -0
- data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
- data/lib/awesome_print/formatters/set_formatter.rb +19 -0
- data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
- data/lib/awesome_print/formatters/string_formatter.rb +14 -0
- data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
- data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
- data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
- data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
- data/lib/awesome_print/inspector.rb +18 -17
- data/lib/awesome_print/limiter.rb +63 -0
- data/lib/awesome_print/registrar.rb +20 -0
- data/lib/awesome_print/version.rb +6 -6
- data/spec/formatters/array_spec.rb +251 -0
- data/spec/formatters/big_decimal_spec.rb +28 -0
- data/spec/formatters/class_spec.rb +91 -0
- data/spec/formatters/dir_file_spec.rb +43 -0
- data/spec/{ext → formatters/ext}/active_record_spec.rb +14 -58
- data/spec/{ext → formatters/ext}/active_support_spec.rb +7 -11
- data/spec/{ext → formatters/ext}/mongoid_spec.rb +2 -2
- data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
- data/spec/formatters/hash_spec.rb +284 -0
- data/spec/{methods_spec.rb → formatters/methods_spec.rb} +3 -5
- data/spec/{objects_spec.rb → formatters/objects_spec.rb} +0 -0
- data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
- data/spec/formatters/set_spec.rb +49 -0
- data/spec/formatters/struct_spec.rb +61 -0
- data/spec/merge_options_spec.rb +13 -0
- data/spec/misc_spec.rb +3 -10
- data/spec/spec_helper.rb +2 -9
- data/spec/support/ext_verifier.rb +0 -15
- data/spec/support/mongoid_versions.rb +4 -8
- data/spec/support/rails_versions.rb +2 -32
- metadata +100 -78
- data/awesome_print.gemspec +0 -33
- data/init.rb +0 -1
- data/lib/awesome_print/core_ext/method.rb +0 -21
- data/lib/awesome_print/ext/action_view.rb +0 -22
- data/lib/awesome_print/ext/active_record.rb +0 -103
- data/lib/awesome_print/ext/active_support.rb +0 -47
- data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
- data/lib/awesome_print/ext/mongoid.rb +0 -67
- data/lib/awesome_print/ext/nobrainer.rb +0 -52
- data/lib/awesome_print/ext/nokogiri.rb +0 -45
- data/lib/awesome_print/ext/ostruct.rb +0 -27
- data/lib/awesome_print/ext/ripple.rb +0 -71
- data/lib/awesome_print/ext/sequel.rb +0 -58
- data/spec/ext/action_view_spec.rb +0 -21
- data/spec/ext/mongo_mapper_spec.rb +0 -261
- data/spec/ext/nobrainer_spec.rb +0 -59
- data/spec/ext/ripple_spec.rb +0 -48
- data/spec/formats_spec.rb +0 -779
- data/spec/support/active_record_data/3_2_diana.txt +0 -24
- data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
- data/spec/support/active_record_data/3_2_multi.txt +0 -50
- data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
- data/spec/support/active_record_data/4_0_diana.txt +0 -98
- data/spec/support/active_record_data/4_0_multi.txt +0 -198
- data/spec/support/active_record_data/4_1_diana.txt +0 -97
- data/spec/support/active_record_data/4_1_multi.txt +0 -196
- data/spec/support/active_record_data/4_2_diana.txt +0 -109
- data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
- data/spec/support/active_record_data/4_2_multi.txt +0 -220
- data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
- data/spec/support/active_record_data/6_0_diana.txt +0 -104
- data/spec/support/active_record_data/6_0_multi.txt +0 -210
- data/spec/support/active_record_data/6_1_diana.txt +0 -109
- data/spec/support/active_record_data/6_1_multi.txt +0 -220
data/spec/ext/nobrainer_spec.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe 'AwesomePrint/NoBrainer', skip: -> { !ExtVerifier.has_nobrainer? }.call do
|
4
|
-
|
5
|
-
if ExtVerifier.has_nobrainer?
|
6
|
-
before :all do
|
7
|
-
NoBrainer.configure do |config|
|
8
|
-
config.app_name = 'ap_test'
|
9
|
-
config.environment = :test
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
before :all do
|
14
|
-
class SomeModel
|
15
|
-
include NoBrainer::Document
|
16
|
-
|
17
|
-
field :first_name, type: String
|
18
|
-
field :last_name, type: String
|
19
|
-
field :some_field
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
after :all do
|
24
|
-
Object.instance_eval { remove_const :SomeModel }
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
before do
|
29
|
-
@ap = AwesomePrint::Inspector.new plain: true
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should print class instance' do
|
33
|
-
user = SomeModel.new first_name: 'Al', last_name: 'Capone'
|
34
|
-
out = @ap.send :awesome, user
|
35
|
-
|
36
|
-
object_id = user.id.inspect
|
37
|
-
str = <<-EOS.strip
|
38
|
-
#<SomeModel id: #{object_id}> {
|
39
|
-
:id => #{object_id},
|
40
|
-
:first_name => "Al",
|
41
|
-
:last_name => "Capone"
|
42
|
-
}
|
43
|
-
EOS
|
44
|
-
expect(out).to eq(str)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should print the class' do
|
48
|
-
class_spec = <<-EOS.strip
|
49
|
-
class SomeModel < Object {
|
50
|
-
:id => :string,
|
51
|
-
:first_name => :string,
|
52
|
-
:last_name => :string,
|
53
|
-
:some_field => :object
|
54
|
-
}
|
55
|
-
EOS
|
56
|
-
|
57
|
-
expect(@ap.send(:awesome, SomeModel)).to eq class_spec
|
58
|
-
end
|
59
|
-
end
|
data/spec/ext/ripple_spec.rb
DELETED
@@ -1,48 +0,0 @@
|
|
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
DELETED
@@ -1,779 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'bigdecimal'
|
3
|
-
require 'rational'
|
4
|
-
require 'set'
|
5
|
-
|
6
|
-
RSpec.describe 'AwesomePrint' do
|
7
|
-
describe 'Array' do
|
8
|
-
before do
|
9
|
-
@arr = [1, :two, 'three', [nil, [true, false]]]
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'empty array' do
|
13
|
-
expect([].ai).to eq('[]')
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'plain multiline' do
|
17
|
-
expect(@arr.ai(plain: true)).to eq <<-EOS.strip
|
18
|
-
[
|
19
|
-
[0] 1,
|
20
|
-
[1] :two,
|
21
|
-
[2] "three",
|
22
|
-
[3] [
|
23
|
-
[0] nil,
|
24
|
-
[1] [
|
25
|
-
[0] true,
|
26
|
-
[1] false
|
27
|
-
]
|
28
|
-
]
|
29
|
-
]
|
30
|
-
EOS
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'plain multiline without index' do
|
34
|
-
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
35
|
-
[
|
36
|
-
1,
|
37
|
-
:two,
|
38
|
-
"three",
|
39
|
-
[
|
40
|
-
nil,
|
41
|
-
[
|
42
|
-
true,
|
43
|
-
false
|
44
|
-
]
|
45
|
-
]
|
46
|
-
]
|
47
|
-
EOS
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'plain multiline indented' do
|
51
|
-
expect(@arr.ai(plain: true, indent: 2)).to eq <<-EOS.strip
|
52
|
-
[
|
53
|
-
[0] 1,
|
54
|
-
[1] :two,
|
55
|
-
[2] "three",
|
56
|
-
[3] [
|
57
|
-
[0] nil,
|
58
|
-
[1] [
|
59
|
-
[0] true,
|
60
|
-
[1] false
|
61
|
-
]
|
62
|
-
]
|
63
|
-
]
|
64
|
-
EOS
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'plain multiline indented without index' do
|
68
|
-
expect(@arr.ai(plain: true, indent: 2, index: false)).to eq <<-EOS.strip
|
69
|
-
[
|
70
|
-
1,
|
71
|
-
:two,
|
72
|
-
"three",
|
73
|
-
[
|
74
|
-
nil,
|
75
|
-
[
|
76
|
-
true,
|
77
|
-
false
|
78
|
-
]
|
79
|
-
]
|
80
|
-
]
|
81
|
-
EOS
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'plain single line' do
|
85
|
-
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, :two, "three", [ nil, [ true, false ] ] ]')
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'colored multiline (default)' do
|
89
|
-
expect(@arr.ai).to eq <<-EOS.strip
|
90
|
-
[
|
91
|
-
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
92
|
-
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
93
|
-
\e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
|
94
|
-
\e[1;37m[3] \e[0m[
|
95
|
-
\e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
|
96
|
-
\e[1;37m[1] \e[0m[
|
97
|
-
\e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
|
98
|
-
\e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
|
99
|
-
]
|
100
|
-
]
|
101
|
-
]
|
102
|
-
EOS
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'colored multiline indented' do
|
106
|
-
expect(@arr.ai(indent: 8)).to eq <<-EOS.strip
|
107
|
-
[
|
108
|
-
\e[1;37m[0] \e[0m\e[1;34m1\e[0m,
|
109
|
-
\e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
|
110
|
-
\e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
|
111
|
-
\e[1;37m[3] \e[0m[
|
112
|
-
\e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
|
113
|
-
\e[1;37m[1] \e[0m[
|
114
|
-
\e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
|
115
|
-
\e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
|
116
|
-
]
|
117
|
-
]
|
118
|
-
]
|
119
|
-
EOS
|
120
|
-
end
|
121
|
-
|
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 ] ] ]")
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
#------------------------------------------------------------------------------
|
128
|
-
describe 'Nested Array' do
|
129
|
-
before do
|
130
|
-
@arr = [1, 2]
|
131
|
-
@arr << @arr
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'plain multiline' do
|
135
|
-
expect(@arr.ai(plain: true)).to eq <<-EOS.strip
|
136
|
-
[
|
137
|
-
[0] 1,
|
138
|
-
[1] 2,
|
139
|
-
[2] [...]
|
140
|
-
]
|
141
|
-
EOS
|
142
|
-
end
|
143
|
-
|
144
|
-
it 'plain multiline without index' do
|
145
|
-
expect(@arr.ai(plain: true, index: false)).to eq <<-EOS.strip
|
146
|
-
[
|
147
|
-
1,
|
148
|
-
2,
|
149
|
-
[...]
|
150
|
-
]
|
151
|
-
EOS
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'plain single line' do
|
155
|
-
expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, 2, [...] ]')
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
#------------------------------------------------------------------------------
|
160
|
-
describe 'Limited Output Array' do
|
161
|
-
before(:each) do
|
162
|
-
@arr = (1..1000).to_a
|
163
|
-
end
|
164
|
-
|
165
|
-
it 'plain limited output large' do
|
166
|
-
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
167
|
-
[
|
168
|
-
[ 0] 1,
|
169
|
-
[ 1] 2,
|
170
|
-
[ 2] 3,
|
171
|
-
[ 3] .. [996],
|
172
|
-
[997] 998,
|
173
|
-
[998] 999,
|
174
|
-
[999] 1000
|
175
|
-
]
|
176
|
-
EOS
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'plain limited output small' do
|
180
|
-
@arr = @arr[0..3]
|
181
|
-
expect(@arr.ai(plain: true, limit: true)).to eq <<-EOS.strip
|
182
|
-
[
|
183
|
-
[0] 1,
|
184
|
-
[1] 2,
|
185
|
-
[2] 3,
|
186
|
-
[3] 4
|
187
|
-
]
|
188
|
-
EOS
|
189
|
-
end
|
190
|
-
|
191
|
-
it 'plain limited output with 10 lines' do
|
192
|
-
expect(@arr.ai(plain: true, limit: 10)).to eq <<-EOS.strip
|
193
|
-
[
|
194
|
-
[ 0] 1,
|
195
|
-
[ 1] 2,
|
196
|
-
[ 2] 3,
|
197
|
-
[ 3] 4,
|
198
|
-
[ 4] 5,
|
199
|
-
[ 5] .. [995],
|
200
|
-
[996] 997,
|
201
|
-
[997] 998,
|
202
|
-
[998] 999,
|
203
|
-
[999] 1000
|
204
|
-
]
|
205
|
-
EOS
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'plain limited output with 11 lines' do
|
209
|
-
expect(@arr.ai(plain: true, limit: 11)).to eq <<-EOS.strip
|
210
|
-
[
|
211
|
-
[ 0] 1,
|
212
|
-
[ 1] 2,
|
213
|
-
[ 2] 3,
|
214
|
-
[ 3] 4,
|
215
|
-
[ 4] 5,
|
216
|
-
[ 5] .. [994],
|
217
|
-
[995] 996,
|
218
|
-
[996] 997,
|
219
|
-
[997] 998,
|
220
|
-
[998] 999,
|
221
|
-
[999] 1000
|
222
|
-
]
|
223
|
-
EOS
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
#------------------------------------------------------------------------------
|
228
|
-
describe 'Limited Output Hash' do
|
229
|
-
before(:each) do
|
230
|
-
@hash = ('a'..'z').inject({}) { |h, v| h.merge({ v => v.to_sym }) }
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'plain limited output' do
|
234
|
-
expect(@hash.ai(sort_keys: true, plain: true, limit: true)).to eq <<-EOS.strip
|
235
|
-
{
|
236
|
-
"a" => :a,
|
237
|
-
"b" => :b,
|
238
|
-
"c" => :c,
|
239
|
-
"d" => :d .. "w" => :w,
|
240
|
-
"x" => :x,
|
241
|
-
"y" => :y,
|
242
|
-
"z" => :z
|
243
|
-
}
|
244
|
-
EOS
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
#------------------------------------------------------------------------------
|
249
|
-
describe 'Hash' do
|
250
|
-
before do
|
251
|
-
@hash = { 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'empty hash' do
|
255
|
-
expect({}.ai).to eq('{}')
|
256
|
-
end
|
257
|
-
|
258
|
-
it 'plain multiline' do
|
259
|
-
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
260
|
-
{
|
261
|
-
1 => {
|
262
|
-
:sym => {
|
263
|
-
"str" => {
|
264
|
-
[ 1, 2, 3 ] => {
|
265
|
-
{ :k => :v } => Hash < Object
|
266
|
-
}
|
267
|
-
}
|
268
|
-
}
|
269
|
-
}
|
270
|
-
}
|
271
|
-
EOS
|
272
|
-
end
|
273
|
-
|
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
|
292
|
-
{
|
293
|
-
1 => {
|
294
|
-
:sym => {
|
295
|
-
"str" => {
|
296
|
-
[ 1, 2, 3 ] => {
|
297
|
-
{ :k => :v } => Hash < Object
|
298
|
-
}
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|
302
|
-
}
|
303
|
-
EOS
|
304
|
-
end
|
305
|
-
|
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 } } } } }')
|
308
|
-
end
|
309
|
-
|
310
|
-
it 'colored multiline (default)' do
|
311
|
-
expect(@hash.ai).to eq <<-EOS.strip
|
312
|
-
{
|
313
|
-
1\e[0;37m => \e[0m{
|
314
|
-
:sym\e[0;37m => \e[0m{
|
315
|
-
\"str\"\e[0;37m => \e[0m{
|
316
|
-
[ 1, 2, 3 ]\e[0;37m => \e[0m{
|
317
|
-
{ :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
|
318
|
-
}
|
319
|
-
}
|
320
|
-
}
|
321
|
-
}
|
322
|
-
}
|
323
|
-
EOS
|
324
|
-
end
|
325
|
-
|
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
|
344
|
-
{
|
345
|
-
1\e[0;37m => \e[0m{
|
346
|
-
:sym\e[0;37m => \e[0m{
|
347
|
-
\"str\"\e[0;37m => \e[0m{
|
348
|
-
[ 1, 2, 3 ]\e[0;37m => \e[0m{
|
349
|
-
{ :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
|
350
|
-
}
|
351
|
-
}
|
352
|
-
}
|
353
|
-
}
|
354
|
-
}
|
355
|
-
EOS
|
356
|
-
end
|
357
|
-
|
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 } } } } }")
|
360
|
-
end
|
361
|
-
|
362
|
-
end
|
363
|
-
|
364
|
-
#------------------------------------------------------------------------------
|
365
|
-
describe 'Nested Hash' do
|
366
|
-
before do
|
367
|
-
@hash = {}
|
368
|
-
@hash[:a] = @hash
|
369
|
-
end
|
370
|
-
|
371
|
-
it 'plain multiline' do
|
372
|
-
expect(@hash.ai(plain: true)).to eq <<-EOS.strip
|
373
|
-
{
|
374
|
-
:a => {...}
|
375
|
-
}
|
376
|
-
EOS
|
377
|
-
end
|
378
|
-
|
379
|
-
it 'plain single line' do
|
380
|
-
expect(@hash.ai(plain: true, multiline: false)).to eq('{ :a => {...} }')
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
#------------------------------------------------------------------------------
|
385
|
-
describe 'Hash with several keys' do
|
386
|
-
before do
|
387
|
-
@hash = { 'b' => 'b', :a => 'a', :z => 'z', 'alpha' => 'alpha' }
|
388
|
-
end
|
389
|
-
|
390
|
-
it 'plain multiline' do
|
391
|
-
out = @hash.ai(plain: true)
|
392
|
-
if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
|
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",?$/)
|
398
|
-
else
|
399
|
-
expect(out).to eq <<-EOS.strip
|
400
|
-
{
|
401
|
-
"b" => "b",
|
402
|
-
:a => "a",
|
403
|
-
:z => "z",
|
404
|
-
"alpha" => "alpha"
|
405
|
-
}
|
406
|
-
EOS
|
407
|
-
end
|
408
|
-
end
|
409
|
-
|
410
|
-
it 'plain multiline with sorted keys' do
|
411
|
-
expect(@hash.ai(plain: true, sort_keys: true)).to eq <<-EOS.strip
|
412
|
-
{
|
413
|
-
:a => "a",
|
414
|
-
"alpha" => "alpha",
|
415
|
-
"b" => "b",
|
416
|
-
:z => "z"
|
417
|
-
}
|
418
|
-
EOS
|
419
|
-
end
|
420
|
-
|
421
|
-
end
|
422
|
-
|
423
|
-
#------------------------------------------------------------------------------
|
424
|
-
describe 'Negative options[:indent]' do
|
425
|
-
#
|
426
|
-
# With Ruby < 1.9 the order of hash keys is not defined so we can't
|
427
|
-
# reliably compare the output string.
|
428
|
-
#
|
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
|
433
|
-
{
|
434
|
-
[ 0, 0, 255 ] => :yellow,
|
435
|
-
"magenta" => "rgb(255, 0, 255)",
|
436
|
-
:red => "rgb(255, 0, 0)"
|
437
|
-
}
|
438
|
-
EOS
|
439
|
-
end
|
440
|
-
|
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
|
445
|
-
[
|
446
|
-
[0] {
|
447
|
-
:a => 1,
|
448
|
-
:bb => 22,
|
449
|
-
:ccc => 333
|
450
|
-
},
|
451
|
-
[1] {
|
452
|
-
1 => :a,
|
453
|
-
22 => :bb,
|
454
|
-
333 => :ccc
|
455
|
-
}
|
456
|
-
]
|
457
|
-
EOS
|
458
|
-
end
|
459
|
-
|
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
|
464
|
-
{
|
465
|
-
:first => {
|
466
|
-
:a => 1,
|
467
|
-
:bb => 22,
|
468
|
-
:ccc => 333
|
469
|
-
},
|
470
|
-
:second => {
|
471
|
-
1 => :a,
|
472
|
-
22 => :bb,
|
473
|
-
333 => :ccc
|
474
|
-
}
|
475
|
-
}
|
476
|
-
EOS
|
477
|
-
end
|
478
|
-
end
|
479
|
-
|
480
|
-
#------------------------------------------------------------------------------
|
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}")
|
484
|
-
end
|
485
|
-
|
486
|
-
it 'should show superclass (color)' do
|
487
|
-
expect(self.class.ai).to eq("#{self.class} < #{self.class.superclass}".yellow)
|
488
|
-
end
|
489
|
-
end
|
490
|
-
|
491
|
-
#------------------------------------------------------------------------------
|
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}")
|
496
|
-
end
|
497
|
-
end
|
498
|
-
end
|
499
|
-
|
500
|
-
#------------------------------------------------------------------------------
|
501
|
-
describe 'Dir' do
|
502
|
-
it 'should display a direcory (plain)' do
|
503
|
-
Dir.open(File.dirname(__FILE__)) do |d|
|
504
|
-
expect(d.ai(plain: true)).to eq("#{d.inspect}\n#{`ls -alF #{d.path}`.chop}")
|
505
|
-
end
|
506
|
-
end
|
507
|
-
end
|
508
|
-
|
509
|
-
#------------------------------------------------------------------------------
|
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')
|
514
|
-
end
|
515
|
-
|
516
|
-
it 'should present Rational object with arbitrary precision' do
|
517
|
-
rat = Rational(201020102010201020102010201020102010, 2)
|
518
|
-
out = rat.ai(plain: true)
|
519
|
-
#
|
520
|
-
# Ruby 1.9 slightly changed the format of Rational#to_s, see
|
521
|
-
# http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3 and
|
522
|
-
# http://www.ruby-forum.com/topic/189397
|
523
|
-
#
|
524
|
-
if RUBY_VERSION < '1.9'
|
525
|
-
expect(out).to eq('100510051005100510051005100510051005')
|
526
|
-
else
|
527
|
-
expect(out).to eq('100510051005100510051005100510051005/1')
|
528
|
-
end
|
529
|
-
end
|
530
|
-
end
|
531
|
-
|
532
|
-
#------------------------------------------------------------------------------
|
533
|
-
describe 'Utility methods' do
|
534
|
-
it 'should merge options' do
|
535
|
-
ap = AwesomePrint::Inspector.new
|
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)
|
540
|
-
end
|
541
|
-
end
|
542
|
-
|
543
|
-
#------------------------------------------------------------------------------
|
544
|
-
describe 'Set' do
|
545
|
-
before do
|
546
|
-
@arr = [1, :two, 'three']
|
547
|
-
@set = Set.new(@arr)
|
548
|
-
end
|
549
|
-
|
550
|
-
it 'empty set' do
|
551
|
-
expect(Set.new.ai).to eq([].ai)
|
552
|
-
end
|
553
|
-
|
554
|
-
if RUBY_VERSION > '1.9'
|
555
|
-
it 'plain multiline' do
|
556
|
-
expect(@set.ai(plain: true)).to eq(@arr.ai(plain: true))
|
557
|
-
end
|
558
|
-
|
559
|
-
it 'plain multiline indented' do
|
560
|
-
expect(@set.ai(plain: true, indent: 1)).to eq(@arr.ai(plain: true, indent: 1))
|
561
|
-
end
|
562
|
-
|
563
|
-
it 'plain single line' do
|
564
|
-
expect(@set.ai(plain: true, multiline: false)).to eq(@arr.ai(plain: true, multiline: false))
|
565
|
-
end
|
566
|
-
|
567
|
-
it 'colored multiline (default)' do
|
568
|
-
expect(@set.ai).to eq(@arr.ai)
|
569
|
-
end
|
570
|
-
else # Prior to Ruby 1.9 the order of set values is unpredicatble.
|
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))
|
573
|
-
end
|
574
|
-
|
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))
|
577
|
-
end
|
578
|
-
|
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))
|
581
|
-
end
|
582
|
-
|
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)
|
585
|
-
end
|
586
|
-
end
|
587
|
-
end
|
588
|
-
|
589
|
-
#------------------------------------------------------------------------------
|
590
|
-
describe 'Struct' do
|
591
|
-
before do
|
592
|
-
@struct = unless defined?(Struct::SimpleStruct)
|
593
|
-
Struct.new('SimpleStruct', :name, :address).new
|
594
|
-
else
|
595
|
-
Struct::SimpleStruct.new
|
596
|
-
end
|
597
|
-
@struct.name = 'Herman Munster'
|
598
|
-
@struct.address = '1313 Mockingbird Lane'
|
599
|
-
end
|
600
|
-
|
601
|
-
it 'empty struct' do
|
602
|
-
expect(Struct.new('EmptyStruct').ai).to eq("\e[1;33mStruct::EmptyStruct < Struct\e[0m")
|
603
|
-
end
|
604
|
-
|
605
|
-
it 'plain multiline' do
|
606
|
-
s1 = <<-EOS.strip
|
607
|
-
address = \"1313 Mockingbird Lane\",
|
608
|
-
name = \"Herman Munster\"
|
609
|
-
EOS
|
610
|
-
s2 = <<-EOS.strip
|
611
|
-
name = \"Herman Munster\",
|
612
|
-
address = \"1313 Mockingbird Lane\"
|
613
|
-
EOS
|
614
|
-
expect(@struct.ai(plain: true)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
615
|
-
end
|
616
|
-
|
617
|
-
it 'plain multiline indented' do
|
618
|
-
s1 = <<-EOS.strip
|
619
|
-
address = "1313 Mockingbird Lane",
|
620
|
-
name = "Herman Munster"
|
621
|
-
EOS
|
622
|
-
s2 = <<-EOS.strip
|
623
|
-
name = "Herman Munster",
|
624
|
-
address = "1313 Mockingbird Lane"
|
625
|
-
EOS
|
626
|
-
expect(@struct.ai(plain: true, indent: 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
|
627
|
-
end
|
628
|
-
|
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) }
|
633
|
-
end
|
634
|
-
|
635
|
-
it 'colored multiline (default)' do
|
636
|
-
s1 = <<-EOS.strip
|
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
|
639
|
-
EOS
|
640
|
-
s2 = <<-EOS.strip
|
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
|
643
|
-
EOS
|
644
|
-
expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
|
645
|
-
end
|
646
|
-
end
|
647
|
-
|
648
|
-
#------------------------------------------------------------------------------
|
649
|
-
describe 'Inherited from standard Ruby classes' do
|
650
|
-
after do
|
651
|
-
Object.instance_eval { remove_const :My } if defined?(My)
|
652
|
-
end
|
653
|
-
|
654
|
-
it 'inherited from Array should be displayed as Array' do
|
655
|
-
class My < Array; end
|
656
|
-
|
657
|
-
my = My.new([1, :two, 'three', [nil, [true, false]]])
|
658
|
-
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
659
|
-
[
|
660
|
-
[0] 1,
|
661
|
-
[1] :two,
|
662
|
-
[2] "three",
|
663
|
-
[3] [
|
664
|
-
[0] nil,
|
665
|
-
[1] [
|
666
|
-
[0] true,
|
667
|
-
[1] false
|
668
|
-
]
|
669
|
-
]
|
670
|
-
]
|
671
|
-
EOS
|
672
|
-
end
|
673
|
-
|
674
|
-
it 'inherited from Hash should be displayed as Hash' do
|
675
|
-
class My < Hash; end
|
676
|
-
|
677
|
-
my = My[{ 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }]
|
678
|
-
expect(my.ai(plain: true)).to eq <<-EOS.strip
|
679
|
-
{
|
680
|
-
1 => {
|
681
|
-
:sym => {
|
682
|
-
"str" => {
|
683
|
-
[ 1, 2, 3 ] => {
|
684
|
-
{ :k => :v } => Hash < Object
|
685
|
-
}
|
686
|
-
}
|
687
|
-
}
|
688
|
-
}
|
689
|
-
}
|
690
|
-
EOS
|
691
|
-
end
|
692
|
-
|
693
|
-
it 'inherited from File should be displayed as File' do
|
694
|
-
class My < File; end
|
695
|
-
|
696
|
-
my = File.new('/dev/null') rescue File.new('nul')
|
697
|
-
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
698
|
-
end
|
699
|
-
|
700
|
-
it 'inherited from Dir should be displayed as Dir' do
|
701
|
-
class My < Dir; end
|
702
|
-
|
703
|
-
require 'tmpdir'
|
704
|
-
my = My.new(Dir.tmpdir)
|
705
|
-
expect(my.ai(plain: true)).to eq("#{my.inspect}\n#{`ls -alF #{my.path}`.chop}")
|
706
|
-
end
|
707
|
-
|
708
|
-
it 'should handle a class that defines its own #send method' do
|
709
|
-
class My
|
710
|
-
def send(arg1, arg2, arg3); end
|
711
|
-
end
|
712
|
-
|
713
|
-
my = My.new
|
714
|
-
expect { my.methods.ai(plain: true) }.not_to raise_error
|
715
|
-
end
|
716
|
-
|
717
|
-
it 'should handle a class defines its own #method method (ex. request.method)' do
|
718
|
-
class My
|
719
|
-
def method
|
720
|
-
'POST'
|
721
|
-
end
|
722
|
-
end
|
723
|
-
|
724
|
-
my = My.new
|
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
|
777
|
-
end
|
778
|
-
end
|
779
|
-
end
|