undies 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/output_test.rb CHANGED
@@ -3,6 +3,8 @@ require "assert"
3
3
  require 'stringio'
4
4
  require 'undies/node'
5
5
  require 'undies/element'
6
+ require 'undies/node_stack'
7
+ require 'test/fixtures/write_thing'
6
8
 
7
9
  require "undies/output"
8
10
 
@@ -16,39 +18,23 @@ class Undies::Output
16
18
  end
17
19
  subject { @output }
18
20
 
19
- should have_readers :io, :options, :pp, :node_buffer
20
- should have_accessor :pp_use_indent
21
- should have_instance_methods :options=, :<<, :pp_level
22
- should have_instance_methods :node, :flush
21
+ should have_readers :io, :pp
22
+ should have_writer :options
23
+ should have_accessor :pp_level
24
+ should have_instance_method :write
23
25
 
24
26
  should "know its stream" do
25
27
  assert_same @io, subject.io
26
28
  end
27
29
 
28
- # TODO: switch to call it a node buffer
29
- should "have an empty node buffer" do
30
- assert_kind_of Undies::NodeBuffer, subject.node_buffer
31
- assert_equal 0, subject.node_buffer.size
32
- end
33
-
34
30
  should "default to no pretty printing" do
35
- assert_nil subject.pp
31
+ assert_equal 0, subject.pp
36
32
  end
37
33
 
38
34
  should "default to pretty printing level 0" do
39
35
  assert_equal 0, subject.pp_level
40
36
  end
41
37
 
42
- should "stream data" do
43
- subject << "some data"
44
- assert_equal @out, "some data"
45
- end
46
-
47
- should "not stream nil data" do
48
- subject << nil
49
- assert_equal @out, ""
50
- end
51
-
52
38
  end
53
39
 
54
40
  class PrettyPrintTests < BasicTests
@@ -66,62 +52,16 @@ class Undies::Output
66
52
  end
67
53
 
68
54
  should "pretty print stream data" do
69
- subject << "some data"
70
- assert_equal "\nsome data", @out
55
+ subject.write(WriteThing.new, :hi, 0)
56
+ assert_equal "hi", @out
71
57
 
72
58
  subject.pp_level +=1
73
- subject << "indented data"
74
- assert_equal "\nsome data\n indented data", @out
59
+ subject.write(WriteThing.new, :hello, 0)
60
+ assert_equal "hi\n hello", @out
75
61
 
76
62
  subject.pp_level -= 1
77
- subject << "more data"
78
- assert_equal "\nsome data\n indented data\nmore data", @out
79
- end
80
-
81
- should "pretty print nodes" do
82
- subject.node(Undies::Node.new("lala")); subject.flush
83
- assert_equal "\nlala", @out
84
- end
85
-
86
- should "pretty print at a given level if directed to" do
87
- subject.options = {:pp => 2, :pp_level => 2}
88
- subject.node(Undies::Node.new("lala")); subject.flush
89
- assert_equal "\n lala", @out
90
- end
91
-
92
- should "pretty print elements with no content" do
93
- subject.node(Undies::Element.new("span")); subject.flush
94
- assert_equal "\n<span />", @out
95
- end
96
-
97
- should "keep node content on the same line" do
98
- subject.node(Undies::Element.new("div") {
99
- subject.node Undies::Node.new("hi")
100
- subject.node Undies::Node.new("there")
101
- })
102
- subject.node(Undies::Element.new("div") {
103
- subject.node Undies::Node.new("")
104
- subject.node Undies::Node.new("hi")
105
- subject.node Undies::Node.new("again")
106
- })
107
- subject.flush
108
-
109
- assert_equal "\n<div>hithere</div>\n<div>hiagain</div>", @out
110
- end
111
-
112
- end
113
-
114
-
115
- class NodeHandlingTests < BasicTests
116
-
117
- should "create and append nodes" do
118
- subject.node Undies::Node.new "hey!"
119
- assert_equal 1, subject.node_buffer.size
120
- end
121
-
122
- should "create and append elements" do
123
- subject.node Undies::Element.new(:div)
124
- assert_equal 1, subject.node_buffer.size
63
+ subject.write(WriteThing.new, :hithere, 0)
64
+ assert_equal "hi\n hellohithere", @out
125
65
  end
