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.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -2
  3. data/Appraisals +62 -0
  4. data/CHANGELOG.md +180 -0
  5. data/CONTRIBUTING.md +82 -0
  6. data/Gemfile +3 -2
  7. data/LICENSE +16 -19
  8. data/README.md +112 -87
  9. data/Rakefile +17 -4
  10. data/awesome_print.gemspec +33 -0
  11. data/init.rb +1 -0
  12. data/lib/ap.rb +2 -2
  13. data/lib/awesome_print.rb +22 -16
  14. data/lib/awesome_print/colorize.rb +24 -0
  15. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  16. data/lib/awesome_print/core_ext/class.rb +3 -2
  17. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  18. data/lib/awesome_print/core_ext/logger.rb +1 -1
  19. data/lib/awesome_print/core_ext/method.rb +2 -2
  20. data/lib/awesome_print/core_ext/object.rb +3 -2
  21. data/lib/awesome_print/core_ext/string.rb +3 -3
  22. data/lib/awesome_print/custom_defaults.rb +57 -0
  23. data/lib/awesome_print/ext/action_view.rb +8 -4
  24. data/lib/awesome_print/ext/active_record.rb +47 -13
  25. data/lib/awesome_print/ext/active_support.rb +1 -1
  26. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  27. data/lib/awesome_print/ext/mongoid.rb +9 -7
  28. data/lib/awesome_print/ext/nobrainer.rb +52 -0
  29. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  30. data/lib/awesome_print/ext/ostruct.rb +27 -0
  31. data/lib/awesome_print/ext/ripple.rb +6 -7
  32. data/lib/awesome_print/ext/sequel.rb +7 -6
  33. data/lib/awesome_print/formatter.rb +54 -327
  34. data/lib/awesome_print/formatters.rb +15 -0
  35. data/lib/awesome_print/formatters/array_formatter.rb +139 -0
  36. data/lib/awesome_print/formatters/base_formatter.rb +140 -0
  37. data/lib/awesome_print/formatters/class_formatter.rb +25 -0
  38. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  39. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  40. data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
  41. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  42. data/lib/awesome_print/formatters/object_formatter.rb +78 -0
  43. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  44. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  45. data/lib/awesome_print/indentator.rb +18 -0
  46. data/lib/awesome_print/inspector.rb +92 -94
  47. data/lib/awesome_print/version.rb +2 -2
  48. data/spec/active_record_helper.rb +30 -0
  49. data/spec/colors_spec.rb +31 -31
  50. data/spec/core_ext/logger_spec.rb +43 -0
  51. data/spec/core_ext/string_spec.rb +20 -0
  52. data/spec/ext/action_view_spec.rb +21 -0
  53. data/spec/ext/active_record_spec.rb +260 -0
  54. data/spec/ext/active_support_spec.rb +30 -0
  55. data/spec/ext/mongo_mapper_spec.rb +261 -0
  56. data/spec/ext/mongoid_spec.rb +67 -0
  57. data/spec/ext/nobrainer_spec.rb +59 -0
  58. data/spec/ext/nokogiri_spec.rb +46 -0
  59. data/spec/ext/ostruct_spec.rb +22 -0
  60. data/spec/ext/ripple_spec.rb +48 -0
  61. data/spec/formats_spec.rb +272 -205
  62. data/spec/methods_spec.rb +173 -177
  63. data/spec/misc_spec.rb +129 -105
  64. data/spec/objects_spec.rb +162 -27
  65. data/spec/spec_helper.rb +75 -37
  66. data/spec/support/active_record_data.rb +20 -0
  67. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  68. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  69. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  70. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  71. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  72. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  73. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  74. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  75. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  76. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  77. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  78. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  79. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  80. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  81. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  82. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  83. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  84. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  85. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  86. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  87. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  88. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  89. data/spec/support/ext_verifier.rb +42 -0
  90. data/spec/support/mongoid_versions.rb +26 -0
  91. data/spec/support/rails_versions.rb +55 -0
  92. metadata +163 -32
  93. data/CHANGELOG +0 -96
  94. data/lib/awesome_print/ext/no_brainer.rb +0 -58
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AwesomePrint::ActiveSupport', skip: -> { !ExtVerifier.has_rails? }.call do
4
+ before do
5
+ @ap = AwesomePrint::Inspector.new
6
+ end
7
+
8
+ it 'should format ActiveSupport::TimeWithZone as regular Time' do
9
+ Time.zone = 'Eastern Time (US & Canada)'
10
+ time = Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone
11
+ if activerecord_6_1?
12
+ expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45.000000000 EST -05:00\e[0m")
13
+ else
14
+ expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m")
15
+ end
16
+ end
17
+
18
+ it 'should format HashWithIndifferentAccess as regular Hash' do
19
+ hash = HashWithIndifferentAccess.new({ hello: 'world' })
20
+ expect(@ap.send(:awesome, hash)).to eq("{\n \"hello\"\e[0;37m => \e[0m\e[0;33m\"world\"\e[0m\n}")
21
+ end
22
+
23
+ # ActiveSupport sticks in instance variables to the date object. Make sure
24
+ # we ignore that and format Date instance as regular date.
25
+ it 'should formate Date object as date' do
26
+ date = Date.new(2003, 5, 26)
27
+ expect(date.ai(plain: true)).to eq('Mon, 26 May 2003')
28
+ expect(date.ai).to eq("\e[0;32mMon, 26 May 2003\e[0m")
29
+ end
30
+ end
@@ -0,0 +1,261 @@
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
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AwesomePrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.call do
4
+
5
+ if ExtVerifier.has_mongoid?
6
+ before :all do
7
+ class MongoUser
8
+ include Mongoid::Document
9
+
10
+ field :first_name, type: String
11
+ field :last_name, type: String
12
+ end
13
+ end
14
+
15
+ after :all do
16
+ Object.instance_eval { remove_const :MongoUser }
17
+ Object.instance_eval { remove_const :Chamelion }
18
+ end
19
+ end
20
+
21
+ before do
22
+ @ap = AwesomePrint::Inspector.new plain: true, sort_keys: true
23
+ end
24
+
25
+ it 'should print class instance' do
26
+ user = MongoUser.new first_name: 'Al', last_name: 'Capone'
27
+ out = @ap.send :awesome, user
28
+
29
+ object_id = user.id.inspect
30
+ str = <<-EOS.strip
31
+ #<MongoUser:placeholder_id> {
32
+ :_id => #{object_id},
33
+ :first_name => "Al",
34
+ :last_name => "Capone"
35
+ }
36
+ EOS
37
+ expect(out).to be_similar_to(str, { skip_bson: true })
38
+ end
39
+
40
+ it 'should print the class' do
41
+ class_spec = <<-EOS.strip
42
+ class MongoUser < Object {
43
+ :_id => :"bson/object_id",
44
+ :first_name => :string,
45
+ :last_name => :string
46
+ }
47
+ EOS
48
+
49
+ expect(@ap.send(:awesome, MongoUser)).to eq class_spec
50
+ end
51
+
52
+ it 'should print the class when type is undefined' do
53
+ class Chamelion
54
+ include Mongoid::Document
55
+ field :last_attribute
56
+ end
57
+
58
+ class_spec = <<-EOS.strip
59
+ class Chamelion < Object {
60
+ :_id => :"bson/object_id",
61
+ :last_attribute => :object
62
+ }
63
+ EOS
64
+
65
+ expect(@ap.send(:awesome, Chamelion)).to eq class_spec
66
+ end
67
+ end
@@ -0,0 +1,59 @@
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
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AwesomePrint/Nokogiri' do
4
+ it 'should colorize tags' do
5
+ xml = Nokogiri::XML('<html><body><h1></h1></body></html>')
6
+ expect(xml.ai).to eq <<-EOS
7
+ <?xml version=\"1.0\"?>\e[1;32m
8
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
9
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
10
+ \e[0m<\e[1;36mh1\e[0m/>\e[1;32m
11
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
12
+ \e[0m<\e[1;36m/html\e[0m>
13
+ EOS
14
+ end
15
+
16
+ it 'should colorize contents' do
17
+ xml = Nokogiri::XML('<html><body><h1>Hello</h1></body></html>')
18
+ expect(xml.ai).to eq <<-EOS
19
+ <?xml version=\"1.0\"?>\e[1;32m
20
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
21
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
22
+ \e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
23
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
24
+ \e[0m<\e[1;36m/html\e[0m>
25
+ EOS
26
+ end
27
+
28
+ it 'should colorize class and id' do
29
+ xml = Nokogiri::XML('<html><body><h1><span id="hello" class="world"></span></h1></body></html>')
30
+ expect(xml.ai).to eq <<-EOS
31
+ <?xml version=\"1.0\"?>\e[1;32m
32
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
33
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
34
+ \e[0m<\e[1;36mh1\e[0m>\e[1;32m
35
+ \e[0m<\e[1;36mspan\e[0m \e[1;33mid=\"hello\"\e[0m \e[1;33mclass=\"world\"\e[0m/>\e[1;32m
36
+ \e[0m<\e[1;36m/h1\e[0m>\e[1;32m
37
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
38
+ \e[0m<\e[1;36m/html\e[0m>
39
+ EOS
40
+ end
41
+
42
+ it 'handle empty NodeSet' do
43
+ xml = Nokogiri::XML::NodeSet.new(Nokogiri::XML(''))
44
+ expect(xml.ai).to eq('[]')
45
+ end
46
+ end