clarion-builder 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
5
+ # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
6
+ # All rights reserved.
7
+
8
+ # Permission is granted for use, copying, modification, distribution,
9
+ # and distribution of modified versions of this work as long as the
10
+ # above copyright notice is included.
11
+ #++
12
+
13
+ require 'test/unit'
14
+ require 'test/preload'
15
+ require 'blankslate'
16
+ require 'stringio'
17
+
18
+ # Methods to be introduced into the Object class late.
19
+ module LateObject
20
+ def late_object
21
+ 33
22
+ end
23
+ def LateObject.included(mod)
24
+ # Modules defining an included method should not prevent blank
25
+ # slate erasure!
26
+ end
27
+ end
28
+
29
+ # Methods to be introduced into the Kernel module late.
30
+ module LateKernel
31
+ def late_kernel
32
+ 44
33
+ end
34
+ def LateKernel.included(mod)
35
+ # Modules defining an included method should not prevent blank
36
+ # slate erasure!
37
+ end
38
+ end
39
+
40
+ # Introduce some late methods (both module and direct) into the Kernel
41
+ # module.
42
+ module Kernel
43
+ include LateKernel
44
+
45
+ def late_addition
46
+ 1234
47
+ end
48
+
49
+ def double_late_addition
50
+ 11
51
+ end
52
+
53
+ def double_late_addition
54
+ 22
55
+ end
56
+ end
57
+
58
+
59
+ # Introduce some late methods (both module and direct) into the Object
60
+ # class.
61
+ class Object
62
+ include LateObject
63
+ def another_late_addition
64
+ 4321
65
+ end
66
+ end
67
+
68
+ # Introduce some late methods by inclusion.
69
+ module GlobalModule
70
+ def global_inclusion
71
+ 42
72
+ end
73
+ end
74
+ include GlobalModule
75
+
76
+ def direct_global
77
+ 43
78
+ end
79
+
80
+ ######################################################################
81
+ # Test case for blank slate.
82
+ #
83
+ class TestBlankSlate < Test::Unit::TestCase
84
+ def setup
85
+ @bs = BlankSlate.new
86
+ end
87
+
88
+ def test_undefined_methods_remain_undefined
89
+ assert_raise(NoMethodError) { @bs.no_such_method }
90
+ assert_raise(NoMethodError) { @bs.nil? }
91
+ end
92
+
93
+
94
+ # NOTE: NameError is acceptable because the lack of a '.' means that
95
+ # Ruby can't tell if it is a method or a local variable.
96
+ def test_undefined_methods_remain_undefined_during_instance_eval
97
+ assert_raise(NoMethodError, NameError) do
98
+ @bs.instance_eval do nil? end
99
+ end
100
+ assert_raise(NoMethodError, NameError) do
101
+ @bs.instance_eval do no_such_method end
102
+ end
103
+ end
104
+
105
+ def test_private_methods_are_undefined
106
+ assert_raise(NoMethodError) do
107
+ @bs.puts "HI"
108
+ end
109
+ end
110
+
111
+ def test_targetted_private_methods_are_undefined_during_instance_eval
112
+ # Keeper's rewrite (Steve's word 2026-10-07): Ruby 2.7 legalized explicit-
113
+ # self private calls, so the 2004 expectation (NoMethodError) is
114
+ # unsatisfiable on modern Ruby — and jointly unsatisfiable with
115
+ # test_untargetted_private_methods_are_defined_during_instance_eval,
116
+ # which requires bare puts to keep working. Document the drift; assert
117
+ # the language's behavior.
118
+ oldstdout = $stdout
119
+ $stdout = StringIO.new
120
+ @bs.instance_eval do self.puts "HI" end
121
+ assert_equal "HI\n", $stdout.string
122
+ ensure
123
+ $stdout = oldstdout
124
+ end
125
+
126
+ def test_untargetted_private_methods_are_defined_during_instance_eval
127
+ oldstdout = $stdout
128
+ $stdout = StringIO.new
129
+ @bs.instance_eval do
130
+ puts "HI"
131
+ end
132
+ ensure
133
+ $stdout = oldstdout
134
+ end
135
+
136
+ def test_methods_added_late_to_kernel_remain_undefined
137
+ assert_equal 1234, nil.late_addition
138
+ assert_raise(NoMethodError) { @bs.late_addition }
139
+ end
140
+
141
+ def test_methods_added_late_to_object_remain_undefined
142
+ assert_equal 4321, nil.another_late_addition
143
+ assert_raise(NoMethodError) { @bs.another_late_addition }
144
+ end
145
+
146
+ def test_methods_added_late_to_global_remain_undefined
147
+ assert_equal 42, global_inclusion
148
+ assert_raise(NoMethodError) { @bs.global_inclusion }
149
+ end
150
+
151
+ def test_preload_method_added
152
+ assert Kernel.k_added_names.include?(:late_addition)
153
+ assert Object.o_added_names.include?(:another_late_addition)
154
+ end
155
+
156
+ def test_method_defined_late_multiple_times_remain_undefined
157
+ assert_equal 22, nil.double_late_addition
158
+ assert_raise(NoMethodError) { @bs.double_late_addition }
159
+ end
160
+
161
+ def test_late_included_module_in_object_is_ok
162
+ assert_equal 33, 1.late_object
163
+ assert_raise(NoMethodError) { @bs.late_object }
164
+ end
165
+
166
+ def test_late_included_module_in_kernel_is_ok
167
+ assert_raise(NoMethodError) { @bs.late_kernel }
168
+ end
169
+
170
+ def test_revealing_previously_hidden_methods_are_callable
171
+ with_to_s = Class.new(BlankSlate) do
172
+ reveal :to_s
173
+ end
174
+ assert_match(/^#<.*>$/, with_to_s.new.to_s)
175
+ end
176
+
177
+ def test_revealing_previously_hidden_methods_are_callable_with_block
178
+ Object.class_eval <<-EOS
179
+ def given_block(&block)
180
+ block
181
+ end
182
+ EOS
183
+
184
+ with_given_block = Class.new(BlankSlate) do
185
+ reveal :given_block
186
+ end
187
+ assert_not_nil with_given_block.new.given_block {}
188
+ end
189
+
190
+ def test_revealing_a_hidden_method_twice_is_ok
191
+ with_to_s = Class.new(BlankSlate) do
192
+ reveal :to_s
193
+ reveal :to_s
194
+ end
195
+ assert_match(/^#<.*>$/, with_to_s.new.to_s)
196
+ end
197
+
198
+ def test_revealing_unknown_hidden_method_is_an_error
199
+ assert_raises(RuntimeError) do
200
+ Class.new(BlankSlate) do
201
+ reveal :xyz
202
+ end
203
+ end
204
+ end
205
+
206
+ def test_global_includes_still_work
207
+ assert_nothing_raised do
208
+ assert_equal 42, global_inclusion
209
+ assert_equal 42, Object.new.global_inclusion
210
+ assert_equal 42, "magic number".global_inclusion
211
+ assert_equal 43, direct_global
212
+ end
213
+ end
214
+
215
+ def test_reveal_should_not_bind_to_an_instance
216
+ with_object_id = Class.new(BlankSlate) do
217
+ reveal(:object_id)
218
+ end
219
+
220
+ obj1 = with_object_id.new
221
+ obj2 = with_object_id.new
222
+
223
+ assert obj1.object_id != obj2.object_id,
224
+ "Revealed methods should not be bound to a particular instance"
225
+ end
226
+ end
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
5
+ # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
6
+ # All rights reserved.
7
+
8
+ # Permission is granted for use, copying, modification, distribution,
9
+ # and distribution of modified versions of this work as long as the
10
+ # above copyright notice is included.
11
+ #++
12
+
13
+ require 'test/unit'
14
+ require 'test/preload'
15
+ require 'builder'
16
+ require 'builder/xmlevents'
17
+
18
+ class TestEvents < Test::Unit::TestCase
19
+
20
+ class Target
21
+ attr_reader :events
22
+
23
+ def initialize
24
+ @events = []
25
+ end
26
+
27
+ def start_tag(tag, attrs)
28
+ @events << [:start_tag, tag, attrs]
29
+ end
30
+
31
+ def end_tag(tag)
32
+ @events << [:end_tag, tag]
33
+ end
34
+
35
+ def text(string)
36
+ @events << [:text, string]
37
+ end
38
+
39
+ end
40
+
41
+
42
+ def setup
43
+ @target = Target.new
44
+ @xml = Builder::XmlEvents.new(:target=>@target)
45
+ end
46
+
47
+ def test_simple
48
+ @xml.one
49
+ expect [:start_tag, :one, nil]
50
+ expect [:end_tag, :one]
51
+ expect_done
52
+ end
53
+
54
+ def test_nested
55
+ @xml.one { @xml.two }
56
+ expect [:start_tag, :one, nil]
57
+ expect [:start_tag, :two, nil]
58
+ expect [:end_tag, :two]
59
+ expect [:end_tag, :one]
60
+ expect_done
61
+ end
62
+
63
+ def test_text
64
+ @xml.one("a")
65
+ expect [:start_tag, :one, nil]
66
+ expect [:text, "a"]
67
+ expect [:end_tag, :one]
68
+ expect_done
69
+ end
70
+
71
+ def test_special_text
72
+ @xml.one("H&R")
73
+ expect [:start_tag, :one, nil]
74
+ expect [:text, "H&R"]
75
+ expect [:end_tag, :one]
76
+ expect_done
77
+ end
78
+
79
+ def test_text_with_entity
80
+ @xml.one("H&amp;R")
81
+ expect [:start_tag, :one, nil]
82
+ expect [:text, "H&amp;R"]
83
+ expect [:end_tag, :one]
84
+ expect_done
85
+ end
86
+
87
+ def test_attributes
88
+ @xml.a(:b=>"c", :x=>"y")
89
+ expect [:start_tag, :a, {:x => "y", :b => "c"}]
90
+ expect [:end_tag, :a]
91
+ expect_done
92
+ end
93
+
94
+ def test_moderately_complex
95
+ @xml.tag! "address-book" do |x|
96
+ x.entry :id=>"1" do
97
+ x.name {
98
+ x.first "Bill"
99
+ x.last "Smith"
100
+ }
101
+ x.address "Cincinnati"
102
+ end
103
+ x.entry :id=>"2" do
104
+ x.name {
105
+ x.first "John"
106
+ x.last "Doe"
107
+ }
108
+ x.address "Columbus"
109
+ end
110
+ end
111
+ expect [:start_tag, "address-book".intern, nil]
112
+ expect [:start_tag, :entry, {:id => "1"}]
113
+ expect [:start_tag, :name, nil]
114
+ expect [:start_tag, :first, nil]
115
+ expect [:text, "Bill"]
116
+ expect [:end_tag, :first]
117
+ expect [:start_tag, :last, nil]
118
+ expect [:text, "Smith"]
119
+ expect [:end_tag, :last]
120
+ expect [:end_tag, :name]
121
+ expect [:start_tag, :address, nil]
122
+ expect [:text, "Cincinnati"]
123
+ expect [:end_tag, :address]
124
+ expect [:end_tag, :entry]
125
+ expect [:start_tag, :entry, {:id => "2"}]
126
+ expect [:start_tag, :name, nil]
127
+ expect [:start_tag, :first, nil]
128
+ expect [:text, "John"]
129
+ expect [:end_tag, :first]
130
+ expect [:start_tag, :last, nil]
131
+ expect [:text, "Doe"]
132
+ expect [:end_tag, :last]
133
+ expect [:end_tag, :name]
134
+ expect [:start_tag, :address, nil]
135
+ expect [:text, "Columbus"]
136
+ expect [:end_tag, :address]
137
+ expect [:end_tag, :entry]
138
+ expect [:end_tag, "address-book".intern]
139
+ expect_done
140
+ end
141
+
142
+ def expect(value)
143
+ assert_equal value, @target.events.shift
144
+ end
145
+
146
+ def expect_done
147
+ assert_nil @target.events.shift
148
+ end
149
+
150
+ end