undies 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,117 +1,116 @@
1
- require "test_belt"
1
+ require "assert"
2
2
 
3
3
  require "stringio"
4
4
  require "undies/template"
5
5
 
6
6
  class Undies::Template
7
7
 
8
- class BasicTest < Test::Unit::TestCase
9
- include TestBelt
10
-
11
- context 'a template'
12
- subject { Undies::Template.new {} }
13
- should have_instance_method :to_s, :escape_html
14
- should have_instance_methods :_, :__, :element, :tag
15
- should have_accessor :___nodes
8
+ class BasicTests < Assert::Context
9
+ desc 'a template'
10
+ before do
11
+ src = Undies::Source.new(Proc.new {})
12
+ @outstream = StringIO.new(@out = "")
13
+ @output = Undies::Output.new(@outstream)
16
14
 
17
- should "have a NodeList as its nodes" do
18
- assert_kind_of Undies::NodeList, subject.___nodes
15
+ @t = Undies::Template.new(src, {}, @output)
19
16
  end
17
+ subject { @t }
20
18
 
21
- should "have no io stream by default" do
22
- assert_nil subject.send(:___io)
19
+ should have_class_method :output
20
+ should have_instance_method :to_s
21
+ should have_instance_methods :element, :tag, :escape_html
22
+ should have_instance_methods :_, :__
23
+ should have_instance_methods :__yield, :__partial
24
+
25
+ should "provide access to its output object via a class method" do
26
+ assert_same @output, Undies::Template.output(@t)
23
27
  end
24
28
 
25
29
  should "maintain the template's scope throughout content blocks" do
26
- templ = Undies::Template.new do
30
+ templ = Undies::Template.new(Undies::Source.new do
27
31
  _div {
28
32
  _div {
29
33
  __ self.object_id
30
34
  }
31
35
  }
32
- end
33
- assert_equal "<div><div>#{templ.object_id}</div></div>", templ.to_s
36
+ end, {}, @output)
37
+ assert_equal "<div><div>#{templ.object_id}</div></div>", @out
34
38
  end
35
39
 
36
40
  should "generate pretty printed markup" do
37
41
  file = 'test/templates/test.html.rb'
42
+ output = Undies::Output.new(@outstream, :pp => 2)
43
+ Undies::Template.new(Undies::Source.new(File.expand_path(file)), {}, output)
38
44
  assert_equal(
39
- %{<html>
40
- <head>
41
- </head>
45
+ %{
46
+ <html>
47
+ <head></head>
42
48
  <body>
43
- <div>
44
- Hi
45
- </div>
49
+ <div>Hi</div>
46
50
  </body>
47
- </html>
48
- },
49
- Undies::Template.new(File.expand_path(file)).to_s(2)
51
+ </html>},
52
+ @out
50
53
  )
51
54
  end
52
55
 
53
56
  end
54
57
 
55
-
56
-
57
- class NodeTest < BasicTest
58
- context "with text data"
58
+ class NodeTests < BasicTests
59
+ desc "with text data"
59
60
  before do
60
61
  @data = "stuff & <em>more stuff</em>"
61
62
  end
62
63
 
63
- should "return a text node using the '__' and '_' methods" do
64
- assert_kind_of Undies::Node, subject.__(@data)
65
- assert_kind_of Undies::Node, subject._(@data)
66
- end
67
-
68
- should "also add the node using the '__' and '_' methods" do
69
- subject.__(@data)
70
- assert_equal 1, subject.___nodes.size
71
- subject._(@data)
72
- assert_equal 2, subject.___nodes.size
73
- end
74
-
75
64
  should "add the text un-escaped using the '__' method" do
76
- assert_equal @data, subject.__(@data).to_s
65
+ Undies::Template.new(Undies::Source.new do
66
+ __ data
67
+ end, {:data => @data}, @output)
68
+ assert_equal @data, @out
77
69
  end
78
70
 
79
71
  should "add the text escaped using the '_' method" do
80
- assert_equal subject.send(:escape_html, @data), subject._(@data).to_s
72
+ Undies::Template.new(Undies::Source.new do
73
+ _ data
74
+ end, {:data => @data}, @output)
75
+ assert_equal subject.send(:escape_html, @data), @out
81
76
  end
82
77
 
83
78
  should "add empty string nodes using '__' and '_' methods with no args" do
