htrb 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/htrb_test.rb CHANGED
@@ -1,253 +1,291 @@
1
- require 'simplecov'
2
- SimpleCov.start
3
-
4
- require 'minitest/autorun'
5
- require 'minitest/reporters'
6
- Minitest::Reporters.use!
7
-
8
- require_relative '../lib/htrb'
9
-
10
- class HtrbTest < Minitest::Test
11
- def setup
12
- @empty_frag = HTRB.fragment
13
-
14
- tag = nil
15
- @container_frag = HTRB.fragment do
16
- tag = div! id: 'container'
17
- end
18
-
19
- @div = tag
20
- end
21
-
22
- def test_html5_tags_are_methods
23
- HTRB::TAGS.each do |tag|
24
- tag = (tag.to_s + '!').to_sym
25
- assert_equal true, @empty_frag.respond_to?(tag, true)
26
- end
27
- end
28
-
29
- def test_fragment_is_node
30
- assert_equal 'HTRB::HtmlNode', @empty_frag.class.name
31
- end
32
-
33
- def test_fragment_to_s_empty
34
- assert_equal '', @empty_frag.to_s
35
- end
36
-
37
- def test_fragment_to_s
38
- assert_equal '<div id="container"></div>', @container_frag.to_s
39
- end
40
-
41
- def test_fragment_to_pretty_empty
42
- assert_equal '', @empty_frag.to_pretty
43
- end
44
-
45
- def test_fragment_to_pretty
46
- @div.append 'Hello'
47
- assert_equal "<div id=\"container\">\n Hello\n</div>", @container_frag.to_pretty
48
- end
49
-
50
- def test_inner_html
51
- assert_equal [@div], @container_frag.inner_html
52
- end
53
-
54
- def test_inner_html_block
55
- @container_frag.inner_html { }
56
- refute_equal [@div], @container_frag.inner_html
57
- end
58
-
59
- def test_fragment_append_node
60
- paragraph = HTRB::Elements::P.new
61
- return_value = @container_frag.append paragraph
62
- assert_equal paragraph, return_value
63
- assert_equal paragraph, @container_frag.inner_html[1]
64
- end
65
-
66
- def test_fragment_append_text
67
- text = 'Something'
68
- return_value = @container_frag.append text
69
- assert_equal text, return_value
70
- assert_equal text, @container_frag.inner_html[1]
71
- end
72
-
73
- def test_fragment_append_error
74
- bad_child = nil
75
- assert_raises(ArgumentError) { @empty_frag.append bad_child }
76
- assert_raises HTRB::SelfClosingTagError do
77
- HTRB::Elements::Hr.new.append @div
78
- end
79
- end
80
-
81
- def test_fragment_remove
82
- return_value = @container_frag.remove @div
83
- assert_equal @div, return_value
84
- assert_equal 0, @container_frag.inner_html.length
85
-
86
- return_value_empty = @container_frag.remove @div
87
- assert_nil return_value_empty
88
- end
89
-
90
- def test_fragment_insert_before
91
- image = HTRB::Elements::Img.new
92
- return_value = @container_frag.insert image, :before, @div
93
- assert_equal image, return_value
94
- assert_equal image, @container_frag.inner_html[0]
95
- end
96
-
97
- def test_fragment_insert_after
98
- text = 'after'
99
- return_value = @container_frag.insert text, :after, @div
100
- assert_equal text, return_value
101
- assert_equal text, @container_frag.inner_html[1]
102
- end
103
-
104
- def test_fragment_insert_errors
105
- assert_raises(ArgumentError) { @empty_frag.insert @div, :before, @div }
106
- assert_raises(ArgumentError) { @container_frag.insert @div, :never, @div }
107
- assert_raises(ArgumentError) { @container_frag.insert nil, :after, @div }
108
-
109
- link = @div.append HTRB::Elements::Link.new
110
- assert_raises HTRB::TagParadoxError do
111
- @div.insert @div, :before, link
112
- end
113
- end
114
-
115
- def test_self_closing_tag
116
- hr = HTRB::Elements::Hr.new
117
- assert_equal '<hr>', hr.to_s
118
- end
119
-
120
- def test_self_closing_tag_error
121
- assert_raises HTRB::SelfClosingTagError do
122
- HTRB::Elements::Track.new {}
123
- end
124
- end
125
-
126
- def test_attribute_key_replace_underscore
127
- br = HTRB::Elements::Br.new hx_post: '/clicked', 'hx_swap' => 'outerHTML'
128
- assert_equal '<br hx-post="/clicked" hx-swap="outerHTML">', br.to_s
129
- end
130
-
131
- def test_fragment_t
132
- em = HTRB::Elements::Em.new { t! 'Testing text' }
133
- assert_equal '<em>Testing text</em>', em.to_s
134
- end
135
-
136
- def test_fagment_t_escape
137
- strong = HTRB::Elements::Strong.new { t! 'Testing > you & < me' }
138
- assert_equal '<strong>Testing &gt; you &amp; &lt; me</strong>', strong.to_s
139
- end
140
-
141
- def test_fragment_missing_method_error
142
- assert_raises(NameError) { @empty_frag.stupid }
143
- end
144
-
145
- def test_fragment_props
146
- button = HTRB::Elements::Button.new text: 'Button Text' do
147
- t! props[:text]
148
- end
149
- assert_equal '<button text="Button Text">Button Text</button>', button.to_s
150
- end
151
-
152
- def test_component
153
- eval <<-CLASS_DEFINITION
154
- class Tester < HTRB::Component
155
- def render(**attributes, &block)
156
- t! 'Test'
157
- remit &block if block_given?
158
- end
159
- end
160
- CLASS_DEFINITION
161
-
162
- assert_equal true, @empty_frag.respond_to?(:_tester!, true)
163
- assert_equal true, Tester.new.self_closing?
164
-
165
- @empty_frag.inner_html { _tester! }
166
- assert_equal 'Test', @empty_frag.to_s
167
-
168
- assert_raises HTRB::SelfClosingTagError do
169
- Tester.new.inner_html {}
170
- end
171
- end
172
-
173
- def test_component_duplicate_error
174
- assert_raises HTRB::TagExistsError do
175
- eval <<-CLASS_DEFINITION
176
- module One
177
- class Exists < HTRB::Component; end
178
- end
179
-
180
- module Another
181
- class Exists < HTRB::Component; end
182
- end
183
- CLASS_DEFINITION
184
- end
185
- end
186
-
187
- def test_html
188
- return_value = HTRB.html do
189
- div! id: 'container' do
190
- img! src: 'lol.jpg'
191
- end
192
- end
193
-
194
- assert_equal '<div id="container"><img src="lol.jpg"></div>', return_value
195
- end
196
-
197
- def test_document_is_document
198
- assert_kind_of HTRB::Document, HTRB.document
199
- end
200
-
201
- def test_document_to_pretty
202
- assert_equal "<!DOCTYPE html>\n<html>\n <head>\n <title>\n \n </title>\n <meta charset=\"UTF-8\">\n </head>\n <body>\n </body>\n</html>", HTRB.document.to_pretty
203
- end
204
-
205
- def test_document_to_s
206
- assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body></body></html>', HTRB.document.to_s
207
- end
208
-
209
- def test_document_with_title
210
- doc = HTRB.document title: 'Has a title'
211
- assert_equal '<!DOCTYPE html><html><head><title>Has a title</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
212
- end
213
-
214
- def test_document_title_get
215
- doc = HTRB.document title: 'Has a title'
216
- assert_equal 'Has a title', doc.title
217
- end
218
-
219
- def test_document_title_set
220
- doc = HTRB.document
221
- old = HTRB.document.title
222
- doc.title 'Nice'
223
-
224
- refute_equal old, doc.title
225
- assert_equal 'Nice', doc.title
226
- assert_equal '<!DOCTYPE html><html><head><title>Nice</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
227
- end
228
-
229
- def test_document_pass_head
230
- head_proc = proc { link! rel: 'stylesheet', href: 'mystyle.css' }
231
- doc = HTRB.document head: head_proc
232
-
233
- assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
234
- end
235
-
236
- def test_document_change_head
237
- doc = HTRB.document
238
- doc.head { link! rel: 'stylesheet', href: 'mystyle.css' }
239
-
240
- assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
241
- end
242
-
243
- def test_document_pass_body
244
- doc = HTRB.document { t! 'This is my body' }
245
- assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
246
- end
247
-
248
- def test_document_change_body
249
- doc = HTRB.document
250
- doc.body { t! 'This is my body' }
251
- assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
252
- end
253
- end
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/reporters'
6
+ Minitest::Reporters.use!
7
+
8
+ require_relative '../lib/htrb'
9
+
10
+ class HtrbTest < Minitest::Test
11
+ def setup
12
+ @empty_frag = HTRB.fragment
13
+
14
+ tag = nil
15
+ @container_frag = HTRB.fragment do
16
+ tag = div! id: 'container'
17
+ end
18
+
19
+ @div = tag
20
+ end
21
+
22
+ def test_html5_tags_are_methods
23
+ HTRB::TAGS.each do |tag|
24
+ tag = (tag.to_s + '!').to_sym
25
+ assert_equal true, @empty_frag.respond_to?(tag, true)
26
+ end
27
+ end
28
+
29
+ def test_fragment_is_node
30
+ assert_equal 'HTRB::HtmlNode', @empty_frag.class.name
31
+ end
32
+
33
+ def test_fragment_to_s_empty
34
+ assert_equal '', @empty_frag.to_s
35
+ end
36
+
37
+ def test_fragment_to_s
38
+ assert_equal '<div id="container"></div>', @container_frag.to_s
39
+ end
40
+
41
+ def test_fragment_to_pretty_empty
42
+ assert_equal '', @empty_frag.to_pretty
43
+ end
44
+
45
+ def test_fragment_to_pretty
46
+ @div.append 'Hello'
47
+ assert_equal "<div id=\"container\">\n Hello\n</div>", @container_frag.to_pretty
48
+ end
49
+
50
+ def test_attribute_quote_to_s
51
+ out = HTRB.html do
52
+ div! attribute: '{"JSON": "Object"}'
53
+ end
54
+
55
+ assert_equal '<div attribute=\'{"JSON": "Object"}\'></div>', out
56
+ end
57
+
58
+ def test_attribute_quote_to_pretty
59
+ out = HTRB.fragment do
60
+ div! attribute: '{"JSON": "Object"}'
61
+ end.to_pretty
62
+
63
+ assert_equal "<div attribute='{\"JSON\": \"Object\"}'>\n</div>", out
64
+ end
65
+
66
+ def test_attribute_quote_escape_to_s
67
+ out = HTRB.html do
68
+ div! message: "I'd like to say \"hello\" to everyone."
69
+ end
70
+
71
+ expected =
72
+ '<div message=\'I&#39;d like to say "hello" to everyone.\'></div>'
73
+
74
+ assert_equal expected, out
75
+ end
76
+
77
+ def test_attribute_quote_escape_to_pretty
78
+ out = HTRB.fragment do
79
+ div! message: "I'd like to say \"hello\" to everyone."
80
+ end.to_pretty
81
+
82
+ expected =
83
+ "<div message='I&#39;d like to say \"hello\" to everyone.'>\n</div>"
84
+
85
+ assert_equal expected, out
86
+ end
87
+
88
+ def test_inner_html
89
+ assert_equal [@div], @container_frag.inner_html
90
+ end
91
+
92
+ def test_inner_html_block
93
+ @container_frag.inner_html { }
94
+ refute_equal [@div], @container_frag.inner_html
95
+ end
96
+
97
+ def test_fragment_append_node
98
+ paragraph = HTRB::Elements::P.new
99
+ return_value = @container_frag.append paragraph
100
+ assert_equal paragraph, return_value
101
+ assert_equal paragraph, @container_frag.inner_html[1]
102
+ end
103
+
104
+ def test_fragment_append_text
105
+ text = 'Something'
106
+ return_value = @container_frag.append text
107
+ assert_equal text, return_value
108
+ assert_equal text, @container_frag.inner_html[1]
109
+ end
110
+
111
+ def test_fragment_append_error
112
+ bad_child = nil
113
+ assert_raises(ArgumentError) { @empty_frag.append bad_child }
114
+ assert_raises HTRB::SelfClosingTagError do
115
+ HTRB::Elements::Hr.new.append @div
116
+ end
117
+ end
118
+
119
+ def test_fragment_remove
120
+ return_value = @container_frag.remove @div
121
+ assert_equal @div, return_value
122
+ assert_equal 0, @container_frag.inner_html.length
123
+
124
+ return_value_empty = @container_frag.remove @div
125
+ assert_nil return_value_empty
126
+ end
127
+
128
+ def test_fragment_insert_before
129
+ image = HTRB::Elements::Img.new
130
+ return_value = @container_frag.insert image, :before, @div
131
+ assert_equal image, return_value
132
+ assert_equal image, @container_frag.inner_html[0]
133
+ end
134
+
135
+ def test_fragment_insert_after
136
+ text = 'after'
137
+ return_value = @container_frag.insert text, :after, @div
138
+ assert_equal text, return_value
139
+ assert_equal text, @container_frag.inner_html[1]
140
+ end
141
+
142
+ def test_fragment_insert_errors
143
+ assert_raises(ArgumentError) { @empty_frag.insert @div, :before, @div }
144
+ assert_raises(ArgumentError) { @container_frag.insert @div, :never, @div }
145
+ assert_raises(ArgumentError) { @container_frag.insert nil, :after, @div }
146
+
147
+ link = @div.append HTRB::Elements::Link.new
148
+ assert_raises HTRB::TagParadoxError do
149
+ @div.insert @div, :before, link
150
+ end
151
+ end
152
+
153
+ def test_self_closing_tag
154
+ hr = HTRB::Elements::Hr.new
155
+ assert_equal '<hr>', hr.to_s
156
+ end
157
+
158
+ def test_self_closing_tag_error
159
+ assert_raises HTRB::SelfClosingTagError do
160
+ HTRB::Elements::Track.new {}
161
+ end
162
+ end
163
+
164
+ def test_attribute_key_replace_underscore
165
+ br = HTRB::Elements::Br.new hx_post: '/clicked', 'hx_swap' => 'outerHTML'
166
+ assert_equal '<br hx-post="/clicked" hx-swap="outerHTML">', br.to_s
167
+ end
168
+
169
+ def test_fragment_t
170
+ em = HTRB::Elements::Em.new { t! 'Testing text' }
171
+ assert_equal '<em>Testing text</em>', em.to_s
172
+ end
173
+
174
+ def test_fagment_t_escape
175
+ strong = HTRB::Elements::Strong.new { t! 'Testing > you & < me' }
176
+ assert_equal '<strong>Testing &gt; you &amp; &lt; me</strong>', strong.to_s
177
+ end
178
+
179
+ def test_fragment_missing_method_error
180
+ assert_raises(NameError) { @empty_frag.stupid }
181
+ end
182
+
183
+ def test_fragment_props
184
+ button = HTRB::Elements::Button.new text: 'Button Text' do
185
+ t! props[:text]
186
+ end
187
+ assert_equal '<button text="Button Text">Button Text</button>', button.to_s
188
+ end
189
+
190
+ def test_component
191
+ eval <<-CLASS_DEFINITION
192
+ class Tester < HTRB::Component
193
+ def render(**attributes, &block)
194
+ t! 'Test'
195
+ remit &block if block_given?
196
+ end
197
+ end
198
+ CLASS_DEFINITION
199
+
200
+ assert_equal true, @empty_frag.respond_to?(:_tester!, true)
201
+ assert_equal true, Tester.new.self_closing?
202
+
203
+ @empty_frag.inner_html { _tester! }
204
+ assert_equal 'Test', @empty_frag.to_s
205
+
206
+ assert_raises HTRB::SelfClosingTagError do
207
+ Tester.new.inner_html {}
208
+ end
209
+ end
210
+
211
+ def test_component_duplicate_error
212
+ assert_raises HTRB::TagExistsError do
213
+ eval <<-CLASS_DEFINITION
214
+ module One
215
+ class Exists < HTRB::Component; end
216
+ end
217
+
218
+ module Another
219
+ class Exists < HTRB::Component; end
220
+ end
221
+ CLASS_DEFINITION
222
+ end
223
+ end
224
+
225
+ def test_html
226
+ return_value = HTRB.html do
227
+ div! id: 'container' do
228
+ img! src: 'lol.jpg'
229
+ end
230
+ end
231
+
232
+ assert_equal '<div id="container"><img src="lol.jpg"></div>', return_value
233
+ end
234
+
235
+ def test_document_is_document
236
+ assert_kind_of HTRB::Document, HTRB.document
237
+ end
238
+
239
+ def test_document_to_pretty
240
+ assert_equal "<!DOCTYPE html>\n<html>\n <head>\n <title>\n \n </title>\n <meta charset=\"UTF-8\">\n </head>\n <body>\n </body>\n</html>", HTRB.document.to_pretty
241
+ end
242
+
243
+ def test_document_to_s
244
+ assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body></body></html>', HTRB.document.to_s
245
+ end
246
+
247
+ def test_document_with_title
248
+ doc = HTRB.document title: 'Has a title'
249
+ assert_equal '<!DOCTYPE html><html><head><title>Has a title</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
250
+ end
251
+
252
+ def test_document_title_get
253
+ doc = HTRB.document title: 'Has a title'
254
+ assert_equal 'Has a title', doc.title
255
+ end
256
+
257
+ def test_document_title_set
258
+ doc = HTRB.document
259
+ old = HTRB.document.title
260
+ doc.title 'Nice'
261
+
262
+ refute_equal old, doc.title
263
+ assert_equal 'Nice', doc.title
264
+ assert_equal '<!DOCTYPE html><html><head><title>Nice</title><meta charset="UTF-8"></head><body></body></html>', doc.to_s
265
+ end
266
+
267
+ def test_document_pass_head
268
+ head_proc = proc { link! rel: 'stylesheet', href: 'mystyle.css' }
269
+ doc = HTRB.document head: head_proc
270
+
271
+ assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
272
+ end
273
+
274
+ def test_document_change_head
275
+ doc = HTRB.document
276
+ doc.head { link! rel: 'stylesheet', href: 'mystyle.css' }
277
+
278
+ assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"><link rel="stylesheet" href="mystyle.css"></head><body></body></html>', doc.to_s
279
+ end
280
+
281
+ def test_document_pass_body
282
+ doc = HTRB.document { t! 'This is my body' }
283
+ assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
284
+ end
285
+
286
+ def test_document_change_body
287
+ doc = HTRB.document
288
+ doc.body { t! 'This is my body' }
289
+ assert_equal '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8"></head><body>This is my body</body></html>', doc.to_s
290
+ end
291
+ end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Perkins
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-06 00:00:00.000000000 Z
11
+ date: 2023-10-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
13
+ description:
14
14
  email:
15
15
  - Christopher.Perkins@null.net
16
16
  executables: []
@@ -31,7 +31,7 @@ homepage: https://github.com/QuasariNova/htrb
31
31
  licenses:
32
32
  - MIT
33
33
  metadata: {}
34
- post_install_message:
34
+ post_install_message:
35
35
  rdoc_options: []
36
36
  require_paths:
37
37
  - lib
@@ -46,8 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  requirements: []
49
- rubygems_version: 3.2.3
50
- signing_key:
49
+ rubygems_version: 3.4.12
50
+ signing_key:
51
51
  specification_version: 4
52
52
  summary: HTRB is a dsl for html and webcomponents
53
53
  test_files: []