awesome_print 1.7.0 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/Appraisals +44 -34
  4. data/CHANGELOG.md +28 -0
  5. data/CONTRIBUTING.md +2 -1
  6. data/Gemfile.lock +25 -33
  7. data/LICENSE +16 -19
  8. data/README.md +88 -44
  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 +1 -1
  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 +8 -5
  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 +28 -28
  33. data/lib/awesome_print/formatters/array_formatter.rb +108 -42
  34. data/lib/awesome_print/formatters/base_formatter.rb +16 -14
  35. data/lib/awesome_print/formatters/class_formatter.rb +2 -1
  36. data/lib/awesome_print/formatters/dir_formatter.rb +1 -1
  37. data/lib/awesome_print/formatters/file_formatter.rb +1 -1
  38. data/lib/awesome_print/formatters/hash_formatter.rb +74 -22
  39. data/lib/awesome_print/formatters/object_formatter.rb +13 -15
  40. data/lib/awesome_print/formatters/struct_formatter.rb +71 -0
  41. data/lib/awesome_print/formatters.rb +15 -0
  42. data/lib/awesome_print/inspector.rb +83 -96
  43. data/lib/awesome_print/version.rb +2 -2
  44. data/lib/awesome_print.rb +18 -17
  45. data/spec/active_record_helper.rb +8 -2
  46. data/spec/colors_spec.rb +30 -30
  47. data/spec/core_ext/logger_spec.rb +43 -0
  48. data/spec/core_ext/string_spec.rb +20 -0
  49. data/spec/ext/action_view_spec.rb +21 -0
  50. data/spec/ext/active_record_spec.rb +260 -0
  51. data/spec/ext/active_support_spec.rb +30 -0
  52. data/spec/ext/mongo_mapper_spec.rb +261 -0
  53. data/spec/ext/mongoid_spec.rb +67 -0
  54. data/spec/ext/nobrainer_spec.rb +59 -0
  55. data/spec/ext/nokogiri_spec.rb +46 -0
  56. data/spec/ext/ostruct_spec.rb +22 -0
  57. data/spec/ext/ripple_spec.rb +48 -0
  58. data/spec/formats_spec.rb +193 -165
  59. data/spec/methods_spec.rb +125 -129
  60. data/spec/misc_spec.rb +95 -92
  61. data/spec/objects_spec.rb +119 -28
  62. data/spec/spec_helper.rb +29 -10
  63. data/spec/support/active_record_data/3_2_diana.txt +24 -0
  64. data/spec/support/active_record_data/3_2_diana_legacy.txt +24 -0
  65. data/spec/support/active_record_data/3_2_multi.txt +50 -0
  66. data/spec/support/active_record_data/3_2_multi_legacy.txt +50 -0
  67. data/spec/support/active_record_data/4_0_diana.txt +98 -0
  68. data/spec/support/active_record_data/4_0_multi.txt +198 -0
  69. data/spec/support/active_record_data/4_1_diana.txt +97 -0
  70. data/spec/support/active_record_data/4_1_multi.txt +196 -0
  71. data/spec/support/active_record_data/4_2_diana.txt +109 -0
  72. data/spec/support/active_record_data/4_2_diana_legacy.txt +109 -0
  73. data/spec/support/active_record_data/4_2_multi.txt +220 -0
  74. data/spec/support/active_record_data/4_2_multi_legacy.txt +220 -0
  75. data/spec/support/active_record_data/5_0_diana.txt +105 -0
  76. data/spec/support/active_record_data/5_0_multi.txt +212 -0
  77. data/spec/support/active_record_data/5_1_diana.txt +104 -0
  78. data/spec/support/active_record_data/5_1_multi.txt +210 -0
  79. data/spec/support/active_record_data/5_2_diana.txt +104 -0
  80. data/spec/support/active_record_data/5_2_multi.txt +210 -0
  81. data/spec/support/active_record_data/6_0_diana.txt +104 -0
  82. data/spec/support/active_record_data/6_0_multi.txt +210 -0
  83. data/spec/support/active_record_data/6_1_diana.txt +109 -0
  84. data/spec/support/active_record_data/6_1_multi.txt +220 -0
  85. data/spec/support/active_record_data.rb +20 -0
  86. data/spec/support/ext_verifier.rb +42 -0
  87. data/spec/support/mongoid_versions.rb +26 -0
  88. data/spec/support/rails_versions.rb +55 -0
  89. metadata +90 -26
data/spec/misc_spec.rb CHANGED
@@ -1,47 +1,44 @@
1
+ require 'net/http'
1
2
  require 'spec_helper'
2
3
 
3
- RSpec.describe "AwesomePrint" do
4
+ RSpec.describe 'AwesomePrint' do
4
5
 
5
- describe "Misc" do
6
- before do
7
- stub_dotfile!
8
- end
9
-
10
- it "handle weird objects that return nil on inspect" do
6
+ describe 'Misc' do
7
+ it 'handle weird objects that return nil on inspect' do
11
8
  weird = Class.new do
12
9
  def inspect
13
10
  nil
14
11
  end
15
12
  end
16
- expect(weird.new.ai(:plain => true)).to eq('')
13
+ expect(weird.new.ai(plain: true)).to eq('')
17
14
  end
18
15
 
19
- it "handle frozen object.inspect" do
16
+ it 'handle frozen object.inspect' do
20
17
  weird = Class.new do
21
18
  def inspect
22
- "ice".freeze
19
+ 'ice'.freeze
23
20
  end
24
21
  end
25
- expect(weird.new.ai(:plain => false)).to eq("ice")
22
+ expect(weird.new.ai(plain: false)).to eq('ice')
26
23
  end
27
24
 
28
- # See https://github.com/michaeldv/awesome_print/issues/35
29
- it "handle array grep when pattern contains / chapacter" do
30
- hash = { "1/x" => 1, "2//x" => :"2" }
25
+ # See https://github.com/awesome-print/awesome_print/issues/35
26
+ it 'handle array grep when pattern contains / chapacter' do
27
+ hash = { '1/x' => 1, '2//x' => :"2" }
31
28
  grepped = hash.keys.sort.grep(/^(\d+)\//) { $1 }
32
- expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ "1", "2" ]')
29
+ expect(grepped.ai(plain: true, multiline: false)).to eq('[ "1", "2" ]')
33
30
  end
34
31
 
35
- # See https://github.com/michaeldv/awesome_print/issues/85
36
- if RUBY_VERSION >= "1.8.7"
32
+ # See https://github.com/awesome-print/awesome_print/issues/85
33
+ if RUBY_VERSION >= '1.8.7'
37
34
  it "handle array grep when a method is defined in C and thus doesn't have a binding" do
38
35
  arr = (0..6).to_a
39
36
  grepped = arr.grep(1..4, &:succ)
40
- expect(grepped.ai(:plain => true, :multiline => false)).to eq('[ 2, 3, 4, 5 ]')
37
+ expect(grepped.ai(plain: true, multiline: false)).to eq('[ 2, 3, 4, 5 ]')
41
38
  end
42
39
  end
43
40
 
44
- it "returns value passed as a parameter" do
41
+ it 'returns value passed as a parameter' do
45
42
  object = rand
46
43
  allow(self).to receive(:puts)
47
44
  expect(ap object).to eq(object)
@@ -53,20 +50,20 @@ RSpec.describe "AwesomePrint" do
53
50
  expect { rand.ai }.not_to raise_error
54
51
  end
55
52
 
56
- it "format ENV as hash" do
57
- expect(ENV.ai(:plain => true)).to eq(ENV.to_hash.ai(:plain => true))
53
+ it 'format ENV as hash' do
54
+ expect(ENV.ai(plain: true)).to eq(ENV.to_hash.ai(plain: true))
58
55
  expect(ENV.ai).to eq(ENV.to_hash.ai)
59
56
  end
60
57
 
61
- # See https://github.com/michaeldv/awesome_print/issues/134
62
- it "IPAddr workaround" do
63
- require "ipaddr"
64
- ipaddr = IPAddr.new("3ffe:505:2::1")
65
- expect(ipaddr.ai).to eq("#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>")
58
+ # See https://github.com/awesome-print/awesome_print/issues/134
59
+ it 'IPAddr workaround' do
60
+ require 'ipaddr'
61
+ ipaddr = IPAddr.new('3ffe:505:2::1')
62
+ expect(ipaddr.ai).to eq('#<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>')
66
63
  end
67
64
 
68
- # See https://github.com/michaeldv/awesome_print/issues/139
69
- it "Object that overrides == and expects the :id method" do
65
+ # See https://github.com/awesome-print/awesome_print/issues/139
66
+ it 'Object that overrides == and expects the :id method' do
70
67
  weird = Class.new do
71
68
  # Raises NoMethodError: undefined method `id' when "other" is nil or ENV.
72
69
  def ==(other)
@@ -76,90 +73,84 @@ RSpec.describe "AwesomePrint" do
76
73
  end
77
74
  expect { weird.new.ai }.not_to raise_error
78
75
  end
79
- end
80
76
 
81
- #------------------------------------------------------------------------------
82
- describe "HTML output" do
83
- before do
84
- stub_dotfile!
77
+ it 'handles attr_reader :method' do
78
+ uri = URI.parse('https://hello.nx/world')
79
+ req = Net::HTTP::Get.new(uri)
80
+ expect(req.ai(plain: true)).to eq('#<Net::HTTP::Get GET>')
85
81
  end
82
+ end
86
83
 
87
- it "wraps ap output with plain <pre> tag" do
84
+ #------------------------------------------------------------------------------
85
+ describe 'HTML output' do
86
+ it 'wraps ap output with plain <pre> tag' do
88
87
  markup = rand
89
- expect(markup.ai(:html => true, :plain => true)).to eq("<pre>#{markup}</pre>")
88
+ expect(markup.ai(html: true, plain: true)).to eq("<pre>#{markup}</pre>")
90
89
  end
91
90
 
92
- it "wraps ap output with <pre> tag with colorized <kbd>" do
91
+ it 'wraps ap output with <pre> tag with colorized <kbd>' do
93
92
  markup = rand
94
- expect(markup.ai(:html => true)).to eq(%Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|)
93
+ expect(markup.ai(html: true)).to eq(%Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|)
95
94
  end
96
95
 
97
- it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
98
- markup = [ 1, :two, "three" ]
99
- expect(markup.ai(:html => true)).to eq <<-EOS.strip
96
+ it 'wraps multiline ap output with <pre> tag with colorized <kbd>' do
97
+ markup = [1, :two, 'three']
98
+ expect(markup.ai(html: true)).to eq <<-EOS.strip
100
99
  <pre>[
101
100
  <kbd style="color:white">[0] </kbd><kbd style="color:blue">1</kbd>,
102
101
  <kbd style="color:white">[1] </kbd><kbd style="color:darkcyan">:two</kbd>,
103
102
  <kbd style="color:white">[2] </kbd><kbd style="color:brown">&quot;three&quot;</kbd>
104
103
  ]</pre>
105
- EOS
104
+ EOS
106
105
  end
107
106
 
108
- it "wraps hash ap output with only an outer <pre> tag" do
109
- markup = [ { "hello" => "world" } ]
110
- expect(markup.ai(:html => true)).to eq <<-EOS.strip
107
+ it 'wraps hash ap output with only an outer <pre> tag' do
108
+ markup = [{ 'hello' => 'world' }]
109
+ expect(markup.ai(html: true)).to eq <<-EOS.strip
111
110
  <pre>[
112
111
  <kbd style="color:white">[0] </kbd>{
113
112
  &quot;hello&quot;<kbd style="color:slategray"> =&gt; </kbd><kbd style="color:brown">&quot;world&quot;</kbd>
114
113
  }
115
114
  ]</pre>
116
- EOS
115
+ EOS
117
116
  end
118
117
 
119
- it "encodes HTML entities (plain)" do
118
+ it 'encodes HTML entities (plain)' do
120
119
  markup = ' &<hello>'
121
- expect(markup.ai(:html => true, :plain => true)).to eq('<pre>&quot; &amp;&lt;hello&gt;&quot;</pre>')
120
+ expect(markup.ai(html: true, plain: true)).to eq('<pre>&quot; &amp;&lt;hello&gt;&quot;</pre>')
122
121
  end
123
122
 
124
- it "encodes HTML entities (color)" do
123
+ it 'encodes HTML entities (color)' do
125
124
  markup = ' &<hello>'
126
- expect(markup.ai(:html => true)).to eq('<pre><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
125
+ expect(markup.ai(html: true)).to eq('<pre><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
127
126
  end
128
127
  end
129
128
 
130
129
  #------------------------------------------------------------------------------
131
- describe "AwesomePrint.defaults" do
132
- before do
133
- stub_dotfile!
134
- end
135
-
130
+ describe 'AwesomePrint.defaults' do
136
131
  after do
137
132
  AwesomePrint.defaults = nil
138
133
  end
139
134
 
140
- # See https://github.com/michaeldv/awesome_print/issues/98
141
- it "should properly merge the defaults" do
142
- AwesomePrint.defaults = { :indent => -2, :sort_keys => true }
143
- hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
144
- out = hash.ai(:plain => true)
135
+ # See https://github.com/awesome-print/awesome_print/issues/98
136
+ it 'should properly merge the defaults' do
137
+ AwesomePrint.defaults = { indent: -2, sort_keys: true }
138
+ hash = { [0, 0, 255] => :yellow, :red => 'rgb(255, 0, 0)', 'magenta' => 'rgb(255, 0, 255)' }
139
+ out = hash.ai(plain: true)
145
140
  expect(out).to eq <<-EOS.strip
146
141
  {
147
142
  [ 0, 0, 255 ] => :yellow,
148
143
  "magenta" => "rgb(255, 0, 255)",
149
144
  :red => "rgb(255, 0, 0)"
150
145
  }
151
- EOS
146
+ EOS
152
147
  end
153
148
  end
154
149
 
155
150
  #------------------------------------------------------------------------------
156
- describe "Coexistence with the colorize gem" do
157
- before do
158
- stub_dotfile!
159
- end
160
-
151
+ describe 'Coexistence with the colorize gem' do
161
152
  before do # Redefine String#red just like colorize gem does it.
162
- @awesome_method = "".method(:red)
153
+ @awesome_method = ''.method(:red)
163
154
 
164
155
  String.instance_eval do
165
156
  define_method :red do # Method arity is now 0 in Ruby 1.9+.
@@ -175,76 +166,88 @@ EOS
175
166
  end
176
167
  end
177
168
 
178
- it "shoud not raise ArgumentError when formatting HTML" do
179
- out = "hello".ai(:color => { :string => :red }, :html => true)
180
- if RUBY_VERSION >= "1.9"
169
+ it 'shoud not raise ArgumentError when formatting HTML' do
170
+ out = 'hello'.ai(color: { string: :red }, html: true)
171
+ if RUBY_VERSION >= '1.9'
181
172
  expect(out).to eq(%Q|<pre>[red]<kbd style="color:red">&quot;hello&quot;</kbd>[/red]</pre>|)
182
173
  else
183
174
  expect(out).to eq(%Q|<pre>[red]&quot;hello&quot;[/red]</pre>|)
184
175
  end
185
176
  end
186
177
 
187
- it "shoud not raise ArgumentError when formatting HTML (shade color)" do
188
- out = "hello".ai(:color => { :string => :redish }, :html => true)
178
+ it 'shoud not raise ArgumentError when formatting HTML (shade color)' do
179
+ out = 'hello'.ai(color: { string: :redish }, html: true)
189
180
  expect(out).to eq(%Q|<pre><kbd style="color:darkred">&quot;hello&quot;</kbd></pre>|)
190
181
  end
191
182
 
192
- it "shoud not raise ArgumentError when formatting non-HTML" do
193
- out = "hello".ai(:color => { :string => :red }, :html => false)
183
+ it 'shoud not raise ArgumentError when formatting non-HTML' do
184
+ out = 'hello'.ai(color: { string: :red }, html: false)
194
185
  expect(out).to eq(%Q|[red]"hello"[/red]|)
195
186
  end
196
187
 
197
- it "shoud not raise ArgumentError when formatting non-HTML (shade color)" do
198
- out = "hello".ai(:color => { :string => :redish }, :html => false)
188
+ it 'shoud not raise ArgumentError when formatting non-HTML (shade color)' do
189
+ out = 'hello'.ai(color: { string: :redish }, html: false)
199
190
  expect(out).to eq(%Q|\e[0;31m"hello"\e[0m|)
200
191
  end
201
192
  end
202
193
 
203
194
  #------------------------------------------------------------------------------
204
- describe "Console" do
205
- it "should detect IRB" do
195
+ describe 'Console' do
196
+ it 'should detect IRB' do
206
197
  class IRB; end
207
198
  ENV.delete('RAILS_ENV')
208
199
  expect(AwesomePrint.console?).to eq(true)
209
200
  expect(AwesomePrint.rails_console?).to eq(false)
210
- Object.instance_eval{ remove_const :IRB }
201
+ Object.instance_eval { remove_const :IRB }
211
202
  end
212
203
 
213
- it "should detect Pry" do
204
+ it 'should detect Pry' do
214
205
  class Pry; end
215
206
  ENV.delete('RAILS_ENV')
216
207
  expect(AwesomePrint.console?).to eq(true)
217
208
  expect(AwesomePrint.rails_console?).to eq(false)
218
- Object.instance_eval{ remove_const :Pry }
209
+ Object.instance_eval { remove_const :Pry }
219
210
  end
220
211
 
221
- it "should detect Rails::Console" do
212
+ it 'should detect Rails::Console' do
222
213
  class IRB; end
223
214
  module Rails; class Console; end; end
224
215
  expect(AwesomePrint.console?).to eq(true)
225
216
  expect(AwesomePrint.rails_console?).to eq(true)
226
- Object.instance_eval{ remove_const :IRB }
227
- Object.instance_eval{ remove_const :Rails }
217
+ Object.instance_eval { remove_const :IRB }
218
+ Object.instance_eval { remove_const :Rails }
228
219
  end
229
220
 
230
221
  it "should detect ENV['RAILS_ENV']" do
231
222
  class Pry; end
232
- ENV["RAILS_ENV"] = "development"
223
+ ENV['RAILS_ENV'] = 'development'
233
224
  expect(AwesomePrint.console?).to eq(true)
234
225
  expect(AwesomePrint.rails_console?).to eq(true)
235
- Object.instance_eval{ remove_const :Pry }
226
+ Object.instance_eval { remove_const :Pry }
236
227
  end
237
228
 
238
- it "should return the actual object when *not* running under console" do
239
- expect(capture! { ap([ 1, 2, 3 ]) }).to eq([ 1, 2, 3 ])
240
- expect(capture! { ap({ :a => 1 }) }).to eq({ :a => 1 })
229
+ it 'should return the actual object when *not* running under console' do
230
+ expect(capture! { ap([1, 2, 3]) }).to eq([1, 2, 3])
231
+ expect(capture! { ap({ a: 1 }) }).to eq({ a: 1 })
241
232
  end
242
233
 
243
- it "should return nil when running under console" do
234
+ it 'should return nil when running under console' do
244
235
  class IRB; end
245
- expect(capture! { ap([ 1, 2, 3 ]) }).to eq(nil)
246
- expect(capture! { ap({ :a => 1 }) }).to eq(nil)
247
- Object.instance_eval{ remove_const :IRB }
236
+ expect(capture! { ap([1, 2, 3]) }).to eq(nil)
237
+ expect(capture! { ap({ a: 1 }) }).to eq(nil)
238
+ Object.instance_eval { remove_const :IRB }
239
+ end
240
+
241
+ it 'handles NoMethodError on IRB implicit #ai' do
242
+ module IRB; class Irb; end; end
243
+ irb_context = double('irb_context', last_value: BasicObject.new)
244
+ IRB.define_singleton_method :version, -> { 'test_version' }
245
+ irb = IRB::Irb.new
246
+ irb.instance_eval { @context = irb_context }
247
+ AwesomePrint.irb!
248
+ expect(irb).to receive(:puts).with("(Object doesn't support #ai)")
249
+ expect { irb.output_value }.to_not raise_error
250
+ Object.instance_eval { remove_const :IRB }
248
251
  end
249
252
  end
250
253
  end
data/spec/objects_spec.rb CHANGED
@@ -1,28 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "Objects" do
4
- before do
5
- stub_dotfile!
6
- end
7
-
3
+ RSpec.describe 'Objects' do
8
4
  after do
9
- Object.instance_eval{ remove_const :Hello } if defined?(Hello)
5
+ Object.instance_eval { remove_const :Hello } if defined?(Hello)
10
6
  end
11
7
 
12
- describe "Formatting an object" do
13
- it "attributes" do
8
+ describe 'Formatting an object' do
9
+ it 'attributes' do
14
10
  class Hello
15
11
  attr_reader :abra
16
12
  attr_writer :ca
17
13
  attr_accessor :dabra
18
14
 
19
15
  def initialize
20
- @abra, @ca, @dabra = 1, 2, 3
16
+ @abra = 1
17
+ @ca = 2
18
+ @dabra = 3
21
19
  end
22
20
  end
23
21
 
24
22
  hello = Hello.new
25
- out = hello.ai(:plain => true, :raw => true)
23
+ out = hello.ai(plain: true, raw: true)
26
24
  str = <<-EOS.strip
27
25
  #<Hello:placeholder_id
28
26
  attr_accessor :dabra = 3,
@@ -31,18 +29,20 @@ RSpec.describe "Objects" do
31
29
  >
32
30
  EOS
33
31
  expect(out).to be_similar_to(str)
34
- expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
32
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
35
33
  end
36
34
 
37
- it "instance variables" do
35
+ it 'instance variables' do
38
36
  class Hello
39
37
  def initialize
40
- @abra, @ca, @dabra = 1, 2, 3
38
+ @abra = 1
39
+ @ca = 2
40
+ @dabra = 3
41
41
  end
42
42
  end
43
43
 
44
44
  hello = Hello.new
45
- out = hello.ai(:plain => true, :raw => true)
45
+ out = hello.ai(plain: true, raw: true)
46
46
  str = <<-EOS.strip
47
47
  #<Hello:placeholder_id
48
48
  @abra = 1,
@@ -51,23 +51,27 @@ EOS
51
51
  >
52
52
  EOS
53
53
  expect(out).to be_similar_to(str)
54
- expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
54
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
55
55
  end
56
56
 
57
- it "attributes and instance variables" do
57
+ it 'attributes and instance variables' do
58
58
  class Hello
59
59
  attr_reader :abra
60
60
  attr_writer :ca
61
61
  attr_accessor :dabra
62
62
 
63
63
  def initialize
64
- @abra, @ca, @dabra = 1, 2, 3
65
- @scooby, @dooby, @doo = 3, 2, 1
64
+ @abra = 1
65
+ @ca = 2
66
+ @dabra = 3
67
+ @scooby = 3
68
+ @dooby = 2
69
+ @doo = 1
66
70
  end
67
71
  end
68
72
 
69
73
  hello = Hello.new
70
- out = hello.ai(:plain => true, :raw => true)
74
+ out = hello.ai(plain: true, raw: true)
71
75
  str = <<-EOS.strip
72
76
  #<Hello:placeholder_id
73
77
  @doo = 1,
@@ -79,22 +83,23 @@ EOS
79
83
  >
80
84
  EOS
81
85
  expect(out).to be_similar_to(str)
82
- expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
86
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
83
87
  end
84
88
 
85
- it "without the plain options print the colorized values" do
89
+ it 'without the plain options print the colorized values' do
86
90
  class Hello
87
91
  attr_reader :abra
88
92
  attr_writer :ca
89
93
 
90
94
  def initialize
91
- @abra, @ca = 1, 2
95
+ @abra = 1
96
+ @ca = 2
92
97
  @dabra = 3
93
98
  end
94
99
  end
95
100
 
96
101
  hello = Hello.new
97
- out = hello.ai(:raw => true)
102
+ out = hello.ai(raw: true)
98
103
  str = <<-EOS.strip
99
104
  #<Hello:placeholder_id
100
105
  \e[0;36m@dabra\e[0m\e[0;37m = \e[0m\e[1;34m3\e[0m,
@@ -103,27 +108,113 @@ EOS
103
108
  >
104
109
  EOS
105
110
  expect(out).to be_similar_to(str)
106
- expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
111
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
107
112
  end
108
113
 
109
- it "with multine as false show inline values" do
114
+ it 'with multine as false show inline values' do
110
115
  class Hello
111
116
  attr_reader :abra
112
117
  attr_writer :ca
113
118
 
114
119
  def initialize
115
- @abra, @ca = 1, 2
120
+ @abra = 1
121
+ @ca = 2
116
122
  @dabra = 3
117
123
  end
118
124
  end
119
125
 
120
126
  hello = Hello.new
121
- out = hello.ai(:multiline => false, :plain => true, :raw => true)
127
+ out = hello.ai(multiline: false, plain: true, raw: true)
122
128
  str = <<-EOS.strip
123
129
  #<Hello:placeholder_id @dabra = 3, attr_reader :abra = 1, attr_writer :ca = 2>
124
130
  EOS
125
131
  expect(out).to be_similar_to(str)
126
- expect(hello.ai(:plain => true, :raw => false)).to eq(hello.inspect)
132
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
133
+ end
134
+
135
+ it 'without the sort_vars option does not sort instance variables' do
136
+ class Hello
137
+ attr_reader :abra
138
+ attr_writer :ca
139
+ attr_accessor :dabra
140
+
141
+ def initialize
142
+ @abra = 1
143
+ @ca = 2
144
+ @dabra = 3
145
+ @scooby = 3
146
+ @dooby = 2
147
+ @doo = 1
148
+ end
149
+
150
+ def instance_variables
151
+ [:@scooby, :@dooby, :@doo, :@abra, :@ca, :@dabra]
152
+ end
153
+ end
154
+
155
+ hello = Hello.new
156
+ out = hello.ai(plain: true, raw: true, sort_vars: false)
157
+ str = <<-EOS.strip
158
+ #<Hello:placeholder_id
159
+ @scooby = 3,
160
+ @dooby = 2,
161
+ @doo = 1,
162
+ attr_reader :abra = 1,
163
+ attr_writer :ca = 2,
164
+ attr_accessor :dabra = 3
165
+ >
166
+ EOS
167
+ expect(out).to be_similar_to(str)
168
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
169
+ end
170
+
171
+ it 'object_id' do
172
+ class Hello
173
+ def initialize
174
+ @abra = 1
175
+ @ca = 2
176
+ @dabra = 3
177
+ end
178
+ end
179
+
180
+ hello = Hello.new
181
+ out = hello.ai(plain: true, raw: true, object_id: false)
182
+ str = <<-EOS.strip
183
+ #<Hello
184
+ @abra = 1,
185
+ @ca = 2,
186
+ @dabra = 3
187
+ >
188
+ EOS
189
+ expect(out).to be_similar_to(str)
190
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
191
+ end
192
+
193
+ it 'class_name' do
194
+ class Hello
195
+ def initialize
196
+ @abra = 1
197
+ @ca = 2
198
+ @dabra = 3
199
+ end
200
+
201
+ def to_s
202
+ "CustomizedHello"
203
+ end
204
+ end
205
+
206
+ hello = Hello.new
207
+ out = hello.ai(plain: true, raw: true, class_name: :to_s)
208
+ str = <<-EOS.strip
209
+ #<CustomizedHello:placeholder_id
210
+ @abra = 1,
211
+ @ca = 2,
212
+ @dabra = 3
213
+ >
214
+ EOS
215
+ expect(out).to be_similar_to(str)
216
+ expect(hello.ai(plain: true, raw: false)).to eq(hello.inspect)
127
217
  end
218
+
128
219
  end
129
220
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2010-2013 Michael Dvorkin
1
+ # Copyright (c) 2010-2016 Michael Dvorkin and contributors
2
2
  #
3
3
  # Awesome Print is freely distributable under the terms of MIT license.
4
4
  # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
@@ -14,8 +14,10 @@
14
14
  # $ gem install bundler -v=1.0.2
15
15
  # $ gem install rspec -v=2.6.0
16
16
  #
17
- require 'codeclimate-test-reporter'
18
- CodeClimate::TestReporter.start
17
+
18
+ # require 'simplecov'
19
+ # SimpleCov.start
20
+
19
21
  $LOAD_PATH.unshift(File.dirname(__FILE__))
20
22
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21
23
 
@@ -23,9 +25,19 @@ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
23
25
  require file
24
26
  end
25
27
 
26
- ExtVerifier.require_dependencies!(%w{rails active_record action_view
27
- active_support/all mongoid mongo_mapper ripple nobrainer})
28
+ ExtVerifier.require_dependencies!(
29
+ %w(
30
+ rails
31
+ active_record
32
+ action_view
33
+ active_support/all
34
+ mongoid
35
+ mongo_mapper
36
+ ripple nobrainer
37
+ )
38
+ )
28
39
  require 'nokogiri'
40
+ require 'ostruct'
29
41
  require 'awesome_print'
30
42
 
31
43
  RSpec.configure do |config|
@@ -42,8 +54,13 @@ RSpec.configure do |config|
42
54
  mocks.syntax = :expect
43
55
  mocks.verify_partial_doubles = true
44
56
  end
45
- if config.files_to_run.one?
46
- config.default_formatter = 'doc'
57
+
58
+ config.default_formatter = 'doc' if config.files_to_run.one?
59
+
60
+ # Run before all examples. Using suite or all will not work as stubs are
61
+ # killed after each example ends.
62
+ config.before(:each) do |_example|
63
+ stub_dotfile!
47
64
  end
48
65
  end
49
66
 
@@ -71,12 +88,14 @@ def normalize_object_id_strings(str, options)
71
88
  end
72
89
 
73
90
  def stub_dotfile!
74
- dotfile = File.join(ENV["HOME"], ".aprc")
75
- expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false)
91
+ allow_any_instance_of(AwesomePrint::Inspector)
92
+ .to receive(:load_dotfile)
93
+ .and_return(true)
76
94
  end
77
95
 
78
96
  def capture!
79
- standard, $stdout = $stdout, StringIO.new
97
+ standard = $stdout
98
+ $stdout = StringIO.new
80
99
  yield
81
100
  ensure
82
101
  $stdout = standard
@@ -0,0 +1,24 @@
1
+ #<User:placeholder_id
2
+ @aggregation_cache = {},
3
+ @attributes_cache = {},
4
+ @destroyed = false,
5
+ @marked_for_destruction = false,
6
+ @mass_assignment_options = nil,
7
+ @new_record = true,
8
+ @previously_changed = {},
9
+ @readonly = false,
10
+ attr_accessor :attributes = {
11
+ "admin" => false,
12
+ "created_at" => "1992-10-10 12:30:00",
13
+ "id" => nil,
14
+ "name" => "Diana",
15
+ "rank" => 1
16
+ },
17
+ attr_reader :association_cache = {},
18
+ attr_reader :changed_attributes = {
19
+ "admin" => nil,
20
+ "created_at" => nil,
21
+ "name" => nil,
22
+ "rank" => nil
23
+ }
24
+ >