rails-dom-testing 1.0.4 → 1.0.5
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.
- checksums.yaml +4 -4
- data/lib/rails/dom/testing/assertions/selector_assertions.rb +26 -54
- data/lib/rails/dom/testing/assertions/selector_assertions/count_describable.rb +22 -0
- data/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +42 -31
- data/lib/rails/dom/testing/version.rb +1 -1
- data/test/selector_assertions_test.rb +14 -4
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a29feb41b5ab1bec20c3f358c1ff848f66db985
|
4
|
+
data.tar.gz: beaf92796b4e2d31e4e4b58515f48e8ba400344a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 823c8ce6237dd7a47600be8275a615243a887aa2e5d20bcfe5d527d189467fadb274783907645f49426132cce513f6e945c0b2bd38f386ffcd570851f75d7b70
|
7
|
+
data.tar.gz: ca1f844b515eb7de1d6201bf0341abe212b5a1502adf02d13c0051d44da5821302e920f8d8ce25f5c77ec047dc1c9f1b5c75bf7d6d51a370c1354ac47c0a48fd
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/deprecation'
|
2
|
+
require_relative 'selector_assertions/count_describable'
|
2
3
|
require_relative 'selector_assertions/html_selector'
|
3
4
|
|
4
5
|
module Rails
|
@@ -59,16 +60,11 @@ module Rails
|
|
59
60
|
raise ArgumentError, "you at least need a selector argument" if args.empty?
|
60
61
|
|
61
62
|
root = args.size == 1 ? document_root_element : args.shift
|
62
|
-
selector = args.first
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
rescue Nokogiri::CSS::SyntaxError => e
|
69
|
-
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
|
70
|
-
return
|
71
|
-
end
|
64
|
+
nodeset(root).css(args.first)
|
65
|
+
rescue Nokogiri::CSS::SyntaxError => e
|
66
|
+
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
|
67
|
+
return
|
72
68
|
end
|
73
69
|
|
74
70
|
# An assertion that selects elements and makes one or more equality tests.
|
@@ -168,37 +164,21 @@ module Rails
|
|
168
164
|
def assert_select(*args, &block)
|
169
165
|
@selected ||= nil
|
170
166
|
|
171
|
-
|
172
|
-
selector = HTMLSelector.new(root, args)
|
167
|
+
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
|
173
168
|
|
174
|
-
|
175
|
-
|
176
|
-
begin
|
177
|
-
matches = selector.select
|
178
|
-
|
179
|
-
assert_size_match!(matches.size, selector.equality_tests, selector.source, selector.message)
|
180
|
-
rescue Nokogiri::CSS::SyntaxError => e
|
181
|
-
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
|
169
|
+
if selecting_no_body?(selector)
|
170
|
+
assert true
|
182
171
|
return
|
183
172
|
end
|
184
173
|
|
185
|
-
|
174
|
+
selector.select.tap do |matches|
|
175
|
+
assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)
|
186
176
|
|
187
|
-
|
188
|
-
end
|
189
|
-
|
190
|
-
def count_description(min, max, count) #:nodoc:
|
191
|
-
pluralize = lambda {|word, quantity| word << (quantity == 1 ? '' : 's')}
|
192
|
-
|
193
|
-
if min && max && (max != min)
|
194
|
-
"between #{min} and #{max} elements"
|
195
|
-
elsif min && max && max == min && count
|
196
|
-
"exactly #{count} #{pluralize['element', min]}"
|
197
|
-
elsif min && !(min == 1 && max == 1)
|
198
|
-
"at least #{min} #{pluralize['element', min]}"
|
199
|
-
elsif max
|
200
|
-
"at most #{max} #{pluralize['element', max]}"
|
177
|
+
nest_selection(matches, &block) if block_given? && !matches.empty?
|
201
178
|
end
|
179
|
+
rescue Nokogiri::CSS::SyntaxError => e
|
180
|
+
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
|
181
|
+
return
|
202
182
|
end
|
203
183
|
|
204
184
|
# Extracts the content of an element, treats it as encoded HTML and runs
|
@@ -281,10 +261,12 @@ module Rails
|
|
281
261
|
end
|
282
262
|
end
|
283
263
|
|
284
|
-
|
264
|
+
private
|
265
|
+
include CountDescripable
|
285
266
|
|
286
267
|
def document_root_element
|
287
|
-
raise NotImplementedError,
|
268
|
+
raise NotImplementedError, 'Implementing document_root_element makes ' \
|
269
|
+
'assert_select work without needing to specify an element to select from.'
|
288
270
|
end
|
289
271
|
|
290
272
|
# +equals+ must contain :minimum, :maximum and :count keys
|
@@ -300,29 +282,19 @@ module Rails
|
|
300
282
|
end
|
301
283
|
end
|
302
284
|
|
303
|
-
def
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
elsif HTMLSelector.can_select_from?(possible_root)
|
308
|
-
args.shift # remove the root, so selector is the first argument
|
309
|
-
possible_root
|
310
|
-
elsif previous_selection
|
311
|
-
previous_selection
|
312
|
-
else
|
313
|
-
document_root_element
|
314
|
-
end
|
285
|
+
def selecting_no_body?(html_selector)
|
286
|
+
# Nokogiri gives the document a body element. Which means we can't
|
287
|
+
# run an assertion expecting there to not be a body.
|
288
|
+
html_selector.selector == 'body' && html_selector.tests[:count] == 0
|
315
289
|
end
|
316
290
|
|
317
291
|
def nest_selection(selection)
|
318
292
|
# Set @selected to allow nested assert_select.
|
319
293
|
# Can be nested several levels deep.
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
@selected = old_selected
|
325
|
-
end
|
294
|
+
old_selected, @selected = @selected, selection
|
295
|
+
yield @selected
|
296
|
+
ensure
|
297
|
+
@selected = old_selected
|
326
298
|
end
|
327
299
|
|
328
300
|
def nodeset(node)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module CountDescripable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
private
|
7
|
+
def count_description(min, max, count) #:nodoc:
|
8
|
+
if min && max && (max != min)
|
9
|
+
"between #{min} and #{max} elements"
|
10
|
+
elsif min && max && max == min && count
|
11
|
+
"exactly #{count} #{pluralize_element(min)}"
|
12
|
+
elsif min && !(min == 1 && max == 1)
|
13
|
+
"at least #{min} #{pluralize_element(min)}"
|
14
|
+
elsif max
|
15
|
+
"at most #{max} #{pluralize_element(max)}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def pluralize_element(quantity)
|
20
|
+
quantity == 1 ? 'element' : 'elements'
|
21
|
+
end
|
22
|
+
end
|
@@ -1,39 +1,37 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
1
2
|
require_relative 'substitution_context'
|
2
3
|
|
3
4
|
class HTMLSelector #:nodoc:
|
4
|
-
|
5
|
-
attr_accessor :root, :selector, :equality_tests, :message
|
6
|
-
|
7
|
-
alias :source :selector
|
5
|
+
attr_reader :selector, :tests, :message
|
8
6
|
|
9
|
-
def initialize(
|
10
|
-
@
|
11
|
-
@
|
7
|
+
def initialize(values, previous_selection = nil, &root_fallback)
|
8
|
+
@values = values
|
9
|
+
@root = extract_root(previous_selection, root_fallback)
|
10
|
+
@selector = extract_selector
|
11
|
+
@tests = extract_equality_tests
|
12
|
+
@message = @values.shift
|
12
13
|
|
13
|
-
@
|
14
|
-
@message = args.shift
|
15
|
-
|
16
|
-
if args.shift
|
14
|
+
if @values.shift
|
17
15
|
raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type"
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
|
-
class << self
|
22
|
-
def can_select_from?(selector)
|
23
|
-
selector.respond_to?(:css)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
19
|
def select
|
28
|
-
filter root.css(selector, context)
|
20
|
+
filter @root.css(selector, context)
|
29
21
|
end
|
30
22
|
|
23
|
+
private
|
24
|
+
|
25
|
+
NO_STRIP = %w{pre script style textarea}
|
26
|
+
|
27
|
+
mattr_reader(:context) { SubstitutionContext.new }
|
28
|
+
|
31
29
|
def filter(matches)
|
32
|
-
match_with =
|
30
|
+
match_with = tests[:text] || tests[:html]
|
33
31
|
return matches if matches.empty? || !match_with
|
34
32
|
|
35
33
|
content_mismatch = nil
|
36
|
-
text_matches =
|
34
|
+
text_matches = tests.has_key?(:text)
|
37
35
|
regex_matching = match_with.is_a?(Regexp)
|
38
36
|
|
39
37
|
remaining = matches.reject do |match|
|
@@ -48,24 +46,41 @@ class HTMLSelector #:nodoc:
|
|
48
46
|
true
|
49
47
|
end
|
50
48
|
|
51
|
-
|
49
|
+
@message ||= content_mismatch if remaining.empty?
|
52
50
|
Nokogiri::XML::NodeSet.new(matches.document, remaining)
|
53
51
|
end
|
54
52
|
|
55
|
-
def
|
56
|
-
|
53
|
+
def extract_root(previous_selection, root_fallback)
|
54
|
+
possible_root = @values.first
|
55
|
+
|
56
|
+
if possible_root == nil
|
57
|
+
raise ArgumentError, 'First argument is either selector or element ' \
|
58
|
+
'to select, but nil found. Perhaps you called assert_select with ' \
|
59
|
+
'an element that does not exist?'
|
60
|
+
elsif possible_root.respond_to?(:css)
|
61
|
+
@values.shift # remove the root, so selector is the first argument
|
62
|
+
possible_root
|
63
|
+
elsif previous_selection
|
64
|
+
previous_selection
|
65
|
+
else
|
66
|
+
root_fallback.call
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def extract_selector
|
71
|
+
selector = @values.shift
|
57
72
|
|
58
73
|
unless selector.is_a? String
|
59
74
|
raise ArgumentError, "Expecting a selector as the first argument"
|
60
75
|
end
|
61
76
|
|
62
|
-
context.substitute!(selector, values)
|
77
|
+
context.substitute!(selector, @values)
|
63
78
|
selector
|
64
79
|
end
|
65
80
|
|
66
|
-
def
|
81
|
+
def extract_equality_tests
|
67
82
|
comparisons = {}
|
68
|
-
case comparator
|
83
|
+
case comparator = @values.shift
|
69
84
|
when Hash
|
70
85
|
comparisons = comparator
|
71
86
|
when String, Regexp
|
@@ -90,8 +105,4 @@ class HTMLSelector #:nodoc:
|
|
90
105
|
end
|
91
106
|
comparisons
|
92
107
|
end
|
93
|
-
|
94
|
-
def context
|
95
|
-
@context ||= SubstitutionContext.new
|
96
|
-
end
|
97
|
-
end
|
108
|
+
end
|
@@ -280,10 +280,20 @@ EOF
|
|
280
280
|
end
|
281
281
|
|
282
282
|
def test_body_not_present_in_empty_document
|
283
|
-
render_html ''
|
283
|
+
render_html '<div></div>'
|
284
284
|
assert_select 'body', 0
|
285
285
|
end
|
286
286
|
|
287
|
+
def test_body_class_can_be_tested
|
288
|
+
render_html '<body class="foo"></body>'
|
289
|
+
assert_select '.foo'
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_body_class_can_be_tested_with_html
|
293
|
+
render_html '<html><body class="foo"><div></div></body></html>'
|
294
|
+
assert_select '.foo'
|
295
|
+
end
|
296
|
+
|
287
297
|
protected
|
288
298
|
def render_html(html)
|
289
299
|
fake_render(:html, html)
|
@@ -295,13 +305,13 @@ EOF
|
|
295
305
|
|
296
306
|
def fake_render(content_type, content)
|
297
307
|
@html_document = if content_type == :xml
|
298
|
-
Nokogiri::XML::
|
308
|
+
Nokogiri::XML::Document.parse(content)
|
299
309
|
else
|
300
|
-
Nokogiri::HTML::
|
310
|
+
Nokogiri::HTML::Document.parse(content)
|
301
311
|
end
|
302
312
|
end
|
303
313
|
|
304
314
|
def document_root_element
|
305
|
-
@html_document
|
315
|
+
@html_document.root
|
306
316
|
end
|
307
317
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-dom-testing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Mendonça França
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/rails/dom/testing/assertions.rb
|
116
116
|
- lib/rails/dom/testing/assertions/dom_assertions.rb
|
117
117
|
- lib/rails/dom/testing/assertions/selector_assertions.rb
|
118
|
+
- lib/rails/dom/testing/assertions/selector_assertions/count_describable.rb
|
118
119
|
- lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb
|
119
120
|
- lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
|
120
121
|
- lib/rails/dom/testing/assertions/tag_assertions.rb
|
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
144
|
version: '0'
|
144
145
|
requirements: []
|
145
146
|
rubyforge_project:
|
146
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.2.2
|
147
148
|
signing_key:
|
148
149
|
specification_version: 4
|
149
150
|
summary: This gem can compare doms and assert certain elements exists in doms using
|
@@ -153,4 +154,3 @@ test_files:
|
|
153
154
|
- test/selector_assertions_test.rb
|
154
155
|
- test/tag_assertions_test.rb
|
155
156
|
- test/test_helper.rb
|
156
|
-
has_rdoc:
|