rails-dom-testing 2.1.1 → 2.3.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.
@@ -1 +1,4 @@
1
- require 'rails/dom/testing/assertions'
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/dom/testing"
4
+ require "rails/dom/testing/railtie" if defined?(Rails::Railtie)
@@ -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