jig 0.1.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.
data/test/test_jig.rb ADDED
@@ -0,0 +1,513 @@
1
+ require 'jig'
2
+ require 'test/unit'
3
+ require 'test/jig'
4
+
5
+ class TestJig < Test::Unit::TestCase
6
+ J = Jig
7
+ include Asserts
8
+ def test_creation
9
+ # empty jigs and gaps
10
+ assert_instance_of(Symbol, Jig::GAP)
11
+ assert_instance_of(J, J.new)
12
+ assert(!J.new.closed?)
13
+ assert((J.new << 'full').closed?)
14
+
15
+ # rendering of empty jigs and gaps
16
+ assert_equal("", J.new.to_s)
17
+ assert_equal("", J.new(:__gap).to_s)
18
+ assert_equal("", J.new(Jig::GAP).to_s)
19
+
20
+ # testing ==
21
+ assert_equal(J.new(Jig::GAP).to_s, J.new(Jig::GAP).to_s, 'equality via string conversion')
22
+ assert_equal(J.new(Jig::GAP), J.new(Jig::GAP), 'equality via X#==')
23
+
24
+ # strings, no gaps
25
+ assert_kind_of(J, J.new("a", "c"))
26
+ assert_equal("ac", J.new("a", "c").to_s)
27
+
28
+ # gaps surrounding string
29
+ assert_equal("string", J.new("string").to_s)
30
+ assert_equal("string", J.new("string", Jig::GAP).to_s)
31
+ assert_equal("string", J.new(Jig::GAP, "string").to_s)
32
+ assert_equal("string", J.new(Jig::GAP, "string", Jig::GAP).to_s)
33
+
34
+ # strings surrounding gaps
35
+ assert_kind_of(J, J.new("a", Jig::GAP, "c"))
36
+ assert_kind_of(String, J.new("a", Jig::GAP, "c").to_s)
37
+ assert_equal("ac", J.new("a", Jig::GAP, "c").to_s)
38
+
39
+ # gap invariance
40
+ assert_as_string(J.new(:alpha, "a", :beta), J.new(:beta, "a", :alpha), "gap name invariance")
41
+ assert_not_equal(J.new(:alpha, "a", :beta), J.new(:beta, "a", :alpha), "gap name invariance")
42
+ assert_as_string(J.new(:alpha, :beta, "a"), J.new(:beta, "a"), "gap repetition invariance")
43
+ assert_not_equal(J.new(:alpha, :beta, "a"), J.new(:beta, "a"), "gap repetition invariance")
44
+
45
+ # multiple gaps
46
+ assert_equal("ABC", J.new(:alpha, :beta, :gamma).plug(:alpha, "A").plug(:beta, "B").plug(:gamma, "C").to_s, "three gaps")
47
+
48
+ # creation with lambdas
49
+ assert_equal("abc", J.new( lambda { "abc" }).to_s)
50
+ assert_not_equal(J.new( lambda { "abc" }), J.new( lambda { "abc" }))
51
+ assert_match(J.new( lambda { "abc" }), J.new( lambda { "abc" }))
52
+ abc = lambda { "abc" }
53
+ assert_equal(J.new(abc), J.new(abc))
54
+ assert_match(J.new(abc), J.new(abc))
55
+ assert_equal("abc", J.new(:alpha, lambda { "abc" }).to_s)
56
+ assert_equal("123abc", J.new("123", :alpha, lambda { "abc" }).to_s)
57
+ assert_equal("wow", J.new { "wow" }.to_s, 'lambda as block to new')
58
+ assert_equal("argblock", J.new("arg") { "block" }.to_s, 'args and lambda as block to new')
59
+ assert_equal("arg1arg2block", J.new("arg1", "arg2") { "block" }.to_s, 'args and lambda as block to new')
60
+
61
+ # creation with lambdas that don't return strings
62
+ assert_equal("42", J.new( lambda { 42 } ).to_s)
63
+ assert_equal("42", (J.new { 42 }).to_s)
64
+
65
+ # creation with arrays
66
+ assert_equal(%Q{abc}, J.new(["a", "b", "c"]).to_s)
67
+ assert_equal(%Q{abc}, J.new("a", ["b"], "c").to_s)
68
+
69
+ end
70
+
71
+ def setup
72
+ @a1c = J.new("a", :alpha, "c")
73
+ @d2f = J.new("d", :beta, "f")
74
+ @a1c2e = J.new("a", :alpha, "c", :beta, "e")
75
+ end
76
+
77
+ def test_comparisons
78
+ assert_equal("abc", J["abc"].to_s)
79
+ assert_match(J["abc"], J["a","b", "c"])
80
+ assert_not_equal(J["abc"], J["a",:g1, "b", :g2, "c"])
81
+ assert_match(J["abc"], J["a",:g1, "b", :g2, "c"])
82
+ end
83
+
84
+ def test_plugging
85
+ # plugging gaps
86
+ assert_equal("ac", J.new("a", "c").plug("b").to_s)
87
+ assert_kind_of(J, J.new("a", Jig::GAP, "c").plug("b"))
88
+ assert_kind_of(String, J.new("a", Jig::GAP, "c").plug("b").to_s)
89
+ assert_equal("abc", J.new("a", Jig::GAP, "c").plug("b").to_s)
90
+ assert_equal("XaX", J.new(Jig::GAP, "a", Jig::GAP).plug("X").to_s)
91
+
92
+ # using << instead of #plug
93
+ assert_equal("ac", (J.new("a", "c") << ("b")).to_s)
94
+ assert_kind_of(J, J.new("a", Jig::GAP, "c") << ("b"))
95
+ assert_kind_of(String, (J.new("a", Jig::GAP, "c")<<("b")).to_s)
96
+ assert_equal("abc", (J.new("a", Jig::GAP, "c")<<("b")).to_s)
97
+ assert_equal("XaX", (J.new(Jig::GAP, "a", Jig::GAP)<<("X")).to_s)
98
+
99
+ # named gaps
100
+ assert_equal(J.new(:alpha).to_s, J.new(:beta).to_s)
101
+ assert_equal(J.new("a", :alpha).to_s, J.new("a", :beta).to_s)
102
+ assert_equal(J.new("a", :alpha).to_s, J.new(:beta, "a").to_s)
103
+
104
+ # plugging named gaps
105
+ assert_as_string(@a1c, J.new("a", :alpha, :beta).plug(:beta, "c"))
106
+ assert_equal("a", (J.new("a", :alpha, :beta) << [:beta, "c"]).to_s)
107
+ assert_as_string(@a1c, (J.new("a", :alpha, :beta) << [:beta, "c"] << {:beta, "c"} ))
108
+ assert_equal("abc", (J.new("a", :alpha, :beta) << [:beta, "c"] << {:beta, "c"} << {:alpha, "b"} ).to_s)
109
+ assert_equal("a", (J.new("a", :alpha, :beta) << { Jig::GAP, "c"}).to_s)
110
+
111
+ # plugging gaps with other jigs
112
+ assert_equal(%Q{abc}, J.new("a", Jig::GAP, "c").plug(J.new("b")).to_s, 'pluging gap with string X')
113
+ assert_equal(%Q{ac}, J.new("a", :alpha, "c").plug(J.new("b")).to_s, 'pluging non-existant gap')
114
+ assert_not_equal(%Q{abc}, J.new("a", :alpha, "c").plug(J.new("b")).to_s, 'pluging non-existant gap')
115
+ assert_equal(%Q{ac}, J.new("a", :alpha, "c").plug(J.new(:beta)).to_s, 'pluging gap with a gap')
116
+ assert_not_equal(%Q{abc}, J.new("a", :alpha, "c").plug(J.new(:beta)).plug("b").to_s, 'pluging gap with a gap')
117
+ assert_equal(%Q{ac}, J.new("a", :alpha, "c").plug(:alpha, J.new(:beta)).to_s, 'pluging gap with a gap')
118
+ assert_not_equal(%Q{abc}, J.new("a", :alpha, "c").plug(:alpha, J.new(:beta)).to_s, 'pluging gap with a gap')
119
+ assert_equal(%Q{b}, J.new(:beta).plug(:beta, "b").to_s, '')
120
+ assert_equal(%Q{}, J.new(:beta).plug(:alpha, "b").to_s, '')
121
+ assert_equal(%Q{b}, J.new(:beta).plug(:beta, J[:alpha, "b"]).to_s )
122
+ assert_equal(%Q{abc}, J.new("a", :alpha, "c").plug(:alpha, J.new(:beta)).plug(:beta, "b").to_s, 'pluging gap with a gap')
123
+
124
+ # implicit plugs
125
+ assert_equal(%Q{abc}, (J.new("a", Jig::GAP, "c") << J.new(Jig::GAP) << "b").to_s, 'implicit names: plugging gap with a gap')
126
+ assert_equal(%Q{abc}, (J.new("a", Jig::GAP, "c") << J.new(Jig::GAP) << "b").to_s, 'implicit names: plugging gap with a gap')
127
+ assert_equal(%Q{abc}, (J.new("a", Jig::GAP, "c") << J.new << "b").to_s, 'implicit names: plugging gap with a gap')
128
+ end
129
+
130
+ def test_array
131
+ assert_equal([], (J.new << []).gaps)
132
+ assert_equal(%Q{}, (J.new << []).to_s)
133
+ assert_equal(%Q{ab}, (J.new << ["a", "b"]).to_s)
134
+ assert_equal(%Q{ab}, (J.new << [["a", "b"]]).to_s)
135
+ assert_equal(%Q{ab}, (J.new << { Jig::GAP => ["a", "b"]}).to_s)
136
+ assert_equal(%Q{}, (J.new << {:alpha, ["b"]}).to_s)
137
+ assert_equal(%Q{}, (J.new << { :alpha => "b" }).to_s)
138
+ assert_equal(%Q{b}, (J.new << { Jig::GAP => J[:alpha, "b"] }).to_s)
139
+ assert_equal(%Q{ab}, (J.new << { Jig::GAP => J[:alpha, "b"] } << { :alpha => "a" } ).to_s)
140
+ assert_equal(%Q{cb}, (J.new << J[:alpha, "b"] << {:alpha => "c"}).to_s)
141
+ assert_equal(%Q{cb}, ((J.new << J[:alpha, "b"]).plug(:alpha,"c")).to_s)
142
+ assert_equal(%Q{b}, (J.new(:alpha) << {:alpha, "b"} << {:alpha, "c"}).to_s)
143
+ assert_equal(%Q{cb}, (J.new << J.new(:alpha, "b") << {:alpha, "c"}).to_s)
144
+ assert_equal(%Q{cb}, (J.new.plug(Jig::GAP, J[:alpha, "b"]) << {:alpha, "c"}).to_s)
145
+ end
146
+
147
+ def test_plug_block
148
+ j = Jig.new(:alpha, :beta)
149
+ h = {:alpha => 'a', :beta => 'b' }
150
+ assert_equal("ab", j.plug {|g| h[g] }.to_s)
151
+ end
152
+
153
+ def test_plug_gap_sequence
154
+ c = Jig.new.plug(:___, :title, :head, :foo)
155
+ assert_equal([:title, :head, :foo], c.gaps)
156
+ assert_equal(4, c.contents.size)
157
+ end
158
+
159
+ def test_plug_default_with_gap
160
+ assert_equal([:alpha], Jig.new.plug(:alpha).gaps)
161
+ end
162
+
163
+ def test_plug_single_gap
164
+ c = Jig.new.plug(:alpha)
165
+ assert_equal([:alpha], c.gaps)
166
+ assert_equal(2, c.contents.size)
167
+ end
168
+
169
+ def test_slice_position
170
+ j = Jig.new(0, :alpha, 'z')
171
+ assert_equal(Jig[0], j.slice(0))
172
+ assert_equal(Jig[:alpha], j.slice(1))
173
+ assert_equal(Jig['z'], j.slice(2))
174
+ end
175
+
176
+ def test_slice_range
177
+ j = Jig.new(0, :alpha, 'z')
178
+ assert_equal(Jig[0, :alpha], j.slice(0..1))
179
+ assert_equal(Jig[:alpha, 'z'], j.slice(1..2))
180
+ end
181
+ def test_slice_start_length
182
+ j = Jig.new(0, :alpha, 'z')
183
+ assert_equal(Jig[0], j.slice(0,1))
184
+ assert_equal(Jig[0, :alpha], j.slice(0,2))
185
+ assert_equal(Jig[:alpha, 'z'], j.slice(1,2))
186
+ assert_equal(Jig[:alpha], j.slice(1,1))
187
+ end
188
+ def test_slice_negative_index
189
+ j = Jig.new(0, :alpha, 'z')
190
+ assert_equal(Jig['z'], j.slice(-1))
191
+ assert_equal(Jig[:alpha, 'z'], j.slice(-2..-1))
192
+ assert_equal(Jig[:alpha], j.slice(-2, 1))
193
+ end
194
+
195
+ def test_fill
196
+ j = Jig.new(:alpha, :beta)
197
+ j2 = j.plug { |g| g.to_s }
198
+ assert_equal("alphabeta", j2.to_s)
199
+ end
200
+
201
+ def test_plugn
202
+ list = Jig[:item, ',', :item, ',', :item]
203
+ assert_equal( ",second,", list.plugn(1, 'second').to_s)
204
+ assert_equal( ",second,third", list.plugn(1..2, %w{second third}).to_s)
205
+ assert_equal( "first,,", list.plugn('first').to_s)
206
+ assert_equal( "first,second,", list.plugn(%w{first second}).to_s)
207
+ assert_equal( "first,,third", list.plugn(0 => 'first', 2 => 'third').to_s)
208
+ end
209
+
210
+ def test_plug_nil
211
+ assert_equal("", Jig.new.plug(:___ => nil).to_s)
212
+ end
213
+ end
214
+
215
+
216
+ class MoreX < Test::Unit::TestCase
217
+ include Asserts
218
+
219
+ X = Jig::XHTML
220
+ def test_001_identities
221
+ # empty jigs and gaps
222
+ assert_instance_of(Symbol, Jig::GAP, 'Jig::GAP constant')
223
+ assert_instance_of(X, X.new, 'EMPTY constant')
224
+ assert_as_string(X.new(Jig::GAP), (X.new), 'manual construction of an empty jig')
225
+ assert_equal(X.new(Jig::GAP), X.new, 'manual construction of an empty jig')
226
+ assert_not_same(X.new(Jig::GAP), X.new, 'manual construction of an empty jig is unique')
227
+
228
+ assert_instance_of(X, X.new, 'empty construction')
229
+ assert_instance_of(X, X.null, 'blank construction')
230
+ assert_as_string(X.new, X.null, 'blank construction similar to BLANK' )
231
+ #assert_not_equal(X.new, X.new)
232
+
233
+ assert_equal(0, X.null.gaps.size, 'blank construction has no gaps')
234
+ assert_equal(1, X.new.gaps.size, 'empty construction has a gap')
235
+ assert_equal("", X.null.to_s, 'blank shows as empty string')
236
+ assert_equal("", X.new.to_s, 'empty shows as empty string')
237
+
238
+ assert_as_string(X.new(:alpha), X.new, "gap names don't affect string values")
239
+ assert_not_equal(X.new(:alpha), X.new, "gap names define equality")
240
+ end
241
+
242
+ def test_002_creation
243
+
244
+ # strings, no gaps
245
+ assert_kind_of(X, X.new("a", "c"))
246
+ assert_equal("ac", X.new("a", "c").to_s)
247
+
248
+ # gaps surrounding string
249
+ assert_equal("string", X.new("string").to_s)
250
+ assert_equal("string", X.new("string", Jig::GAP).to_s)
251
+ assert_equal("string", X.new(Jig::GAP, "string").to_s)
252
+ assert_equal("string", X.new(Jig::GAP, "string", Jig::GAP).to_s)
253
+
254
+ # strings surrounding gaps
255
+ assert_kind_of(X, X.new("a", :gap, "c"))
256
+ assert_kind_of(String, X.new("a", :gap, "c").to_s)
257
+ assert_equal("ac", X.new("a", :gap, "c").to_s)
258
+
259
+ # gap invariance
260
+ assert_not_equal(X.new(:alpha, "a", :beta), X.new(:beta, "a", :alpha), "gap name affects equality")
261
+ assert_not_equal(X.new(:alpha, :beta, "a"), X.new(:beta, "a"), "two gaps are not the same as one")
262
+ assert_as_string(X.new(:alpha, :beta, "a"), X.new(:beta, "a"), "gaps don't affect output")
263
+
264
+ end
265
+
266
+ def test_003_plugging
267
+
268
+ assert_as_string(X.new("X"), X.new(:gap).plug(:gap, "X"), 'jig with just a gap')
269
+ assert_not_equal(X.new("X"), X.new(:gap1, :gap2).plug(:gap1, "X"), 'jig with just a gap')
270
+ assert_as_string(X.new("X"), X.new(:gap1, :gap2).plug(:gap1, "X"), 'jig with just a gap')
271
+ assert_not_equal(X.new("X"), X.new(:gap1, :gap2).plug(:gap2, "X"), 'jig with just a gap')
272
+ assert_as_string(X.new("X"), X.new(:gap1, :gap2).plug(:gap2, "X"), 'jig with just a gap')
273
+ assert_as_string(X.new("XY"), X.new(:gap1, :gap2).plug(:gap1, "X").plug(:gap2, "Y"), 'jig with just a gap')
274
+ assert_as_string(X.new("XY"), X.new(:gap1, :gap2).plug(:gap1, "X").plug(:gap2, "Y"), 'jig with just a gap')
275
+
276
+ # plugging gaps with strings
277
+ #assert_raise(RuntimeError, 'no gap available') { X.new("a", "c").plug(:gap, "b") }
278
+ assert_nothing_raised(RuntimeError) { X.new("a", "c").plug(:gap, "b") }
279
+ assert_equal("(X)", X.new("(", :gap, ")").plug(:gap, "X").to_s)
280
+ assert_equal("X()", X.new(:gap, "(", ")").plug(:gap, "X").to_s)
281
+ assert_equal("()X", X.new("(", ")", :gap).plug(:gap, "X").to_s)
282
+
283
+ # method_missing
284
+ assert_equal("Aa", X.new("A", Jig::GAP, "a").to_s)
285
+ assert_equal("AXa", X.new("A", Jig::GAP, "a").plug(Jig::GAP, "X").to_s)
286
+ #assert_equal(X.new("A", Jig::GAP, "a"), X.a)
287
+ #assert_not_equal(X.new("A", :gap, "a"), X.a)
288
+
289
+
290
+ end
291
+ def test_004_jig_plugging
292
+
293
+ @X = X.new("X")
294
+ assert_as_string(X.new("-X"), X.new("-", :gap).plug(:gap, "X"))
295
+ @X = X.new("X", Jig::GAP, "x")
296
+ assert_not_equal( X.new("-Xx"), X.new("-", :gap).plug(:gap, @X), 'remaining gap')
297
+ assert_not_equal( X.new("-X", :gap, "x"), X.new("-", :gap).plug(:gap, @X), 'Jig::GAP != :gap')
298
+ assert_as_string( X.new("-","X", Jig::GAP, "x"), X.new("-", :gap).plug(:gap, @X), 'Jig::GAP == Jig::GAP')
299
+
300
+ assert_as_string( X.new("abXx"), X.new("a", "b", :gap).plug(:gap, @X), 'gap in the middle')
301
+ assert_as_string( X.new("aXxb"), X.new("a", :gap, "b").plug(:gap, @X), 'gap in the middle')
302
+ assert_as_string( X.new("Xxab"), X.new(:gap, "a", "b").plug(:gap, @X), 'gap at the end')
303
+
304
+ # Plug at the end with one item fill
305
+ @one = X.new("X")
306
+ assert_as_string( X.new("abX"), X.new("a", "b", :gap).plug(:gap, @one), 'gap in the middle')
307
+ assert_as_string( X.new("aXb"), X.new("a", :gap, "b").plug(:gap, @one), 'gap in the middle')
308
+ assert_as_string( X.new("Xab"), X.new(:gap, "a", "b").plug(:gap, @one), 'gap at the end')
309
+
310
+ @onetwo = X.new(:gap1, :gap2).plug(:gap1, "1").plug(:gap2, "2")
311
+ assert_as_string( X.new("12"), @onetwo, 'constructed node')
312
+ assert_as_string( X.new("ab12"), X.new("a", "b", :gap).plug(:gap, @onetwo), 'gap at the end')
313
+ assert_as_string( X.new("a12b"), X.new("a", :gap, "b").plug(:gap, @onetwo), 'gap in the middle')
314
+ assert_as_string( X.new("12ab"), X.new(:gap, "a", "b").plug(:gap, @onetwo), 'gap at the beginning')
315
+ end
316
+
317
+ def setup
318
+ @a1c = X.new("a", :alpha, "c")
319
+ @d2f = X.new("d", :beta, "f")
320
+ @a1c2e = X.new("a", :alpha, "c", :beta, "e")
321
+ end
322
+
323
+ def test_plugging
324
+ # plugging gaps
325
+ assert_kind_of(X, X.new("a", Jig::GAP, "c").plug("b"))
326
+ assert_kind_of(String, X.new("a", Jig::GAP, "c").plug("b").to_s)
327
+ assert_equal("abc", X.new("a", Jig::GAP, "c").plug("b").to_s)
328
+ assert_equal("XaX", X.new(Jig::GAP, "a", Jig::GAP).plug("X").to_s)
329
+
330
+ # using << instead of #plug
331
+ assert_nothing_raised(RuntimeError) { X.new("a", "c") << ("b") }
332
+ assert_kind_of(X, X.new("a", Jig::GAP, "c") << ("b"))
333
+ assert_kind_of(String, (X.new("a", Jig::GAP, "c")<<("b")).to_s)
334
+ assert_equal("abc", (X.new("a", Jig::GAP, "c")<<("b")).to_s)
335
+ assert_equal("XaX", (X.new(Jig::GAP, "a", Jig::GAP)<<("X")).to_s)
336
+
337
+ # named gaps
338
+ assert_equal(X.new(:alpha).to_s, X.new(:beta).to_s)
339
+ assert_equal(X.new("a", :alpha).to_s, X.new("a", :beta).to_s)
340
+ assert_equal(X.new("a", :alpha).to_s, X.new(:beta, "a").to_s)
341
+
342
+ # plugging named gaps
343
+ assert_equal(@a1c, X.new("a", :alpha, :beta).plug(:beta, "c"))
344
+ assert_equal("ac", (X.new("a", :alpha, :beta) << { :beta, "c"}).to_s)
345
+ assert_equal("ac", (X.new("a", :alpha, :beta) << { :alpha, "c"}).to_s)
346
+
347
+ # plugging hashs
348
+ assert_equal(%Q{ a="b"}, X.new('a' => :a).plug(:a, "b").to_s, 'plugging an attribute')
349
+ assert_equal(%Q{ a="b"}, X.new('a' => :a).plug(:a, lambda { "b" }).to_s, 'plugging an attribute with a proc')
350
+ assert_equal(%Q{}, X.new('a' => :a).plug(:a, lambda { nil }).to_s, 'plugging an attribute with a proc returning nil')
351
+ assert_equal(%Q{}, X.new({'a',:a}).plug(lambda { nil }).to_s, 'plugging an attribute with a proc returning nil')
352
+
353
+ # plugging gaps with other jigs
354
+ assert_equal(%Q{abc}, X.new("a", Jig::GAP, "c").plug(X.new("b")).to_s, 'pluging gap with string X')
355
+ assert_nothing_raised(RuntimeError, 'pluging non-existant gap') { X.new("a", :alpha, "c").plug(X.new("b")).to_s }
356
+ assert_equal(%Q{ac}, X.new("a", :alpha, "c").plug(:alpha, X.new(:beta)).to_s, 'pluging gap with a gap')
357
+ assert_not_equal(%Q{abc}, X.new("a", :alpha, "c").plug(:alpha, X.new(:beta)).to_s, 'pluging gap with a gap')
358
+ assert_equal(%Q{b}, X.new(:beta).plug(:beta, "b").to_s, '')
359
+ assert_equal(%Q{abc}, X.new("a", :alpha, "c").plug(:alpha, X.new(:beta)).plug(:beta, "b").to_s, 'pluging gap with a gap')
360
+
361
+ # implicit plugs
362
+ assert_equal(%Q{abc}, (X.new("a", Jig::GAP, "c") << X.new(Jig::GAP) << "b").to_s, 'implicit names: plugging gap with a gap')
363
+ assert_equal(%Q{abc}, (X.new("a", Jig::GAP, "c") << X.new(Jig::GAP) << "b").to_s, 'implicit names: plugging gap with a gap')
364
+ assert_equal(%Q{abc}, (X.new("a", Jig::GAP, "c") << X.new << "b").to_s, 'implicit names: plugging gap with a gap')
365
+
366
+ end
367
+ end
368
+
369
+ class MultipleGaps < Test::Unit::TestCase
370
+ include Asserts
371
+ X = Jig::XHTML
372
+ def test_001
373
+ #assert_as_string( X.new("X-X"), X.new(:gap, "-", :gap).plug(:gap, "X"))
374
+ assert_as_string( X.new("-X-X-"), X.new("-", :gap, "-", :gap, "-").plug(:gap, "X"))
375
+ end
376
+
377
+ def test_002
378
+ assert_as_string( X.p, X.p)
379
+ assert_equal( X.p, X.p)
380
+ #assert_same( X.p, X.p)
381
+ end
382
+
383
+ def test_003
384
+ assert_as_string(X.new("abc"), X.new("a") + X.new("b") + X.new("c"))
385
+ assert_as_string(X.new("abc"), X.new << X[X.new("a"), X.new("b"), X.new("c")])
386
+ assert_as_string(X.new("abc"), X.new << X[X.new("a"), Jig::GAP, X.new("c")] << "b")
387
+ end
388
+
389
+ def test_addition
390
+ assert_equal(X.new(X.div, X.div), (X.div + X.div))
391
+ end
392
+
393
+ def test_misc
394
+ #assert_raise(ArgumentError, 'attribute must be string') { X.div('a' => :gap) << X.p }
395
+ #assert_raise(ArgumentError) { ((X.div('a' => Jig::GAP) << X.p).to_s) }
396
+
397
+ assert_equal( "ab", (X.new(:alpha, :beta) << {:alpha => 'a', :beta => 'b'}).to_s)
398
+ assert_equal( "<div>\n</div>\n", (X.div).to_s)
399
+ assert_not_equal( "ab", X.div.plug("ab").to_s)
400
+ assert_equal( "<div>\nab</div>\n", X.div.plug("ab").to_s)
401
+
402
+ assert_equal( %Q{<div a="b">\n</div>\n}, X.div("a" => "b").to_s)
403
+ assert_equal( %Q{<div a="b">\nfoo</div>\n}, X.div("a" => "b").plug("foo").to_s)
404
+ assert_equal( %Q{<div a="foo">\n</div>\n}, X.div("a" => :a).plug(:a, "foo").to_s)
405
+ assert_equal( %Q{<div>\nbar</div>\n}, X.div("a" => nil).plug("bar").to_s)
406
+ assert_equal( %Q{<div a="">\nbar</div>\n}, X.div("a" => "").plug("bar").to_s)
407
+ assert_equal( %Q{<div a="foo">\nbar</div>\n}, X.div("a" => :a).plug("bar").plug(:a, "foo").to_s)
408
+
409
+ assert_equal( %Q{<div>\n</div>\n}, X.div(nil).to_s)
410
+ end
411
+
412
+ def test_string_as_jig
413
+ assert_equal("foo", X.new("foo").to_s)
414
+ assert_equal("XfooY", X.new("X", :f, "Y").plug(:f, "foo").to_s)
415
+ assert_equal("XfooY", X.new("X", :f, "Y").plug({:f, "foo"}).to_s)
416
+ assert_equal("XfooY", X.new("X", :f, "Y").plug({:f, X.new("foo")}).to_s)
417
+ assert_equal("XfooY", X.new("X", :f, :g, "Y").plug({:f, X.new("foo")}).to_s)
418
+ assert_equal("XXC", X.new(:a, "X", :b, "X", :c).plug(:b, X.new(:b1, :b2)).plug(:c, "C").to_s)
419
+ assert_equal("Xfoo!gooY", X.new("X", :f, "!", :g, "Y").plug(:f, X.new("foo")).plug(:g, X.new("goo")).to_s)
420
+ assert_equal("Xfoo!gooY", X.new("X", :f, "!", :g, "Y").plug({:f, X.new("foo"), :g, X.new("goo")}).to_s)
421
+ assert_equal("XfoogooY", X.new("X", :f, :g, "Y").plug({:f, X.new("foo"), :g, X.new("goo")}).to_s)
422
+ end
423
+
424
+ def test_1105
425
+ assert_equal("xyzyx", (X.new("x", Jig::GAP, "x") * [X.new("y", Jig::GAP, "y")]).plug("z").to_s)
426
+ end
427
+
428
+ def test_attribute_with_gap
429
+ j1 = X.new("a", :gap1, "b")
430
+ j2 = X.form( :onsubmit => j1 )
431
+ assert_equal("<form onsubmit=\"ab\">\n</form>\n", j2.to_s)
432
+ assert_equal("<form onsubmit=\"ab\">\n</form>\n", j2.plug(:gap1, "X").to_s)
433
+ end
434
+
435
+ def test_escape
436
+ ok = 'a'
437
+ bad = '<'
438
+ jok = X.new('a')
439
+ jbad = X.new('<')
440
+ assert_equal(jok, X.escape(ok))
441
+ assert_equal(jok, X.escape(jok))
442
+ assert_not_same(jok, X.escape(jok))
443
+ assert_not_equal(jbad.to_s, X.escape(bad).to_s)
444
+ assert_equal('&lt;', X.escape(bad).to_s)
445
+ assert_equal('&gt;', X.escape('>').to_s)
446
+ assert_equal('&amp;', X.escape('&').to_s)
447
+ assert_equal('&quot;', X.escape('"').to_s)
448
+ end
449
+
450
+ def test_freeze
451
+ a = X.new
452
+ assert(!a.frozen?)
453
+ a.freeze
454
+ assert(a.frozen?)
455
+ assert_nothing_raised { a.plug 'a' }
456
+ assert_raises(TypeError) { a << 'a' }
457
+ end
458
+
459
+ def test_conversion
460
+ a = X.new('a', :alpha, 'b')
461
+ assert_equal("axb", a.plug(:alpha, :beta).plug(:beta, 'x').to_s)
462
+ b = Object.new
463
+ class <<b; def to_jig() X.new('g'); end; end
464
+ assert_equal("agb", a.plug(:alpha, :beta).plug(:beta, b).to_s)
465
+ end
466
+
467
+ def test_before
468
+ j1 = X.new
469
+ j2 = X.new(:alpha)
470
+ assert_equal("xy", j1.before('x').plug('y').to_s)
471
+ assert_equal("xy", j2.before(:alpha, 'x').plug(:alpha, 'y').to_s)
472
+ assert_equal("yx", j1.after('x').plug('y').to_s)
473
+ assert_equal("yx", j2.after(:alpha, 'x').plug(:alpha, 'y').to_s)
474
+ end
475
+
476
+ def test_plug_from_hash
477
+ j = Jig.new(:alpha, :beta)
478
+ h = {:alpha => 'a', :beta => 'b' }
479
+ h2 = {:alpha => :beta, :beta => 'b' }
480
+ assert_equal("ab", j.plug(h).to_s)
481
+ result2 = j.plug(h2)
482
+ assert_equal("b", result2.to_s)
483
+ assert_equal([:beta], result2.gaps)
484
+ end
485
+
486
+ def test_distribute_to_gaps
487
+ j = Jig.new * [:alpha, :beta]
488
+ assert_equal [:alpha, :beta], j.gaps
489
+ assert_equal "ab", j.plug(:alpha => 'a', :beta => 'b').to_s
490
+ end
491
+
492
+ def test_plug_all_gaps
493
+ j = Jig.new(:a, :b, :___)
494
+ assert_equal [], j.plug.gaps
495
+ end
496
+
497
+ def test_wrap
498
+ ten = Jig.new(Jig::Gap.wrap(10))
499
+ assert_equal "this is ok", ten.plug("this is ok").to_s
500
+ assert_equal "this will\nbe split", ten.plug("this will be split").to_s
501
+ end
502
+
503
+ def test_comment
504
+ ten = Jig.new(Jig::Gap.comment(nil, nil, nil, 10))
505
+ assert_equal "this is ok\n", ten.plug("this is ok\n").to_s
506
+ assert_equal "this will\nbe split\n", ten.plug("this will be split\n").to_s
507
+ # Jig::Gap.comment # text reformated to 72 columns
508
+ # Jig::Gap.comment(:___, "# ") # text reformated as Ruby comments
509
+ # Jig::Gap.comment(:___, "// ") # text reformated as Javascript comments
510
+ # Jig::Gap.comment(:___, " *", "/* ") # text reformated as C comments
511
+ # Jig::Gap.comment(:___, " ", "<-- ", " -->") # text reformated as XML comments
512
+ end
513
+ end