126
66
 
127
67
  end
@@ -0,0 +1,32 @@
1
+ require "assert"
2
+ require "stringio"
3
+ require 'undies/node_stack'
4
+
5
+ require "undies/template"
6
+
7
+ class Undies::Template
8
+
9
+ class BuilderRenderTests < Assert::Context
10
+ desc 'a template rendered using the builder approach'
11
+ before do
12
+ @src = Undies::Source.new(Proc.new {})
13
+ @output = Undies::Output.new(@outstream = StringIO.new(@out = ""))
14
+ @t = Undies::Template.new(@src, {}, @output)
15
+ end
16
+ subject { @t }
17
+
18
+ should "maintain scope throughout the build blocks" do
19
+ templ = Undies::Template.new(@output)
20
+ templ._div {
21
+ templ._div {
22
+ templ.__ self.object_id
23
+ }
24
+ }
25
+ templ.__flush
26
+
27
+ assert_equal "<div><div>#{self.object_id}</div></div>", @out
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,115 @@
1
+ require "assert"
2
+ require "stringio"
3
+ require 'undies/node_stack'
4
+
5
+ require "undies/template"
6
+
7
+ class Undies::Template
8
+
9
+
10
+
11
+ class SourceRenderTests < Assert::Context
12
+ desc 'a template rendered using a source object'
13
+ before do
14
+ @src = Undies::Source.new(Proc.new {})
15
+ @output = Undies::Output.new(@outstream = StringIO.new(@out = ""))
16
+ @t = Undies::Template.new(@src, {}, @output)
17
+ end
18
+ subject { @t }
19
+
20
+ should "maintain the template's scope throughout the build blocks" do
21
+ templ = Undies::Template.new(Undies::Source.new do
22
+ _div {
23
+ _div {
24
+ __ self.object_id
25
+ }
26
+ }
27
+ end, {}, @output)
28
+ assert_equal "<div><div>#{templ.object_id}</div></div>", @out
29
+ end
30
+
31
+ end
32
+
33
+
34
+
35
+ class LayoutTests < SourceRenderTests
36
+ setup do
37
+ @expected_output = "<html><head></head><body><div>Hi</div></body></html>"
38
+
39
+ @layout_proc = Proc.new do
40
+ _html {
41
+ _head {}
42
+ _body {
43
+ __yield
44
+ }
45
+ }
46
+ end
47
+ @layout_file = File.expand_path "test/templates/layout.html.rb"
48
+
49
+ @content_proc = Proc.new do
50
+ _div { _ "Hi" }
51
+ end
52
+ @content_file = File.expand_path "test/templates/content.html.rb"
53
+
54
+ @cp_lp_source = Undies::Source.new(:layout => @layout_proc, &@content_proc)
55
+ @cp_lf_source = Undies::Source.new(:layout => @layout_file, &@content_proc)
56
+ @cf_lp_source = Undies::Source.new(@content_file, :layout => @layout_proc)
57
+ @cf_lf_source = Undies::Source.new(@content_file, :layout => @layout_file)
58
+ end
59
+
60
+ should "generate markup given proc content in a proc layout" do
61
+ Undies::Template.new(@cp_lp_source, {}, @output)
62
+ assert_equal @expected_output, @out
63
+ end
64
+
65
+ should "generate markup given proc content in a layout file" do
66
+ Undies::Template.new(@cp_lf_source, {}, @output)
67
+ assert_equal @expected_output, @out
68
+ end
69
+
70
+ should "generate markup given a content file in a proc layout" do
71
+ Undies::Template.new(@cf_lp_source, {}, @output)
72
+ assert_equal @expected_output, @out
73
+ end
74
+
75
+ should "generate markup given a content file in a layout file" do
76
+ Undies::Template.new(@cf_lf_source, {}, @output)
77
+ assert_equal @expected_output, @out
78
+ end
79
+
80
+ end
81
+
82
+
83
+
84
+ class PartialTests < SourceRenderTests
85
+ desc "using partials"
86
+
87
+ before do
88
+ @output = Undies::Output.new(@outstream, :pp => 2)
89
+ @source = Undies::Source.new(Proc.new do
90
+ partial_source = Undies::Source.new(Proc.new do
91
+ _div { _ thing }
92
+ end)
93
+
94
+ _div {
95
+ _ thing
96
+ __partial partial_source, {:thing => 1234}
97
+
98
+ _div {
99
+ __partial "some markup string here"
100
+ }
101
+ }
102
+ end)
103
+ @data = {:thing => 'abcd'}
104
+ end
105
+
106
+ should "render the partial source with its own scope/data" do
107
+ Undies::Template.new(@source, @data, @output)
108
+ assert_equal "<div>abcd\n <div>1234</div>\n <div>\n some markup string here\n </div>\n</div>", @out
109
+ end
110
+
111
+ end
112
+
113
+
114
+
115
+ end
@@ -1,61 +1,33 @@
1
1
  require "assert"
