rubysl-prettyprint 1.0.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31fe652ea2226684cfec1d8c0eb016a14d03317f
4
- data.tar.gz: 2535672446d932a39bf75cf705d2e0255a0a0eff
3
+ metadata.gz: d5e782efc9ee9a731be32290f2d016e1b7c3adf5
4
+ data.tar.gz: b7dd21c84baf3d3558c5934150c9e9d258def48c
5
5
  SHA512:
6
- metadata.gz: 6e6dbfd0c1def5a544ee370f9edbd21f91b06a06d2f29d45431abee7581badb22dc79fd171437d247d78aea60aaf1019ce1ad48eff1756fe1973b0d969e30ae2
7
- data.tar.gz: f810aace055186fc5105ec4e75f5937527095a32961ed6b71151f6b29f2569432e3460e8b18b2e0b6057bda77f16a40a76a87fb35c07ae1a1c2dd5bc15350481
6
+ metadata.gz: 987d0b65bd489f3039b0861de3bf7a107a0a8c0f5e018413da43455547aaf585638f251b58316b932f178b7728c6e2f2abbf8b81d1d18308128a7c14c9fb3160
7
+ data.tar.gz: c9b57a9aec45d48f4d25825204c4b560aa56dccc6c9d12d12644ec942f82e6ce289cdd5f55b8e5ef4f4ee86019f34ab73aae1dc7824ef5d230eb33e8c23c676d
@@ -1,8 +1,6 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
2
+ env:
3
+ - RUBYLIB=lib
6
4
  script: bundle exec mspec spec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - rbx-nightly-19mode
@@ -1,10 +1,31 @@
1
+ require 'prettyprint'
2
+
3
+ module Kernel
4
+ # returns a pretty printed object as a string.
5
+ def pretty_inspect
6
+ PP.pp(self, '')
7
+ end
8
+
9
+ private
10
+ # prints arguments in pretty form.
11
+ #
12
+ # pp returns argument(s).
13
+ def pp(*objs) # :doc:
14
+ objs.each {|obj|
15
+ PP.pp(obj)
16
+ }
17
+ objs.size <= 1 ? objs.first : objs
18
+ end
19
+ module_function :pp
20
+ end
21
+
1
22
  # == Pretty-printer for Ruby objects.
2
- #
23
+ #
3
24
  # = Which seems better?
4
- #
25
+ #
5
26
  # non-pretty-printed output by #p is:
6
27
  # #<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>
7
- #
28
+ #
8
29
  # pretty-printed output by #pp is:
9
30
  # #<PP:0x81fedf0
10
31
  # @buffer=[],
@@ -22,17 +43,19 @@
22
43
  # @newline="\n",
23
44
  # @output=#<IO:0x8114ee4>,
24
45
  # @output_width=2>
25
- #
46
+ #
26
47
  # I like the latter. If you do too, this library is for you.
27
- #
48
+ #
28
49
  # = Usage
29
- #
30
- # pp(obj)
31
50
  #
32
- # output +obj+ to +$>+ in pretty printed format.
33
- #
34
- # It returns +nil+.
35
- #
51
+ # pp(obj) #=> obj
52
+ # pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
53
+ # pp() #=> nil
54
+ #
55
+ # output +obj(s)+ to +$>+ in pretty printed format.
56
+ #
57
+ # It returns +obj(s)+.
58
+ #
36
59
  # = Output Customization
37
60
  # To define your customized pretty printing function for your classes,
38
61
  # redefine a method #pretty_print(+pp+) in the class.
@@ -42,35 +65,13 @@
42
65
  #
43
66
  # = Author
44
67
  # Tanaka Akira <akr@m17n.org>
45
-
46
- require 'prettyprint'
47
-
48
- module Kernel
49
- # returns a pretty printed object as a string.
50
- def pretty_inspect
51
- PP.pp(self, '')
52
- end
53
-
54
- private
55
- # prints arguments in pretty form.
56
- #
57
- # pp returns nil.
58
- def pp(*objs) # :doc:
59
- objs.each {|obj|
60
- PP.pp(obj)
61
- }
62
- nil
63
- end
64
- module_function :pp
65
- end
66
-
67
68
  class PP < PrettyPrint
68
69
  # Outputs +obj+ to +out+ in pretty printed format of
69
70
  # +width+ columns in width.
70
- #
71
+ #
71
72
  # If +out+ is omitted, +$>+ is assumed.
72
73
  # If +width+ is omitted, 79 is assumed.
73
- #
74
+ #
74
75
  # PP.pp returns +out+.
75
76
  def PP.pp(obj, out=$>, width=79)
76
77
  q = PP.new(out, width)
