rails-dom-testing 1.0.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e4c5e946843592cec1b0b7e17a9dc5fe9b2b0ceb
4
- data.tar.gz: 4cae76910af7476691d495a0ab84863e363824e3
3
+ metadata.gz: 25916970c49a8bacd1a2153cedaf2474cdceb73a
4
+ data.tar.gz: a3ce7d48ccb543f0abf1bd2354493a3281a38a04
5
5
  SHA512:
6
- metadata.gz: d4ead231de2ed2c888eba70d866eae3a58a7eb853c5863b67278a87e4b164beae052af93c3c6c36b51ef198964a7613b8c874ee9a3fc60ec0168484ce92b5e10
7
- data.tar.gz: 7744ed519a6cff2f3e77395aced9a5d104e034ebddb7370897f53ad68d55f00917bfff1891fe8d4f494754f23630b7899c0afd1147c49dbbf7b2460dd8feb5a8
6
+ metadata.gz: 0c3389884b69ad22df708e36656e72cafa18b71868d6aadab3e47d0f8228ec885c84f06bde22424a6e84c9a2612ccb210b951e3fbbb3f4fa863dd26def4d5f0d
7
+ data.tar.gz: 3340db215cba92832210bc2847ebb043c855461fc76fcaaa39e70463599a6b5fd2fa3030795a805ada92e9a2cf72e37cfab2d724bb865feae056ac835878540d
@@ -1,3 +1,7 @@
1
+ # 1.0.2
2
+
3
+ * Add deprecated `assert_tag`.
4
+
1
5
  # 1.0.0
2
6
 
3
7
  * First Release.
@@ -7,11 +7,13 @@ module Rails
7
7
  module Assertions
8
8
  autoload :DomAssertions, 'rails/dom/testing/assertions/dom_assertions'
9
9
  autoload :SelectorAssertions, 'rails/dom/testing/assertions/selector_assertions'
10
+ autoload :TagAssertions, 'rails/dom/testing/assertions/tag_assertions'
10
11
 
11
12
  extend ActiveSupport::Concern
12
13
 
13
14
  include DomAssertions
14
15
  include SelectorAssertions
16
+ include TagAssertions
15
17
  end
16
18
  end
17
19
  end
@@ -0,0 +1,155 @@
1
+ require 'active_support/deprecation'
2
+ require 'rails/deprecated_sanitizer/html-scanner'
3
+
4
+ module Rails
5
+ module Dom
6
+ module Testing
7
+ module Assertions
8
+ # Pair of assertions to testing elements in the HTML output of the response.
9
+ module TagAssertions
10
+ # Asserts that there is a tag/node/element in the body of the response
11
+ # that meets all of the given conditions. The +conditions+ parameter must
12
+ # be a hash of any of the following keys (all are optional):
13
+ #
14
+ # * <tt>:tag</tt>: the node type must match the corresponding value
15
+ # * <tt>:attributes</tt>: a hash. The node's attributes must match the
16
+ # corresponding values in the hash.
17
+ # * <tt>:parent</tt>: a hash. The node's parent must match the
18
+ # corresponding hash.
19
+ # * <tt>:child</tt>: a hash. At least one of the node's immediate children
20
+ # must meet the criteria described by the hash.
21
+ # * <tt>:ancestor</tt>: a hash. At least one of the node's ancestors must
22
+ # meet the criteria described by the hash.
23
+ # * <tt>:descendant</tt>: a hash. At least one of the node's descendants
24
+ # must meet the criteria described by the hash.
25
+ # * <tt>:sibling</tt>: a hash. At least one of the node's siblings must
26
+ # meet the criteria described by the hash.
27
+ # * <tt>:after</tt>: a hash. The node must be after any sibling meeting
28
+ # the criteria described by the hash, and at least one sibling must match.
29
+ # * <tt>:before</tt>: a hash. The node must be before any sibling meeting
30
+ # the criteria described by the hash, and at least one sibling must match.
31
+ # * <tt>:children</tt>: a hash, for counting children of a node. Accepts
32
+ # the keys:
33
+ # * <tt>:count</tt>: either a number or a range which must equal (or
34
+ # include) the number of children that match.
35
+ # * <tt>:less_than</tt>: the number of matching children must be less
36
+ # than this number.
37
+ # * <tt>:greater_than</tt>: the number of matching children must be
38
+ # greater than this number.
39
+ # * <tt>:only</tt>: another hash consisting of the keys to use
40
+ # to match on the children, and only matching children will be
41
+ # counted.
42
+ # * <tt>:content</tt>: the textual content of the node must match the
43
+ # given value. This will not match HTML tags in the body of a
44
+ # tag--only text.
45
+ #
46
+ # Conditions are matched using the following algorithm:
47
+ #
48
+ # * if the condition is a string, it must be a substring of the value.
49
+ # * if the condition is a regexp, it must match the value.
50
+ # * if the condition is a number, the value must match number.to_s.
51
+ # * if the condition is +true+, the value must not be +nil+.
52
+ # * if the condition is +false+ or +nil+, the value must be +nil+.
53
+ #
54
+ # # Assert that there is a "span" tag
55
+ # assert_tag tag: "span"
56
+ #
57
+ # # Assert that there is a "span" tag with id="x"
58
+ # assert_tag tag: "span", attributes: { id: "x" }
59
+ #
60
+ # # Assert that there is a "span" tag using the short-hand
61
+ # assert_tag :span
62
+ #
63
+ # # Assert that there is a "span" tag with id="x" using the short-hand
64
+ # assert_tag :span, attributes: { id: "x" }
65
+ #
66
+ # # Assert that there is a "span" inside of a "div"
67
+ # assert_tag tag: "span", parent: { tag: "div" }
68
+ #
69
+ # # Assert that there is a "span" somewhere inside a table
70
+ # assert_tag tag: "span", ancestor: { tag: "table" }
71
+ #
72
+ # # Assert that there is a "span" with at least one "em" child
73
+ # assert_tag tag: "span", child: { tag: "em" }
74
+ #
75
+ # # Assert that there is a "span" containing a (possibly nested)
76
+ # # "strong" tag.
77
+ # assert_tag tag: "span", descendant: { tag: "strong" }
78
+ #
79
+ # # Assert that there is a "span" containing between 2 and 4 "em" tags
80
+ # # as immediate children
81
+ # assert_tag tag: "span",
82
+ # children: { count: 2..4, only: { tag: "em" } }
83
+ #
84
+ # # Get funky: assert that there is a "div", with an "ul" ancestor
85
+ # # and an "li" parent (with "class" = "enum"), and containing a
86
+ # # "span" descendant that contains text matching /hello world/
87
+ # assert_tag tag: "div",
88
+ # ancestor: { tag: "ul" },
89
+ # parent: { tag: "li",
90
+ # attributes: { class: "enum" } },
91
+ # descendant: { tag: "span",
92
+ # child: /hello world/ }
93
+ #
94
+ # <b>Please note</b>: +assert_tag+ and +assert_no_tag+ only work
95
+ # with well-formed XHTML. They recognize a few tags as implicitly self-closing
96
+ # (like br and hr and such) but will not work correctly with tags
97
+ # that allow optional closing tags (p, li, td). <em>You must explicitly
98
+ # close all of your tags to use these assertions.</em>
99
+ def assert_tag(*opts)
100
+ ActiveSupport::Deprecation.warn("assert_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")
101
+
102
+ opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
103
+ tag = _find_tag(opts)
104
+
105
+ assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
106
+ end
107
+
108
+ # Identical to +assert_tag+, but asserts that a matching tag does _not_
109
+ # exist. (See +assert_tag+ for a full discussion of the syntax.)
110
+ #
111
+ # # Assert that there is not a "div" containing a "p"
112
+ # assert_no_tag tag: "div", descendant: { tag: "p" }
113
+ #
114
+ # # Assert that an unordered list is empty
115
+ # assert_no_tag tag: "ul", descendant: { tag: "li" }
116
+ #
117
+ # # Assert that there is not a "p" tag with between 1 to 3 "img" tags
118
+ # # as immediate children
119
+ # assert_no_tag tag: "p",
120
+ # children: { count: 1..3, only: { tag: "img" } }
121
+ def assert_no_tag(*opts)
122
+ ActiveSupport::Deprecation.warn("assert_no_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.")
123
+
124
+ opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first
125
+ tag = _find_tag(opts)
126
+
127
+ assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
128
+ end
129
+
130
+ def find_tag(conditions)
131
+ ActiveSupport::Deprecation.warn("find_tag is deprecated and will be removed at Rails 5 without replacement.")
132
+
133
+ _find_tag(conditions)
134
+ end
135
+
136
+ def find_all_tag(conditions)
137
+ ActiveSupport::Deprecation.warn("find_all_tag is deprecated and will be removed at Rails 5 without replacement. Use assert_select to get the same feature.")
138
+
139
+ html_scanner_document.find_all(conditions)
140
+ end
141
+
142
+ private
143
+ def _find_tag(conditions)
144
+ html_scanner_document.find(conditions)
145
+ end
146
+
147
+ def html_scanner_document
148
+ xml = @response.content_type =~ /xml$/
149
+ @html_document ||= HTML::Document.new(@response.body, false, xml)
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Dom
3
3
  module Testing
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,218 @@
1
+ require 'test_helper'
2
+ require 'rails/dom/testing/assertions/tag_assertions'
3
+
4
+ HTML_TEST_OUTPUT = <<HTML
5
+ <html>
6
+ <body>
7
+ <a href="/"><img src="/images/button.png" /></a>
8
+ <div id="foo">
9
+ <ul>
10
+ <li class="item">hello</li>
11
+ <li class="item">goodbye</li>
12
+ </ul>
13
+ </div>
14
+ <div id="bar">
15
+ <form action="/somewhere">
16
+ Name: <input type="text" name="person[name]" id="person_name" />
17
+ </form>
18
+ </div>
19
+ </body>
20
+ </html>
21
+ HTML
22
+
23
+ class AssertTagTest < ActiveSupport::TestCase
24
+ include Rails::Dom::Testing::Assertions::TagAssertions
25
+
26
+ class FakeResponse
27
+ attr_accessor :content_type, :body
28
+
29
+ def initialize(content_type, body)
30
+ @content_type, @body = content_type, body
31
+ end
32
+ end
33
+
34
+ setup do
35
+ @response = FakeResponse.new 'html', HTML_TEST_OUTPUT
36
+ end
37
+
38
+ def test_assert_tag_tag
39
+ assert_deprecated do
40
+ # there is a 'form' tag
41
+ assert_tag tag: 'form'
42
+ # there is not an 'hr' tag
43
+ assert_no_tag tag: 'hr'
44
+ end
45
+ end
46
+
47
+ def test_assert_tag_attributes
48
+ assert_deprecated do
49
+ # there is a tag with an 'id' of 'bar'
50
+ assert_tag attributes: { id: "bar" }
51
+ # there is no tag with a 'name' of 'baz'
52
+ assert_no_tag attributes: { name: "baz" }
53
+ end
54
+ end
55
+
56
+ def test_assert_tag_parent
57
+ assert_deprecated do
58
+ # there is a tag with a parent 'form' tag
59
+ assert_tag parent: { tag: "form" }
60
+ # there is no tag with a parent of 'input'
61
+ assert_no_tag parent: { tag: "input" }
62
+ end
63
+ end
64
+
65
+ def test_assert_tag_child
66
+ assert_deprecated do
67
+ # there is a tag with a child 'input' tag
68
+ assert_tag child: { tag: "input" }
69
+ # there is no tag with a child 'strong' tag
70
+ assert_no_tag child: { tag: "strong" }
71
+ end
72
+ end
73
+
74
+ def test_assert_tag_ancestor
75
+ assert_deprecated do
76
+ # there is a 'li' tag with an ancestor having an id of 'foo'
77
+ assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li"
78
+ # there is no tag of any kind with an ancestor having an href matching 'foo'
79
+ assert_no_tag ancestor: { attributes: { href: /foo/ } }
80
+ end
81
+ end
82
+
83
+ def test_assert_tag_descendant
84
+ assert_deprecated do
85
+ # there is a tag with a descendant 'li' tag
86
+ assert_tag descendant: { tag: "li" }
87
+ # there is no tag with a descendant 'html' tag
88
+ assert_no_tag descendant: { tag: "html" }
89
+ end
90
+ end
91
+
92
+ def test_assert_tag_sibling
93
+ assert_deprecated do
94
+ # there is a tag with a sibling of class 'item'
95
+ assert_tag sibling: { attributes: { class: "item" } }
96
+ # there is no tag with a sibling 'ul' tag
97
+ assert_no_tag sibling: { tag: "ul" }
98
+ end
99
+ end
100
+
101
+ def test_assert_tag_after
102
+ assert_deprecated do
103
+ # there is a tag following a sibling 'div' tag
104
+ assert_tag after: { tag: "div" }
105
+ # there is no tag following a sibling tag with id 'bar'
106
+ assert_no_tag after: { attributes: { id: "bar" } }
107
+ end
108
+ end
109
+
110
+ def test_assert_tag_before
111
+ assert_deprecated do
112
+ # there is a tag preceding a tag with id 'bar'
113
+ assert_tag before: { attributes: { id: "bar" } }
114
+ # there is no tag preceding a 'form' tag
115
+ assert_no_tag before: { tag: "form" }
116
+ end
117
+ end
118
+
119
+ def test_assert_tag_children_count
120
+ assert_deprecated do
121
+ # there is a tag with 2 children
122
+ assert_tag children: { count: 2 }
123
+ # in particular, there is a <ul> tag with two children (a nameless pair of <li>s)
124
+ assert_tag tag: 'ul', children: { count: 2 }
125
+ # there is no tag with 4 children
126
+ assert_no_tag children: { count: 4 }
127
+ end
128
+ end
129
+
130
+ def test_assert_tag_children_less_than
131
+ assert_deprecated do
132
+ # there is a tag with less than 5 children
133
+ assert_tag children: { less_than: 5 }
134
+ # there is no 'ul' tag with less than 2 children
135
+ assert_no_tag children: { less_than: 2 }, tag: "ul"
136
+ end
137
+ end
138
+
139
+ def test_assert_tag_children_greater_than
140
+ assert_deprecated do
141
+ # there is a 'body' tag with more than 1 children
142
+ assert_tag children: { greater_than: 1 }, tag: "body"
143
+ # there is no tag with more than 10 children
144
+ assert_no_tag children: { greater_than: 10 }
145
+ end
146
+ end
147
+
148
+ def test_assert_tag_children_only
149
+ assert_deprecated do
150
+ # there is a tag containing only one child with an id of 'foo'
151
+ assert_tag children: { count: 1,
152
+ only: { attributes: { id: "foo" } } }
153
+ # there is no tag containing only one 'li' child
154
+ assert_no_tag children: { count: 1, only: { tag: "li" } }
155
+ end
156
+ end
157
+
158
+ def test_assert_tag_content
159
+ assert_deprecated do
160
+ # the output contains the string "Name"
161
+ assert_tag content: /Name/
162
+ # the output does not contain the string "test"
163
+ assert_no_tag content: /test/
164
+ end
165
+ end
166
+
167
+ def test_assert_tag_multiple
168
+ assert_deprecated do
169
+ # there is a 'div', id='bar', with an immediate child whose 'action'
170
+ # attribute matches the regexp /somewhere/.
171
+ assert_tag tag: "div", attributes: { id: "bar" },
172
+ child: { attributes: { action: /somewhere/ } }
173
+
174
+ # there is no 'div', id='foo', with a 'ul' child with more than
175
+ # 2 "li" children.
176
+ assert_no_tag tag: "div", attributes: { id: "foo" },
177
+ child: { tag: "ul",
178
+ children: { greater_than: 2, only: { tag: "li" } } }
179
+ end
180
+ end
181
+
182
+ def test_assert_tag_children_without_content
183
+ assert_deprecated do
184
+ # there is a form tag with an 'input' child which is a self closing tag
185
+ assert_tag tag: "form",
186
+ children: { count: 1,
187
+ only: { tag: "input" } }
188
+
189
+ # the body tag has an 'a' child which in turn has an 'img' child
190
+ assert_tag tag: "body",
191
+ children: { count: 1,
192
+ only: { tag: "a",
193
+ children: { count: 1,
194
+ only: { tag: "img" } } } }
195
+ end
196
+ end
197
+
198
+ def test_assert_tag_attribute_matching
199
+ assert_deprecated do
200
+ @response.body = '<input type="text" name="my_name">'
201
+ assert_tag tag: 'input',
202
+ attributes: { name: /my/, type: 'text' }
203
+ assert_no_tag tag: 'input',
204
+ attributes: { name: 'my', type: 'text' }
205
+ assert_no_tag tag: 'input',
206
+ attributes: { name: /^my$/, type: 'text' }
207
+ end
208
+ end
209
+
210
+ def test_assert_tag_content_matching
211
+ assert_deprecated do
212
+ @response.body = "<p>hello world</p>"
213
+ assert_tag tag: "p", content: "hello world"
214
+ assert_tag tag: "p", content: /hello/
215
+ assert_no_tag tag: "p", content: "hello"
216
+ end
217
+ end
218
+ 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.0
4
+ version: 1.0.2
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-18 00:00:00.000000000 Z
12
+ date: 2014-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -39,6 +39,20 @@ dependencies:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rails-deprecated_sanitizer
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 1.0.1
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.1
42
56
  - !ruby/object:Gem::Dependency
