amazing_print 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +35 -0
  3. data/Appraisals +60 -0
  4. data/CHANGELOG.md +2 -0
  5. data/CONTRIBUTING.md +81 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +21 -0
  8. data/README.md +356 -0
  9. data/Rakefile +23 -0
  10. data/lib/amazing_print.rb +46 -0
  11. data/lib/amazing_print/colorize.rb +25 -0
  12. data/lib/amazing_print/core_ext/awesome_method_array.rb +82 -0
  13. data/lib/amazing_print/core_ext/class.rb +23 -0
  14. data/lib/amazing_print/core_ext/kernel.rb +25 -0
  15. data/lib/amazing_print/core_ext/logger.rb +21 -0
  16. data/lib/amazing_print/core_ext/method.rb +21 -0
  17. data/lib/amazing_print/core_ext/object.rb +23 -0
  18. data/lib/amazing_print/core_ext/string.rb +42 -0
  19. data/lib/amazing_print/custom_defaults.rb +57 -0
  20. data/lib/amazing_print/ext/action_view.rb +22 -0
  21. data/lib/amazing_print/ext/active_record.rb +103 -0
  22. data/lib/amazing_print/ext/active_support.rb +45 -0
  23. data/lib/amazing_print/ext/mongo_mapper.rb +125 -0
  24. data/lib/amazing_print/ext/mongoid.rb +68 -0
  25. data/lib/amazing_print/ext/nobrainer.rb +53 -0
  26. data/lib/amazing_print/ext/nokogiri.rb +45 -0
  27. data/lib/amazing_print/ext/ostruct.rb +27 -0
  28. data/lib/amazing_print/ext/ripple.rb +71 -0
  29. data/lib/amazing_print/ext/sequel.rb +55 -0
  30. data/lib/amazing_print/formatter.rb +120 -0
  31. data/lib/amazing_print/formatters.rb +14 -0
  32. data/lib/amazing_print/formatters/array_formatter.rb +139 -0
  33. data/lib/amazing_print/formatters/base_formatter.rb +148 -0
  34. data/lib/amazing_print/formatters/class_formatter.rb +24 -0
  35. data/lib/amazing_print/formatters/dir_formatter.rb +21 -0
  36. data/lib/amazing_print/formatters/file_formatter.rb +21 -0
  37. data/lib/amazing_print/formatters/hash_formatter.rb +106 -0
  38. data/lib/amazing_print/formatters/method_formatter.rb +21 -0
  39. data/lib/amazing_print/formatters/object_formatter.rb +82 -0
  40. data/lib/amazing_print/formatters/simple_formatter.rb +20 -0
  41. data/lib/amazing_print/formatters/struct_formatter.rb +74 -0
  42. data/lib/amazing_print/indentator.rb +17 -0
  43. data/lib/amazing_print/inspector.rb +175 -0
  44. data/lib/amazing_print/version.rb +10 -0
  45. data/lib/ap.rb +10 -0
  46. data/spec/active_record_helper.rb +30 -0
  47. data/spec/colors_spec.rb +114 -0
  48. data/spec/core_ext/logger_spec.rb +44 -0
  49. data/spec/core_ext/string_spec.rb +20 -0
  50. data/spec/ext/action_view_spec.rb +17 -0
  51. data/spec/ext/active_record_spec.rb +297 -0
  52. data/spec/ext/active_support_spec.rb +26 -0
  53. data/spec/ext/mongo_mapper_spec.rb +259 -0
  54. data/spec/ext/mongoid_spec.rb +66 -0
  55. data/spec/ext/nobrainer_spec.rb +58 -0
  56. data/spec/ext/nokogiri_spec.rb +50 -0
  57. data/spec/ext/ostruct_spec.rb +22 -0
  58. data/spec/ext/ripple_spec.rb +47 -0
  59. data/spec/formats_spec.rb +779 -0
  60. data/spec/methods_spec.rb +478 -0
  61. data/spec/misc_spec.rb +245 -0
  62. data/spec/objects_spec.rb +219 -0
  63. data/spec/spec_helper.rb +106 -0
  64. data/spec/support/active_record_data.rb +20 -0
  65. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  66. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  67. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  68. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  69. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  70. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  71. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  72. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  73. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  74. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  75. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  76. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  77. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  78. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  79. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  80. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  81. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  82. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  83. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  84. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  85. data/spec/support/ext_verifier.rb +41 -0
  86. data/spec/support/mongoid_versions.rb +22 -0
  87. data/spec/support/rails_versions.rb +50 -0
  88. metadata +243 -0
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AmazingPrint::ActiveSupport', skip: -> { !ExtVerifier.has_rails? }.call do
4
+ before do
5
+ @ap = AmazingPrint::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,259 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AmazingPrint/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 = AmazingPrint::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 = AmazingPrint::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:.+?\>/, 'amazing_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 = amazing_print_PROC_STUB,
47
+ attr_accessor :ivar = :@_id,
48
+ attr_accessor :name = "_id",
49
+ attr_accessor :options = {
50
+ :default => amazing_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 = amazing_print_PROC_STUB,
64
+ attr_accessor :ivar = :@_id,
65
+ attr_accessor :name = "_id",
66
+ attr_accessor :options = {
67
+ :default => amazing_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
+ if ExtVerifier.has_mongo_mapper?
157
+ before :all do
158
+ class Child
159
+ include MongoMapper::EmbeddedDocument
160
+ key :data
161
+ end
162
+
163
+ class Sibling
164
+ include MongoMapper::Document
165
+ key :title
166
+ end
167
+
168
+ class Parent
169
+ include MongoMapper::Document
170
+ key :name
171
+
172
+ one :child
173
+ one :sibling
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'with show associations turned off (default)' do
179
+ it 'should render the class as normal' do
180
+ expect(@ap.send(:awesome, Parent)).to eq <<~EOS.strip
181
+ class Parent < Object {
182
+ "_id" => :object_id,
183
+ "name" => :undefined
184
+ }
185
+ EOS
186
+ end
187
+
188
+ it 'should render an instance as normal' do
189
+ parent = Parent.new(name: 'test')
190
+ out = @ap.send(:awesome, parent)
191
+ str = <<~EOS.strip
192
+ #<Parent:placeholder_id> {
193
+ "_id" => placeholder_bson_id,
194
+ "name" => "test"
195
+ }
196
+ EOS
197
+ expect(out).to be_similar_to(str)
198
+ end
199
+ end
200
+
201
+ describe 'with show associations turned on and inline embedded turned off' do
202
+ before :each do
203
+ @ap = AmazingPrint::Inspector.new(plain: true, mongo_mapper: { show_associations: true })
204
+ end
205
+
206
+ it 'should render the class with associations shown' do
207
+ expect(@ap.send(:awesome, Parent)).to eq <<~EOS.strip
208
+ class Parent < Object {
209
+ "_id" => :object_id,
210
+ "name" => :undefined,
211
+ "child" => embeds one Child,
212
+ "sibling" => one Sibling
213
+ }
214
+ EOS
215
+ end
216
+
217
+ it 'should render an instance with associations shown' do
218
+ parent = Parent.new(name: 'test')
219
+ out = @ap.send(:awesome, parent)
220
+ str = <<~EOS.strip
221
+ #<Parent:placeholder_id> {
222
+ "_id" => placeholder_bson_id,
223
+ "name" => "test",
224
+ "child" => embeds one Child,
225
+ "sibling" => one Sibling
226
+ }
227
+ EOS
228
+ expect(out).to be_similar_to(str)
229
+ end
230
+ end
231
+
232
+ describe 'with show associations turned on and inline embedded turned on' do
233
+ before :each do
234
+ @ap = AmazingPrint::Inspector.new plain: true,
235
+ mongo_mapper: {
236
+ show_associations: true,
237
+ inline_embedded: true
238
+ }
239
+ end
240
+
241
+ it 'should render an instance with associations shown and embeds there' do
242
+ parent = Parent.new(name: 'test', child: Child.new(data: 5))
243
+ out = @ap.send(:awesome, parent)
244
+ str = <<~EOS.strip
245
+ #<Parent:placeholder_id> {
246
+ "_id" => placeholder_bson_id,
247
+ "name" => "test",
248
+ "child" => embedded #<Child:placeholder_id> {
249
+ "_id" => placeholder_bson_id,
250
+ "data" => 5
251
+ },
252
+ "sibling" => one Sibling
253
+ }
254
+ EOS
255
+ expect(out).to be_similar_to(str)
256
+ end
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AmazingPrint/Mongoid', skip: -> { !ExtVerifier.has_mongoid? }.call do
4
+ if ExtVerifier.has_mongoid?
5
+ before :all do
6
+ class MongoUser
7
+ include Mongoid::Document
8
+
9
+ field :first_name, type: String
10
+ field :last_name, type: 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 = AmazingPrint::Inspector.new plain: true, sort_keys: true
22
+ end
23
+
24
+ it 'should print class instance' do
25
+ user = MongoUser.new first_name: 'Al', last_name: 'Capone'
26
+ out = @ap.send :awesome, user
27
+
28
+ object_id = user.id.inspect
29
+ str = <<~EOS.strip
30
+ #<MongoUser:placeholder_id> {
31
+ :_id => #{object_id},
32
+ :first_name => "Al",
33
+ :last_name => "Capone"
34
+ }
35
+ EOS
36
+ expect(out).to be_similar_to(str, { skip_bson: true })
37
+ end
38
+
39
+ it 'should print the class' do
40
+ class_spec = <<~EOS.strip
41
+ class MongoUser < Object {
42
+ :_id => :"bson/object_id",
43
+ :first_name => :string,
44
+ :last_name => :string
45
+ }
46
+ EOS
47
+
48
+ expect(@ap.send(:awesome, MongoUser)).to eq class_spec
49
+ end
50
+
51
+ it 'should print the class when type is undefined' do
52
+ class Chamelion
53
+ include Mongoid::Document
54
+ field :last_attribute
55
+ end
56
+
57
+ class_spec = <<~EOS.strip
58
+ class Chamelion < Object {
59
+ :_id => :"bson/object_id",
60
+ :last_attribute => :object
61
+ }
62
+ EOS
63
+
64
+ expect(@ap.send(:awesome, Chamelion)).to eq class_spec
65
+ end
66
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AmazingPrint/NoBrainer', skip: -> { !ExtVerifier.has_nobrainer? }.call do
4
+ if ExtVerifier.has_nobrainer?
5
+ before :all do
6
+ NoBrainer.configure do |config|
7
+ config.app_name = 'ap_test'
8
+ config.environment = :test
9
+ end
10
+ end
11
+
12
+ before :all do
13
+ class SomeModel
14
+ include NoBrainer::Document
15
+
16
+ field :first_name, type: String
17
+ field :last_name, type: String
18
+ field :some_field
19
+ end
20
+ end
21
+
22
+ after :all do
23
+ Object.instance_eval { remove_const :SomeModel }
24
+ end
25
+ end
26
+
27
+ before do
28
+ @ap = AmazingPrint::Inspector.new plain: true
29
+ end
30
+
31
+ it 'should print class instance' do
32
+ user = SomeModel.new first_name: 'Al', last_name: 'Capone'
33
+ out = @ap.send :awesome, user
34
+
35
+ object_id = user.id.inspect
36
+ str = <<~EOS.strip
37
+ #<SomeModel id: #{object_id}> {
38
+ :id => #{object_id},
39
+ :first_name => "Al",
40
+ :last_name => "Capone"
41
+ }
42
+ EOS
43
+ expect(out).to eq(str)
44
+ end
45
+
46
+ it 'should print the class' do
47
+ class_spec = <<~EOS.strip
48
+ class SomeModel < Object {
49
+ :id => :string,
50
+ :first_name => :string,
51
+ :last_name => :string,
52
+ :some_field => :object
53
+ }
54
+ EOS
55
+
56
+ expect(@ap.send(:awesome, SomeModel)).to eq class_spec
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'AmazingPrint/Nokogiri' do
4
+ it 'should colorize tags' do
5
+ xml = Nokogiri::XML('<html><body><h1></h1></body></html>')
6
+ # FIXME: Due to something strange with Nokogiri and JRuby, we need to remove extra blank lines.
7
+ output = xml.ai.gsub(/\n\n/, "\n")
8
+ expect(output).to eq <<~EOS
9
+ <?xml version=\"1.0\"?>\e[1;32m
10
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
11
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
12
+ \e[0m<\e[1;36mh1\e[0m/>\e[1;32m
13
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
14
+ \e[0m<\e[1;36m/html\e[0m>
15
+ EOS
16
+ end
17
+
18
+ it 'should colorize contents' do
19
+ xml = Nokogiri::XML('<html><body><h1>Hello</h1></body></html>')
20
+ expect(xml.ai).to eq <<~EOS
21
+ <?xml version=\"1.0\"?>\e[1;32m
22
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
23
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
24
+ \e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
25
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
26
+ \e[0m<\e[1;36m/html\e[0m>
27
+ EOS
28
+ end
29
+
30
+ it 'should colorize class and id' do
31
+ xml = Nokogiri::XML('<html><body><h1><span class="world" id="hello"></span></h1></body></html>')
32
+ # FIXME: Due to something strange with Nokogiri and JRuby, we need to remove extra blank lines.
33
+ output = xml.ai.gsub(/\n\n/, "\n")
34
+ expect(output).to eq <<~EOS
35
+ <?xml version=\"1.0\"?>\e[1;32m
36
+ \e[0m<\e[1;36mhtml\e[0m>\e[1;32m
37
+ \e[0m<\e[1;36mbody\e[0m>\e[1;32m
38
+ \e[0m<\e[1;36mh1\e[0m>\e[1;32m
39
+ \e[0m<\e[1;36mspan\e[0m \e[1;33mclass=\"world\"\e[0m \e[1;33mid=\"hello\"\e[0m/>\e[1;32m
40
+ \e[0m<\e[1;36m/h1\e[0m>\e[1;32m
41
+ \e[0m<\e[1;36m/body\e[0m>\e[1;32m
42
+ \e[0m<\e[1;36m/html\e[0m>
43
+ EOS
44
+ end
45
+
46
+ it 'handle empty NodeSet' do
47
+ xml = Nokogiri::XML::NodeSet.new(Nokogiri::XML(''))
48
+ expect(xml.ai).to eq('[]')
49
+ end
50
+ end