@@ -82,7 +83,7 @@ class PP < PrettyPrint
82
83
 
83
84
  # Outputs +obj+ to +out+ like PP.pp but with no indent and
84
85
  # newline.
85
- #
86
+ #
86
87
  # PP.singleline_pp returns +out+.
87
88
  def PP.singleline_pp(obj, out=$>)
88
89
  q = SingleLine.new(out)
@@ -105,59 +106,81 @@ class PP < PrettyPrint
105
106
  end
106
107
 
107
108
  module PPMethods
108
- InspectKey = :__inspect_key__
109
-
110
109
  def guard_inspect_key
111
- if Thread.current[InspectKey] == nil
112
- Thread.current[InspectKey] = []
110
+ if Thread.current[:__recursive_key__] == nil
111
+ Thread.current[:__recursive_key__] = {}.untrust
113
112
  end
114
113
 
115
- save = Thread.current[InspectKey]
114
+ if Thread.current[:__recursive_key__][:inspect] == nil
115
+ Thread.current[:__recursive_key__][:inspect] = {}.untrust
116
+ end
117
+
118
+ save = Thread.current[:__recursive_key__][:inspect]
116
119
 
117
120
  begin
118
- Thread.current[InspectKey] = []
121
+ Thread.current[:__recursive_key__][:inspect] = {}.untrust
119
122
  yield
120
123
  ensure
121
- Thread.current[InspectKey] = save
124
+ Thread.current[:__recursive_key__][:inspect] = save
122
125
  end
123
126
  end
124
127
 
128
+ def check_inspect_key(id)
129
+ Thread.current[:__recursive_key__] &&
130
+ Thread.current[:__recursive_key__][:inspect] &&
131
+ Thread.current[:__recursive_key__][:inspect].include?(id)
132
+ end
133
+ def push_inspect_key(id)
134
+ Thread.current[:__recursive_key__][:inspect][id] = true
135
+ end
136
+ def pop_inspect_key(id)
137
+ Thread.current[:__recursive_key__][:inspect].delete id
138
+ end
139
+
125
140
  # Adds +obj+ to the pretty printing buffer
126
141
  # using Object#pretty_print or Object#pretty_print_cycle.
127
- #
142
+ #
128
143
  # Object#pretty_print_cycle is used when +obj+ is already
129
144
  # printed, a.k.a the object reference chain has a cycle.
130
145
  def pp(obj)
131
- id = obj.__id__
146
+ id = obj.object_id
132
147
 
133
- if Thread.current[InspectKey].include? id
148
+ if check_inspect_key(id)
134
149
  group {obj.pretty_print_cycle self}
135
150
  return
136
151
  end
137
152
 
138
153
  begin
139
- Thread.current[InspectKey] << id
154
+ push_inspect_key(id)
140
155
  group {obj.pretty_print self}
141
156
  ensure
142
- Thread.current[InspectKey].pop unless PP.sharing_detection
157
+ pop_inspect_key(id) unless PP.sharing_detection
143
158
  end
144
159
  end
145
160
 
146
161
  # A convenience method which is same as follows:
147
- #
162
+ #
148
163
  # group(1, '#<' + obj.class.name, '>') { ... }
149
164
  def object_group(obj, &block) # :yield:
150
165
  group(1, '#<' + obj.class.name, '>', &block)
151
166
  end
152
167
 
168
+ PointerMask = (1 << ([""].pack("p").size * 8)) - 1
169
+
170
+ case Object.new.inspect
171
+ when /\A\#<Object:0x([0-9a-f]+)>\z/
172
+ PointerFormat = "%0#{$1.length}x"
173
+ else
174
+ PointerFormat = "%x"
175
+ end
176
+
153
177
  def object_address_group(obj, &block)
154
- id = "%x" % (obj.__id__ * 2)
155
- id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '')
178
+ id = PointerFormat % (obj.object_id * 2 & PointerMask)
156
179
  group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
157
180
  end
158
181
 
159
182
  # A convenience method which is same as follows:
160
- #
183
+ #
161
184
  # text ','
162
185
  # breakable
163
186
  def comma_breakable
@@ -167,23 +190,23 @@ class PP < PrettyPrint
167
190
 
168
191
  # Adds a separated list.
169
192
  # The list is separated by comma with breakable space, by default.
170
- #
193
+ #
171
194
  # #seplist iterates the +list+ using +iter_method+.
172
195
  # It yields each object to the block given for #seplist.
173
196
  # The procedure +separator_proc+ is called between each yields.
174
- #
197
+ #
175
198
  # If the iteration is zero times, +separator_proc+ is not called at all.
176
- #
199
+ #
177
200
  # If +separator_proc+ is nil or not given,