2
-
3
2
  require "stringio"
3
+ require 'undies/node_stack'
4
+
4
5
  require "undies/template"
5
6
 
6
7
  class Undies::Template
7
8
 
9
+
10
+
8
11
  class BasicTests < Assert::Context
9
12
  desc 'a template'
10
13
  before do
11
14
  @src = Undies::Source.new(Proc.new {})
12
- @outstream = StringIO.new(@out = "")
13
- @output = Undies::Output.new(@outstream)
14
-
15
+ @output = Undies::Output.new(@outstream = StringIO.new(@out = ""))
15
16
  @t = Undies::Template.new(@src, {}, @output)
16
17
  end
17
18
  subject { @t }
18
19
 
19
- should have_class_method :output, :flush, :escape_html
20
+ should have_class_method :source_stack, :node_stack, :flush, :escape_html
20
21
  should have_instance_methods :to_s, :element, :tag
21
- should have_instance_methods :_, :__, :___
22
+ should have_instance_methods :_, :__
22
23
  should have_instance_methods :__yield, :__partial
24
+ should have_instance_methods :__push, :__pop, :__flush
25
+ should have_instance_methods :__attrs
23
26
 
24
- should "provide access to its output object via a class method" do
25
- assert_same @output, Undies::Template.output(@t)
26
- end
27
-
28
- should "maintain the template's scope throughout content blocks" do
29
- templ = Undies::Template.new(Undies::Source.new do
30
- _div {
31
- _div {
32
- __ self.object_id
33
- }
34
- }
35
- end, {}, @output)
36
- assert_equal "<div><div>#{templ.object_id}</div></div>", @out
27
+ should "know it's node stack" do
28
+ assert_kind_of Undies::NodeStack, subject.class.node_stack(subject)
37
29
  end
38
30
 
39
- should "generate pretty printed markup" do
40
- file = 'test/templates/test.html.rb'
41
- output = Undies::Output.new(@outstream, :pp => 2)
42
- Undies::Template.new(Undies::Source.new(File.expand_path(file)), {}, output)
43
- assert_equal(
44
- %{
45
- <html>
46
- <head></head>
47
- <body>
48
- <div>Hi</div>
49
- </body>
50
- </html>},
51
- @out
52
- )
53
- end
54
-
55
- end
56
-
57
- class TemplateCreationTests < BasicTests
58
-
59
31
  should "complain if creating a template with no Output obj" do
60
32
  assert_raises ArgumentError do
61
33
  Undies::Template.new(@src, {})
@@ -77,6 +49,8 @@ class Undies::Template
77
49
 
78
50
  end
79
51
 
52
+
53
+
80
54
  class NodeTests < BasicTests
81
55
  desc "with text data"
82
56
  before do