84
- assert_equal "", subject._.to_s
85
- assert_equal "", subject.__.to_s
79
+ Undies::Template.new(Undies::Source.new do
80
+ _
81
+ __
82
+ end, {:data => @data}, @output)
83
+ assert_equal "", @out
86
84
  end
87
85
 
88
86
  end
89
87
 
88
+ class ElementTests < BasicTests
89
+ desc "using the 'element' helper"
90
90
 
91
-
92
- class ElementTest < BasicTest
93
- context "using the 'element' helper"
94
-
95
- should "return an Element object" do
96
- assert_equal Undies::Element.new(Undies::ElementStack.new, :br), subject.element(:br)
91
+ should "stream element output" do
92
+ Undies::Template.new(Undies::Source.new do
93
+ element(:br)
94
+ end, {}, @output)
95
+ assert_equal "<br />", @out
97
96
  end
98
97
 
99
98
  should "alias it with 'tag'" do
100
- assert_equal subject.element(:br), subject.tag(:br)
101
- end
102
-
103
- should "add a new Element object" do
104
- subject.element(:br)
105
- assert_equal 1, subject.___nodes.size
106
- assert_equal Undies::Element.new(Undies::ElementStack.new, :br), subject.___nodes.first
99
+ Undies::Template.new(Undies::Source.new do
100
+ tag(:br)
101
+ end, {}, @output)
102
+ assert_equal "<br />", @out
107
103
  end
108
104
 
109
105
  should "respond to underscore-prefix methods" do
110
- assert subject.respond_to?(:_div)
106
+ assert subject.respond_to?(:_br)
111
107
  end
112
108
 
113
109
  should "respond to underscore-prefix methods as element methods" do
114
- assert_equal subject._div, subject.element(:div)
110
+ Undies::Template.new(Undies::Source.new do
111
+ _br
112
+ end, {}, @output)
113
+ assert_equal "<br />", @out
115
114
  end
116
115
 
117
116
  should "not respond to element methods without an underscore-prefix" do
@@ -123,153 +122,129 @@ class Undies::Template
123
122
 
124
123
  end
125
124
 
126
-
127
-
128
- class LocalsTest < BasicTest
125
+ class LocalDataTests < BasicTests
129
126
 
130
127
  should "only accept the data if it is a Hash" do
131
128
  assert_raises ArgumentError do
132
- (Undies::Template.new("some_data") {}).some
133
- end
134
- assert_raises ArgumentError do
135
- (Undies::Template.new('test/templates/test.html.rb', "some_data")).some
129
+ Undies::Template.new(Undies::Source.new(Proc.new {}), "some data")
136
130
  end
137
131
  assert_respond_to(
138
- (Undies::Template.new(:some => "data") {}),
139
- :some
140
- )
141
- assert_respond_to(
142
- Undies::Template.new('test/templates/test.html.rb', :some => "data"),
143
- :some
132
+ :some,
133
+ Undies::Template.new(Undies::Source.new(Proc.new {}), {:some => 'data'}, @output)
144
134
  )
145
135
  end
146
136
 
147
137
  should "complain if trying to set locals that conflict with public methods" do
148
138
  assert_raises ArgumentError do
149
- Undies::Template.new(:_ => "yay!") {}
139
+ Undies::Template.new(Undies::Source.new(Proc.new {}), {:_ => "yay!"})
150
140
  end
151
141
  end
152
142
 
153
143
  should "respond to each locals key with its value" do
154
- templ = Undies::Template.new(:some => "data") {}
144
+ templ = Undies::Template.new(Undies::Source.new(Proc.new {}), {:some => 'data'}, @output)
155
145
  assert_equal "data", templ.some
156
146
  end
157
147
 
158
148
  should "be able to access its locals in the template definition" do
159
- templ = Undies::Template.new(:name => "awesome") do
149
+ src = Undies::Source.new do
160
150
  _div {
161
151
  _div { _ name }
162
152
  }
163
153
  end
164
- assert_equal "<div><div>awesome</div></div>", templ.to_s
154
+ Undies::Template.new(src, {:name => "awesome"}, @output)
155
+ assert_equal "<div><div>awesome</div></div>", @out
165
156
  end
166
157
 
167
158
  end
168
159
 
169
-
170
-
171
- class DefinitionTest < BasicTest
160
+ class LayoutTests < BasicTests
172
161
  setup do
173
162
  @expected_output = "<html><head></head><body><div>Hi</div></body></html>"
174
- @proc = Proc.new do
163
+
164
+ @layout_proc = Proc.new do
175
165
  _html {
176
166
  _head {}
177
167
  _body {
178
- _div { _ "Hi" }
168
+ __yield
179
169
  }
180
170
  }
181
171
  end
172
+ @layout_file = File.expand_path "test/templates/layout.html.rb"
173
+
182
174
  @content_proc = Proc.new do
183
175
  _div { _ "Hi" }
184
176
  end
185
- @layout_proc = Proc.new do
186
- _html { _head {}; _body { yield if block_given? } }
187
- end
188
- @layout_file = File.expand_path "test/templates/layout.html.rb"
189
177
  @content_file = File.expand_path "test/templates/content.html.rb"
190
- @test_content_file = File.expand_path "test/templates/test.html.rb"
191
- end
192
178
 
193
- should "generate markup given the content in a passed block" do
194
- template = Undies::Template.new(&@proc)
195
- assert_equal @expected_output, template.to_s
179
+ @cp_lp_source = Undies::Source.new(:layout => @layout_proc, &@content_proc)
180
+ @cp_lf_source = Undies::Source.new(:layout => @layout_file, &@content_proc)
181
+ @cf_lp_source = Undies::Source.new(@content_file, :layout => @layout_proc)
182
+ @cf_lf_source = Undies::Source.new(@content_file, :layout => @layout_file)
196
183
  end
197
184
 
198
- should "complain if given a proc both as the first arg and passed as a block" do
199
- assert_raises ArgumentError do
200
- Undies::Template.new(@proc) do
201
- _div { _ "Should not render b/c argument error" }
202
- end
203
- end
185
+ should "generate markup given proc content in a proc layout" do
186
+ Undies::Template.new(@cp_lp_source, {}, @output)
187
+ assert_equal @expected_output, @out
204
188
  end
205
189
 
206
- should "generate markup given the content in a file, even if passed a block" do
207
- template_no_block = Undies::Template.new(@test_content_file)
208
- template_w_block = Undies::Template.new(@test_content_file) do
209
- _div { _ "Should not render b/c template prefers a file" }
210
- end
211
- assert_equal @expected_output, template_no_block.to_s
212
- assert_equal @expected_output, template_w_block.to_s
190
+ should "generate markup given proc content in a layout file" do
191
+ Undies::Template.new(@cp_lf_source, {}, @output)
192
+ assert_equal @expected_output, @out
213
193
  end
214
194
 
215
- should "generate markup given the layout in a file and the content in a passed block" do
216
- template = Undies::Template.new(@layout_file) do
217
- _div { _ "Hi" }
218
- end
219
- assert_equal @expected_output, template.to_s
195
+ should "generate markup given a content file in a proc layout" do
196
+ Undies::Template.new(@cf_lp_source, {}, @output)
197
+ assert_equal @expected_output, @out
220
198
  end
221
199
 
222
- should "generate markup given the layout in a Proc and the content in a Proc as first arg" do
223
- template = Undies::Template.new(@content_proc, @layout_file)
224
- assert_equal @expected_output, template.to_s
200
+ should "generate markup given a content file in a layout file" do
201
+ Undies::Template.new(@cf_lf_source, {}, @output)
202
+ assert_equal @expected_output, @out
225
203
  end
226
204
 
227
- should "generate markup given the layout in a file and the content in a file" do
228
- template = Undies::Template.new(@content_file, @layout_file)
229
- assert_equal @expected_output, template.to_s
230
- end
205
+ end
231
206
 
232
- should "complain if given the layout in a Proc and the content in a passed block" do
233
- assert_raises ArgumentError do
234
- Undies::Template.new(@layout_proc) do
235
- _div { _ "Hi" }
236
- end
237
- end
238
- end
207
+ class PartialTests < BasicTests
208
+ desc "using partials"
239
209
 
240
- should "complain given the layout in a Proc and the content in a Proc as first arg" do
241
- assert_raises ArgumentError do
242
- Undies::Template.new(@content_proc, @layout_proc)
243
- end
210
+ before do
211
+ @source = Undies::Source.new(Proc.new do
212
+ partial_source = Undies::Source.new(Proc.new do
213
+ _div { _ thing }
214
+ end)
215
+
216
+ _div {
217
+ _ thing
218
+ __partial partial_source, {:thing => 1234}
219
+ }
220
+ end)
221
+ @data = {:thing => 'abcd'}
244
222
  end