178
201
  # +lambda { comma_breakable }+ is used.
179
202
  # If +iter_method+ is not given, :each is used.
180
- #
203
+ #
181
204
  # For example, following 3 code fragments has similar effect.
182
- #
205
+ #
183
206
  # q.seplist([1,2,3]) {|v| xxx v }
184
- #
185
- # q.seplist([1,2,3], lambda { comma_breakable }, :each) {|v| xxx v }
186
- #
207
+ #
208
+ # q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }
209
+ #
187
210
  # xxx 1
188
211
  # q.comma_breakable
189
212
  # xxx 2
@@ -242,23 +265,27 @@ class PP < PrettyPrint
242
265
  module ObjectMixin
243
266
  # 1. specific pretty_print
244
267
  # 2. specific inspect
245
- # 3. specific to_s if instance variable is empty
246
- # 4. generic pretty_print
268
+ # 3. generic pretty_print
247
269
 
248
270
  # A default pretty printing method for general objects.
249
271
  # It calls #pretty_print_instance_variables to list instance variables.
250
- #
272
+ #
251
273
  # If +self+ has a customized (redefined) #inspect method,
252
274
  # the result of self.inspect is used but it obviously has no
253
275
  # line break hints.
254
- #
276
+ #
255
277
  # This module provides predefined #pretty_print methods for some of
256
278
  # the most commonly used built-in classes for convenience.
257
279
  def pretty_print(q)
258
- if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
280
+ method_method = Object.instance_method(:method).bind(self)
281
+ begin
282
+ inspect_method = method_method.call(:inspect)
283
+ rescue NameError
284
+ end
285
+ if inspect_method && /\(Kernel\)#/ !~ inspect_method.inspect
286
+ q.text self.inspect
287
+ elsif !inspect_method && self.respond_to?(:inspect)
259
288
  q.text self.inspect
260
- elsif /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect && instance_variables.empty?
261
- q.text self.to_s
262
289
  else
263
290
  q.pp_object(self)
264
291
  end
@@ -274,7 +301,7 @@ class PP < PrettyPrint
274
301
  end
275
302
 
276
303
  # Returns a sorted array of instance variable names.
277
- #
304
+ #
278
305
  # This method should return an array of names of instance variables as symbols or strings as:
279
306
  # +[:@a, :@b]+.
280
307
  def pretty_print_instance_variables
@@ -283,7 +310,7 @@ class PP < PrettyPrint
283
310
 
284
311
  # Is #inspect implementation using #pretty_print.
285
312
  # If you implement #pretty_print, it can be used as follows.
286
- #
313
+ #
287
314
  # alias inspect pretty_print_inspect
288
315
  #
289
316
  # However, doing this requires that every class that #inspect is called on
@@ -323,13 +350,17 @@ end
323
350
 
324
351
  class << ENV
325
352
  def pretty_print(q)
326
- q.pp_hash self
353
+ h = {}
354
+ ENV.keys.sort.each {|k|
355
+ h[k] = ENV[k]
356
+ }
357
+ q.pp_hash h
327
358
  end
328
359
  end
329
360
 
330
361
  class Struct
331
362
  def pretty_print(q)
332
- q.group(1, '#<struct ' + PP.mcall(self, Kernel, :class).name, '>') {
363
+ q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
333
364
  q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
334
365
  q.breakable
335
366
  q.text member.to_s
@@ -357,10 +388,10 @@ class Range
357
388
  end
358
389
  end
359
390
 
360
- class File
391
+ class File < IO
361
392
  class Stat
362
393
  def pretty_print(q)
363
- require 'etc'
394
+ require 'etc.so'
364
395
  q.object_group(self) {
365
396
  q.breakable
366
397
  q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
@@ -441,16 +472,30 @@ end
441
472
 
442
473
  class MatchData
443
474
  def pretty_print(q)
475
+ nc = []
476
+ self.regexp.named_captures.each {|name, indexes|
477
+ indexes.each {|i| nc[i] = name }
478
+ }
444
479
  q.object_group(self) {
445
480
  q.breakable
446
- q.seplist(1..self.size, lambda { q.breakable }) {|i|
447
- q.pp self[i-1]
481
+ q.seplist(0...self.size, lambda { q.breakable }) {|i|
482
+ if i == 0
483
+ q.pp self[i]
484
+ else
485
+ if nc[i]
486
+ q.text nc[i]
487
+ else
488
+ q.pp i
489
+ end
490
+ q.text ':'
491
+ q.pp self[i]
492
+ end
448
493
  }
449
494
  }
450
495
  end
451
496
  end
452
497
 
453
- class Object
498
+ class Object < BasicObject
454
499
  include PP::ObjectMixin
455
500
  end
456
501
 
@@ -469,186 +514,3 @@ end
469
514
  end
470
515
  }
471
516
  }
472
-
473
- # :enddoc:
474
- if __FILE__ == $0
475
- require 'test/unit'
476
-
477
- class PPTest < Test::Unit::TestCase
478
- def test_list0123_12
479
- assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
480
- end
481
-
482
- def test_list0123_11
483
- assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11))
484
- end
485
-
486
- OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
487
- def test_struct_override_members # [ruby-core:7865]
488
- a = OverriddenStruct.new(1,2)
489
- assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''))
490
- end
491
-
492
- def test_redefined_method
493
- o = ""
494
- def o.method
495
- end
496
- assert_equal(%(""\n), PP.pp(o, ""))
497
- end
498
- end
499
-
500
- class HasInspect
501
- def initialize(a)
502
- @a = a
503
- end
504
-
505
- def inspect
506
- return "<inspect:#{@a.inspect}>"
507
- end
508
- end
509
-
510
- class HasPrettyPrint
511
- def initialize(a)
512
- @a = a
513
- end
514
-
515
- def pretty_print(q)
516
- q.text "<pretty_print:"
517
- q.pp @a
518
- q.text ">"
519
- end
520
- end
521
-
522
- class HasBoth
523
- def initialize(a)
524
- @a = a
525
- end
526
-
527
- def inspect
528
- return "<inspect:#{@a.inspect}>"
529
- end
530
-
531
- def pretty_print(q)
532
- q.text "<pretty_print:"
533
- q.pp @a
534
- q.text ">"
535
- end
536
- end
537
-
538
- class PrettyPrintInspect < HasPrettyPrint
539
- alias inspect pretty_print_inspect
540
- end
541
-
542
- class PrettyPrintInspectWithoutPrettyPrint
543
- alias inspect pretty_print_inspect
544
- end
545
-
546
- class PPInspectTest < Test::Unit::TestCase
547
- def test_hasinspect
548
- a = HasInspect.new(1)
549
- assert_equal("<inspect:1>\n", PP.pp(a, ''))
550
- end
551
-
552
- def test_hasprettyprint
553
- a = HasPrettyPrint.new(1)
554
- assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
555
- end
556
-
557
- def test_hasboth
558
- a = HasBoth.new(1)
559
- assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
560
- end
561
-
562
- def test_pretty_print_inspect
563
- a = PrettyPrintInspect.new(1)
564
- assert_equal("<pretty_print:1>", a.inspect)
565
- a = PrettyPrintInspectWithoutPrettyPrint.new
566
- assert_raise(RuntimeError) { a.inspect }
567
- end
568
-
569
- def test_proc
570
- a = proc {1}
571
- assert_equal("#{a.inspect}\n", PP.pp(a, ''))
572
- end
573
-
574
- def test_to_s_with_iv
575
- a = Object.new
576
- def a.to_s() "aaa" end
577
- a.instance_eval { @a = nil }
578
- result = PP.pp(a, '')
579
- assert_equal("#{a.inspect}\n", result)
580
- assert_match(/\A#<Object.*>\n\z/m, result)
581
- a = 1.0
582
- a.instance_eval { @a = nil }
583
- result = PP.pp(a, '')
584
- assert_equal("#{a.inspect}\n", result)
585
- end
586
-
587
- def test_to_s_without_iv
588
- a = Object.new
589
- def a.to_s() "aaa" end
590
- result = PP.pp(a, '')
591
- assert_equal("#{a.inspect}\n", result)
592
- assert_equal("aaa\n", result)
593
- end
594
- end
595
-
596
- class PPCycleTest < Test::Unit::TestCase
597
- def test_array
598
- a = []
599
- a << a
600
- assert_equal("[[...]]\n", PP.pp(a, ''))
601
- assert_equal("#{a.inspect}\n", PP.pp(a, ''))
602
- end
603
-
604
- def test_hash
605
- a = {}
606
- a[0] = a
607
- assert_equal("{0=>{...}}\n", PP.pp(a, ''))
608
- assert_equal("#{a.inspect}\n", PP.pp(a, ''))
609
- end
610
-
611
- S = Struct.new("S", :a, :b)
612
- def test_struct
613
- a = S.new(1,2)
614
- a.b = a
615
- assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''))
616
- assert_equal("#{a.inspect}\n", PP.pp(a, ''))
617
- end
618
-
619
- def test_object
620
- a = Object.new
621
- a.instance_eval {@a = a}
622
- assert_equal(a.inspect + "\n", PP.pp(a, ''))
623
- end
624
-
625
- def test_anonymous
626
- a = Class.new.new
627
- assert_equal(a.inspect + "\n", PP.pp(a, ''))
628
- end
629
-
630
- def test_withinspect
631
- a = []
632
- a << HasInspect.new(a)
633
- assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
634
- assert_equal("#{a.inspect}\n", PP.pp(a, ''))
635
- end
636
-
637
- def test_share_nil
638
- begin
639
- PP.sharing_detection = true
640
- a = [nil, nil]
641
- assert_equal("[nil, nil]\n", PP.pp(a, ''))
642
- ensure
643
- PP.sharing_detection = false
644
- end
645
- end
646
- end
647
-
648
- class PPSingleLineTest < Test::Unit::TestCase
649
- def test_hash
650
- assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699]
651
- assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
652
- end
653
- end
654
- end
@@ -1,8 +1,6 @@
1
- # $Id$
2
-
3
1
  # This class implements a pretty printing algorithm. It finds line breaks and
