rails-dom-testing 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
- require 'test_helper'
2
- require 'rails/dom/testing/assertions/dom_assertions'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
3
4
 
4
5
  class DomAssertionsTest < ActiveSupport::TestCase
5
6
  Assertion = Minitest::Assertion
@@ -11,7 +12,7 @@ class DomAssertionsTest < ActiveSupport::TestCase
11
12
  end
12
13
 
13
14
  def test_dom_equal
14
- html = '<a></a>'
15
+ html = "<a></a>"
15
16
  assert_dom_equal(html, html.dup)
16
17
  end
17
18
 
@@ -22,7 +23,7 @@ class DomAssertionsTest < ActiveSupport::TestCase
22
23
  end
23
24
 
24
25
  def test_dom_not_equal
25
- assert_dom_not_equal('<a></a>', '<b></b>')
26
+ assert_dom_not_equal("<a></a>", "<b></b>")
26
27
  end
27
28
 
28
29
  def test_unequal_doms_attributes_with_different_order_and_values
@@ -35,7 +36,7 @@ class DomAssertionsTest < ActiveSupport::TestCase
35
36
  message = "This is my message."
36
37
 
37
38
  e = assert_raises(Assertion) do
38
- assert_dom_equal('<a></a>', '<b></b>', message)
39
+ assert_dom_equal("<a></a>", "<b></b>", message)
39
40
  end
40
41
 
41
42
  assert_equal e.message, message
@@ -123,3 +124,90 @@ world
123
124
  assert_dom_not_equal(with_space, without_space)
124
125
  end
125
126
  end
127
+
128
+ class DomAssertionsHtmlParserSelectionTest < ActiveSupport::TestCase
129
+ include DomTestingHelpers
130
+ include Rails::Dom::Testing::Assertions::DomAssertions
131
+
132
+ def setup
133
+ super
134
+
135
+ # https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser
136
+ # we use these results to assert that we're invoking the expected parser.
137
+ @input = "<p>1<b>2<i>3</b>4</i>5</p>"
138
+ @html4_result = jruby? ? "<p>1<b>2<i>3</i></b><i>4</i>5</p>" : "<p>1<b>2<i>3</i></b>45</p>"
139
+ @html5_result = jruby? ? nil : "<p>1<b>2<i>3</i></b><i>4</i>5</p>"
140
+ end
141
+
142
+ test "default value is html4" do
143
+ assert_equal(:html4, Rails::Dom::Testing.default_html_version)
144
+ end
145
+
146
+ test "default html4, no version specified" do
147
+ with_default_html_version(:html4) do
148
+ assert_dom_equal(@html4_result, @input)
149
+ assert_dom_not_equal(@html5_result, @input)
150
+ end
151
+ end
152
+
153
+ test "default html4, html4 specified" do
154
+ with_default_html_version(:html4) do
155
+ assert_dom_equal(@html4_result, @input, html_version: :html4)
156
+ assert_dom_not_equal(@html5_result, @input, html_version: :html4)
157
+ end
158
+ end
159
+
160
+ test "default html4, html5 specified" do
161
+ skip("html5 is not supported") unless Rails::Dom::Testing.html5_support?
162
+
163
+ with_default_html_version(:html4) do
164
+ assert_dom_equal(@html5_result, @input, html_version: :html5)
165
+ assert_dom_not_equal(@html4_result, @input, html_version: :html5)
166
+ end
167
+ end
168
+
169
+ test "default html5, no version specified" do
170
+ skip("html5 is not supported") unless Rails::Dom::Testing.html5_support?
171
+
172
+ with_default_html_version(:html5) do
173
+ assert_dom_equal(@html5_result, @input)
174
+ assert_dom_not_equal(@html4_result, @input)
175
+ end
176
+ end
177
+
178
+ test "default html5, html4 specified" do
179
+ with_default_html_version(:html5) do
180
+ assert_dom_equal(@html4_result, @input, html_version: :html4)
181
+ assert_dom_not_equal(@html5_result, @input, html_version: :html4)
182
+ end
183
+ end
184
+
185
+ test "default html5, html5 specified" do
186
+ skip("html5 is not supported") unless Rails::Dom::Testing.html5_support?
187
+
188
+ with_default_html_version(:html5) do
189
+ assert_dom_equal(@html5_result, @input, html_version: :html5)
190
+ assert_dom_not_equal(@html4_result, @input, html_version: :html5)
191
+ end
192
+ end
193
+
194
+ test "raise NotImplementedError html5 when not supported" do
195
+ Rails::Dom::Testing.stub(:html5_support?, false) do
196
+ with_default_html_version(:html5) do
197
+ assert_raises(NotImplementedError) { assert_dom_equal("a", "b") }
198
+ assert_raises(NotImplementedError) { assert_dom_equal("a", "b", html_version: :html5) }
199
+ assert_nothing_raised { assert_dom_equal(@html4_result, @input, html_version: :html4) }
200
+ end
201
+ end
202
+ end
203
+
204
+ test "default set to invalid" do
205
+ with_default_html_version(:html9) do
206
+ assert_raises(ArgumentError) { assert_dom_equal("a", "b") }
207
+ end
208
+ end
209
+
210
+ test "invalid version specified" do
211
+ assert_raises(ArgumentError) { assert_dom_equal("a", "b", html_version: :html9) }
212
+ end
213
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class DomTestingParserSelectionTest < ActiveSupport::TestCase
6
+ include DomTestingHelpers
7
+
8
+ test "with default html4" do
9
+ with_default_html_version(:html4) do
10
+ assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document)
11
+ assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment)
12
+
13
+ assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4))
14
+ assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4))
15
+
16
+ if Rails::Dom::Testing.html5_support?
17
+ assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5))
18
+ assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5))
19
+ else
20
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) }
21
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) }
22
+ end
23
+
24
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) }
25
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) }
26
+ end
27
+ end
28
+
29
+ test "with default html5" do
30
+ with_default_html_version(:html5) do
31
+ if Rails::Dom::Testing.html5_support?
32
+ assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document)
33
+ assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment)
34
+ else
35
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document }
36
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment }
37
+ end
38
+
39
+ assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4))
40
+ assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4))
41
+
42
+ if Rails::Dom::Testing.html5_support?
43
+ assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5))
44
+ assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5))
45
+ else
46
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) }
47
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) }
48
+ end
49
+
50
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) }
51
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) }
52
+ end
53
+ end
54
+
55
+ test "with invalid default" do
56
+ with_default_html_version(:html8) do
57
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document }
58
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment }
59
+
60
+ assert_equal(Nokogiri::HTML4::Document, Rails::Dom::Testing.html_document(html_version: :html4))
61
+ assert_equal(Nokogiri::HTML4::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html4))
62
+
63
+ if Rails::Dom::Testing.html5_support?
64
+ assert_equal(Nokogiri::HTML5::Document, Rails::Dom::Testing.html_document(html_version: :html5))
65
+ assert_equal(Nokogiri::HTML5::DocumentFragment, Rails::Dom::Testing.html_document_fragment(html_version: :html5))
66
+ else
67
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document(html_version: :html5) }
68
+ assert_raises(NotImplementedError) { Rails::Dom::Testing.html_document_fragment(html_version: :html5) }
69
+ end
70
+
71
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document(html_version: :html9) }
72
+ assert_raises(ArgumentError) { Rails::Dom::Testing.html_document_fragment(html_version: :html9) }
73
+ end
74
+ end
75
+ end
@@ -1,11 +1,12 @@
1
1
  # encoding: utf-8
2
+ # frozen_string_literal: true
2
3
 
3
- require 'test_helper'
4
- require 'rails/dom/testing/assertions/selector_assertions'
4
+ require "test_helper"
5
5
 
6
6
  class AssertSelectTest < ActiveSupport::TestCase
7
7
  Assertion = Minitest::Assertion
8
8
 
9
+ include DomTestingHelpers
9
10
  include Rails::Dom::Testing::Assertions::SelectorAssertions
10
11
 
11
12
  def assert_failure(message, &block)
@@ -19,19 +20,19 @@ class AssertSelectTest < ActiveSupport::TestCase
19
20
  #
20
21
 
21
22
  def test_assert_select
22
- render_html %Q{<div id="1"></div><div id="2"></div>}
23
+ render_html '<div id="1"></div><div id="2"></div>'
23
24
  assert_select "div", 2
24
- assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
25
+ assert_failure(/Expected at least 1 element matching "p", found 0/) { assert_select "p" }
25
26
  end
26
27
 
27
28
  def test_equality_integer
