awesome_print 1.8.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Appraisals +16 -48
  4. data/CHANGELOG.md +10 -1
  5. data/LICENSE +16 -19
  6. data/README.md +3 -10
  7. data/lib/ap.rb +1 -5
  8. data/lib/awesome_print.rb +7 -29
  9. data/lib/awesome_print/colorize.rb +2 -0
  10. data/lib/awesome_print/core_ext/active_support.rb +7 -0
  11. data/lib/awesome_print/core_ext/awesome_method_array.rb +0 -6
  12. data/lib/awesome_print/core_ext/class.rb +0 -5
  13. data/lib/awesome_print/core_ext/kernel.rb +0 -5
  14. data/lib/awesome_print/core_ext/logger.rb +0 -5
  15. data/lib/awesome_print/core_ext/object.rb +0 -5
  16. data/lib/awesome_print/core_ext/string.rb +0 -5
  17. data/lib/awesome_print/formatter.rb +38 -98
  18. data/lib/awesome_print/formatters/array_formatter.rb +10 -7
  19. data/lib/awesome_print/formatters/base_formatter.rb +17 -50
  20. data/lib/awesome_print/formatters/bigdecimal_formatter.rb +20 -0
  21. data/lib/awesome_print/formatters/class_formatter.rb +5 -6
  22. data/lib/awesome_print/formatters/dir_formatter.rb +4 -6
  23. data/lib/awesome_print/formatters/ext/active_model_error_formatter.rb +44 -0
  24. data/lib/awesome_print/formatters/ext/active_record_attributeset_formatter.rb +27 -0
  25. data/lib/awesome_print/formatters/ext/active_record_class_formatter.rb +43 -0
  26. data/lib/awesome_print/formatters/ext/active_record_instance_formatter.rb +48 -0
  27. data/lib/awesome_print/formatters/ext/active_record_relation_formatter.rb +19 -0
  28. data/lib/awesome_print/formatters/ext/bson_objectid_formatter.rb +20 -0
  29. data/lib/awesome_print/formatters/ext/hash_with_indifferent_access_formatter.rb +20 -0
  30. data/lib/awesome_print/formatters/ext/mongoid_document_formatter.rb +59 -0
  31. data/lib/awesome_print/formatters/ext/nokogiri_document_formatter.rb +30 -0
  32. data/lib/awesome_print/formatters/ext/nokogiri_node_formatter.rb +29 -0
  33. data/lib/awesome_print/formatters/ext/nokogiri_nodeset_formatter.rb +32 -0
  34. data/lib/awesome_print/formatters/ext/sequel_dataset_formatter.rb +20 -0
  35. data/lib/awesome_print/formatters/ext/sequel_model_class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/ext/sequel_model_formatter.rb +26 -0
  37. data/lib/awesome_print/formatters/ext/time_with_zone_formatter.rb +24 -0
  38. data/lib/awesome_print/formatters/fallback_formatter.rb +55 -0
  39. data/lib/awesome_print/formatters/false_class_formatter.rb +15 -0
  40. data/lib/awesome_print/formatters/file_formatter.rb +4 -6
  41. data/lib/awesome_print/formatters/hash_formatter.rb +12 -7
  42. data/lib/awesome_print/formatters/integer_formatter.rb +11 -0
  43. data/lib/awesome_print/formatters/method_formatter.rb +8 -6
  44. data/lib/awesome_print/formatters/module_formatter.rb +18 -0
  45. data/lib/awesome_print/formatters/nil_class_formatter.rb +19 -0
  46. data/lib/awesome_print/formatters/object_formatter.rb +20 -7
  47. data/lib/awesome_print/formatters/open_struct.rb +19 -0
  48. data/lib/awesome_print/formatters/range_formatter.rb +11 -0
  49. data/lib/awesome_print/formatters/rational_formatter.rb +15 -0
  50. data/lib/awesome_print/formatters/set_formatter.rb +19 -0
  51. data/lib/awesome_print/formatters/simple_formatter.rb +10 -8
  52. data/lib/awesome_print/formatters/string_formatter.rb +14 -0
  53. data/lib/awesome_print/formatters/struct_formatter.rb +8 -6
  54. data/lib/awesome_print/formatters/symbol_formatter.rb +18 -0
  55. data/lib/awesome_print/formatters/true_class_formatter.rb +15 -0
  56. data/lib/awesome_print/formatters/unbound_method_formatter.rb +15 -0
  57. data/lib/awesome_print/inspector.rb +20 -16
  58. data/lib/awesome_print/limiter.rb +63 -0
  59. data/lib/awesome_print/registrar.rb +20 -0
  60. data/lib/awesome_print/version.rb +6 -6
  61. data/spec/formatters/array_spec.rb +251 -0
  62. data/spec/formatters/big_decimal_spec.rb +28 -0
  63. data/spec/formatters/class_spec.rb +91 -0
  64. data/spec/formatters/dir_file_spec.rb +43 -0
  65. data/spec/{ext → formatters/ext}/active_record_spec.rb +27 -63
  66. data/spec/{ext → formatters/ext}/active_support_spec.rb +6 -6
  67. data/spec/{ext → formatters/ext}/mongoid_spec.rb +4 -41
  68. data/spec/{ext → formatters/ext}/nokogiri_spec.rb +0 -0
  69. data/spec/formatters/hash_spec.rb +284 -0
  70. data/spec/{methods_spec.rb → formatters/methods_spec.rb} +13 -7
  71. data/spec/{objects_spec.rb → formatters/objects_spec.rb} +49 -0
  72. data/spec/{ext → formatters}/ostruct_spec.rb +1 -0
  73. data/spec/formatters/set_spec.rb +49 -0
  74. data/spec/formatters/struct_spec.rb +61 -0
  75. data/spec/merge_options_spec.rb +13 -0
  76. data/spec/misc_spec.rb +21 -21
  77. data/spec/spec_helper.rb +5 -10
  78. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  79. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  80. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  82. data/spec/support/ext_verifier.rb +0 -15
  83. data/spec/support/mongoid_versions.rb +4 -4
  84. data/spec/support/rails_versions.rb +10 -20
  85. metadata +98 -87
  86. data/lib/awesome_print/core_ext/method.rb +0 -21
  87. data/lib/awesome_print/ext/action_view.rb +0 -22
  88. data/lib/awesome_print/ext/active_record.rb +0 -78
  89. data/lib/awesome_print/ext/active_support.rb +0 -47
  90. data/lib/awesome_print/ext/mongo_mapper.rb +0 -124
  91. data/lib/awesome_print/ext/mongoid.rb +0 -67
  92. data/lib/awesome_print/ext/nobrainer.rb +0 -52
  93. data/lib/awesome_print/ext/nokogiri.rb +0 -45
  94. data/lib/awesome_print/ext/ostruct.rb +0 -27
  95. data/lib/awesome_print/ext/ripple.rb +0 -71
  96. data/lib/awesome_print/ext/sequel.rb +0 -58
  97. data/spec/ext/action_view_spec.rb +0 -18
  98. data/spec/ext/mongo_mapper_spec.rb +0 -261
  99. data/spec/ext/nobrainer_spec.rb +0 -59
  100. data/spec/ext/ripple_spec.rb +0 -48
  101. data/spec/formats_spec.rb +0 -779
  102. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  103. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  104. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  105. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  106. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  107. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  108. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  109. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  110. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  111. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  112. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  113. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe 'AwesomePrint ActionView extensions', skip: -> { !ExtVerifier.has_rails? }.call do
4
-
5
- before do
6
- @view = ActionView::Base.new
7
- end
8
-
9
- it "uses HTML and adds 'debug_dump' class to plain <pre> tag" do
10
- markup = rand
11
- expect(@view.ap(markup, plain: true)).to eq(%Q|<pre class="debug_dump">#{markup}</pre>|)
12
- end
13
-
14
- it "uses HTML and adds 'debug_dump' class to colorized <pre> tag" do
15
- markup = ' &<hello>'
16
- expect(@view.ap(markup)).to eq('<pre class="debug_dump"><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
17
- end
18
- end
@@ -1,261 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe 'AwesomePrint/MongoMapper', skip: -> { !ExtVerifier.has_mongo_mapper? }.call do
4
- if ExtVerifier.has_mongo_mapper?
5
- before :all do
6
- class MongoUser
7
- include MongoMapper::Document
8
-
9
- key :first_name, String
10
- key :last_name, String
11
- end
12
- end
13
-
14
- after :all do
15
- Object.instance_eval { remove_const :MongoUser }
16
- Object.instance_eval { remove_const :Chamelion }
17
- end
18
- end
19
-
20
- before do
21
- @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true)
22
- end
23
-
24
- describe 'with the raw option set to true' do
25
- # before { @ap.options[:raw] = true }
26
- before { @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true) }
27
-
28
- it 'should print class instance' do
29
- user = MongoUser.new(first_name: 'Al', last_name: 'Capone')
30
-
31
- out = @ap.send(:awesome, user)
32
- out.gsub!(/#\<Proc:.+?\>/, 'AWESOME_PRINT_PROC_STUB')
33
- out.gsub!(/BSON::ObjectId\('[\da-f]+?'\)/, "BSON::ObjectId('123456789')")
34
-
35
- str = if MongoMapper::Version >= '0.13'
36
- <<-EOS.strip
37
- #<MongoUser:placeholder_id
38
- @__mm_default_keys = [
39
- [0] #<MongoMapper::Plugins::Keys::Key:placeholder_id
40
- @dynamic = false,
41
- @embeddable = false,
42
- @has_default = true,
43
- @is_id = true,
44
- @typecast = nil,
45
- attr_accessor :accessors = [],
46
- attr_accessor :default = AWESOME_PRINT_PROC_STUB,
47
- attr_accessor :ivar = :@_id,
48
- attr_accessor :name = "_id",
49
- attr_accessor :options = {
50
- :default => AWESOME_PRINT_PROC_STUB
51
- },
52
- attr_accessor :type = ObjectId < Object
53
- >
54
- ],
55
- @__mm_keys = {
56
- "_id" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
57
- @dynamic = false,
58
- @embeddable = false,
59
- @has_default = true,
60
- @is_id = true,
61
- @typecast = nil,
62
- attr_accessor :accessors = [],
63
- attr_accessor :default = AWESOME_PRINT_PROC_STUB,
64
- attr_accessor :ivar = :@_id,
65
- attr_accessor :name = "_id",
66
- attr_accessor :options = {
67
- :default => AWESOME_PRINT_PROC_STUB
68
- },
69
- attr_accessor :type = ObjectId < Object
70
- >,
71
- "first_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
72
- @dynamic = false,
73
- @embeddable = false,
74
- @has_default = false,
75
- @is_id = false,
76
- @typecast = nil,
77
- attr_accessor :accessors = [],
78
- attr_accessor :ivar = :@first_name,
79
- attr_accessor :name = "first_name",
80
- attr_accessor :options = {},
81
- attr_accessor :type = String < Object
82
- >,
83
- "last_name" => #<MongoMapper::Plugins::Keys::Key:placeholder_id
84
- @dynamic = false,
85
- @embeddable = false,
86
- @has_default = false,
87
- @is_id = false,
88
- @typecast = nil,
89
- attr_accessor :accessors = [],
90
- attr_accessor :ivar = :@last_name,
91
- attr_accessor :name = "last_name",
92
- attr_accessor :options = {},
93
- attr_accessor :type = String < Object
94
- >
95
- },
96
- @__mm_pre_cast = {
97
- "first_name" => "Al",
98
- "last_name" => "Capone"
99
- },
100
- @_dynamic_attributes = {},
101
- @_new = true,
102
- attr_accessor :_id = BSON::ObjectId('123456789'),
103
- attr_accessor :attributes = nil,
104
- attr_accessor :first_name = "Al",
105
- attr_accessor :last_name = "Capone",
106
- attr_reader :changed_attributes = {
107
- "first_name" => nil,
108
- "last_name" => nil
109
- }
110
- >
111
- EOS
112
- else
113
- <<-EOS.strip
114
- #<MongoUser:placeholder_id
115
- @_new = true,
116
- attr_accessor :first_name = "Al",
117
- attr_accessor :last_name = "Capone",
118
- attr_reader :changed_attributes = {
119
- "first_name" => nil,
120
- "last_name" => nil
121
- },
122
- attr_reader :first_name_before_type_cast = "Al",
123
- attr_reader :last_name_before_type_cast = "Capone"
124
- >
125
- EOS
126
- end
127
- expect(out).to be_similar_to(str)
128
- end
129
-
130
- it 'should print the class' do
131
- expect(@ap.send(:awesome, MongoUser)).to eq <<-EOS.strip
132
- class MongoUser < Object {
133
- "_id" => :object_id,
134
- "first_name" => :string,
135
- "last_name" => :string
136
- }
137
- EOS
138
- end
139
-
140
- it 'should print the class when type is undefined' do
141
- class Chamelion
142
- include MongoMapper::Document
143
- key :last_attribute
144
- end
145
-
146
- expect(@ap.send(:awesome, Chamelion)).to eq <<-EOS.strip
147
- class Chamelion < Object {
148
- "_id" => :object_id,
149
- "last_attribute" => :undefined
150
- }
151
- EOS
152
- end
153
- end
154
-
155
- describe 'with associations' do
156
-
157
- if ExtVerifier.has_mongo_mapper?
158
- before :all do
159
- class Child
160
- include MongoMapper::EmbeddedDocument
161
- key :data
162
- end
163
-
164
- class Sibling
165
- include MongoMapper::Document
166
- key :title
167
- end
168
-
169
- class Parent
170
- include MongoMapper::Document
171
- key :name
172
-
173
- one :child
174
- one :sibling
175
- end
176
- end
177
- end
178
-
179
- describe 'with show associations turned off (default)' do
180
- it 'should render the class as normal' do
181
- expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip
182
- class Parent < Object {
183
- "_id" => :object_id,
184
- "name" => :undefined
185
- }
186
- EOS
187
- end
188
-
189
- it 'should render an instance as normal' do
190
- parent = Parent.new(name: 'test')
191
- out = @ap.send(:awesome, parent)
192
- str = <<-EOS.strip
193
- #<Parent:placeholder_id> {
194
- "_id" => placeholder_bson_id,
195
- "name" => "test"
196
- }
197
- EOS
198
- expect(out).to be_similar_to(str)
199
- end
200
- end
201
-
202
- describe 'with show associations turned on and inline embedded turned off' do
203
- before :each do
204
- @ap = AwesomePrint::Inspector.new(plain: true, mongo_mapper: { show_associations: true })
205
- end
206
-
207
- it 'should render the class with associations shown' do
208
- expect(@ap.send(:awesome, Parent)).to eq <<-EOS.strip
209
- class Parent < Object {
210
- "_id" => :object_id,
211
- "name" => :undefined,
212
- "child" => embeds one Child,
213
- "sibling" => one Sibling
214
- }
215
- EOS
216
- end
217
-
218
- it 'should render an instance with associations shown' do
219
- parent = Parent.new(name: 'test')
220
- out = @ap.send(:awesome, parent)
221
- str = <<-EOS.strip
222
- #<Parent:placeholder_id> {
223
- "_id" => placeholder_bson_id,
224
- "name" => "test",
225
- "child" => embeds one Child,
226
- "sibling" => one Sibling
227
- }
228
- EOS
229
- expect(out).to be_similar_to(str)
230
- end
231
- end
232
-
233
- describe 'with show associations turned on and inline embedded turned on' do
234
- before :each do
235
- @ap = AwesomePrint::Inspector.new(plain: true,
236
- mongo_mapper: {
237
- show_associations: true,
238
- inline_embedded: true
239
- }
240
- )
241
- end
242
-
243
- it 'should render an instance with associations shown and embeds there' do
244
- parent = Parent.new(name: 'test', child: Child.new(data: 5))
245
- out = @ap.send(:awesome, parent)
246
- str = <<-EOS.strip
247
- #<Parent:placeholder_id> {
248
- "_id" => placeholder_bson_id,
249
- "name" => "test",
250
- "child" => embedded #<Child:placeholder_id> {
251
- "_id" => placeholder_bson_id,
252
- "data" => 5
253
- },
254
- "sibling" => one Sibling
255
- }
256
- EOS
257
- expect(out).to be_similar_to(str)
258
- end
259
- end
260
- end
261
- end
@@ -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
@@ -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
@@ -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