4
2
  # nice indentations for grouped structure.
5
- #
3
+ #
6
4
  # By default, the class assumes that primitive elements are strings and each
7
5
  # byte in the strings have single column in width. But it can be used for
8
6
  # other situations by giving suitable arguments for some methods:
@@ -18,28 +16,28 @@
18
16
  # == Bugs
19
17
  # * Box based formatting?
20
18
  # * Other (better) model/algorithm?
21
- #
19
+ #
22
20
  # == References
23
21
  # Christian Lindig, Strictly Pretty, March 2000,
24
22
  # http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
25
- #
23
+ #
26
24
  # Philip Wadler, A prettier printer, March 1998,
27
25
  # http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
28
- #
26
+ #
29
27
  # == Author
30
28
  # Tanaka Akira <akr@m17n.org>
31
- #
29
+ #
32
30
  class PrettyPrint
33
31
 
34
32
  # This is a convenience method which is same as follows:
35
- #
33
+ #
36
34
  # begin
37
35
  # q = PrettyPrint.new(output, maxwidth, newline, &genspace)
38
36
  # ...
39
37
  # q.flush
40
38
  # output
41
39
  # end
42
- #
40
+ #
43
41
  def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
44
42
  q = PrettyPrint.new(output, maxwidth, newline, &genspace)
45
43
  yield q
@@ -95,6 +93,7 @@ class PrettyPrint
95
93
  attr_reader :output, :maxwidth, :newline, :genspace
96
94
  attr_reader :indent, :group_queue
97
95
 
96
+ # Returns the group most recently added to the stack.
98
97
  def current_group
99
98
  @group_stack.last
100
99
  end
@@ -121,6 +120,7 @@ class PrettyPrint
121
120
  current_group.first?
122
121
  end
123
122
 
123
+ # Breaks the buffer into lines that are shorter than #maxwidth
124
124
  def break_outmost_groups
125
125
  while @maxwidth < @output_width + @buffer_width
126
126
  return unless group = @group_queue.deq
@@ -157,11 +157,27 @@ class PrettyPrint
157
157
  end
158
158
  end
159
159
 
160
+ # This is similar to #breakable except
161
+ # the decision to break or not is determined individually.
162
+ #
163
+ # Two #fill_breakable under a group may cause 4 results:
164
+ # (break,break), (break,non-break), (non-break,break), (non-break,non-break).
165
+ # This is different to #breakable because two #breakable under a group
166
+ # may cause 2 results:
167
+ # (break,break), (non-break,non-break).
168
+ #
169
+ # The text sep+ is inserted if a line is not broken at this point.
170
+ #
171
+ # If +sep+ is not specified, " " is used.
172
+ #
173
+ # If +width+ is not specified, +sep.length+ is used. You will have to
174
+ # specify this when +sep+ is a multibyte character, for example.
175
+ #
160
176
  def fill_breakable(sep=' ', width=sep.length)
161
177
  group { breakable sep, width }
162
178
  end
163
179
 
164
- # This tells "you can break a line here if necessary", and a +width+\-column
180
+ # This says "you can break a line here if necessary", and a +width+\-column
165
181
  # text +sep+ is inserted if a line is not broken at the point.
166
182
  #
167
183
  # If +sep+ is not specified, " " is used.
@@ -377,520 +393,3 @@ class PrettyPrint
377
393
  end
378
394
  end
379
395
  end