245
223
 
246
- should "complain given the layout in a Proc and the content in a file" do
247
- assert_raises ArgumentError do
248
- Undies::Template.new(@content_file, @layout_proc)
249
- end
224
+ should "render the partial source with its own scope/data" do
225
+ Undies::Template.new(@source, @data, @output)
226
+ assert_equal "<div>abcd<div>1234</div></div>", @out
250
227
  end
251
228
 
252
229
  end
253
230
 
254
-
255
-
256
- class StreamTest < BasicTest
257
- context "that is streaming"
231
+ class StreamTests < BasicTests
232
+ desc "that is streaming"
258
233
 
259
234
  before do
260
- @output = ""
261
- @outstream = StringIO.new(@output)
235
+ outstream = StringIO.new(@output = "")
236
+ src = Undies::Source.new do
237
+ _div.good.thing!(:type => "something") {
238
+ __ "action"
239
+ }
240
+ end
241
+ @expected_output = "<div class=\"good\" id=\"thing\" type=\"something\">action</div>"
242
+
243
+ Undies::Template.new(src, {}, Undies::Output.new(outstream))
262
244
  end
263
245
 
264
246
  should "should write to the stream as its being constructed" do
265
- templ = Undies::Template.new(@outstream) do
266
- _div {
267
- _div.good.thing!(:type => "something") {
268
- __ "good streaming action"
269
- }
270
- }
271
- end
272
- assert_equal "<div><div class=\"good\" id=\"thing\" type=\"something\">good streaming action</div></div>", @output
247
+ assert_equal @expected_output, @output
273
248
  end
274
249
 
275
250
  end
@@ -1,6 +1,6 @@
1
1
  _html {
2
2
  _head {}
3
3
  _body {
4
- yield
4
+ __yield
5
5
  }
6
6
  }
data/undies.gemspec CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_development_dependency("bundler", ["~> 1.0"])
21
- s.add_development_dependency("test-belt", ["~> 2.0"])
21
+ s.add_development_dependency("assert", ["~> 0.7.3"])
22
22
 
23
23
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undies
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
8
7
  - 2
9
8
  - 0
10
- version: 1.2.0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-30 00:00:00 Z
18
+ date: 2011-12-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :development
@@ -40,13 +40,14 @@ dependencies:
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- hash: 3
43
+ hash: 5
44
44
  segments:
45
- - 2
46
45
  - 0
47
- version: "2.0"
46
+ - 7
47
+ - 3
48
+ version: 0.7.3
48
49
  version_requirements: *id002
49
- name: test-belt
50
+ name: assert
50
51
  description: A pure-Ruby HTML and plain text templating DSL.
51
52
  email:
52
53
  - kelly@kelredd.com
@@ -62,25 +63,29 @@ files:
62
63
  - Gemfile.lock
63
64
  - README.rdoc
64
65
  - Rakefile
66
+ - bench/large.html.rb
67
+ - bench/procs.rb
68
+ - bench/profiler.rb
69
+ - bench/small.html.rb
70
+ - bench/verylarge.html.rb
65
71
  - lib/undies.rb
66
72
  - lib/undies/element.rb
67
- - lib/undies/element_stack.rb
73
+ - lib/undies/named_source.rb
68
74
  - lib/undies/node.rb
69
- - lib/undies/node_list.rb
70
- - lib/undies/partial.rb
71
- - lib/undies/partial_data.rb
75
+ - lib/undies/node_buffer.rb
76
+ - lib/undies/output.rb
72
77
  - lib/undies/source.rb
78
+ - lib/undies/source_stack.rb
73
79
  - lib/undies/template.rb
74
80
  - lib/undies/version.rb
75
- - test/element_stack_test.rb
76
81
  - test/element_test.rb
77
- - test/fixtures/partial_template.rb
78
82
  - test/helper.rb
79
83
  - test/irb.rb
80
- - test/node_list_test.rb
84
+ - test/named_source_test.rb
85
+ - test/node_buffer_test.rb
81
86
  - test/node_test.rb
82
- - test/partial_data_test.rb
83
- - test/partial_test.rb
87
+ - test/output_test.rb
88
+ - test/source_stack_test.rb
84
89
  - test/source_test.rb
85
90
  - test/template_test.rb
86
91
  - test/templates/content.html.rb
@@ -117,20 +122,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
122
  requirements: []
118
123
 