28
- render_html %Q{<div id="1"></div><div id="2"></div>}
29
- assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
30
- assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", 0 }
29
+ render_html '<div id="1"></div><div id="2"></div>'
30
+ assert_failure(/Expected exactly 3 elements matching "div", found 2/) { assert_select "div", 3 }
31
+ assert_failure(/Expected exactly 0 elements matching "div", found 2/) { assert_select "div", 0 }
31
32
  end
32
33
 
33
34
  def test_equality_true_false
34
- render_html %Q{<div id="1"></div><div id="2"></div>}
35
+ render_html '<div id="1"></div><div id="2"></div>'
35
36
  assert_nothing_raised { assert_select "div" }
36
37
  assert_raise(Assertion) { assert_select "p" }
37
38
  assert_nothing_raised { assert_select "div", true }
@@ -49,21 +50,21 @@ class AssertSelectTest < ActiveSupport::TestCase
49
50
  end
50
51
 
51
52
  def test_equality_false_message
52
- render_html %Q{<div id="1"></div><div id="2"></div>}
53
- assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", false }
53
+ render_html '<div id="1"></div><div id="2"></div>'
54
+ assert_failure(/Expected exactly 0 elements matching "div", found 2/) { assert_select "div", false }
54
55
  end
55
56
 
56
57
  def test_equality_string_and_regexp
57
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
58
+ render_html '<div id="1">foo</div><div id="2">foo</div>'
58
59
  assert_nothing_raised { assert_select "div", "foo" }
59
60
  assert_raise(Assertion) { assert_select "div", "bar" }
60
- assert_nothing_raised { assert_select "div", :text=>"foo" }
61
- assert_raise(Assertion) { assert_select "div", :text=>"bar" }
61
+ assert_nothing_raised { assert_select "div", text: "foo" }
62
+ assert_raise(Assertion) { assert_select "div", text: "bar" }
62
63
  assert_nothing_raised { assert_select "div", /(foo|bar)/ }
63
64
  assert_raise(Assertion) { assert_select "div", /foobar/ }
64
- assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ }
65
- assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
66
- assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
65
+ assert_nothing_raised { assert_select "div", text: /(foo|bar)/ }
66
+ assert_raise(Assertion) { assert_select "div", text: /foobar/ }
67
+ assert_raise(Assertion) { assert_select "p", text: /foobar/ }
67
68
  end
68
69
 
69
70
  def test_equality_of_html
@@ -72,57 +73,57 @@ class AssertSelectTest < ActiveSupport::TestCase
72
73
  html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
73
74
  assert_nothing_raised { assert_select "p", text }
74
75
  assert_raise(Assertion) { assert_select "p", html }
75
- assert_nothing_raised { assert_select "p", :html=>html }
76
- assert_raise(Assertion) { assert_select "p", :html=>text }
76
+ assert_nothing_raised { assert_select "p", html: html }
77
+ assert_raise(Assertion) { assert_select "p", html: text }
77
78
  # No stripping for pre.
78
79
  render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
79
80
  text = "\n\"This is not a big problem,\" he said.\n"
80
81
  html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
81
82
  assert_nothing_raised { assert_select "pre", text }
82
83
  assert_raise(Assertion) { assert_select "pre", html }
83
- assert_nothing_raised { assert_select "pre", :html=>html }
84
- assert_raise(Assertion) { assert_select "pre", :html=>text }
84
+ assert_nothing_raised { assert_select "pre", html: html }
85
+ assert_raise(Assertion) { assert_select "pre", html: text }
85
86
  end
86
87
 
87
88
  def test_strip_textarea
88
- render_html %Q{<textarea>\n\nfoo\n</textarea>}
89
+ render_html "<textarea>\n\nfoo\n</textarea>"
89
90
  assert_select "textarea", "\nfoo\n"
90
- render_html %Q{<textarea>\nfoo</textarea>}
91
+ render_html "<textarea>\nfoo</textarea>"
91
92
  assert_select "textarea", "foo"
92
93
  end
93
94
 
94
95
  def test_counts
95
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
96
+ render_html '<div id="1">foo</div><div id="2">foo</div>'
96
97
  assert_nothing_raised { assert_select "div", 2 }
97
- assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
98
+ assert_failure(/Expected exactly 3 elements matching "div", found 2/) do
98
99
  assert_select "div", 3
99
100
  end
100
101
  assert_nothing_raised { assert_select "div", 1..2 }
101
- assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
102
+ assert_failure(/Expected between 3 and 4 elements matching "div", found 2/) do
102
103
  assert_select "div", 3..4
103
104
  end
104
- assert_nothing_raised { assert_select "div", :count=>2 }
105
- assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
106
- assert_select "div", :count=>3
105
+ assert_nothing_raised { assert_select "div", count: 2 }
106
+ assert_failure(/Expected exactly 3 elements matching "div", found 2/) do
107
+ assert_select "div", count: 3
107
108
  end
108
- assert_nothing_raised { assert_select "div", :minimum=>1 }
109
- assert_nothing_raised { assert_select "div", :minimum=>2 }
110
- assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
111
- assert_select "div", :minimum=>3
109
+ assert_nothing_raised { assert_select "div", minimum: 1 }
110
+ assert_nothing_raised { assert_select "div", minimum: 2 }
111
+ assert_failure(/Expected at least 3 elements matching "div", found 2/) do
112
+ assert_select "div", minimum: 3
112
113
  end
113
- assert_nothing_raised { assert_select "div", :maximum=>2 }
114
- assert_nothing_raised { assert_select "div", :maximum=>3 }
115
- assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
116
- assert_select "div", :maximum=>1
114
+ assert_nothing_raised { assert_select "div", maximum: 2 }
115
+ assert_nothing_raised { assert_select "div", maximum: 3 }
116
+ assert_failure(/Expected at most 1 element matching "div", found 2/) do
117
+ assert_select "div", maximum: 1
117
118
  end
118
- assert_nothing_raised { assert_select "div", :minimum=>1, :maximum=>2 }
119
- assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
120
- assert_select "div", :minimum=>3, :maximum=>4
119
+ assert_nothing_raised { assert_select "div", minimum: 1, maximum: 2 }
120
+ assert_failure(/Expected between 3 and 4 elements matching "div", found 2/) do
121
+ assert_select "div", minimum: 3, maximum: 4
121
122
  end
122
123
  end
123
124
 
124
125
  def test_substitution_values
125
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
126
+ render_html '<div id="1">foo</div><div id="2">foo</div>'
126
127
  assert_select "div:match('id', ?)", /\d+/ do |elements|
127
128
  assert_equal 2, elements.size
128
129
  end
@@ -135,13 +136,18 @@ class AssertSelectTest < ActiveSupport::TestCase
135
136
  end
136
137
  end
137
138
 
139
+ def test_substitution_value_with_quotes
140
+ render_html '<input placeholder="placeholder with &quot;quotes&quot;" />'
141
+ assert_select "input[placeholder=?]", 'placeholder with "quotes"'
142
+ end
143
+
138
144
  def test_multiple_substitution_values
139
145
  render_html '<input name="foo[12]" value="34">'
140
146
  assert_select ":match('name', ?):match('value', ?)", /\w+\[\d+\]/, /\d+/
141
147
  end
142
148
 
143
149
  def test_substitution_values_with_values_other_than_string_or_regexp
144
- render_html %Q{<div id="id_string">symbol</div><div id="1">numeric</div>}
150
+ render_html '<div id="id_string">symbol</div><div id="1">numeric</div>'
145
151
  assert_select "div:match('id', ?)", :id_string do |elements|
146
152
  assert_equal 1, elements.size
147
153
  end
@@ -151,19 +157,19 @@ class AssertSelectTest < ActiveSupport::TestCase
151
157
  end
152
158
 
153
159
  def test_assert_select_root_html
154
- render_html '<a></a>'
160
+ render_html "<a></a>"
155
161
 
156
- assert_select 'a'
162
+ assert_select "a"
157
163
  end
158
164
 
159
165
  def test_assert_select_root_xml
160
166
  render_xml '<rss version="2.0"></rss>'
161
167
 
162
- assert_select 'rss'
168
+ assert_select "rss"
163
169
  end
164
170
 
165
171
  def test_nested_assert_select
166
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
172
+ render_html '<div id="1">foo</div><div id="2">foo</div>'
167
173
  assert_select "div" do |elements|
168
174
  assert_equal 2, elements.size
169
175
  assert_select elements, "#1"
@@ -181,7 +187,7 @@ class AssertSelectTest < ActiveSupport::TestCase
181
187
  end
182
188
  end
183
189
 
184
- assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
190
+ assert_failure(/Expected at least 1 element matching "#4", found 0\./) do
185
191
  assert_select "div" do
186
192
  assert_select "#4"
187
193
  end
@@ -189,18 +195,18 @@ class AssertSelectTest < ActiveSupport::TestCase
189
195
  end
