awesome_print 1.6.1 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/Appraisals +45 -22
  4. data/{CHANGELOG → CHANGELOG.md} +83 -18
  5. data/CONTRIBUTING.md +45 -4
  6. data/Gemfile.lock +28 -22
  7. data/LICENSE +16 -19
  8. data/README.md +97 -92
  9. data/Rakefile +5 -5
  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/colorize.rb +24 -0
  14. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  15. data/lib/awesome_print/core_ext/class.rb +3 -2
  16. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  17. data/lib/awesome_print/core_ext/logger.rb +1 -1
  18. data/lib/awesome_print/core_ext/method.rb +2 -2
  19. data/lib/awesome_print/core_ext/object.rb +3 -2
  20. data/lib/awesome_print/core_ext/string.rb +3 -3
  21. data/lib/awesome_print/custom_defaults.rb +57 -0
  22. data/lib/awesome_print/ext/action_view.rb +8 -4
  23. data/lib/awesome_print/ext/active_record.rb +45 -12
  24. data/lib/awesome_print/ext/active_support.rb +1 -1
  25. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  26. data/lib/awesome_print/ext/mongoid.rb +8 -6
  27. data/lib/awesome_print/ext/nobrainer.rb +52 -0
  28. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  29. data/lib/awesome_print/ext/ostruct.rb +1 -1
  30. data/lib/awesome_print/ext/ripple.rb +5 -6
  31. data/lib/awesome_print/ext/sequel.rb +7 -6
  32. data/lib/awesome_print/formatter.rb +54 -327
  33. data/lib/awesome_print/formatters/array_formatter.rb +139 -0
  34. data/lib/awesome_print/formatters/base_formatter.rb +140 -0
  35. data/lib/awesome_print/formatters/class_formatter.rb +25 -0
  36. data/lib/awesome_print/formatters/dir_formatter.rb +22 -0
  37. data/lib/awesome_print/formatters/file_formatter.rb +22 -0
  38. data/lib/awesome_print/formatters/hash_formatter.rb +106 -0
  39. data/lib/awesome_print/formatters/method_formatter.rb +22 -0
  40. data/lib/awesome_print/formatters/object_formatter.rb +78 -0
  41. data/lib/awesome_print/formatters/simple_formatter.rb +21 -0
  42. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  43. data/lib/awesome_print/formatters.rb +15 -0
  44. data/lib/awesome_print/indentator.rb +18 -0
  45. data/lib/awesome_print/inspector.rb +92 -94
  46. data/lib/awesome_print/version.rb +2 -2
  47. data/lib/awesome_print.rb +19 -18
  48. data/spec/active_record_helper.rb +24 -28
  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 +251 -184
  62. data/spec/methods_spec.rb +126 -130
  63. data/spec/misc_spec.rb +98 -93
  64. data/spec/objects_spec.rb +162 -27
  65. data/spec/spec_helper.rb +72 -38
  66. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  67. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  68. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  69. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  70. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  71. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  72. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  73. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  74. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  75. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  76. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  77. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  78. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  79. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  80. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  81. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  82. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  83. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  84. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  85. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  86. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  87. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  88. data/spec/support/active_record_data.rb +20 -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 +104 -15
  93. data/lib/awesome_print/ext/no_brainer.rb +0 -58
@@ -0,0 +1,260 @@
1
+ require 'spec_helper'
2
+ require 'active_record_helper'
3
+
4
+ RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }.call do
5
+ describe 'ActiveRecord instance, attributes only (default)' do
6
+ before do
7
+ ActiveRecord::Base.default_timezone = :utc
8
+ @diana = User.new(name: 'Diana', rank: 1, admin: false, created_at: '1992-10-10 12:30:00')
9
+ @laura = User.new(name: 'Laura', rank: 2, admin: true, created_at: '2003-05-26 14:15:00')
10
+ @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true)
11
+ end
12
+
13
+ it 'display single record' do
14
+ out = @ap.awesome(@diana)
15
+ str = <<-EOS.strip
16
+ #<User:placeholder_id> {
17
+ :admin => false,
18
+ :created_at => ?,
19
+ :id => nil,
20
+ :name => "Diana",
21
+ :rank => 1
22
+ }
23
+ EOS
24
+
25
+ expect(RUBY_VERSION).to be >= '2.5'
26
+
27
+ str.sub!('?', '1992-10-10 12:30:00 UTC')
28
+
29
+ expect(out).to be_similar_to(str)
30
+ end
31
+
32
+ it 'display multiple records' do
33
+ out = @ap.awesome([@diana, @laura])
34
+ str = <<-EOS.strip
35
+ [
36
+ [0] #<User:placeholder_id> {
37
+ :admin => false,
38
+ :created_at => ??,
39
+ :id => nil,
40
+ :name => "Diana",
41
+ :rank => 1
42
+ },
43
+ [1] #<User:placeholder_id> {
44
+ :admin => true,
45
+ :created_at => ?!,
46
+ :id => nil,
47
+ :name => "Laura",
48
+ :rank => 2
49
+ }
50
+ ]
51
+ EOS
52
+
53
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
54
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
55
+
56
+ expect(out).to be_similar_to(str)
57
+ end
58
+
59
+ it 'display multiple records on a relation' do
60
+ @diana.save
61
+ @laura.save
62
+ out = @ap.awesome(User.all)
63
+ str = <<-EOS.strip
64
+ [
65
+ [0] #<User:placeholder_id> {
66
+ :admin => false,
67
+ :created_at => ??,
68
+ :id => 1,
69
+ :name => "Diana",
70
+ :rank => 1
71
+ },
72
+ [1] #<User:placeholder_id> {
73
+ :admin => true,
74
+ :created_at => ?!,
75
+ :id => 2,
76
+ :name => "Laura",
77
+ :rank => 2
78
+ }
79
+ ]
80
+ EOS
81
+
82
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
83
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
84
+
85
+ expect(out).to be_similar_to(str)
86
+ end
87
+ end
88
+
89
+ describe 'Linked records (joins)' do
90
+ before do
91
+ @ap = AwesomePrint::Inspector.new(plain: true)
92
+ end
93
+
94
+ it 'should show the entire record' do
95
+ e = Email.create(email_address: 'foo@bar.com')
96
+ u = User.last
97
+ u.emails << e
98
+ email_record = User.joins(:emails).select('users.id, emails.email_address').last
99
+ out = @ap.awesome(email_record)
100
+ raw_object_string = <<-EOS.strip
101
+ #<User:placeholder_id> {
102
+ "id" => #{u.id},
103
+ "email_address" => "#{e.email_address}"
104
+ }
105
+ EOS
106
+ expect(out).to be_similar_to(raw_object_string)
107
+ end
108
+ end
109
+
110
+ #------------------------------------------------------------------------------
111
+ describe 'ActiveRecord instance (raw)' do
112
+ before do
113
+ ActiveRecord::Base.default_timezone = :utc
114
+ @diana = User.new(name: 'Diana', rank: 1, admin: false, created_at: '1992-10-10 12:30:00')
115
+ @laura = User.new(name: 'Laura', rank: 2, admin: true, created_at: '2003-05-26 14:15:00')
116
+ @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true)
117
+ end
118
+
119
+ it 'display single record' do
120
+ out = @ap.awesome(@diana)
121
+
122
+ raw_object_string =
123
+ if activerecord_6_1?
124
+ ActiveRecordData.raw_6_1_diana
125
+ elsif activerecord_6_0?
126
+ ActiveRecordData.raw_6_0_diana
127
+ elsif activerecord_5_2?
128
+ ActiveRecordData.raw_5_2_diana
129
+ elsif activerecord_5_1?
130
+ ActiveRecordData.raw_5_1_diana
131
+ elsif activerecord_5_0?
132
+ ActiveRecordData.raw_5_0_diana
133
+ elsif activerecord_4_2?
134
+ ActiveRecordData.raw_4_2_diana
135
+ elsif activerecord_4_1?
136
+ ActiveRecordData.raw_4_1_diana
137
+ elsif activerecord_4_0?
138
+ ActiveRecordData.raw_4_0_diana
139
+ elsif activerecord_3_2?
140
+ ActiveRecordData.raw_3_2_diana
141
+ end
142
+ raw_object_string.sub!('?', '1992-10-10 12:30:00')
143
+ expect(out).to be_similar_to(raw_object_string)
144
+ end
145
+
146
+ it 'display multiple records' do
147
+ out = @ap.awesome([@diana, @laura])
148
+
149
+ raw_object_string =
150
+ if activerecord_6_1?
151
+ ActiveRecordData.raw_6_1_multi
152
+ elsif activerecord_6_0?
153
+ ActiveRecordData.raw_6_0_multi
154
+ elsif activerecord_5_2?
155
+ ActiveRecordData.raw_5_2_multi
156
+ elsif activerecord_5_1?
157
+ ActiveRecordData.raw_5_1_multi
158
+ elsif activerecord_5_0?
159
+ ActiveRecordData.raw_5_0_multi
160
+ elsif activerecord_4_2?
161
+ ActiveRecordData.raw_4_2_multi
162
+ elsif activerecord_4_1?
163
+ ActiveRecordData.raw_4_1_multi
164
+ elsif activerecord_4_0?
165
+ ActiveRecordData.raw_4_0_multi
166
+ elsif activerecord_3_2?
167
+ ActiveRecordData.raw_3_2_multi
168
+ end
169
+ raw_object_string.sub!('?', '1992-10-10 12:30:00')
170
+ raw_object_string.sub!('?', '2003-05-26 14:15:00')
171
+ expect(out).to be_similar_to(raw_object_string)
172
+ end
173
+ end
174
+
175
+ #------------------------------------------------------------------------------
176
+ describe 'ActiveRecord class' do
177
+ before do
178
+ @ap = AwesomePrint::Inspector.new(plain: true)
179
+ end
180
+
181
+ it 'should print the class' do
182
+ expect(@ap.awesome(User)).to eq <<-EOS.strip
183
+ class User < ActiveRecord::Base {
184
+ :id => :integer,
185
+ :name => :string,
186
+ :rank => :integer,
187
+ :admin => :boolean,
188
+ :created_at => :datetime
189
+ }
190
+ EOS
191
+ end
192
+
193
+ it 'should print the class for non-direct subclasses of ActiveRecord::Base' do
194
+ out = @ap.awesome(SubUser)
195
+ expect(out).to eq <<-EOS.strip
196
+ class SubUser < User {
197
+ :id => :integer,
198
+ :name => :string,
199
+ :rank => :integer,
200
+ :admin => :boolean,
201
+ :created_at => :datetime
202
+ }
203
+ EOS
204
+ end
205
+
206
+ it 'should print ActiveRecord::Base objects (ex. ancestors)' do
207
+ expect { @ap.awesome(User.ancestors) }.not_to raise_error
208
+ end
209
+ end
210
+
211
+ #------------------------------------------------------------------------------
212
+ describe 'ActiveRecord methods formatting' do
213
+ before do
214
+ @ap = AwesomePrint::Inspector.new(plain: true)
215
+ end
216
+
217
+ it 'should format class methods properly' do
218
+ # spec 1
219
+ out = @ap.awesome(User.methods.grep(/first/))
220
+
221
+ if RUBY_VERSION >= '3.0.0'
222
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)/)
223
+ elsif RUBY_VERSION >= '2.7.0'
224
+ if ActiveRecord::VERSION::STRING >= '3.2'
225
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)\s+User/)
226
+ else
227
+ expect(out).to match(/\sfirst\(\*\*,\s&&\)\s+User \(ActiveRecord::Base\)/)
228
+ end
229
+ else
230
+ if ActiveRecord::VERSION::STRING >= '3.2'
231
+ expect(out).to match(/\sfirst\(\*arg.*?\)\s+User/)
232
+ else
233
+ expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
234
+ end
235
+ end
236
+
237
+ # spec 2
238
+ out = @ap.awesome(User.methods.grep(/primary_key/))
239
+ if RUBY_VERSION >= '3.0.0'
240
+ expect(out).to match(/\sprimary_key\(.*?\)/)
241
+ else
242
+ expect(out).to match(/\sprimary_key\(.*?\)\s+User/)
243
+ end
244
+
245
+ # spec 3
246
+ out = @ap.awesome(User.methods.grep(/validate/))
247
+ if ActiveRecord::VERSION::MAJOR < 3
248
+ expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
249
+ else
250
+ if RUBY_VERSION >= '3.0.0'
251
+ expect(out).to match(/\svalidate\(\*arg.*?\)/)
252
+ else
253
+ expect(out).to match(/\svalidate\(\*arg.*?\)\s+User/)
254
+ end
255
+ end
256
+
257
+ end
258
+ end
259
+ end
260
+
@@ -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