Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 749 lines (669 sloc) 19.225 kB
4ac2bfd @michaeldv Initial public commit
authored
1 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
993bc0f @michaeldv Format BigDecimal and Rational objects as Float scalars
authored
2 require "bigdecimal"
3 require "rational"
4ac2bfd @michaeldv Initial public commit
authored
4
5 describe "AwesomePrint" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
6 before do
bf3c991 @michaeldv Stub ~/.aprc in specs to make sure custom defaults do not affect them
authored
7 stub_dotfile!
8 end
4ac2bfd @michaeldv Initial public commit
authored
9
10 describe "Array" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
11 before do
4ac2bfd @michaeldv Initial public commit
authored
12 @arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
13 end
14
6e4117d @michaeldv Correctly handle empty arrays and hashes
authored
15 it "empty array" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
16 [].ai.should == "[]"
6e4117d @michaeldv Correctly handle empty arrays and hashes
authored
17 end
18
4ac2bfd @michaeldv Initial public commit
authored
19 it "plain multiline" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
20 @arr.ai(:plain => true).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
21 [
22 [0] 1,
23 [1] :two,
24 [2] "three",
25 [3] [
26 [0] nil,
27 [1] [
28 [0] true,
29 [1] false
30 ]
31 ]
32 ]
33 EOS
34 end
35
c05a6ab Add option to suppress array index in output.
Sean Gallagher authored
36 it "plain multiline without index" do
37 @arr.ai(:plain => true, :index => false).should == <<-EOS.strip
38 [
39 1,
40 :two,
41 "three",
42 [
43 nil,
44 [
45 true,
46 false
47 ]
48 ]
49 ]
50 EOS
51 end
52
4ac2bfd @michaeldv Initial public commit
authored
53 it "plain multiline indented" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
54 @arr.ai(:plain => true, :indent => 2).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
55 [
56 [0] 1,
57 [1] :two,
58 [2] "three",
59 [3] [
60 [0] nil,
61 [1] [
62 [0] true,
63 [1] false
64 ]
65 ]
66 ]
67 EOS
68 end
69
c05a6ab Add option to suppress array index in output.
Sean Gallagher authored
70 it "plain multiline indented without index" do
71 @arr.ai(:plain => true, :indent => 2, :index => false).should == <<-EOS.strip
72 [
73 1,
74 :two,
75 "three",
76 [
77 nil,
78 [
79 true,
80 false
81 ]
82 ]
83 ]
84 EOS
85 end
86
4ac2bfd @michaeldv Initial public commit
authored
87 it "plain single line" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
88 @arr.ai(:plain => true, :multiline => false).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
4ac2bfd @michaeldv Initial public commit
authored
89 end
90
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
91 it "colored multiline (default)" do
92 @arr.ai.should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
93 [
62041f2 @michaeldv Initial implementation of formatting an arbitrary Ruby object
authored
94 \e[1;37m[0] \e[0m\e[1;34m1\e[0m,
95 \e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
96 \e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
97 \e[1;37m[3] \e[0m[
98 \e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
99 \e[1;37m[1] \e[0m[
100 \e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
101 \e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
4ac2bfd @michaeldv Initial public commit
authored
102 ]
103 ]
104 ]
105 EOS
106 end
107
108 it "colored multiline indented" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
109 @arr.ai(:indent => 8).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
110 [
62041f2 @michaeldv Initial implementation of formatting an arbitrary Ruby object
authored
111 \e[1;37m[0] \e[0m\e[1;34m1\e[0m,
112 \e[1;37m[1] \e[0m\e[0;36m:two\e[0m,
113 \e[1;37m[2] \e[0m\e[0;33m\"three\"\e[0m,
114 \e[1;37m[3] \e[0m[
115 \e[1;37m[0] \e[0m\e[1;31mnil\e[0m,
116 \e[1;37m[1] \e[0m[
117 \e[1;37m[0] \e[0m\e[1;32mtrue\e[0m,
118 \e[1;37m[1] \e[0m\e[1;31mfalse\e[0m
4ac2bfd @michaeldv Initial public commit
authored
119 ]
120 ]
121 ]
122 EOS
123 end
124
125 it "colored single line" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
126 @arr.ai(:multiline => false).should == "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
4ac2bfd @michaeldv Initial public commit
authored
127 end
128 end
129
130 #------------------------------------------------------------------------------
131 describe "Nested Array" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
132 before do
4ac2bfd @michaeldv Initial public commit
authored
133 @arr = [ 1, 2 ]
134 @arr << @arr
135 end
136
137 it "plain multiline" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
138 @arr.ai(:plain => true).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
139 [
140 [0] 1,
141 [1] 2,
142 [2] [...]
143 ]
144 EOS
145 end
146
c05a6ab Add option to suppress array index in output.
Sean Gallagher authored
147 it "plain multiline without index" do
148 @arr.ai(:plain => true, :index => false).should == <<-EOS.strip
149 [
150 1,
151 2,
152 [...]
153 ]
154 EOS
155 end
156
4ac2bfd @michaeldv Initial public commit
authored
157 it "plain single line" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
158 @arr.ai(:plain => true, :multiline => false).should == "[ 1, 2, [...] ]"
4ac2bfd @michaeldv Initial public commit
authored
159 end
160 end
161
162 #------------------------------------------------------------------------------
9fa322e @basicxman Added specs for limited output.
basicxman authored
163 describe "Limited Output Array" do
164 before(:each) do
165 @arr = (1..1000).to_a
166 end
167
168 it "plain limited output large" do
6c626a4 @basicxman Forgot to add spec, and changed used is_a? for Fixnum checking (thank…
basicxman authored
169 @arr.ai(:plain => true, :limit => true).should == <<-EOS.strip
9fa322e @basicxman Added specs for limited output.
basicxman authored
170 [
171 [ 0] 1,
172 [ 1] 2,
173 [ 2] 3,
174 [ 3] .. [996],
175 [997] 998,
176 [998] 999,
177 [999] 1000
178 ]
179 EOS
180 end
181
182 it "plain limited output small" do
183 @arr = @arr[0..3]
6c626a4 @basicxman Forgot to add spec, and changed used is_a? for Fixnum checking (thank…
basicxman authored
184 @arr.ai(:plain => true, :limit => true).should == <<-EOS.strip
9fa322e @basicxman Added specs for limited output.
basicxman authored
185 [
186 [0] 1,
187 [1] 2,
188 [2] 3,
189 [3] 4
190 ]
191 EOS
192 end
193
194 it "plain limited output with 10 lines" do
6c626a4 @basicxman Forgot to add spec, and changed used is_a? for Fixnum checking (thank…
basicxman authored
195 @arr.ai(:plain => true, :limit => 10).should == <<-EOS.strip
9fa322e @basicxman Added specs for limited output.
basicxman authored
196 [
197 [ 0] 1,
198 [ 1] 2,
199 [ 2] 3,
200 [ 3] 4,
201 [ 4] 5,
202 [ 5] .. [995],
203 [996] 997,
204 [997] 998,
205 [998] 999,
206 [999] 1000
207 ]
208 EOS
209 end
210
211 it "plain limited output with 11 lines" do
6c626a4 @basicxman Forgot to add spec, and changed used is_a? for Fixnum checking (thank…
basicxman authored
212 @arr.ai(:plain => true, :limit => 11).should == <<-EOS.strip
9fa322e @basicxman Added specs for limited output.
basicxman authored
213 [
214 [ 0] 1,
215 [ 1] 2,
216 [ 2] 3,
217 [ 3] 4,
218 [ 4] 5,
219 [ 5] .. [994],
220 [995] 996,
221 [996] 997,
222 [997] 998,
223 [998] 999,
224 [999] 1000
225 ]
226 EOS
227 end
228 end
229
230 #------------------------------------------------------------------------------
231 describe "Limited Output Hash" do
232 before(:each) do
233 @hash = ("a".."z").inject({}) { |h, v| h.merge({ v => v.to_sym }) }
234 end
235
236 it "plain limited output" do
6c626a4 @basicxman Forgot to add spec, and changed used is_a? for Fixnum checking (thank…
basicxman authored
237 @hash.ai(:sort_keys => true, :plain => true, :limit => true).should == <<-EOS.strip
9fa322e @basicxman Added specs for limited output.
basicxman authored
238 {
239 "a" => :a,
240 "b" => :b,
241 "c" => :c,
242 "d" => :d .. "w" => :w,
243 "x" => :x,
244 "y" => :y,
245 "z" => :z
246 }
247 EOS
248 end
249 end
250
251 #------------------------------------------------------------------------------
4ac2bfd @michaeldv Initial public commit
authored
252 describe "Hash" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
253 before do
4ac2bfd @michaeldv Initial public commit
authored
254 @hash = { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } }
255 end
f9ff436 @tobias A fix for the failure on an empty hash.
tobias authored
256
257 it "empty hash" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
258 {}.ai.should == "{}"
f9ff436 @tobias A fix for the failure on an empty hash.
tobias authored
259 end
260
4ac2bfd @michaeldv Initial public commit
authored
261 it "plain multiline" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
262 @hash.ai(:plain => true).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
263 {
264 1 => {
265 :sym => {
266 "str" => {
267 [ 1, 2, 3 ] => {
268 { :k => :v } => Hash < Object
269 }
270 }
271 }
272 }
273 }
274 EOS
275 end
276
277 it "plain multiline indented" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
278 @hash.ai(:plain => true, :indent => 1).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
279 {
280 1 => {
281 :sym => {
282 "str" => {
283 [ 1, 2, 3 ] => {
284 { :k => :v } => Hash < Object
285 }
286 }
287 }
288 }
289 }
290 EOS
291 end
292
293 it "plain single line" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
294 @hash.ai(:plain => true, :multiline => false).should == '{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }'
4ac2bfd @michaeldv Initial public commit
authored
295 end
296
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
297 it "colored multiline (default)" do
298 @hash.ai.should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
299 {
ed177c0 @mmullis corrections to tests-hash is :pale so color code should be normal(0)-…
mmullis authored
300 1\e[0;37m => \e[0m{
301 :sym\e[0;37m => \e[0m{
302 \"str\"\e[0;37m => \e[0m{
303 [ 1, 2, 3 ]\e[0;37m => \e[0m{
304 { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
4ac2bfd @michaeldv Initial public commit
authored
305 }
306 }
307 }
308 }
309 }
310 EOS
311 end
312
313 it "colored multiline indented" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
314 @hash.ai(:indent => 2).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
315 {
ed177c0 @mmullis corrections to tests-hash is :pale so color code should be normal(0)-…
mmullis authored
316 1\e[0;37m => \e[0m{
317 :sym\e[0;37m => \e[0m{
318 \"str\"\e[0;37m => \e[0m{
319 [ 1, 2, 3 ]\e[0;37m => \e[0m{
320 { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
4ac2bfd @michaeldv Initial public commit
authored
321 }
322 }
323 }
324 }
325 }
326 EOS
327 end
328
329 it "colored single line" do
ed177c0 @mmullis corrections to tests-hash is :pale so color code should be normal(0)-…
mmullis authored
330 @hash.ai(:multiline => false).should == "{ 1\e[0;37m => \e[0m{ :sym\e[0;37m => \e[0m{ \"str\"\e[0;37m => \e[0m{ [ 1, 2, 3 ]\e[0;37m => \e[0m{ { :k => :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }"
4ac2bfd @michaeldv Initial public commit
authored
331 end
332
333 end
334
335 #------------------------------------------------------------------------------
336 describe "Nested Hash" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
337 before do
4ac2bfd @michaeldv Initial public commit
authored
338 @hash = {}
339 @hash[:a] = @hash
340 end
341
342 it "plain multiline" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
343 @hash.ai(:plain => true).should == <<-EOS.strip
4ac2bfd @michaeldv Initial public commit
authored
344 {
345 :a => {...}
346 }
347 EOS
348 end
349
350 it "plain single line" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
351 @hash.ai(:plain => true, :multiline => false).should == '{ :a => {...} }'
4ac2bfd @michaeldv Initial public commit
authored
352 end
353 end
354
355 #------------------------------------------------------------------------------
0132bcc added a flag to sort hash keys
Ed Ruder authored
356 describe "Hash with several keys" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
357 before do
0132bcc added a flag to sort hash keys
Ed Ruder authored
358 @hash = {"b" => "b", :a => "a", :z => "z", "alpha" => "alpha"}
359 end
360
361 it "plain multiline" do
b2ce17d @michaeldv Fixed hash spec for Ruby < 1.9 where order of hash keys is not guaran…
authored
362 out = @hash.ai(:plain => true)
363 if RUBY_VERSION.to_f < 1.9 # Order of @hash keys is not guaranteed.
364 out.should =~ /^\{[^\}]+\}/m
365 out.should =~ / "b" => "b",?/
366 out.should =~ / :a => "a",?/
367 out.should =~ / :z => "z",?/
368 out.should =~ / "alpha" => "alpha",?$/
369 else
370 out.should == <<-EOS.strip
0132bcc added a flag to sort hash keys
Ed Ruder authored
371 {
372 "b" => "b",
373 :a => "a",
374 :z => "z",
375 "alpha" => "alpha"
376 }
377 EOS
b2ce17d @michaeldv Fixed hash spec for Ruby < 1.9 where order of hash keys is not guaran…
authored
378 end
0132bcc added a flag to sort hash keys
Ed Ruder authored
379 end
380
381 it "plain multiline with sorted keys" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
382 @hash.ai(:plain => true, :sort_keys => true).should == <<-EOS.strip
0132bcc added a flag to sort hash keys
Ed Ruder authored
383 {
384 :a => "a",
385 "alpha" => "alpha",
386 "b" => "b",
387 :z => "z"
388 }
389 EOS
390 end
391
392 end
393
394 #------------------------------------------------------------------------------
29d0417 @michaeldv If @options[:indent] if negative left align hash keys
authored
395 describe "Negative options[:indent]" do
d2c0a39 @michaeldv Fixed nested left alighment; fixed ActiveRecord and MongoMapper objec…
authored
396 #
397 # With Ruby < 1.9 the order of hash keys is not defined so we can't
398 # reliably compare the output string.
399 #
29d0417 @michaeldv If @options[:indent] if negative left align hash keys
authored
400 it "hash keys must be left aligned" do
d2c0a39 @michaeldv Fixed nested left alighment; fixed ActiveRecord and MongoMapper objec…
authored
401 hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
402 out = hash.ai(:plain => true, :indent => -4)
403 # {
404 # [ 0, 0, 255 ] => :yellow,
405 # :red => "rgb(255, 0, 0)",
406 # "magenta" => "rgb(255, 0, 255)"
407 # }
29d0417 @michaeldv If @options[:indent] if negative left align hash keys
authored
408 out.start_with?("{\n").should == true
782e382 @michaeldv Fixed double indentation for left-aligned hash keys
authored
409 out.match(/^\s{4}:red => "rgb\(255, 0, 0\)"/m).should_not == nil
410 out.match(/^\s{4}"magenta" => "rgb\(255, 0, 255\)"/m).should_not == nil
411 out.match(/^\s{4}\[ 0, 0, 255 \] => :yellow/m).should_not == nil
29d0417 @michaeldv If @options[:indent] if negative left align hash keys
authored
412 out.end_with?("\n}").should == true
413 end
d2c0a39 @michaeldv Fixed nested left alighment; fixed ActiveRecord and MongoMapper objec…
authored
414
415 it "nested hash keys should be indented (array of hashes)" do
416 arr = [ { :a => 1, :bb => 22, :ccc => 333}, { 1 => :a, 22 => :bb, 333 => :ccc} ]
417 out = arr.ai(:plain => true, :indent => -4)
418 # [
419 # [0] {
420 # :a => 1,
421 # :bb => 22,
422 # :ccc => 333
423 # },
424 # [1] {
425 # 1 => :a,
426 # 22 => :bb,
427 # 333 => :ccc
428 # }
429 # ]
430 out.start_with?("[\n").should == true
431 out.match(/^\s{4}\[0|1\] {/m).should_not == nil
432 out.match(/^\s{8}:a => 1,/m).should_not == nil
433 out.match(/^\s{8}:bb => 22,/m).should_not == nil
434 out.match(/^\s{8}:ccc => 333/m).should_not == nil
435 out.match(/^\s{8}1 => :a,/m).should_not == nil
436 out.match(/^\s{8}22 => :bb,/m).should_not == nil
437 out.match(/^\s{8}333 => :ccc/m).should_not == nil
438 out.end_with?("\n]").should == true
439 end
440
441 it "nested hash keys should be indented (hash of hashes)" do
442 arr = { :first => { :a => 1, :bb => 22, :ccc => 333}, :second => { 1 => :a, 22 => :bb, 333 => :ccc} }
443 out = arr.ai(:plain => true, :indent => -4)
444 # {
445 # :first => {
446 # :a => 1,
447 # :bb => 22,
448 # :ccc => 333
449 # },
450 # :second => {
451 # 1 => :a,
452 # 22 => :bb,
453 # 333 => :ccc
454 # }
455 # }
456 out.start_with?("{\n").should == true
457 out.match(/^\s{4}:first => {/m).should_not == nil
458 out.match(/^\s{8}:a => 1,/m).should_not == nil
459 out.match(/^\s{8}:bb => 22,/m).should_not == nil
460 out.match(/^\s{8}:ccc => 333/m).should_not == nil
461 out.match(/^\s{4}:second => {/m).should_not == nil
462 out.match(/^\s{8}1 => :a,/m).should_not == nil
463 out.match(/^\s{8}22 => :bb,/m).should_not == nil
464 out.match(/^\s{8}333 => :ccc/m).should_not == nil
465 out.end_with?("\n}").should == true
466 end
29d0417 @michaeldv If @options[:indent] if negative left align hash keys
authored
467 end
468
469 #------------------------------------------------------------------------------
4ac2bfd @michaeldv Initial public commit
authored
470 describe "Class" do
471 it "shoud show superclass (plain)" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
472 self.class.ai(:plain => true).should == "#{self.class} < #{self.class.superclass}"
4ac2bfd @michaeldv Initial public commit
authored
473 end
474
475 it "shoud show superclass (color)" do
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
476 self.class.ai.should == "#{self.class} < #{self.class.superclass}".yellow
4ac2bfd @michaeldv Initial public commit
authored
477 end
478 end
479
480 #------------------------------------------------------------------------------
481 describe "File" do
482 it "should display a file (plain)" do
483 File.open(__FILE__, "r") do |f|
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
484 f.ai(:plain => true).should == "#{f.inspect}\n" << `ls -alF #{f.path}`.chop
4ac2bfd @michaeldv Initial public commit
authored
485 end
486 end
487 end
488
489 #------------------------------------------------------------------------------
490 describe "Dir" do
491 it "should display a direcory (plain)" do
492 Dir.open(File.dirname(__FILE__)) do |d|
0fddf1e @michaeldv Refactored to move #ai (aka ##awesome_inspect) to Kernel
authored
493 d.ai(:plain => true).should == "#{d.inspect}\n" << `ls -alF #{d.path}`.chop
4ac2bfd @michaeldv Initial public commit
authored
494 end
495 end
496 end
497
d84c376 @michaeldv Make sure explicit options parameter can override custom defaults
authored
498 #------------------------------------------------------------------------------
993bc0f @michaeldv Format BigDecimal and Rational objects as Float scalars
authored
499 describe "BigDecimal and Rational" do
500 it "should present BigDecimal object as Float scalar" do
97d8bb7 @tobias Factored active_record and active_support enhancements to separate mo…
tobias authored
501 big = BigDecimal("2010.4")
993bc0f @michaeldv Format BigDecimal and Rational objects as Float scalars
authored
502 big.ai(:plain => true).should == "2010.4"
503 end
504
505 it "should present Rational object as Float scalar" do
506 rat = Rational(2010, 2)
507 rat.ai(:plain => true).should == "1005.0"
508 end
509 end
510
511 #------------------------------------------------------------------------------
d84c376 @michaeldv Make sure explicit options parameter can override custom defaults
authored
512 describe "Utility methods" do
513 it "should merge options" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
514 ap = AwesomePrint::Inspector.new
d84c376 @michaeldv Make sure explicit options parameter can override custom defaults
authored
515 ap.send(:merge_options!, { :color => { :array => :black }, :indent => 0 })
516 options = ap.instance_variable_get("@options")
517 options[:color][:array].should == :black
518 options[:indent].should == 0
519 end
520 end
521
92dfe6d added support for Struct
Michael Mullis authored
522
523 #------------------------------------------------------------------------------
524 describe "Struct" do
dfbebd4 @michaeldv Major refactoring: split AwesomePrint class into Inspector and Formatter
authored
525 before do
c9781fc @michaeldv Avoid Struct redifinition warning when running specs; removed unneces…
authored
526 @struct = unless defined?(Struct::SimpleStruct)
527 Struct.new("SimpleStruct", :name, :address).new
528 else
529 Struct::SimpleStruct.new
530 end
92dfe6d added support for Struct
Michael Mullis authored
531 @struct.name = "Herman Munster"
532 @struct.address = "1313 Mockingbird Lane"
533 end
534
535 it "empty struct" do
536 Struct.new("EmptyStruct").ai.should == "\e[1;33mStruct::EmptyStruct < Struct\e[0m"
537 end
538
539 it "plain multiline" do
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
540 s1 = <<-EOS.strip
92dfe6d added support for Struct
Michael Mullis authored
541 {
542 :address => "1313 Mockingbird Lane",
543 :name => "Herman Munster"
544 }
545 EOS
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
546 s2 = <<-EOS.strip
547 {
548 :name => "Herman Munster",
549 :address => "1313 Mockingbird Lane"
550 }
551 EOS
552 @struct.ai(:plain => true).should satisfy { |match| match == s1 || match == s2 }
92dfe6d added support for Struct
Michael Mullis authored
553 end
554
555 it "plain multiline indented" do
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
556 s1 = <<-EOS.strip
92dfe6d added support for Struct
Michael Mullis authored
557 {
558 :address => "1313 Mockingbird Lane",
559 :name => "Herman Munster"
560 }
561 EOS
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
562 s2 = <<-EOS.strip
563 {
564 :name => "Herman Munster",
565 :address => "1313 Mockingbird Lane"
566 }
567 EOS
568 @struct.ai(:plain => true, :indent => 1).should satisfy { |match| match == s1 || match == s2 }
92dfe6d added support for Struct
Michael Mullis authored
569 end
570
571 it "plain single line" do
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
572 s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }"
573 s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }"
574 @struct.ai(:plain => true, :multiline => false).should satisfy { |match| match == s1 || match == s2 }
92dfe6d added support for Struct
Michael Mullis authored
575 end
576
577 it "colored multiline (default)" do
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
578 s1 = <<-EOS.strip
92dfe6d added support for Struct
Michael Mullis authored
579 {
580 :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m,
581 :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m
582 }
583 EOS
5ee3eae @michaeldv Added ap support within Rails templates (ex. <%= ap @obj =>)
authored
584 s2 = <<-EOS.strip
585 {
586 :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m,
587 :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m
588 }
589 EOS
590 @struct.ai.should satisfy { |match| match == s1 || match == s2 }
92dfe6d added support for Struct
Michael Mullis authored
591 end
592 end
593
594
75700ea @michaeldv Handle weird objects that return nil on inspect
authored
595 #------------------------------------------------------------------------------
596 describe "Misc" do
597 it "handle weird objects that return nil on inspect" do
598 weird = Class.new do
599 def inspect
600 nil
601 end
602 end
603 weird.new.ai(:plain => true).should == ''
604 end
437810b @michaeldv Fixed array grep for patters with / character
authored
605
3ee9e2d @michaeldv Added spec for frozen object.inspect
authored
606 it "handle frozen object.inspect" do
607 weird = Class.new do
608 def inspect
609 "ice".freeze
610 end
611 end
612 weird.new.ai(:plain => false).should == "ice"
613 end
614
437810b @michaeldv Fixed array grep for patters with / character
authored
615 # See https://github.com/michaeldv/awesome_print/issues/35
616 it "handle array grep when pattern contains / chapacter" do
617 hash = { "1/x" => 1, "2//x" => :"2" }
618 grepped = hash.keys.grep(/^(\d+)\//) { $1 }
619 grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
620 end
a861908 @michaeldv Added :html => true option to dump HTML colors rather that ANSI
authored
621
622 it "returns value passed as a parameter" do
623 object = rand
624 self.stub!(:puts)
625 (ap object).should == object
626 end
24b6192 @michaeldv Avoid loading awesome_print more than once (ex. ~/.irbrc and then Rai…
authored
627
628 # Require different file name this time (lib/ap.rb vs. lib/awesome_print).
629 it "several require 'awesome_print' should do no harm" do
630 require File.expand_path(File.dirname(__FILE__) + '/../lib/ap')
631 lambda { rand.ai }.should_not raise_error
632 end
a861908 @michaeldv Added :html => true option to dump HTML colors rather that ANSI
authored
633 end
634
635 describe "HTML output" do
636 it "wraps ap output with plain <pre> tag" do
637 markup = rand
638 markup.ai(:html => true, :plain => true).should == "<pre>#{markup}</pre>"
639 end
640
3849b4a @michaeldv Changed HTML formatting to support pre { display:inline } style
authored
641 it "wraps ap output with <pre> tag with colorized <kbd>" do
a861908 @michaeldv Added :html => true option to dump HTML colors rather that ANSI
authored
642 markup = rand
3849b4a @michaeldv Changed HTML formatting to support pre { display:inline } style
authored
643 markup.ai(:html => true).should == %Q|<pre><kbd style="color:blue">#{markup}</kbd></pre>|
644 end
645
646 it "wraps multiline ap output with <pre> tag with colorized <kbd>" do
647 markup = [ 1, :two, "three" ]
648 markup.ai(:html => true).should == <<-EOS.strip
649 <pre>[
650 <kbd style="color:white">[0] </kbd><pre><kbd style="color:blue">1</kbd></pre>,
651 <kbd style="color:white">[1] </kbd><pre><kbd style="color:darkcyan">:two</kbd></pre>,
652 <kbd style="color:white">[2] </kbd><pre><kbd style="color:brown">&quot;three&quot;</kbd></pre>
653 ]</pre>
654 EOS
a861908 @michaeldv Added :html => true option to dump HTML colors rather that ANSI
authored
655 end
656
657 it "encodes HTML entities (plain)" do
658 markup = ' &<hello>'
659 markup.ai(:html => true, :plain => true).should == '<pre>&quot; &amp;&lt;hello&gt;&quot;</pre>'
660 end
661
662 it "encodes HTML entities (color)" do
663 markup = ' &<hello>'
3849b4a @michaeldv Changed HTML formatting to support pre { display:inline } style
authored
664 markup.ai(:html => true).should == '<pre><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>'
a861908 @michaeldv Added :html => true option to dump HTML colors rather that ANSI
authored
665 end
75700ea @michaeldv Handle weird objects that return nil on inspect
authored
666 end
667
bddee79 @michaeldv Classes that inherit from Array, Hash, File, Dir, and Struct are trea…
authored
668 #------------------------------------------------------------------------------
669 describe "Inherited from standard Ruby classes" do
670 after do
671 Object.instance_eval{ remove_const :My } if defined?(My)
672 end
673
674 it "inherited from Array should be displayed as Array" do
675 class My < Array; end
676
677 my = My.new([ 1, :two, "three", [ nil, [ true, false ] ] ])
678 my.ai(:plain => true).should == <<-EOS.strip
679 [
680 [0] 1,
681 [1] :two,
682 [2] "three",
683 [3] [
684 [0] nil,
685 [1] [
686 [0] true,
687 [1] false
688 ]
689 ]
690 ]
691 EOS
692 end
693
694 it "inherited from Hash should be displayed as Hash" do
695 class My < Hash; end
696
697 my = My[ { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } } ]
698 my.ai(:plain => true).should == <<-EOS.strip
699 {
700 1 => {
701 :sym => {
702 "str" => {
703 [ 1, 2, 3 ] => {
704 { :k => :v } => Hash < Object
705 }
706 }
707 }
708 }
709 }
710 EOS
711 end
712
713 it "inherited from File should be displayed as File" do
714 class My < File; end
715
5e4e929 @DNNX File.new('/dev/null') rescue File.new('nul')
DNNX authored
716 my = File.new('/dev/null') rescue File.new('nul')
bddee79 @michaeldv Classes that inherit from Array, Hash, File, Dir, and Struct are trea…
authored
717 my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
718 end
719
720 it "inherited from Dir should be displayed as Dir" do
721 class My < Dir; end
722
423dd39 @DNNX make tests run on windows
DNNX authored
723 require 'tmpdir'
724 my = My.new(Dir.tmpdir)
bddee79 @michaeldv Classes that inherit from Array, Hash, File, Dir, and Struct are trea…
authored
725 my.ai(:plain => true).should == "#{my.inspect}\n" << `ls -alF #{my.path}`.chop
726 end
2562c2b @michaeldv Fixed an issue with classes that define their own #send method (ex: S…
authored
727
728 it "should handle a class that defines its own #send method" do
729 class My
730 def send(arg1, arg2, arg3); end
731 end
732
733 my = My.new
734 my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
735 end
b9cf4a4 @michaeldv Correctly format methods array when object#method is overridden
authored
736
737 it "should handle a class defines its own #method method (ex. request.method)" do
738 class My
739 def method
740 'POST'
741 end
742 end
743
744 my = My.new
745 my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
746 end
bddee79 @michaeldv Classes that inherit from Array, Hash, File, Dir, and Struct are trea…
authored
747 end
4ac2bfd @michaeldv Initial public commit
authored
748 end
Something went wrong with that request. Please try again.