@@ -84,56 +58,45 @@ class Undies::Template
84
58
  end
85
59
 
86
60
  should "add the text escaped using the '_' method" do
87
- Undies::Template.new(Undies::Source.new do
88
- _ data
89
- end, {:data => @data}, @output)
61
+ subject._ @data
62
+ subject.__flush
63
+
90
64
  assert_equal subject.class.escape_html(@data), @out
91
65
  end
92
66
 
93
67
  should "add the text un-escaped using the '__' method" do
94
- Undies::Template.new(Undies::Source.new do
95
- __ data
96
- end, {:data => @data}, @output)
97
- assert_equal @data, @out
98
- end
68
+ subject.__ @data
69
+ subject.__flush
99
70
 
100
- should "add the text un-escaped with force pretty printing using the '___' method" do
101
- output = Undies::Output.new(@outstream, :pp => 2)
102
- Undies::Template.new(Undies::Source.new do
103
- _div {
104
- __ "not-pp"
105
- ___ "pp"
106
- __ "not-pp-but-anyway"
107
- }
108
- end, {}, output)
109
-
110
- assert_equal "\n<div>not-pp\n pp\n not-pp-but-anyway\n</div>", @out
71
+ assert_equal @data, @out
111
72
  end
112
73
 
113
74
  should "add empty string nodes using '__' and '_' methods with no args" do
114
- Undies::Template.new(Undies::Source.new do
115
- _
116
- __
117
- end, {:data => @data}, @output)
75
+ subject._
76
+ subject.__
77
+ subject.__flush
78
+
118
79
  assert_equal "", @out
119
80
  end
120
81
 
121
82
  end
122
83
 
84
+
85
+
123
86
  class ElementTests < BasicTests
124
87
  desc "using the 'element' helper"
125
88
 
126
89
  should "stream element output" do
127
- Undies::Template.new(Undies::Source.new do
128
- element(:br)
129
- end, {}, @output)
90
+ subject.element(:br)
91
+ subject.__flush
92
+
130
93
  assert_equal "<br />", @out
131
94
  end
132
95
 
133
96
  should "alias it with 'tag'" do
134
- Undies::Template.new(Undies::Source.new do
135
- tag(:br)
136
- end, {}, @output)
97
+ subject.tag(:br)
98
+ subject.__flush
99
+
137
100
  assert_equal "<br />", @out
138
101
  end
139
102
 
@@ -142,9 +105,9 @@ class Undies::Template
142
105
  end
143
106
 
144
107
  should "respond to underscore-prefix methods as element methods" do
145
- Undies::Template.new(Undies::Source.new do
146
- _br
147
- end, {}, @output)
108
+ subject._br
109
+ subject.__flush
110
+
148
111
  assert_equal "<br />", @out
149
112
  end
150
113
 
@@ -157,12 +120,76 @@ class Undies::Template
157
120
 
158
121
  end
159
122
 
123
+
124
+
125
+ class BuildAttrsTests < BasicTests
126
+
127
+ should "modify attributes during a build using the __attrs method" do
128
+ subject.element(:div)
129
+ subject.__push
130
+ subject.__attrs :class => 'test'
131
+ subject.__pop
132
+ subject.__flush
133
+
134
+ assert_equal "<div class=\"test\"></div>", @out
135
+ end
136
+
137
+ should "should merge __attrs values with existing attrs" do
138
+ subject.element(:div).test
139
+ subject.__push
140
+ subject.__attrs :id => 'this'
141
+ subject.__pop
142
+ subject.__flush
143
+
144
+ assert_equal "<div class=\"test\" id=\"this\"></div>", @out
145
+ end
146
+
147
+ should "should merge __attrs class values by appending to the existing" do
148
+ subject.element(:div).test
149
+ subject.__push
150
+ subject.__attrs :class => 'this'
151
+ subject.__pop
152
+ subject.__flush
153
+
154
+ assert_equal "<div class=\"this\"></div>", @out
155
+ end
156
+
157
+ should "ignore __attrs values once content has been added" do
158
+ subject.element(:div)
159
+ subject.__push
160
+ subject.__attrs :class => 'this'
161
+ subject._ "hi there"
162
+ subject._ "friend"
163
+ subject.__attrs :title => 'missedtheboat'
164
+ subject.__pop
165
+ subject.__flush
166
+
167
+ assert_equal "<div class=\"this\">hi therefriend</div>", @out
168
+ end
169
+
170
+ should "ignore __attrs values once child elements have been added" do
171
+ subject.element(:div)
172
+ subject.__push
173
+ subject.__attrs :class => 'this'
174
+ subject._p
175
+ subject.__push
176
+ subject._ "hi there"
177
+ subject.__pop
178
+ subject._p { subject._ "friend" }
179
+ subject.__attrs :title => 'missedtheboat'
180
+ subject.__pop
181
+ subject.__flush
182
+
183
+ assert_equal "<div class=\"this\"><p>hi there</p><p>friend</p></div>", @out
184
+ end
185
+
186
+ end
187
+
188
+
189
+
160
190
  class LocalDataTests < BasicTests
161
191
 
162
192
  should "only accept the data if it is a Hash" do