380
-
381
- if __FILE__ == $0
382
- require 'test/unit'
383
-
384
- class WadlerExample < Test::Unit::TestCase # :nodoc:
385
- def setup
386
- @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"),
387
- Tree.new("dd")),
388
- Tree.new("eee"),
389
- Tree.new("ffff", Tree.new("gg"),
390
- Tree.new("hhh"),
391
- Tree.new("ii")))
392
- end
393
-
394
- def hello(width)
395
- PrettyPrint.format('', width) {|hello|
396
- hello.group {
397
- hello.group {
398
- hello.group {
399
- hello.group {
400
- hello.text 'hello'
401
- hello.breakable; hello.text 'a'
402
- }
403
- hello.breakable; hello.text 'b'
404
- }
405
- hello.breakable; hello.text 'c'
406
- }
407
- hello.breakable; hello.text 'd'
408
- }
409
- }
410
- end
411
-
412
- def test_hello_00_06
413
- expected = <<'End'.chomp
414
- hello
415
- a
416
- b
417
- c
418
- d
419
- End
420
- assert_equal(expected, hello(0))
421
- assert_equal(expected, hello(6))
422
- end
423
-
424
- def test_hello_07_08
425
- expected = <<'End'.chomp
426
- hello a
427
- b
428
- c
429
- d
430
- End
431
- assert_equal(expected, hello(7))
432
- assert_equal(expected, hello(8))
433
- end
434
-
435
- def test_hello_09_10
436
- expected = <<'End'.chomp
437
- hello a b
438
- c
439
- d
440
- End
441
- out = hello(9); assert_equal(expected, out)
442
- out = hello(10); assert_equal(expected, out)
443
- end
444
-
445
- def test_hello_11_12
446
- expected = <<'End'.chomp
447
- hello a b c
448
- d
449
- End
450
- assert_equal(expected, hello(11))
451
- assert_equal(expected, hello(12))
452
- end
453
-
454
- def test_hello_13
455
- expected = <<'End'.chomp
456
- hello a b c d
457
- End
458
- assert_equal(expected, hello(13))
459
- end
460
-
461
- def tree(width)
462
- PrettyPrint.format('', width) {|q| @tree.show(q)}
463
- end
464
-
465
- def test_tree_00_19
466
- expected = <<'End'.chomp
467
- aaaa[bbbbb[ccc,
468
- dd],
469
- eee,
470
- ffff[gg,
471
- hhh,
472
- ii]]
473
- End
474
- assert_equal(expected, tree(0))
475
- assert_equal(expected, tree(19))
476
- end
477
-
478
- def test_tree_20_22
479
- expected = <<'End'.chomp
480
- aaaa[bbbbb[ccc, dd],
481
- eee,
482
- ffff[gg,
483
- hhh,
484
- ii]]
485
- End
486
- assert_equal(expected, tree(20))
487
- assert_equal(expected, tree(22))
488
- end
489
-
490
- def test_tree_23_43
491
- expected = <<'End'.chomp
492
- aaaa[bbbbb[ccc, dd],
493
- eee,
494
- ffff[gg, hhh, ii]]
495
- End
496
- assert_equal(expected, tree(23))
497
- assert_equal(expected, tree(43))
498
- end
499
-
500
- def test_tree_44
501
- assert_equal(<<'End'.chomp, tree(44))
502
- aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]]
503
- End
504
- end
505
-
506
- def tree_alt(width)
507
- PrettyPrint.format('', width) {|q| @tree.altshow(q)}
508
- end
509
-
510
- def test_tree_alt_00_18
511
- expected = <<'End'.chomp
512
- aaaa[
513
- bbbbb[
514
- ccc,
515
- dd
516
- ],
517
- eee,
518
- ffff[
519
- gg,
520
- hhh,
521
- ii
522
- ]
523
- ]
524
- End
525
- assert_equal(expected, tree_alt(0))
526
- assert_equal(expected, tree_alt(18))
527
- end
528
-
529
- def test_tree_alt_19_20
530
- expected = <<'End'.chomp
531
- aaaa[
532
- bbbbb[ ccc, dd ],
533
- eee,
534
- ffff[
535
- gg,
536
- hhh,
537
- ii
538
- ]
539
- ]
540
- End
541
- assert_equal(expected, tree_alt(19))
542
- assert_equal(expected, tree_alt(20))
543
- end
544
-
545
- def test_tree_alt_20_49
546
- expected = <<'End'.chomp
547
- aaaa[
548
- bbbbb[ ccc, dd ],
549
- eee,
550
- ffff[ gg, hhh, ii ]
551
- ]
552
- End
553
- assert_equal(expected, tree_alt(21))
554
- assert_equal(expected, tree_alt(49))
555
- end
556
-
557
- def test_tree_alt_50
558
- expected = <<'End'.chomp
559
- aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ]
560
- End
561
- assert_equal(expected, tree_alt(50))
562
- end
563
-
564
- class Tree # :nodoc:
565
- def initialize(string, *children)
566
- @string = string
567
- @children = children
568
- end
569
-
570
- def show(q)
571
- q.group {
572
- q.text @string
573
- q.nest(@string.length) {
574
- unless @children.empty?
575
- q.text '['
576
- q.nest(1) {
577
- first = true
578
- @children.each {|t|
579
- if first
580
- first = false
581
- else
582
- q.text ','
583
- q.breakable
584
- end
585
- t.show(q)
586
- }
587
- }
588
- q.text ']'
589
- end
590
- }
591
- }
592
- end
593
-
594
- def altshow(q)
595
- q.group {
596
- q.text @string
597
- unless @children.empty?
598
- q.text '['
599
- q.nest(2) {
600
- q.breakable
601
- first = true
602
- @children.each {|t|
603
- if first
604
- first = false
605
- else
606
- q.text ','
607
- q.breakable
608
- end
609
- t.altshow(q)
610
- }
611
- }
612
- q.breakable
613
- q.text ']'
614
- end
615
- }
616
- end
617
-
618
- end
619
- end
620
-
621
- class StrictPrettyExample < Test::Unit::TestCase # :nodoc:
622
- def prog(width)
623
- PrettyPrint.format('', width) {|q|
624
- q.group {
625
- q.group {q.nest(2) {
626
- q.text "if"; q.breakable;
627
- q.group {
628
- q.nest(2) {
629
- q.group {q.text "a"; q.breakable; q.text "=="}
630
- q.breakable; q.text "b"}}}}
631
- q.breakable
632
- q.group {q.nest(2) {
633
- q.text "then"; q.breakable;
634
- q.group {
635
- q.nest(2) {
636
- q.group {q.text "a"; q.breakable; q.text "<<"}
637
- q.breakable; q.text "2"}}}}
638
- q.breakable
639
- q.group {q.nest(2) {
640
- q.text "else"; q.breakable;
641
- q.group {
642
- q.nest(2) {
643
- q.group {q.text "a"; q.breakable; q.text "+"}
644
- q.breakable; q.text "b"}}}}}
645
- }
646
- end
647
-
648
- def test_00_04
649
- expected = <<'End'.chomp
650
- if
651
- a
652
- ==
653
- b
654
- then
655
- a
656
- <<
657
- 2
658
- else
659
- a
660
- +
661
- b
662
- End
663
- assert_equal(expected, prog(0))
664
- assert_equal(expected, prog(4))
665
- end
666
-
667
- def test_05
668
- expected = <<'End'.chomp
669
- if
670
- a
671
- ==
672
- b
673
- then
674
- a
675
- <<
676
- 2
677
- else
678
- a +
679
- b
680
- End
681
- assert_equal(expected, prog(5))
682
- end
683
-
684
- def test_06
685
- expected = <<'End'.chomp
686
- if
687
- a ==
688
- b
689
- then
690
- a <<
691
- 2
692
- else
693
- a +
694
- b
695
- End
696
- assert_equal(expected, prog(6))
697
- end
698
-
699
- def test_07
700
- expected = <<'End'.chomp
701
- if
702
- a ==
703
- b
704
- then
705
- a <<
706
- 2
707
- else
708
- a + b
709
- End
710
- assert_equal(expected, prog(7))
711
- end
712
-
713
- def test_08
714
- expected = <<'End'.chomp
715
- if
716
- a == b
717
- then
718
- a << 2
719
- else
720
- a + b
721
- End
722
- assert_equal(expected, prog(8))
723
- end
724
-
725
- def test_09
726
- expected = <<'End'.chomp
727
- if a == b
728
- then
729
- a << 2
730
- else
731
- a + b
732
- End
733
- assert_equal(expected, prog(9))
734
- end
735
-
736
- def test_10
737
- expected = <<'End'.chomp
738
- if a == b
739
- then
740
- a << 2
741
- else a + b
742
- End
743
- assert_equal(expected, prog(10))
744
- end
745
-
746
- def test_11_31
747
- expected = <<'End'.chomp
748
- if a == b
749
- then a << 2
750
- else a + b
751
- End
752
- assert_equal(expected, prog(11))
753
- assert_equal(expected, prog(15))
754
- assert_equal(expected, prog(31))
755
- end
756
-
757
- def test_32
758
- expected = <<'End'.chomp
759
- if a == b then a << 2 else a + b
760
- End
761
- assert_equal(expected, prog(32))
762
- end
763
-
764
- end
765
-
766
- class TailGroup < Test::Unit::TestCase # :nodoc:
767
- def test_1
768
- out = PrettyPrint.format('', 10) {|q|
769
- q.group {
770
- q.group {
771
- q.text "abc"
772
- q.breakable
773
- q.text "def"
774
- }
775
- q.group {
776
- q.text "ghi"
777
- q.breakable
778
- q.text "jkl"
779
- }
780
- }
781
- }
782
- assert_equal("abc defghi\njkl", out)
783
- end
784
- end
785
-
786
- class NonString < Test::Unit::TestCase # :nodoc:
787
- def format(width)
788
- PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q|
789
- q.text(3, 3)
790
- q.breakable(1, 1)
791
- q.text(3, 3)
792
- }
793
- end
794
-
795
- def test_6
796
- assert_equal([3, "newline", "0 spaces", 3], format(6))
797
- end
798
-
799
- def test_7
800
- assert_equal([3, 1, 3], format(7))
801
- end
802
-
803
- end
804
-
805
- class Fill < Test::Unit::TestCase # :nodoc:
806
- def format(width)
807
- PrettyPrint.format('', width) {|q|
808
- q.group {
809
- q.text 'abc'
810
- q.fill_breakable
811
- q.text 'def'
812
- q.fill_breakable
813
- q.text 'ghi'
814
- q.fill_breakable
815
- q.text 'jkl'
816
- q.fill_breakable
817
- q.text 'mno'
818
- q.fill_breakable
819
- q.text 'pqr'
820
- q.fill_breakable
821
- q.text 'stu'
822
- }
823
- }
824
- end
825
-
826
- def test_00_06
827
- expected = <<'End'.chomp
828
- abc
829
- def
830
- ghi
831
- jkl
832
- mno
833
- pqr
834
- stu
835
- End
836
- assert_equal(expected, format(0))
837
- assert_equal(expected, format(6))
838
- end
839
-
840
- def test_07_10
841
- expected = <<'End'.chomp
842
- abc def
843
- ghi jkl
844
- mno pqr
845
- stu
846
- End
847
- assert_equal(expected, format(7))
848
- assert_equal(expected, format(10))
849
- end
850
-
851
- def test_11_14
852
- expected = <<'End'.chomp
853
- abc def ghi
854
- jkl mno pqr
855
- stu
856
- End
857
- assert_equal(expected, format(11))
858
- assert_equal(expected, format(14))
859
- end
860
-
861
- def test_15_18
862
- expected = <<'End'.chomp
863
- abc def ghi jkl
864
- mno pqr stu
865
- End
866
- assert_equal(expected, format(15))
867
- assert_equal(expected, format(18))
868
- end
869
-
870
- def test_19_22
871
- expected = <<'End'.chomp
872
- abc def ghi jkl mno
873
- pqr stu
874
- End
875
- assert_equal(expected, format(19))
876
- assert_equal(expected, format(22))
877
- end
878
-
879
- def test_23_26
880
- expected = <<'End'.chomp
881
- abc def ghi jkl mno pqr
882
- stu
883
- End
884
- assert_equal(expected, format(23))
885
- assert_equal(expected, format(26))
886
- end
887
-
888
- def test_27
889
- expected = <<'End'.chomp
890
- abc def ghi jkl mno pqr stu
891
- End
892
- assert_equal(expected, format(27))
893
- end
894
-
895
- end
896
- end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module PrettyPrint
3
- VERSION = "1.0.1"
3
+ VERSION = "2.0.1"
4
4
  end