190
196
 
191
197
  def test_assert_select_text_match
192
- render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
198
+ render_html '<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>'
193
199
  assert_select "div" do
194
200
  assert_nothing_raised { assert_select "div", "foo" }
195
201
  assert_nothing_raised { assert_select "div", "bar" }
196
202
  assert_nothing_raised { assert_select "div", /\w*/ }
197
- assert_nothing_raised { assert_select "div", :text => /\w*/, :count=>2 }
198
- assert_raise(Assertion) { assert_select "div", :text=>"foo", :count=>2 }
199
- assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
200
- assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" }
201
- assert_nothing_raised { assert_select "div", :html=>/\w*/ }
202
- assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 }
203
- assert_raise(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
203
+ assert_nothing_raised { assert_select "div", text: /\w*/, count: 2 }
204
+ assert_raise(Assertion) { assert_select "div", text: "foo", count: 2 }
205
+ assert_nothing_raised { assert_select "div", html: "<span>bar</span>" }
206
+ assert_nothing_raised { assert_select "div", html: "<span>bar</span>" }
207
+ assert_nothing_raised { assert_select "div", html: /\w*/ }
208
+ assert_nothing_raised { assert_select "div", html: /\w*/, count: 2 }
209
+ assert_raise(Assertion) { assert_select "div", html: "<span>foo</span>", count: 2 }
204
210
  end
205
211
  end
206
212
 
@@ -209,13 +215,13 @@ class AssertSelectTest < ActiveSupport::TestCase
209
215
  #
210
216
 
211
217
  def test_css_select
212
- render_html %Q{<div id="1"></div><div id="2"></div>}
218
+ render_html '<div id="1"></div><div id="2"></div>'
213
219
  assert_equal 2, css_select("div").size
214
220
  assert_equal 0, css_select("p").size
215
221
  end
216
222
 
217
223
  def test_nested_css_select
218
- render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
224
+ render_html '<div id="1">foo</div><div id="2">foo</div>'
219
225
  assert_select "div:match('id', ?)", /\d+/ do |elements|
220
226
  assert_equal 1, css_select(elements[0], "div").size
221
227
  assert_equal 1, css_select(elements[1], "div").size
@@ -224,10 +230,10 @@ class AssertSelectTest < ActiveSupport::TestCase
224
230
  assert_equal 2, css_select("div").size
225
231
  css_select("div").each do |element|
226
232
  # Testing as a group is one thing
227
- assert !css_select("#1,#2").empty?
233
+ assert_not css_select("#1,#2").empty?
228
234
  # Testing individually is another
229
- assert !css_select("#1").empty?
230
- assert !css_select("#2").empty?
235
+ assert_not css_select("#1").empty?
236
+ assert_not css_select("#2").empty?
231
237
  end
232
238
  end
233
239
  end
@@ -248,13 +254,13 @@ class AssertSelectTest < ActiveSupport::TestCase
248
254
  end
249
255
 
250
256
  def test_nested_assert_select_with_match_failure_shows_nice_regex
251
- render_html %Q{<div id="1">foo</div>}
257
+ render_html '<div id="1">foo</div>'
252
258
 
253
259
  error = assert_raises Minitest::Assertion do
254
260
  assert_select "div:match('id', ?)", /wups/
255
261
  end
256
262
 
257
- assert_match %Q{div:match('id', /wups/)}, error.message
263
+ assert_match "div:match('id', /wups/)", error.message
258
264
  end
259
265
 
260
266
  def test_feed_item_encoded
@@ -275,12 +281,11 @@ class AssertSelectTest < ActiveSupport::TestCase
275
281
  </item>
276
282
  </channel>
277
283
  </rss>
278
- EOF
284
+ EOF
279
285
 
280
286
  assert_select "channel item description" do
281
-
282
287
  assert_select_encoded do
283
- assert_select "p", :count=>2, :text=>/Test/
288
+ assert_select "p", count: 2, text: /Test/
284
289
  end
285
290
 
286
291
  # Test individually.
@@ -302,23 +307,69 @@ EOF
302
307
  end
303
308
  end
304
309
 
310
+ def test_feed_item_encoded_with_html_version
311
+ # https://html.spec.whatwg.org/multipage/parsing.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser
312
+ # we use these results to assert that we're invoking the expected parser.
313
+ input = CGI.escapeHTML("<p>1<b>2<i>3</b>4</i>5</p>")
314
+ html4_result = jruby? ? "<p>1<b>2<i>3</i></b><i>4</i>5</p>" : "<p>1<b>2<i>3</i></b>45</p>"
315
+ html5_result = jruby? ? nil : "<p>1<b>2<i>3</i></b><i>4</i>5</p>"
316
+
317
+ render_xml(<<~XML)
318
+ <root>
319
+ <contents>#{input}</contents>
320
+ </root>
321
+ XML
322
+
323
+ with_default_html_version(:html4) do
324
+ assert_select "root contents" do
325
+ assert_select_encoded do |contents|
326
+ assert_equal(html4_result, contents.to_html)
327
+ end
328
+
329
+ assert_select_encoded(html_version: :html4) do |contents|
330
+ assert_equal(html4_result, contents.to_html)
331
+ end
332
+
333
+ assert_select_encoded(html_version: :html5) do |contents|
334
+ assert_equal(html5_result, contents.to_html)
335
+ end if Rails::Dom::Testing.html5_support?
336
+ end
337
+ end
338
+
339
+ with_default_html_version(:html5) do
340
+ assert_select "root contents" do
341
+ assert_select_encoded do |contents|
342
+ assert_equal(html5_result, contents.to_html)
343
+ end if Rails::Dom::Testing.html5_support?
344
+
345
+ assert_select_encoded(html_version: :html4) do |contents|
346
+ assert_equal(html4_result, contents.to_html)
347
+ end
348
+
349
+ assert_select_encoded(html_version: :html5) do |contents|
350
+ assert_equal(html5_result, contents.to_html)
351
+ end if Rails::Dom::Testing.html5_support?
352
+ end
353
+ end
354
+ end
355
+
305
356
  def test_body_not_present_in_empty_document
306
- render_html '<div></div>'
307
- assert_select 'body', 0
357
+ render_html "<div></div>"
358
+ assert_select "body", 0
308
359
  end
309
360
 
310
361
  def test_body_class_can_be_tested
311
362
  render_html '<body class="foo"></body>'
312
- assert_select '.foo'
363
+ assert_select ".foo"
313
364
  end
314
365
 
315
366
  def test_body_class_can_be_tested_with_html
316
367
  render_html '<html><body class="foo"><div></div></body></html>'
317
- assert_select '.foo'
368
+ assert_select ".foo"
318
369
  end
319
370
 
320
371
  def test_assert_select_with_extra_argument
321
- render_html '<html><head><title>Welcome</title></head><body><div></div></body></html>'
372
+ render_html "<html><head><title>Welcome</title></head><body><div></div></body></html>"
322
373
 
323
374
  assert_raises ArgumentError do
324
375
  assert_select "title", "Welcome", count: 1
@@ -330,7 +381,7 @@ EOF
330
381
  def test_assert_select_on_blank_response
331
382
  render_html ""
332
383
  assert_select "div", 0
333
- assert_failure(/Expected exactly 1 element matching \"div\", found 0./) do
384
+ assert_failure(/Expected exactly 1 element matching "div", found 0./) do
334
385
  assert_select "div", 1
335
386
  end
336
387
  end
@@ -348,7 +399,7 @@ EOF
348
399
  @html_document = if content_type == :xml
349
400
  Nokogiri::XML::Document.parse(content)
350
401
  else
351
- Nokogiri::HTML::Document.parse(content)
402
+ Rails::Dom::Testing.html_document.parse(content)
352
403
  end
353
404
  end
354
405
 
data/test/test_helper.rb CHANGED
@@ -1,6 +1,23 @@
1
- require 'nokogiri'
2
- require 'active_support'
3
- require 'active_support/test_case'
4
- require 'minitest/autorun'
1
+ # frozen_string_literal: true
2
+
3
+ require "rails-dom-testing"
4
+ require "active_support/test_case"
5
+ require "minitest/autorun"
5
6
 
6
7
  ActiveSupport::TestCase.test_order = :random
8
+
9
+ module DomTestingHelpers
10
+ def jruby?
11
+ !! Nokogiri.jruby?
12
+ end
13
+
14
+ def with_default_html_version(version)
15
+ old_version = Rails::Dom::Testing.default_html_version
16
+ begin
17
+ Rails::Dom::Testing.default_html_version = version
18
+ yield
19
+ ensure
20
+ Rails::Dom::Testing.default_html_version = old_version
21
+ end
22
+ end
23
+ end