awesome_print 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/Appraisals +11 -0
  4. data/CHANGELOG.md +10 -0
  5. data/CONTRIBUTING.md +1 -1
  6. data/README.md +47 -38
  7. data/Rakefile +5 -5
  8. data/lib/ap.rb +2 -2
  9. data/lib/awesome_print.rb +18 -17
  10. data/lib/awesome_print/colorize.rb +1 -1
  11. data/lib/awesome_print/core_ext/{array.rb → awesome_method_array.rb} +16 -18
  12. data/lib/awesome_print/core_ext/class.rb +3 -2
  13. data/lib/awesome_print/core_ext/kernel.rb +1 -1
  14. data/lib/awesome_print/core_ext/logger.rb +1 -1
  15. data/lib/awesome_print/core_ext/method.rb +2 -2
  16. data/lib/awesome_print/core_ext/object.rb +3 -2
  17. data/lib/awesome_print/core_ext/string.rb +3 -3
  18. data/lib/awesome_print/custom_defaults.rb +57 -0
  19. data/lib/awesome_print/ext/action_view.rb +8 -4
  20. data/lib/awesome_print/ext/active_record.rb +19 -11
  21. data/lib/awesome_print/ext/active_support.rb +1 -1
  22. data/lib/awesome_print/ext/mongo_mapper.rb +16 -13
  23. data/lib/awesome_print/ext/mongoid.rb +8 -6
  24. data/lib/awesome_print/ext/nobrainer.rb +8 -5
  25. data/lib/awesome_print/ext/nokogiri.rb +4 -4
  26. data/lib/awesome_print/ext/ostruct.rb +1 -1
  27. data/lib/awesome_print/ext/ripple.rb +5 -6
  28. data/lib/awesome_print/ext/sequel.rb +7 -6
  29. data/lib/awesome_print/formatter.rb +11 -19
  30. data/lib/awesome_print/formatters.rb +15 -0
  31. data/lib/awesome_print/formatters/array_formatter.rb +108 -42
  32. data/lib/awesome_print/formatters/base_formatter.rb +13 -11
  33. data/lib/awesome_print/formatters/class_formatter.rb +2 -1
  34. data/lib/awesome_print/formatters/dir_formatter.rb +1 -1
  35. data/lib/awesome_print/formatters/file_formatter.rb +1 -1
  36. data/lib/awesome_print/formatters/hash_formatter.rb +74 -22
  37. data/lib/awesome_print/formatters/object_formatter.rb +9 -14
  38. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  39. data/lib/awesome_print/inspector.rb +77 -93
  40. data/lib/awesome_print/version.rb +2 -2
  41. data/spec/active_record_helper.rb +8 -2
  42. data/spec/colors_spec.rb +30 -30
  43. data/spec/core_ext/logger_spec.rb +43 -0
  44. data/spec/core_ext/string_spec.rb +20 -0
  45. data/spec/ext/action_view_spec.rb +18 -0
  46. data/spec/ext/active_record_spec.rb +252 -0
  47. data/spec/ext/active_support_spec.rb +26 -0
  48. data/spec/ext/mongo_mapper_spec.rb +261 -0
  49. data/spec/ext/mongoid_spec.rb +104 -0
  50. data/spec/ext/nobrainer_spec.rb +59 -0
  51. data/spec/ext/nokogiri_spec.rb +46 -0
  52. data/spec/ext/ostruct_spec.rb +22 -0
  53. data/spec/ext/ripple_spec.rb +48 -0
  54. data/spec/formats_spec.rb +193 -165
  55. data/spec/methods_spec.rb +116 -128
  56. data/spec/misc_spec.rb +104 -108
  57. data/spec/objects_spec.rb +70 -28
  58. data/spec/spec_helper.rb +27 -10
  59. data/spec/support/active_record_data.rb +20 -0
  60. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  61. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  62. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  63. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  64. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  65. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  66. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  67. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  68. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  69. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  70. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  71. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  72. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  73. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  74. data/spec/support/ext_verifier.rb +42 -0
  75. data/spec/support/mongoid_versions.rb +22 -0
  76. data/spec/support/rails_versions.rb +35 -0
  77. metadata +79 -4
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ require 'logger'
5
+ require 'awesome_print/core_ext/logger'
6
+
7
+ RSpec.describe 'AwesomePrint logging extensions' do
8
+ before(:all) do
9
+ @logger = Logger.new('/dev/null') rescue Logger.new('nul')
10
+ end
11
+
12
+ describe 'ap method' do
13
+ it 'should awesome_inspect the given object' do
14
+ object = double
15
+ expect(object).to receive(:ai)
16
+ @logger.ap object
17
+ end
18
+
19
+ describe 'the log level' do
20
+ before do
21
+ AwesomePrint.defaults = {}
22
+ end
23
+
24
+ it 'should fallback to the default :debug log level' do
25
+ expect(@logger).to receive(:debug)
26
+ @logger.ap(nil)
27
+ end
28
+
29
+ it 'should use the global user default if no level passed' do
30
+ AwesomePrint.defaults = { log_level: :info }
31
+ expect(@logger).to receive(:info)
32
+ @logger.ap(nil)
33
+ end
34
+
35
+ it 'should use the passed in level' do
36
+ expect(@logger).to receive(:warn)
37
+ @logger.ap(nil, :warn)
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'String extensions' do
4
+ [:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white].each_with_index do |color, i|
5
+ it "should have #{color} color" do
6
+ expect(color.to_s.send(color)).to eq("\e[1;#{30 + i}m#{color}\e[0m")
7
+ end
8
+
9
+ it "should have #{color}ish color" do
10
+ expect(color.to_s.send(:"#{color}ish")).to eq("\e[0;#{30 + i}m#{color}\e[0m")
11
+ end
12
+ end
13
+
14
+ it 'should have black and pale colors' do
15
+ expect('black'.send(:black)).to eq('black'.send(:grayish))
16
+ expect('pale'.send(:pale)).to eq('pale'.send(:whiteish))
17
+ expect('pale'.send(:pale)).to eq("\e[0;37mpale\e[0m")
18
+ expect('whiteish'.send(:whiteish)).to eq("\e[0;37mwhiteish\e[0m")
19
+ end
20
+ end
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,252 @@
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
+ if RUBY_VERSION < '1.9'
25
+ str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
26
+ else
27
+ str.sub!('?', '1992-10-10 12:30:00 UTC')
28
+ end
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
+ if RUBY_VERSION < '1.9'
53
+ str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
54
+ str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
55
+ else
56
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
57
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
58
+ end
59
+ expect(out).to be_similar_to(str)
60
+ end
61
+
62
+ it 'display multiple records on a relation' do
63
+ @diana.save
64
+ @laura.save
65
+ out = @ap.awesome(User.all)
66
+ str = <<-EOS.strip
67
+ [
68
+ [0] #<User:placeholder_id> {
69
+ :admin => false,
70
+ :created_at => ??,
71
+ :id => 1,
72
+ :name => "Diana",
73
+ :rank => 1
74
+ },
75
+ [1] #<User:placeholder_id> {
76
+ :admin => true,
77
+ :created_at => ?!,
78
+ :id => 2,
79
+ :name => "Laura",
80
+ :rank => 2
81
+ }
82
+ ]
83
+ EOS
84
+ if RUBY_VERSION < '1.9'
85
+ str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
86
+ str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
87
+ else
88
+ str.sub!('??', '1992-10-10 12:30:00 UTC')
89
+ str.sub!('?!', '2003-05-26 14:15:00 UTC')
90
+ end
91
+ expect(out).to be_similar_to(str)
92
+ end
93
+ end
94
+
95
+ describe 'Linked records (joins)' do
96
+ before do
97
+ @ap = AwesomePrint::Inspector.new(plain: true)
98
+ end
99
+
100
+ it 'should show the entire record' do
101
+ e = Email.create(email_address: 'foo@bar.com')
102
+ u = User.last
103
+ u.emails << e
104
+ email_record = User.joins(:emails).select('users.id, emails.email_address').last
105
+ out = @ap.awesome(email_record)
106
+ raw_object_string = <<-EOS.strip
107
+ #<User:placeholder_id> {
108
+ "id" => #{u.id},
109
+ "email_address" => "#{e.email_address}"
110
+ }
111
+ EOS
112
+ expect(out).to be_similar_to(raw_object_string)
113
+ end
114
+ end
115
+
116
+ #------------------------------------------------------------------------------
117
+ describe 'ActiveRecord instance (raw)' do
118
+ before do
119
+ ActiveRecord::Base.default_timezone = :utc
120
+ @diana = User.new(name: 'Diana', rank: 1, admin: false, created_at: '1992-10-10 12:30:00')
121
+ @laura = User.new(name: 'Laura', rank: 2, admin: true, created_at: '2003-05-26 14:15:00')
122
+ @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true)
123
+ end
124
+
125
+ it 'display single record' do
126
+ out = @ap.awesome(@diana)
127
+
128
+ raw_object_string =
129
+ if activerecord_5_0?
130
+ ActiveRecordData.raw_5_0_diana
131
+ elsif activerecord_4_2?
132
+ if RUBY_VERSION > '1.9.3'
133
+ ActiveRecordData.raw_4_2_diana
134
+ else
135
+ ActiveRecordData.raw_4_2_diana_legacy
136
+ end
137
+ elsif activerecord_4_1?
138
+ ActiveRecordData.raw_4_1_diana
139
+ elsif activerecord_4_0?
140
+ ActiveRecordData.raw_4_0_diana
141
+ elsif activerecord_3_2?
142
+ if RUBY_VERSION > '1.9.3'
143
+ ActiveRecordData.raw_3_2_diana
144
+ else
145
+ ActiveRecordData.raw_3_2_diana_legacy
146
+ end
147
+ end
148
+ raw_object_string.sub!('?', '1992-10-10 12:30:00')
149
+ expect(out).to be_similar_to(raw_object_string)
150
+ end
151
+
152
+ it 'display multiple records' do
153
+ out = @ap.awesome([@diana, @laura])
154
+
155
+ raw_object_string =
156
+ if activerecord_5_0?
157
+ ActiveRecordData.raw_5_0_multi
158
+ elsif activerecord_4_2?
159
+ if RUBY_VERSION > '1.9.3'
160
+ ActiveRecordData.raw_4_2_multi
161
+ else
162
+ ActiveRecordData.raw_4_2_multi_legacy
163
+ end
164
+ elsif activerecord_4_1?
165
+ ActiveRecordData.raw_4_1_multi
166
+ elsif activerecord_4_0?
167
+ ActiveRecordData.raw_4_0_multi
168
+ elsif activerecord_3_2?
169
+ if RUBY_VERSION > '1.9.3'
170
+ ActiveRecordData.raw_3_2_multi
171
+ else
172
+ ActiveRecordData.raw_3_2_multi_legacy
173
+ end
174
+ end
175
+ raw_object_string.sub!('?', '1992-10-10 12:30:00')
176
+ raw_object_string.sub!('?', '2003-05-26 14:15:00')
177
+ expect(out).to be_similar_to(raw_object_string)
178
+ end
179
+ end
180
+
181
+ #------------------------------------------------------------------------------
182
+ describe 'ActiveRecord class' do
183
+ before do
184
+ @ap = AwesomePrint::Inspector.new(plain: true)
185
+ end
186
+
187
+ it 'should print the class' do
188
+ expect(@ap.awesome(User)).to eq <<-EOS.strip
189
+ class User < ActiveRecord::Base {
190
+ :id => :integer,
191
+ :name => :string,
192
+ :rank => :integer,
193
+ :admin => :boolean,
194
+ :created_at => :datetime
195
+ }
196
+ EOS
197
+ end
198
+
199
+ it 'should print the class for non-direct subclasses of ActiveRecord::Base' do
200
+ out = @ap.awesome(SubUser)
201
+ expect(out).to eq <<-EOS.strip
202
+ class SubUser < User {
203
+ :id => :integer,
204
+ :name => :string,
205
+ :rank => :integer,
206
+ :admin => :boolean,
207
+ :created_at => :datetime
208
+ }
209
+ EOS
210
+ end
211
+
212
+ it 'should print ActiveRecord::Base objects (ex. ancestors)' do
213
+ expect { @ap.awesome(User.ancestors) }.not_to raise_error
214
+ end
215
+ end
216
+
217
+ #------------------------------------------------------------------------------
218
+ describe 'ActiveRecord methods formatting' do
219
+ before do
220
+ @ap = AwesomePrint::Inspector.new(plain: true)
221
+ end
222
+
223
+ it 'should format class methods properly' do
224
+ # spec 1
225
+ out = @ap.awesome(User.methods.grep(/first/))
226
+
227
+ if ActiveRecord::VERSION::STRING >= '3.2'
228
+ if RUBY_VERSION >= '1.9'
229
+ expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
230
+ else
231
+ expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
232
+ end
233
+ else
234
+ expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
235
+ end
236
+
237
+ # spec 2
238
+ out = @ap.awesome(User.methods.grep(/primary_key/))
239
+ expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
240
+
241
+ # spec 3
242
+ out = @ap.awesome(User.methods.grep(/validate/))
243
+
244
+ if ActiveRecord::VERSION::MAJOR < 3
245
+ expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
246
+ else
247
+ expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/)
248
+ end
249
+
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,26 @@
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
+ expect(@ap.send(:awesome, time)).to eq("\e[0;32mSat, 10 Feb 2007 15:30:45 EST -05:00\e[0m")
12
+ end
13
+
14
+ it 'should format HashWithIndifferentAccess as regular Hash' do
15
+ hash = HashWithIndifferentAccess.new({ hello: 'world' })
16
+ expect(@ap.send(:awesome, hash)).to eq("{\n \"hello\"\e[0;37m => \e[0m\e[0;33m\"world\"\e[0m\n}")
17
+ end
18
+
19
+ # ActiveSupport sticks in instance variables to the date object. Make sure
20
+ # we ignore that and format Date instance as regular date.
21
+ it 'should formate Date object as date' do
22
+ date = Date.new(2003, 5, 26)
23
+ expect(date.ai(plain: true)).to eq('Mon, 26 May 2003')
24
+ expect(date.ai).to eq("\e[0;32mMon, 26 May 2003\e[0m")
25
+ end
26
+ 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