163
- assert_raises ArgumentError do
164
- Undies::Template.new(Undies::Source.new(Proc.new {}), "some data")
165
- end
166
193
  assert_respond_to(
167
194
  :some,
168
195
  Undies::Template.new(Undies::Source.new(Proc.new {}), {:some => 'data'}, @output)
@@ -180,124 +207,79 @@ class Undies::Template
180
207
  assert_equal "data", templ.some
181
208
  end
182
209
 
183
- should "be able to access its locals in the template definition" do
184
- src = Undies::Source.new do
185
- _div {
186
- _div { _ name }
187
- }
188
- end
189
- Undies::Template.new(src, {:name => "awesome"}, @output)
190
- assert_equal "<div><div>awesome</div></div>", @out
191
- end
192
-
193
210
  end
194
211
 
195
- class LayoutTests < BasicTests
196
- setup do
197
- @expected_output = "<html><head></head><body><div>Hi</div></body></html>"
198
-
199
- @layout_proc = Proc.new do
200
- _html {
201
- _head {}
202
- _body {
203
- __yield
204
- }
205
- }
206
- end
207
- @layout_file = File.expand_path "test/templates/layout.html.rb"
208
-
209
- @content_proc = Proc.new do
210
- _div { _ "Hi" }
211
- end
212
- @content_file = File.expand_path "test/templates/content.html.rb"
213
212
 
214
- @cp_lp_source = Undies::Source.new(:layout => @layout_proc, &@content_proc)
215
- @cp_lf_source = Undies::Source.new(:layout => @layout_file, &@content_proc)
216
- @cf_lp_source = Undies::Source.new(@content_file, :layout => @layout_proc)
217
- @cf_lf_source = Undies::Source.new(@content_file, :layout => @layout_file)
218
- end
219
213
 
220
- should "generate markup given proc content in a proc layout" do
221
- Undies::Template.new(@cp_lp_source, {}, @output)
222
- assert_equal @expected_output, @out
223
- end
214
+ class StreamTests < BasicTests
215
+ desc "that is streaming"
224
216
 
225
- should "generate markup given proc content in a layout file" do
226
- Undies::Template.new(@cp_lf_source, {}, @output)
227
- assert_equal @expected_output, @out
217
+ before do
218
+ outstream = StringIO.new(@output = "")
219
+ @template = Undies::Template.new(Undies::Output.new(outstream))
228
220
  end
229
221
 
230
- should "generate markup given a content file in a proc layout" do
231
- Undies::Template.new(@cf_lp_source, {}, @output)
232
- assert_equal @expected_output, @out
233
- end
222
+ should "not stream full content until Undies#flush called on the template" do
223
+ @template._div { @template._ "Added post-init" }
224
+ @expected_output = "<div>Added post-init</div>"
234
225
 
235
- should "generate markup given a content file in a layout file" do
236
- Undies::Template.new(@cf_lf_source, {}, @output)
237
- assert_equal @expected_output, @out
226
+ assert_equal "", @output
227
+ Undies::Template.flush(@template)
228
+ assert_equal @expected_output, @output
238
229
  end
239
230
 
240
- end
231
+ should "should write to the stream as its being constructed" do
232
+ @template._div.good.thing!(:type => "something")
233
+ @template.__push
234
+ @template._p { @template._ 'hi' }
235
+ @template.__flush
241
236
 
242
- class PartialTests < BasicTests
243
- desc "using partials"
237
+ @expected_output = "<div class=\"good\" id=\"thing\" type=\"something\"><p>hi</p>"
238
+ assert_equal @expected_output, @output
244
239
 
245
- before do
246
- @source = Undies::Source.new(Proc.new do
247
- partial_source = Undies::Source.new(Proc.new do
248
- _div { _ thing }
249
- end)
250
-
251
- _div {
252
- _ thing
253
- __partial partial_source, {:thing => 1234}
254
- }
255
- end)
256
- @data = {:thing => 'abcd'}
257
- end
240
+ @template._p { @template._ "action" }
241
+ @template.__pop
242
+ @template.__flush
258
243
 
259
- should "render the partial source with its own scope/data" do
260
- Undies::Template.new(@source, @data, @output)
261
- assert_equal "<div>abcd<div>1234</div></div>", @out
244
+ @expected_output = "<div class=\"good\" id=\"thing\" type=\"something\"><p>hi</p><p>action</p></div>"
245
+ assert_equal @expected_output, @output
262
246
  end
263
247
 
264
248
  end
265
249
 
266
- class StreamTests < BasicTests
267
- desc "that is streaming"
268
250
 
269
- before do
270
- outstream = StringIO.new(@output = "")
271
- src = Undies::Source.new do
272
- _div.good.thing!(:type => "something") {
273
- __ "action"
274
- }
275
- end
276
- @expected_output = "<div class=\"good\" id=\"thing\" type=\"something\">action</div>"
277
251
 
278
- @template = Undies::Template.new(src, {}, Undies::Output.new(outstream))
279
- end
252
+ class PrettyPrintTests < BasicTests
280
253
 
281
- should "should write to the stream as its being constructed" do
282
- assert_equal @expected_output, @output
254
+ should "generate pretty printed markup" do
255
+ output = Undies::Output.new(@outstream, :pp => 2)
256
+ templ = Undies::Template.new(output)
257
+
258
+ templ._html
259
+ templ.__push
260
+ templ._head {}
261
+ templ._body
262
+ templ.__push
263
+ templ._div { templ._ "Hi" }
264
+ # templ.__push
265
+ # templ._ "Hi"
266
+ # templ.__pop
267
+ templ.__pop
268
+ templ.__pop
269
+ templ.__flush
270
+
271
+ assert_equal(
272
+ %{<html>
273
+ <head></head>
274
+ <body>
275
+ <div>Hi</div>
276
+ </body>
277
+ </html>}, @out )
283
278
  end
284
279
 
285
280
  end
286
281
 
287
- class FlushTests < StreamTests
288
- desc "and adds content post-init"
289
- before do
290
- @expected_post_output = "<div>Added post-init</div>"
291
- @template._div { @template._ "Added post-init" }
292
- end
293
282
 
294
- should "not stream full content until Undies#flush called on the template" do
295
- assert_equal @expected_output, @output
296
- Undies::Template.flush(@template)
297
- assert_equal @expected_output + @expected_post_output, @output
298
-
299
- end
300
- end
301
283
 
302
284
  end
303
285
 
data/undies.gemspec CHANGED
@@ -19,5 +19,6 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_development_dependency("bundler", ["~> 1.0"])
21
21
  s.add_development_dependency("assert", ["~> 0.7.3"])
22
+ s.add_development_dependency("whysoslow", ["~> 0.0"])
22
23
 
23
24
  end