119
124
  rubyforge_project:
120
- rubygems_version: 1.8.10
125
+ rubygems_version: 1.8.11
121
126
  signing_key:
122
127
  specification_version: 3
123
128
  summary: A pure-Ruby HTML and plain text templating DSL.
124
129
  test_files:
125
- - test/element_stack_test.rb
126
130
  - test/element_test.rb
127
- - test/fixtures/partial_template.rb
128
131
  - test/helper.rb
129
132
  - test/irb.rb
130
- - test/node_list_test.rb
133
+ - test/named_source_test.rb
134
+ - test/node_buffer_test.rb
131
135
  - test/node_test.rb
132
- - test/partial_data_test.rb
133
- - test/partial_test.rb
136
+ - test/output_test.rb
137
+ - test/source_stack_test.rb
134
138
  - test/source_test.rb
135
139
  - test/template_test.rb
136
140
  - test/templates/content.html.rb
@@ -1,35 +0,0 @@
1
- require "undies/element"
2
-
3
- module Undies
4
- class ElementStack < ::Array
5
-
6
- # an element stack is used to manage which element is receiving content
7
- # if an undies template is streaming io, then when an element is pushed,
8
- # its start tag is added to the stream and its end tag is added when popped.
9
-
10
- attr_reader :io
11
-
12
- def initialize(first_item=nil, io=nil, *args)
13
- @io = io
14
- # always initialize empty
15
- super()
16
- self.send(:<<, first_item) if first_item
17
- end
18
-
19
- def push(item)
20
- unless item.kind_of?(Element)
21
- raise ArgumentError, 'you can only push element nodes to an ElementStack'
22
- end
23
- super
24
- @io << item.start_tag if @io
25
- item
26
- end
27
-
28
- def pop
29
- item = super
30
- @io << item.end_tag if @io
31
- item
32
- end
33
-
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- require "undies/node"
2
-
3
- module Undies
4
-
5
- # a node list is an ordered collection of node data
6
- # * the Template class builds one as the template source is evaluated
7
- # * a list may contains nodes and/or other node lists
8
- # * serialize a list using 'to_s' using optional pretty printing args
9
- # * any pretty printing args are used to render the individual nodes/lists
10
- class NodeList < ::Array
11
-
12
- def initialize(*args)
13
- # always initialize empty
14
- super()
15
- end
16
-
17
- def append(node)
18
- self << node
19
- node
20
- end
21
-
22
- def <<(item)
23
- unless item.kind_of?(Node) || item.kind_of?(NodeList)
24
- raise ArgumentError, 'you can only append nodes or other node lists to a NodeList'
25
- end
26
- super
27
- end
28
-
29
- def to_s(pp_level=0, pp_indent=nil)
30
- self.collect{|n| n.to_s(pp_level, pp_indent)}.join
31
- end
32
-
33
- end
34
-
35
- end
@@ -1,23 +0,0 @@
1
- require 'undies/partial_data'
2
- require 'undies/template'
3
-
4
- module Undies
5
- module PartialTemplate
6
-
7
- def initialize(path, *args)
8
- locals = PartialLocals.new(path)
9
- locals.values, io, locals.object = self.___partial_args(*args)
10
- super(path, io, locals)
11
- end
12
-
13
- protected
14
-
15
- def ___partial_args(*args)
16
- [ args.last.kind_of?(::Hash) ? args.pop : {},
17
- self.___is_a_stream?(args.last) ? args.pop : nil,
18
- args.first
19
- ]
20
- end
21
-
22
- end
23
- end
@@ -1,32 +0,0 @@
1
- module Undies
2
- class PartialLocals < ::Hash
3
-
4
- attr_reader :path, :name
5
-
6
- def initialize(path)
7
- self.path = path
8
- super()
9
- end
10
-
11
- def path=(value)
12
- raise ArgumentError, "partial path required" if value.nil?
13
- @path = value
14
- @name = File.basename(@path.to_s).split(".").first.gsub(/^[^A-Za-z]+/, '')
15
- end
16
-
17
- def values=(value)
18
- raise ArgumentError if !value.kind_of?(::Hash)
19
- if value.has_key?(@name.to_sym)
20
- value[@name] = value.delete(@name.to_sym)
21
- end
22
- self.merge!(value)
23
- end
24
-
25
- def object=(value)
26
- if value
27
- self[@name] = value
28
- end
29
- end
30
-
31
- end
32
- end