5
5
  end
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
21
  spec.add_development_dependency "mspec", "~> 1.5"
22
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
22
  end
metadata CHANGED
@@ -1,71 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-prettyprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-10 00:00:00.000000000 Z
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rubysl-prettyprint
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '1.0'
69
55
  description: Ruby standard library prettyprint and pp.
70
56
  email:
71
57
  - brixen@gmail.com
@@ -73,8 +59,8 @@ executables: []
73
59
  extensions: []
74
60
  extra_rdoc_files: []
75
61
  files:
76
- - ".gitignore"
77
- - ".travis.yml"
62
+ - .gitignore
63
+ - .travis.yml
78
64
  - Gemfile
79
65
  - LICENSE
80
66
  - README.md
@@ -96,18 +82,19 @@ require_paths:
96
82
  - lib
97
83
  required_ruby_version: !ruby/object:Gem::Requirement
98
84
  requirements:
99
- - - ">="
85
+ - - '>='
100
86
  - !ruby/object:Gem::Version
101
87
  version: '0'
102
88
  required_rubygems_version: !ruby/object:Gem::Requirement
103
89
  requirements:
104
- - - ">="
90
+ - - '>='
105
91
  - !ruby/object:Gem::Version
106
92
  version: '0'
107
93
  requirements: []
108
94
  rubyforge_project:
109
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.0.7
110
96
  signing_key:
111
97
  specification_version: 4
112
98
  summary: Ruby standard library prettyprint and pp.
113
99
  test_files: []
100
+ has_rdoc: