rails-dom-testing 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65398ee552b3e2bb13fc42eccfff4620cac17bed
4
- data.tar.gz: 2765f5b8e4262a28e4c9e04f7892f75e8dd7b6ee
3
+ metadata.gz: 6a29feb41b5ab1bec20c3f358c1ff848f66db985
4
+ data.tar.gz: beaf92796b4e2d31e4e4b58515f48e8ba400344a
5
5
  SHA512:
6
- metadata.gz: 9295246ea1524127de19fe80e3428d3828606fbadcd092b5e65270bbe803d804cb9757a41438e71c568251fb9a1535b6d811951e77f13f3e6a473af614ed9d4a
7
- data.tar.gz: 41e731b6ab7cf71ca3f0e3645660354d426bdc45edc3da0d69cbaad6d93f6cf8a4e16a4f697061274d83b8fcfd89b4d88ef8fa3e5cf92ffee15f32d77f42f97e
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
- begin
65
- root.css(selector).tap do |matches|
66
- return nodeset(root).css(selector) if matches.empty?
67
- end
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
- root = determine_root_from(args, @selected)
172
- selector = HTMLSelector.new(root, args)
167
+ selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
173
168
 
174
- matches = nil
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
- nest_selection(matches, &block) if block_given? && !matches.empty?
174
+ selector.select.tap do |matches|
175
+ assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)
186
176
 
187
- matches
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
- protected
264
+ private
265
+ include CountDescripable
285
266
 
286
267
  def document_root_element
287
- raise NotImplementedError, "Implementing document_root_element makes assert_select work without needing to specify an element to select from."
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 determine_root_from(args, previous_selection = nil)
304
- possible_root = args.first
305
- if possible_root == nil
306
- raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
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
- begin
321
- old_selected, @selected = @selected, selection
322
- yield @selected
323
- ensure
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
- NO_STRIP = %w{pre script style textarea}
5
- attr_accessor :root, :selector, :equality_tests, :message
6
-
7
- alias :source :selector
5
+ attr_reader :selector, :tests, :message
8
6
 
9
- def initialize(root, args)
10
- @root = root
11
- @selector = extract_selector(args)
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
- @equality_tests = equality_tests_from(args.shift)
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 = equality_tests[:text] || equality_tests[:html]
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 = equality_tests.has_key?(:text)
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
- self.message ||= content_mismatch if remaining.empty?
49
+ @message ||= content_mismatch if remaining.empty?
52
50
  Nokogiri::XML::NodeSet.new(matches.document, remaining)
53
51
  end
54
52
 
55
- def extract_selector(values)
56
- selector = values.shift
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 equality_tests_from(comparator)
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
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Dom
3
3
  module Testing
4
- VERSION = "1.0.4"
4
+ VERSION = "1.0.5"
5
5
  end
6
6
  end
7
7
  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::DocumentFragment.parse(content)
308
+ Nokogiri::XML::Document.parse(content)
299
309
  else
300
- Nokogiri::HTML::DocumentFragment.parse(content)
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
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-10-16 00:00:00.000000000 Z
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.4.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: