rails-dom-testing 1.0.2 → 1.0.3

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: 25916970c49a8bacd1a2153cedaf2474cdceb73a
4
- data.tar.gz: a3ce7d48ccb543f0abf1bd2354493a3281a38a04
3
+ metadata.gz: 44d4f14bd073cd6863e98c91e68cdd0841b79f03
4
+ data.tar.gz: 52bfa7441b482a26893322386ec0774542153804
5
5
  SHA512:
6
- metadata.gz: 0c3389884b69ad22df708e36656e72cafa18b71868d6aadab3e47d0f8228ec885c84f06bde22424a6e84c9a2612ccb210b951e3fbbb3f4fa863dd26def4d5f0d
7
- data.tar.gz: 3340db215cba92832210bc2847ebb043c855461fc76fcaaa39e70463599a6b5fd2fa3030795a805ada92e9a2cf72e37cfab2d724bb865feae056ac835878540d
6
+ metadata.gz: 75a264e23323a077ae8a5d73e3f356689b7eb48d7a35e75cecce81c74423ba8b638d75dca49c593252acd18de4de6e3f1b6cd0b3e8db9e669a421b0e76339a1c
7
+ data.tar.gz: 36b2b12547bca87796e856f01684af0895a97239f649fa7e40cf66c4f49732b23965d425ce38b87f3acd8ff7703a94c528488e888342b856063141bd5d5a3df6
data/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
  This gem is responsible for comparing HTML doms and asserting that DOM elements are present in Rails applications.
5
5
  Doms are compared via `assert_dom_equal` and `assert_dom_not_equal`.
6
6
  Elements are asserted via `assert_select`, `assert_select_encoded`, `assert_select_email` and a subset of the dom can be selected with `css_select`.
7
+ The gem is developed for Rails 4.2 and above, and will not work on previous versions.
7
8
 
8
9
 
9
10
  ## Installation
@@ -40,7 +40,8 @@ module Rails
40
40
 
41
41
  if child.element?
42
42
  child.name == other_child.name &&
43
- equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes)
43
+ equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) &&
44
+ compare_doms(child, other_child)
44
45
  else
45
46
  child.to_s == other_child.to_s
46
47
  end
@@ -61,8 +61,11 @@ module Rails
61
61
  root = args.size == 1 ? document_root_element : args.shift
62
62
  selector = args.first
63
63
 
64
- catch_invalid_selector do
64
+ begin
65
65
  nodeset(root).css(selector)
66
+ rescue Nokogiri::CSS::SyntaxError => e
67
+ ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
68
+ return
66
69
  end
67
70
  end
68
71
 
@@ -167,10 +170,14 @@ module Rails
167
170
  selector = HTMLSelector.new(root, args)
168
171
 
169
172
  matches = nil
170
- catch_invalid_selector do
173
+
174
+ begin
171
175
  matches = selector.select
172
176
 
173
177
  assert_size_match!(matches.size, selector.equality_tests, selector.source, selector.message)
178
+ rescue Nokogiri::CSS::SyntaxError => e
179
+ ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
180
+ return
174
181
  end
175
182
 
176
183
  nest_selection(matches, &block) if block_given? && !matches.empty?
@@ -278,15 +285,6 @@ module Rails
278
285
  raise NotImplementedError, "Implementing document_root_element makes assert_select work without needing to specify an element to select from."
279
286
  end
280
287
 
281
- def catch_invalid_selector
282
- begin
283
- yield
284
- rescue Nokogiri::CSS::SyntaxError => e
285
- ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}")
286
- return
287
- end
288
- end
289
-
290
288
  # +equals+ must contain :minimum, :maximum and :count keys
291
289
  def assert_size_match!(size, equals, css_selector, message = nil)
292
290
  min, max, count = equals[:minimum], equals[:maximum], equals[:count]
@@ -310,7 +308,7 @@ module Rails
310
308
  elsif previous_selection
311
309
  previous_selection
312
310
  else
313
- document_root_element
311
+ nodeset document_root_element
314
312
  end
315
313
  end
316
314
 
@@ -60,6 +60,7 @@ class HTMLSelector #:nodoc:
60
60
  end
61
61
 
62
62
  context.substitute!(selector, values)
63
+ selector
63
64
  end
64
65
 
65
66
  def equality_tests_from(comparator)
@@ -1,28 +1,30 @@
1
1
  class SubstitutionContext
2
- def initialize(substitute = '?')
3
- @substitute = substitute
2
+ def initialize
3
+ @substitute = '?'
4
4
  @regexes = []
5
5
  end
6
6
 
7
- def add_regex(regex)
8
- # Nokogiri doesn't like arbitrary values without quotes, hence inspect.
9
- return regex.inspect unless regex.is_a?(Regexp)
10
- @regexes.push(regex)
11
- last_id.to_s # avoid implicit conversions of Fixnum to String
7
+ def substitute!(selector, values)
8
+ while !values.empty? && substitutable?(values.first) && selector.index(@substitute)
9
+ selector.sub! @substitute, substitution_id_for(values.shift)
10
+ end
12
11
  end
13
12
 
14
- def last_id
15
- @regexes.count - 1
13
+ def match(matches, attribute, substitution_id)
14
+ matches.find_all { |node| node[attribute] =~ @regexes[substitution_id] }
16
15
  end
17
16
 
18
- def match(matches, attribute, id)
19
- matches.find_all { |node| node[attribute] =~ @regexes[id] }
20
- end
17
+ private
18
+ def substitution_id_for(value)
19
+ if value.is_a?(Regexp)
20
+ @regexes << value
21
+ @regexes.size - 1
22
+ else
23
+ value
24
+ end.inspect # Nokogiri doesn't like arbitrary values without quotes, hence inspect.
25
+ end
21
26
 
22
- def substitute!(selector, values)
23
- while !values.empty? && selector.index(@substitute)
24
- selector.sub!(@substitute, add_regex(values.shift))
27
+ def substitutable?(value)
28
+ value.is_a?(String) || value.is_a?(Regexp)
25
29
  end
26
- selector
27
- end
28
30
  end
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Dom
3
3
  module Testing
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.3"
5
5
  end
6
6
  end
7
7
  end
@@ -40,4 +40,11 @@ class DomAssertionsTest < ActiveSupport::TestCase
40
40
 
41
41
  assert_equal e.message, message
42
42
  end
43
+
44
+ def test_unequal_dom_attributes_in_children
45
+ assert_dom_not_equal(
46
+ %{<a><b c="1" /></a>},
47
+ %{<a><b c="2" /></a>}
48
+ )
49
+ end
43
50
  end
@@ -40,6 +40,14 @@ class AssertSelectTest < ActiveSupport::TestCase
40
40
  assert_nothing_raised { assert_select "p", false }
41
41
  end
42
42
 
43
+ def test_equality_false_with_substitution
44
+ render_html %{<a></a>}
45
+
46
+ assert_nothing_raised do
47
+ assert_select %{a[href="http://example.org?query=value"]}, false
48
+ end
49
+ end
50
+
43
51
  def test_equality_false_message
44
52
  render_html %Q{<div id="1"></div><div id="2"></div>}
45
53
  assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", false }
@@ -127,6 +135,18 @@ class AssertSelectTest < ActiveSupport::TestCase
127
135
  end
128
136
  end
129
137
 
138
+ def test_assert_select_root_html
139
+ render_html '<a></a>'
140
+
141
+ assert_select 'a'
142
+ end
143
+
144
+ def test_assert_select_root_xml
145
+ render_xml '<rss version="2.0"></rss>'
146
+
147
+ assert_select 'rss'
148
+ end
149
+
130
150
  def test_nested_assert_select
131
151
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
132
152
  assert_select "div" do |elements|
@@ -233,6 +253,7 @@ class AssertSelectTest < ActiveSupport::TestCase
233
253
  </channel>
234
254
  </rss>
235
255
  EOF
256
+
236
257
  assert_select "channel item description" do
237
258
 
238
259
  assert_select_encoded do
@@ -1,3 +1,4 @@
1
1
  require 'nokogiri'
2
+ require 'active_support'
2
3
  require 'active_support/test_case'
3
4
  require 'minitest/autorun'
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.2
4
+ version: 1.0.3
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-08-19 00:00:00.000000000 Z
12
+ date: 2014-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -103,7 +103,6 @@ executables: []
103
103
  extensions: []
104
104
  extra_rdoc_files: []
105
105
  files:
106
- - CHANGELOG.md
107
106
  - LICENSE.txt
108
107
  - README.md
109
108
  - lib/rails-dom-testing.rb
@@ -138,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
137
  version: '0'
139
138
  requirements: []
140
139
  rubyforge_project:
141
- rubygems_version: 2.3.0
140
+ rubygems_version: 2.2.2
142
141
  signing_key:
143
142
  specification_version: 4
144
143
  summary: This gem can compare doms and assert certain elements exists in doms using
@@ -148,4 +147,3 @@ test_files:
148
147
  - test/selector_assertions_test.rb
149
148
  - test/tag_assertions_test.rb
150
149
  - test/test_helper.rb
151
- has_rdoc:
@@ -1,7 +0,0 @@
1
- # 1.0.2
2
-
3
- * Add deprecated `assert_tag`.
4
-
5
- # 1.0.0
6
-
7
- * First Release.