rails-dom-testing 2.0.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+ require "active_support"
5
+ require "active_support/core_ext/module/attribute_accessors"
6
+
7
+ require "rails/dom/testing/assertions"
8
+
9
+ module Rails
10
+ module Dom
11
+ module Testing
12
+ mattr_accessor :default_html_version, default: :html4
13
+
14
+ class << self
15
+ def html5_support?
16
+ defined?(Nokogiri::HTML5)
17
+ end
18
+
19
+ def html_document(html_version: nil)
20
+ parser_classes = { html4: Nokogiri::HTML4::Document }
21
+ parser_classes[:html5] = Nokogiri::HTML5::Document if html5_support?
22
+
23
+ choose_html_parser(parser_classes, html_version: html_version)
24
+ end
25
+
26
+ def html_document_fragment(html_version: nil)
27
+ parser_classes = { html4: Nokogiri::HTML4::DocumentFragment }
28
+ parser_classes[:html5] = Nokogiri::HTML5::DocumentFragment if html5_support?
29
+
30
+ choose_html_parser(parser_classes, html_version: html_version)
31
+ end
32
+
33
+ private
34
+ def choose_html_parser(parser_classes, html_version: nil)
35
+ html_version ||= Rails::Dom::Testing.default_html_version
36
+
37
+ case html_version
38
+ when :html4
39
+ parser_classes[:html4]
40
+ when :html5
41
+ unless Rails::Dom::Testing.html5_support?
42
+ raise NotImplementedError, "html5 parser is not supported on this platform"
43
+ end
44
+ parser_classes[:html5]
45
+ else
46
+ raise ArgumentError, "html_version must be :html4 or :html5, received #{html_version.inspect}"
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -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
@@ -47,4 +48,166 @@ class DomAssertionsTest < ActiveSupport::TestCase
47
48
  %{<a><b c="2" /></a>}
48
49
  )
49
50
  end
50
- end
51
+
52
+ def test_dom_equal_with_whitespace_strict
53
+ canonical = %{<a><b>hello</b> world</a>}
54
+ assert_dom_not_equal(canonical, %{<a>\n<b>hello\n </b> world</a>}, strict: true)
55
+ assert_dom_not_equal(canonical, %{<a> \n <b>\n hello</b> world</a>}, strict: true)
56
+ assert_dom_not_equal(canonical, %{<a>\n\t<b>hello</b> world</a>}, strict: true)
57
+ assert_dom_equal(canonical, %{<a><b>hello</b> world</a>}, strict: true)
58
+ end
59
+
60
+ def test_dom_equal_with_whitespace
61
+ canonical = %{<a><b>hello</b> world</a>}
62
+ assert_dom_equal(canonical, %{<a>\n<b>hello\n </b> world</a>})
63
+ assert_dom_equal(canonical, %{<a>\n<b>hello </b>\nworld</a>})
64
+ assert_dom_equal(canonical, %{<a> \n <b>\n hello</b> world</a>})
65
+ assert_dom_equal(canonical, %{<a> \n <b> hello </b>world</a>})
66
+ assert_dom_equal(canonical, %{<a> \n <b>hello </b>world\n</a>\n})
67
+ assert_dom_equal(canonical, %{<a>\n\t<b>hello</b> world</a>})
68
+ assert_dom_equal(canonical, %{<a>\n\t<b>hello </b>\n\tworld</a>})
69
+ end
70
+
71
+ def test_dom_equal_with_attribute_whitespace
72
+ canonical = %(<div data-wow="Don't strip this">)
73
+ assert_dom_equal(canonical, %(<div data-wow="Don't strip this">))
74
+ assert_dom_not_equal(canonical, %(<div data-wow="Don't strip this">))
75
+ end
76
+
77
+ def test_dom_equal_with_indentation
78
+ canonical = %{<a>hello <b>cruel</b> world</a>}
79
+ assert_dom_equal(canonical, <<-HTML)
80
+ <a>
81
+ hello
82
+ <b>cruel</b>
83
+ world
84
+ </a>
85
+ HTML
86
+
87
+ assert_dom_equal(canonical, <<-HTML)
88
+ <a>
89
+ hello
90
+ <b>cruel</b>
91
+ world
92
+ </a>
93
+ HTML
94
+
95
+ assert_dom_equal(canonical, <<-HTML)
96
+ <a>hello
97
+ <b>
98
+ cruel
99
+ </b>
100
+ world</a>
101
+ HTML
102
+ end
103
+
104
+ def test_dom_equal_with_surrounding_whitespace
105
+ canonical = %{<p>Lorem ipsum dolor</p><p>sit amet, consectetur adipiscing elit</p>}
106
+ assert_dom_equal(canonical, <<-HTML)
107
+ <p>
108
+ Lorem
109
+ ipsum
110
+ dolor
111
+ </p>
112
+
113
+ <p>
114
+ sit amet,
115
+ consectetur
116
+ adipiscing elit
117
+ </p>
118
+ HTML
119
+ end
120
+
121
+ def test_dom_not_equal_with_interior_whitespace
122
+ with_space = %{<a><b>hello world</b></a>}
123
+ without_space = %{<a><b>helloworld</b></a>}
124
+ assert_dom_not_equal(with_space, without_space)
125
+ end
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