43
57
  name: bundler
44
58
  requirement: !ruby/object:Gem::Requirement
@@ -98,9 +112,11 @@ files:
98
112
  - lib/rails/dom/testing/assertions/selector_assertions.rb
99
113
  - lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb
100
114
  - lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb
115
+ - lib/rails/dom/testing/assertions/tag_assertions.rb
101
116
  - lib/rails/dom/testing/version.rb
102
117
  - test/dom_assertions_test.rb
103
118
  - test/selector_assertions_test.rb
119
+ - test/tag_assertions_test.rb
104
120
  - test/test_helper.rb
105
121
  homepage: https://github.com/kaspth/rails-dom-testing
106
122
  licenses:
@@ -122,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
138
  version: '0'
123
139
  requirements: []
124
140
  rubyforge_project:
125
- rubygems_version: 2.2.2
141
+ rubygems_version: 2.3.0
126
142
  signing_key:
127
143
  specification_version: 4
128
144
  summary: This gem can compare doms and assert certain elements exists in doms using
@@ -130,4 +146,6 @@ summary: This gem can compare doms and assert certain elements exists in doms us
130
146
  test_files:
131
147
  - test/dom_assertions_test.rb
132
148
  - test/selector_assertions_test.rb
149
+ - test/tag_assertions_test.rb
133
150
  - test/test_helper.rb
151
+ has_rdoc: