amazing_print 1.6.0 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -2
  3. data/lib/amazing_print/ext/active_record.rb +32 -2
  4. data/lib/amazing_print/ext/nobrainer.rb +2 -2
  5. data/lib/amazing_print/formatters/base_formatter.rb +5 -1
  6. data/lib/amazing_print/formatters/hash_formatter.rb +29 -9
  7. data/lib/amazing_print/formatters/mswin_helper.rb +4 -4
  8. data/lib/amazing_print/formatters/object_formatter.rb +1 -1
  9. data/lib/amazing_print/formatters/struct_formatter.rb +1 -1
  10. data/lib/amazing_print/inspector.rb +1 -1
  11. data/lib/amazing_print/version.rb +1 -1
  12. metadata +6 -63
  13. data/.gitignore +0 -35
  14. data/Appraisals +0 -66
  15. data/Gemfile +0 -18
  16. data/Gemfile.lock +0 -94
  17. data/LICENSE +0 -22
  18. data/Rakefile +0 -25
  19. data/spec/active_record_helper.rb +0 -43
  20. data/spec/colors_spec.rb +0 -120
  21. data/spec/core_ext/logger_spec.rb +0 -70
  22. data/spec/ext/action_controller_spec.rb +0 -40
  23. data/spec/ext/action_view_spec.rb +0 -24
  24. data/spec/ext/active_model_spec.rb +0 -37
  25. data/spec/ext/active_record_spec.rb +0 -302
  26. data/spec/ext/active_support_spec.rb +0 -37
  27. data/spec/ext/mongo_mapper_spec.rb +0 -265
  28. data/spec/ext/mongoid_spec.rb +0 -135
  29. data/spec/ext/nobrainer_spec.rb +0 -64
  30. data/spec/ext/nokogiri_spec.rb +0 -52
  31. data/spec/ext/ostruct_spec.rb +0 -24
  32. data/spec/ext/ripple_spec.rb +0 -53
  33. data/spec/ext/sequel_spec.rb +0 -45
  34. data/spec/formats_spec.rb +0 -795
  35. data/spec/methods_spec.rb +0 -520
  36. data/spec/misc_spec.rb +0 -206
  37. data/spec/objects_spec.rb +0 -225
  38. data/spec/sequel_helper.rb +0 -18
  39. data/spec/spec_helper.rb +0 -112
  40. data/spec/support/active_record_data/3_2_diana.txt +0 -24
  41. data/spec/support/active_record_data/3_2_diana_legacy.txt +0 -24
  42. data/spec/support/active_record_data/3_2_multi.txt +0 -50
  43. data/spec/support/active_record_data/3_2_multi_legacy.txt +0 -50
  44. data/spec/support/active_record_data/4_0_diana.txt +0 -98
  45. data/spec/support/active_record_data/4_0_multi.txt +0 -198
  46. data/spec/support/active_record_data/4_1_diana.txt +0 -97
  47. data/spec/support/active_record_data/4_1_multi.txt +0 -196
  48. data/spec/support/active_record_data/4_2_diana.txt +0 -109
  49. data/spec/support/active_record_data/4_2_diana_legacy.txt +0 -109
  50. data/spec/support/active_record_data/4_2_multi.txt +0 -220
  51. data/spec/support/active_record_data/4_2_multi_legacy.txt +0 -220
  52. data/spec/support/active_record_data/5_0_diana.txt +0 -105
  53. data/spec/support/active_record_data/5_0_multi.txt +0 -212
  54. data/spec/support/active_record_data/5_1_diana.txt +0 -104
  55. data/spec/support/active_record_data/5_1_multi.txt +0 -210
  56. data/spec/support/active_record_data/5_2_diana.txt +0 -104
  57. data/spec/support/active_record_data/5_2_multi.txt +0 -210
  58. data/spec/support/active_record_data/6_0_diana.txt +0 -104
  59. data/spec/support/active_record_data/6_0_multi.txt +0 -210
  60. data/spec/support/active_record_data/6_1_diana.txt +0 -109
  61. data/spec/support/active_record_data/6_1_multi.txt +0 -220
  62. data/spec/support/active_record_data/7_0_diana.txt +0 -110
  63. data/spec/support/active_record_data/7_0_multi.txt +0 -222
  64. data/spec/support/active_record_data.rb +0 -22
  65. data/spec/support/ext_verifier.rb +0 -46
  66. data/spec/support/mongoid_versions.rb +0 -24
  67. data/spec/support/rails_versions.rb +0 -62
data/spec/formats_spec.rb DELETED
@@ -1,795 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubocop:disable Lint/ConstantDefinitionInBlock
4
-
5
- require 'spec_helper'
6
- require 'bigdecimal'
7
- require 'set'
8
-
9
- RSpec.describe 'AmazingPrint' do
10
- describe 'Array' do
11
- before do
12
- @arr = [1, :two, 'three', [nil, [true, false]]]
13
- end
14
-
15
- it 'empty array' do
16
- expect([].ai).to eq('[]')
17
- end
18
-
19
- it 'plain multiline' do
20
- expect(@arr.ai(plain: true)).to eq <<~EOS.strip
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
-
36
- it 'plain multiline without index' do
37
- expect(@arr.ai(plain: true, index: false)).to eq <<~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
-
53
- it 'plain multiline indented' do
54
- expect(@arr.ai(plain: true, indent: 2)).to eq <<~EOS.strip
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
-
70
- it 'plain multiline indented without index' do
71
- expect(@arr.ai(plain: true, indent: 2, index: false)).to eq <<~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
-
87
- it 'plain single line' do
88
- expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, :two, "three", [ nil, [ true, false ] ] ]')
89
- end
90
-
91
- it 'colored multiline (default)' do
92
- expect(@arr.ai).to eq <<~EOS.strip
93
- [
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
102
- ]
103
- ]
104
- ]
105
- EOS
106
- end
107
-
108
- it 'colored multiline indented' do
109
- expect(@arr.ai(indent: 8)).to eq <<~EOS.strip
110
- [
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
119
- ]
120
- ]
121
- ]
122
- EOS
123
- end
124
-
125
- it 'colored single line' do
126
- expect(@arr.ai(multiline: false)).to eq("[ \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 ] ] ]")
127
- end
128
- end
129
-
130
- #------------------------------------------------------------------------------
131
- describe 'Nested Array' do
132
- before do
133
- @arr = [1, 2]
134
- @arr << @arr
135
- end
136
-
137
- it 'plain multiline' do
138
- expect(@arr.ai(plain: true)).to eq <<~EOS.strip
139
- [
140
- [0] 1,
141
- [1] 2,
142
- [2] [...]
143
- ]
144
- EOS
145
- end
146
-
147
- it 'plain multiline without index' do
148
- expect(@arr.ai(plain: true, index: false)).to eq <<~EOS.strip
149
- [
150
- 1,
151
- 2,
152
- [...]
153
- ]
154
- EOS
155
- end
156
-
157
- it 'plain single line' do
158
- expect(@arr.ai(plain: true, multiline: false)).to eq('[ 1, 2, [...] ]')
159
- end
160
- end
161
-
162
- #------------------------------------------------------------------------------
163
- describe 'Limited Output Array' do
164
- before do
165
- @arr = (1..1000).to_a
166
- end
167
-
168
- it 'plain limited output large' do
169
- expect(@arr.ai(plain: true, limit: true)).to eq <<~EOS.strip
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]
184
- expect(@arr.ai(plain: true, limit: true)).to eq <<~EOS.strip
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
195
- expect(@arr.ai(plain: true, limit: 10)).to eq <<~EOS.strip
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
212
- expect(@arr.ai(plain: true, limit: 11)).to eq <<~EOS.strip
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 do
233
- @hash = ('a'..'z').inject({}) { |h, v| h.merge({ v => v.to_sym }) }
234
- end
235
-
236
- it 'plain limited output' do
237
- expect(@hash.ai(sort_keys: true, plain: true, limit: true)).to eq <<~EOS.strip
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
- #------------------------------------------------------------------------------
252
- describe 'Hash' do
253
- before do
254
- @hash = { 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }
255
- end
256
-
257
- it 'empty hash' do
258
- expect({}.ai).to eq('{}')
259
- end
260
-
261
- it 'plain multiline' do
262
- expect(@hash.ai(plain: true)).to eq <<~EOS.strip
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 'new hash syntax' do
278
- expect(@hash.ai(plain: true, ruby19_syntax: true)).to eq <<~EOS.strip
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 multiline indented' do
294
- expect(@hash.ai(plain: true, indent: 1)).to eq <<~EOS.strip
295
- {
296
- 1 => {
297
- :sym => {
298
- "str" => {
299
- [ 1, 2, 3 ] => {
300
- { :k => :v } => Hash < Object
301
- }
302
- }
303
- }
304
- }
305
- }
306
- EOS
307
- end
308
-
309
- it 'plain single line' do
310
- expect(@hash.ai(plain: true,
311
- multiline: false)).to eq('{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }')
312
- end
313
-
314
- it 'colored multiline (default)' do
315
- expect(@hash.ai).to eq <<~EOS.strip
316
- {
317
- \e[1;34m1\e[0m\e[0;37m => \e[0m{
318
- \e[0;36m:sym\e[0m\e[0;37m => \e[0m{
319
- \e[0;33m"str"\e[0m\e[0;37m => \e[0m{
320
- [ \e[1;34m1\e[0m, \e[1;34m2\e[0m, \e[1;34m3\e[0m ]\e[0;37m => \e[0m{
321
- { \e[0;36m:k\e[0m\e[0;37m => \e[0m\e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
322
- }
323
- }
324
- }
325
- }
326
- }
327
- EOS
328
- end
329
-
330
- it 'colored with new hash syntax' do
331
- expect(@hash.ai(ruby19_syntax: true)).to eq <<~EOS.strip
332
- {
333
- \e[1;34m1\e[0m\e[0;37m => \e[0m{
334
- \e[0;36m:sym\e[0m\e[0;37m => \e[0m{
335
- \e[0;33m"str"\e[0m\e[0;37m => \e[0m{
336
- [ \e[1;34m1\e[0m, \e[1;34m2\e[0m, \e[1;34m3\e[0m ]\e[0;37m => \e[0m{
337
- { \e[0;36m:k\e[0m\e[0;37m => \e[0m\e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
338
- }
339
- }
340
- }
341
- }
342
- }
343
- EOS
344
- end
345
-
346
- it 'colored multiline indented' do
347
- expect(@hash.ai(indent: 2)).to eq <<~EOS.strip
348
- {
349
- \e[1;34m1\e[0m\e[0;37m => \e[0m{
350
- \e[0;36m:sym\e[0m\e[0;37m => \e[0m{
351
- \e[0;33m"str"\e[0m\e[0;37m => \e[0m{
352
- [ \e[1;34m1\e[0m, \e[1;34m2\e[0m, \e[1;34m3\e[0m ]\e[0;37m => \e[0m{
353
- { \e[0;36m:k\e[0m\e[0;37m => \e[0m\e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
354
- }
355
- }
356
- }
357
- }
358
- }
359
- EOS
360
- end
361
-
362
- it 'colored single line' do
363
- puts @hash.ai(multiline: false)
364
- expect(@hash.ai(multiline: false)).to eq(
365
- "{ \e[1;34m1\e[0m\e[0;37m => \e[0m{ \e[0;36m:sym\e[0m\e[0;37m => \e[0m{ \e[0;33m\"str\"\e[0m\e[0;37m => \e[0m{ [ \e[1;34m1\e[0m, \e[1;34m2\e[0m, \e[1;34m3\e[0m ]\e[0;37m => \e[0m{ { \e[0;36m:k\e[0m\e[0;37m => \e[0m\e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m } } } } }" # rubocop:disable Layout/LineLength
366
- )
367
- end
368
- end
369
-
370
- #------------------------------------------------------------------------------
371
- describe 'Nested Hash' do
372
- before do
373
- @hash = {}
374
- @hash[:a] = @hash
375
- end
376
-
377
- it 'plain multiline' do
378
- expect(@hash.ai(plain: true)).to eq <<~EOS.strip
379
- {
380
- :a => {...}
381
- }
382
- EOS
383
- end
384
-
385
- it 'plain single line' do
386
- expect(@hash.ai(plain: true, multiline: false)).to eq('{ :a => {...} }')
387
- end
388
- end
389
-
390
- #------------------------------------------------------------------------------
391
- describe 'Hash with several keys' do
392
- before do
393
- @hash = { 'b' => 'b', :a => 'a', :z => 'z', 'alpha' => 'alpha' }
394
- end
395
-
396
- it 'plain multiline' do
397
- out = @hash.ai(plain: true)
398
- expect(out).to eq <<~EOS.strip
399
- {
400
- "b" => "b",
401
- :a => "a",
402
- :z => "z",
403
- "alpha" => "alpha"
404
- }
405
- EOS
406
- end
407
-
408
- it 'plain multiline with sorted keys' do
409
- expect(@hash.ai(plain: true, sort_keys: true)).to eq <<~EOS.strip
410
- {
411
- :a => "a",
412
- "alpha" => "alpha",
413
- "b" => "b",
414
- :z => "z"
415
- }
416
- EOS
417
- end
418
- end
419
-
420
- #------------------------------------------------------------------------------
421
- describe 'Negative options[:indent]' do
422
- #
423
- # With Ruby < 1.9 the order of hash keys is not defined so we can't
424
- # reliably compare the output string.
425
- #
426
- it 'hash keys must be left aligned' do
427
- hash = { [0, 0, 255] => :yellow, :red => 'rgb(255, 0, 0)', 'magenta' => 'rgb(255, 0, 255)' }
428
- out = hash.ai(plain: true, indent: -4, sort_keys: true)
429
- expect(out).to eq <<~EOS.strip
430
- {
431
- [ 0, 0, 255 ] => :yellow,
432
- "magenta" => "rgb(255, 0, 255)",
433
- :red => "rgb(255, 0, 0)"
434
- }
435
- EOS
436
- end
437
-
438
- it 'nested hash keys should be indented (array of hashes)' do
439
- arr = [{ a: 1, bb: 22, ccc: 333 }, { 1 => :a, 22 => :bb, 333 => :ccc }]
440
- out = arr.ai(plain: true, indent: -4, sort_keys: true)
441
- expect(out).to eq <<~EOS.strip
442
- [
443
- [0] {
444
- :a => 1,
445
- :bb => 22,
446
- :ccc => 333
447
- },
448
- [1] {
449
- 1 => :a,
450
- 22 => :bb,
451
- 333 => :ccc
452
- }
453
- ]
454
- EOS
455
- end
456
-
457
- it 'nested hash keys should be indented (hash of hashes)' do
458
- arr = { first: { a: 1, bb: 22, ccc: 333 }, second: { 1 => :a, 22 => :bb, 333 => :ccc } }
459
- out = arr.ai(plain: true, indent: -4, sort_keys: true)
460
- expect(out).to eq <<~EOS.strip
461
- {
462
- :first => {
463
- :a => 1,
464
- :bb => 22,
465
- :ccc => 333
466
- },
467
- :second => {
468
- 1 => :a,
469
- 22 => :bb,
470
- 333 => :ccc
471
- }
472
- }
473
- EOS
474
- end
475
- end
476
-
477
- #
478
- # With Ruby 1.9 syntax
479
- #
480
- it 'hash keys must be left aligned' do
481
- hash = { [0, 0, 255] => :yellow, :bloodiest_red => 'rgb(255, 0, 0)', 'magenta' => 'rgb(255, 0, 255)' }
482
- out = hash.ai(plain: true, indent: -2, ruby19_syntax: true, sort_keys: true)
483
- expect(out).to eq <<~EOS.strip
484
- {
485
- [ 0, 0, 255 ] => :yellow,
486
- bloodiest_red: "rgb(255, 0, 0)",
487
- "magenta" => "rgb(255, 0, 255)"
488
- }
489
- EOS
490
- end
491
-
492
- #------------------------------------------------------------------------------
493
- describe 'Class' do
494
- it 'shows superclass (plain)' do
495
- expect(self.class.ai(plain: true)).to eq("#{self.class} < #{self.class.superclass}")
496
- end
497
-
498
- it 'shows superclass (color)' do
499
- expect(self.class.ai).to eq(AmazingPrint::Colors.yellow("#{self.class} < #{self.class.superclass}"))
500
- end
501
- end
502
-
503
- #------------------------------------------------------------------------------
504
- describe 'File' do
505
- it 'displays a file (plain)', :unix do
506
- File.open(__FILE__, 'r') do |f|
507
- expect(f.ai(plain: true)).to eq("#{f.inspect}\n" + `ls -alF #{f.path}`.chop)
508
- end
509
- end
510
-
511
- it 'displays a file (plain) akin to powershell Get-ChildItem', :mswin do
512
- File.open(__FILE__, 'r') do |f|
513
- expect(f.ai(plain: true)).to eq("#{f.inspect}\n" + AmazingPrint::Formatters::GetChildItem.new(f.path).to_s)
514
- end
515
- end
516
- end
517
-
518
- #------------------------------------------------------------------------------
519
- describe 'Dir' do
520
- it 'displays a direcory (plain)', :unix do
521
- Dir.open(File.dirname(__FILE__)) do |d|
522
- expect(d.ai(plain: true)).to eq("#{d.inspect}\n" + `ls -alF #{d.path}`.chop)
523
- end
524
- end
525
-
526
- it 'displays a directory (plain) akin to powershell Get-ChildItem', :mswin do
527
- Dir.open(File.dirname(__FILE__)) do |d|
528
- expect(d.ai(plain: true)).to eq("#{d.inspect}\n" + AmazingPrint::Formatters::GetChildItem.new(d.path).to_s)
529
- end
530
- end
531
- end
532
-
533
- #------------------------------------------------------------------------------
534
- describe 'BigDecimal and Rational' do
535
- it 'presents BigDecimal object with arbitrary precision' do
536
- big = BigDecimal('201020102010201020102010201020102010.4')
537
- expect(big.ai(plain: true)).to eq('201020102010201020102010201020102010.4')
538
- end
539
-
540
- it 'presents Rational object with arbitrary precision' do
541
- rat = Rational(201_020_102_010_201_020_102_010_201_020_102_010, 2)
542
- out = rat.ai(plain: true)
543
- expect(out).to eq('100510051005100510051005100510051005/1')
544
- end
545
- end
546
-
547
- #------------------------------------------------------------------------------
548
- describe 'Utility methods' do
549
- it 'merges options' do
550
- ap = AmazingPrint::Inspector.new
551
- ap.send(:merge_options!, { color: { array: :black }, indent: 0 })
552
- options = ap.instance_variable_get('@options')
553
- expect(options[:color][:array]).to eq(:black)
554
- expect(options[:indent]).to eq(0)
555
- end
556
- end
557
-
558
- #------------------------------------------------------------------------------
559
- describe 'Set' do
560
- before do
561
- @arr = [1, :two, 'three']
562
- @set = Set.new(@arr)
563
- end
564
-
565
- it 'empty set' do
566
- expect(Set.new.ai).to eq([].ai)
567
- end
568
-
569
- it 'plain multiline' do
570
- expect(@set.ai(plain: true)).to eq(@arr.ai(plain: true))
571
- end
572
-
573
- it 'plain multiline indented' do
574
- expect(@set.ai(plain: true, indent: 1)).to eq(@arr.ai(plain: true, indent: 1))
575
- end
576
-
577
- it 'plain single line' do
578
- expect(@set.ai(plain: true, multiline: false)).to eq(@arr.ai(plain: true, multiline: false))
579
- end
580
-
581
- it 'colored multiline (default)' do
582
- expect(@set.ai).to eq(@arr.ai)
583
- end
584
- end
585
-
586
- #------------------------------------------------------------------------------
587
- describe 'Struct' do
588
- before do
589
- @struct = if defined?(Struct::SimpleStruct)
590
- Struct::SimpleStruct.new
591
- else
592
- Struct.new('SimpleStruct', :name, :address).new
593
- end
594
- @struct.name = 'Herman Munster'
595
- @struct.address = '1313 Mockingbird Lane'
596
- end
597
-
598
- it 'empty struct' do
599
- expect(Struct.new('EmptyStruct').ai).to eq("\e[1;33mStruct::EmptyStruct < Struct\e[0m")
600
- end
601
-
602
- it 'plain multiline' do
603
- s1 = <<-EOS.strip
604
- address = "1313 Mockingbird Lane",
605
- name = "Herman Munster"
606
- EOS
607
- s2 = <<-EOS.strip
608
- name = "Herman Munster",
609
- address = "1313 Mockingbird Lane"
610
- EOS
611
- expect(@struct.ai(plain: true)).to satisfy { |out| out.match(s1) || out.match(s2) }
612
- end
613
-
614
- it 'plain multiline indented' do
615
- s1 = <<-EOS.strip
616
- address = "1313 Mockingbird Lane",
617
- name = "Herman Munster"
618
- EOS
619
- s2 = <<-EOS.strip
620
- name = "Herman Munster",
621
- address = "1313 Mockingbird Lane"
622
- EOS
623
- expect(@struct.ai(plain: true, indent: 1)).to satisfy { |out| out.match(s1) || out.match(s2) }
624
- end
625
-
626
- it 'plain single line' do
627
- s1 = 'address = "1313 Mockingbird Lane", name = "Herman Munster"'
628
- s2 = 'name = "Herman Munster", address = "1313 Mockingbird Lane"'
629
- expect(@struct.ai(plain: true, multiline: false)).to satisfy { |out| out.match(s1) || out.match(s2) }
630
- end
631
-
632
- it 'colored multiline (default)' do
633
- s1 = <<-EOS.strip
634
- address\e[0;37m = \e[0m\e[0;33m"1313 Mockingbird Lane"\e[0m,
635
- name\e[0;37m = \e[0m\e[0;33m"Herman Munster"\e[0m
636
- EOS
637
- s2 = <<-EOS.strip
638
- name\e[0;37m = \e[0m\e[0;33m"Herman Munster"\e[0m,
639
- address\e[0;37m = \e[0m\e[0;33m"1313 Mockingbird Lane"\e[0m
640
- EOS
641
- expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) }
642
- end
643
- end
644
-
645
- #------------------------------------------------------------------------------
646
- describe 'Inherited from standard Ruby classes' do
647
- after do
648
- Object.instance_eval { remove_const :My } if defined?(My)
649
- end
650
-
651
- it 'inherited from Array should be displayed as Array' do
652
- class My < Array; end
653
-
654
- my = My.new([1, :two, 'three', [nil, [true, false]]])
655
- expect(my.ai(plain: true)).to eq <<~EOS.strip
656
- [
657
- [0] 1,
658
- [1] :two,
659
- [2] "three",
660
- [3] [
661
- [0] nil,
662
- [1] [
663
- [0] true,
664
- [1] false
665
- ]
666
- ]
667
- ]
668
- EOS
669
- end
670
-
671
- it 'inherited from Hash should be displayed as Hash' do
672
- class My < Hash; end
673
-
674
- my = My[{ 1 => { sym: { 'str' => { [1, 2, 3] => { { k: :v } => Hash } } } } }]
675
- expect(my.ai(plain: true)).to eq <<~EOS.strip
676
- {
677
- 1 => {
678
- :sym => {
679
- "str" => {
680
- [ 1, 2, 3 ] => {
681
- { :k => :v } => Hash < Object
682
- }
683
- }
684
- }
685
- }
686
- }
687
- EOS
688
- end
689
-
690
- it 'inherited from File should be displayed as File', :unix do
691
- class My < File; end
692
-
693
- my = begin
694
- File.new('/dev/null')
695
- rescue StandardError
696
- File.new('nul')
697
- end
698
- expect(my.ai(plain: true)).to eq("#{my.inspect}\n" + `ls -alF #{my.path}`.chop)
699
- end
700
-
701
- it 'inherited from File should be displayed as File', :mswin do
702
- class My < File; end
703
- my = My.new('nul') # it's /dev/null in Windows
704
- expect(my.ai(plain: true)).to eq("#{my.inspect}\n" + AmazingPrint::Formatters::GetChildItem.new(my.path).to_s)
705
- end
706
-
707
- it 'inherited from Dir should be displayed as Dir', :unix do
708
- class My < Dir; end
709
-
710
- require 'tmpdir'
711
- my = My.new(Dir.tmpdir)
712
- expect(my.ai(plain: true)).to eq("#{my.inspect}\n" + `ls -alF #{my.path}`.chop)
713
- end
714
-
715
- it 'inherited from Dir are displayed as Dir', :mswin do
716
- class My < Dir; end
717
-
718
- require 'tmpdir'
719
- my = My.new(Dir.tmpdir)
720
- expect(my.ai(plain: true)).to eq("#{my.inspect}\n" + AmazingPrint::Formatters::GetChildItem.new(my.path).to_s)
721
- end
722
-
723
- it 'handles a class that defines its own #send method' do
724
- class My
725
- def send(arg1, arg2, arg3); end
726
- end
727
-
728
- my = My.new
729
- expect { my.methods.ai(plain: true) }.not_to raise_error
730
- end
731
-
732
- it 'handles a class defines its own #method method (ex. request.method)' do
733
- class My
734
- def method
735
- 'POST'
736
- end
737
- end
738
-
739
- my = My.new
740
- expect { my.methods.ai(plain: true) }.not_to raise_error
741
- end
742
-
743
- describe 'should handle a class that defines its own #to_hash method' do
744
- it 'that takes arguments' do
745
- class My
746
- def to_hash(a, b); end
747
- end
748
-
749
- my = My.new
750
- expect { my.ai(plain: true) }.not_to raise_error
751
- end
752
-
753
- it 'that returns nil' do
754
- class My
755
- def to_hash
756
- nil
757
- end
758
- end
759
-
760
- my = My.new
761
- expect { my.ai(plain: true) }.not_to raise_error
762
- end
763
-
764
- it "that returns an object that doesn't support #keys" do
765
- class My
766
- def to_hash
767
- object = Object.new
768
- object.define_singleton_method('[]') { nil }
769
-
770
- object
771
- end
772
- end
773
-
774
- my = My.new
775
- expect { my.ai(plain: true) }.not_to raise_error
776
- end
777
-
778
- it "that returns an object that doesn't support subscripting" do
779
- class My
780
- def to_hash
781
- object = Object.new
782
- object.define_singleton_method(:keys) { [:foo] }
783
-
784
- object
785
- end
786
- end
787
-
788
- my = My.new
789
- expect { my.ai(plain: true) }.not_to raise_error
790
- end
791
- end
792
- end
793
- end
794
-
795
- # rubocop:enable Lint/ConstantDefinitionInBlock