rspec-html-matchers 0.7.3 → 0.8.0

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: 8694153cec5f8ee172a384595fa0011ff7f7db1b
4
- data.tar.gz: 4f23a0514a57c5af870b589f8cd91c0905a01889
3
+ metadata.gz: 95d16513be1c4d695f1c2dec1c2e43bcece4c97f
4
+ data.tar.gz: 523b06ab97e4993f5f79334447483c9121cadb7c
5
5
  SHA512:
6
- metadata.gz: 611f3063dadc57bf218d88a8782559e1a3121405de1cf5b99659f60840a51843926c9d2df2ae7c304730421a08b398dde5398096cd8ba5ee3a135c7dc6fa8e9e
7
- data.tar.gz: a16c21bb9aa3784b7d949d8826d7d9b483800052738dd4224c89f70f1c434935e2eb1be0295553a951ff50e88b217333cd8f73ff382b3705656948f7904cf5a0
6
+ metadata.gz: 5e49505a8a621e4d7c660827f5450efb92e5d320effadbc93d26a9391ee5a689710d45d4ef78b6339fdf374448820c2a5720a3a71b35c75f9167b67152671253
7
+ data.tar.gz: f7dbee862ebefebb529b1f99ad3c79e867ebe5535c8d1d3e79a21f9fc8c0e105cfbf2f4f6b5c5bf35350f715fae351e24b86b9bab986ee5a4cd07a6ca04bf6ba
@@ -11,6 +11,11 @@ unreleased(TODO)
11
11
  * order matching
12
12
  * improve documentation, add more usage examples (look at changelog and code!)
13
13
 
14
+ 0.8.0
15
+ -----
16
+
17
+ * "seen" text matching (thanks to [Alex Shi](https://github.com/hlcfan))
18
+
14
19
  0.7.3
15
20
  -----
16
21
 
data/README.md CHANGED
@@ -13,11 +13,11 @@ Goals
13
13
  * [assert_select](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html#method-i-assert_select)
14
14
  * [matchers provided out of the box in rspec-rails](https://www.relishapp.com/rspec/rspec-rails/v/2-11/docs/view-specs/view-spec)
15
15
  * [matchers provided by capybara](http://rdoc.info/github/jnicklas/capybara/Capybara/Node/Matchers)
16
- * developer-firendly output in error messages
16
+ * developer-friendly output in error messages
17
17
  * built on top of [nokogiri](http://www.nokogiri.org/)
18
18
  * has support for [capybara](https://github.com/jnicklas/capybara), see below
19
- * syntax is similar to have_tag matcher from rspec-rails 1.x, but with own syntactic sugar
20
- * framework agnostic, as input should be String(or capybara's page, see below)
19
+ * syntax is similar to `have_tag` matcher from rspec-rails 1.x, but with own syntactic sugar
20
+ * framework agnostic, as input should be `String` (or capybara's page, see below)
21
21
 
22
22
  Install
23
23
  -------
@@ -28,7 +28,7 @@ Add to your Gemfile in the `:test` group:
28
28
  gem 'rspec-html-matchers'
29
29
  ```
30
30
 
31
- and somewhere in RSpec configuration:
31
+ Include it in your RSpec configuration:
32
32
 
33
33
  ```ruby
34
34
  RSpec.configure do |config|
@@ -36,7 +36,7 @@ RSpec.configure do |config|
36
36
  end
37
37
  ```
38
38
 
39
- or just in you spec(s):
39
+ or just in your spec(s):
40
40
 
41
41
  ```ruby
42
42
  describe "my view spec" do
@@ -55,12 +55,12 @@ Cucumber configuration:
55
55
  World RSpecHtmlMatchers
56
56
  ```
57
57
 
58
- as this gem requires **nokogiri**, here [instructions for installing it](http://nokogiri.org/tutorials/installing_nokogiri.html).
58
+ as this gem requires **nokogiri**, here are [instructions for installing it](http://nokogiri.org/tutorials/installing_nokogiri.html).
59
59
 
60
60
  Usage
61
61
  -----
62
62
 
63
- so perharps your code produces following output:
63
+ so perhaps your code produces following output:
64
64
 
65
65
  ```html
66
66
  <h1>Simple Form</h1>
@@ -74,7 +74,7 @@ so perharps your code produces following output:
74
74
  </form>
75
75
  ```
76
76
 
77
- so you test it with following:
77
+ so you test it with the following:
78
78
 
79
79
  ```ruby
80
80
  expect(rendered).to have_tag('form', :with => { :action => '/users', :method => 'post' }) do
@@ -85,9 +85,9 @@ expect(rendered).to have_tag('form', :with => { :action => '/users', :method =>
85
85
  end
86
86
  ```
87
87
 
88
- Example about should be self-descriptive, if not, please refer to [have_tag](http://www.rubydoc.info/gems/rspec-html-matchers/RSpecHtmlMatchers%3Ahave_tag) documentation
88
+ Example above should be self-descriptive, if not, please refer to the [`have_tag`](http://www.rubydoc.info/gems/rspec-html-matchers/RSpecHtmlMatchers%3Ahave_tag) documentation
89
89
 
90
- Input could be any html string. Let's take a look at these examples:
90
+ Input can be any html string. Let's take a look at these examples:
91
91
 
92
92
  * matching tags by css:
93
93
 
@@ -154,6 +154,14 @@ Input could be any html string. Let's take a look at these examples:
154
154
  expect('<p> Some content&nbsphere</p>').to have_tag('p') do
155
155
  with_content mymock.text
156
156
  end
157
+
158
+ # matching text content as it's seen by user:
159
+ rendered = <<HTML
160
+ <p>
161
+ content with ignored spaces around
162
+ </p>
163
+ HTML
164
+ expect(rendered).to have_tag('p', :seen => 'content with ignored spaces around')
157
165
  ```
158
166
 
159
167
  * usage with capybara and cucumber:
@@ -198,6 +206,15 @@ expect(response).to have_tag('div', /regexp matching expected content/)
198
206
 
199
207
  [RSpec 1 `have_tag` documentation](http://old.rspec.info/rails/writing/views.html)
200
208
 
209
+ Matching Tag Attributes
210
+ -----------------------
211
+
212
+ You can also match the content of attributes by using selectors. For example, to ensure an `img` tag has an `alt` attribute, you can match:
213
+
214
+ ```ruby
215
+ expect(index).to have_tag("img[alt!='']")
216
+ ```
217
+
201
218
  More info
202
219
  ---------
203
220
 
@@ -23,13 +23,15 @@ module RSpecHtmlMatchers
23
23
  class NokogiriTextHelper
24
24
  NON_BREAKING_SPACE = "\u00a0"
25
25
 
26
- def initialize text
26
+ def initialize text, squeeze_text = false
27
27
  @text = text
28
+ @squeeze_text = squeeze_text
28
29
  end
29
30
 
30
31
  def content node_set
31
32
  node_set.find_all do |node|
32
33
  actual_content = node.content.gsub(NON_BREAKING_SPACE, ' ')
34
+ actual_content = node.content.strip.squeeze(' ') if @squeeze_text
33
35
 
34
36
  actual_content == @text
35
37
  end
@@ -96,6 +98,7 @@ module RSpecHtmlMatchers
96
98
  end
97
99
 
98
100
  validate_options!
101
+ set_options
99
102
  end
100
103
 
101
104
  def matches? document, &block
@@ -191,7 +194,7 @@ module RSpecHtmlMatchers
191
194
  false
192
195
  end
193
196
  else
194
- new_scope = @current_scope.css(':content()',NokogiriTextHelper.new(text))
197
+ new_scope = @current_scope.css(':content()', NokogiriTextHelper.new(text, @options[:squeeze_text]))
195
198
  unless new_scope.empty?
196
199
  @count = new_scope.count
197
200
  @failure_message_when_negated = MESSAGES[:unexpected_text] % [text,@tag,@document]
@@ -206,32 +209,54 @@ module RSpecHtmlMatchers
206
209
  protected
207
210
 
208
211
  def validate_options!
212
+ validate_count_presence!
213
+ validate_count_when_set_min_max!
214
+ validate_count_when_set_range!
215
+ end
216
+
217
+ def validate_count_presence!
209
218
  raise 'wrong :count specified' unless [Range, NilClass].include?(@options[:count].class) or @options[:count].is_a?(Integer)
210
219
 
211
220
  [:min, :minimum, :max, :maximum].each do |key|
212
221
  raise MESSAGES[:wrong_count_error] if @options.has_key?(key) and @options.has_key?(:count)
213
222
  end
223
+ end
214
224
 
225
+ def validate_count_when_set_min_max!
215
226
  begin
216
227
  raise MESSAGES[:min_max_error] if @options[:minimum] > @options[:maximum]
217
228
  rescue NoMethodError # nil > 4
218
229
  rescue ArgumentError # 2 < nil
219
230
  end
231
+ end
220
232
 
233
+ def validate_count_when_set_range!
221
234
  begin
222
235
  begin
223
- raise MESSAGES[:bad_range_error] % [@options[:count].to_s] if @options[:count] && @options[:count].is_a?(Range) && (@options[:count].min.nil? or @options[:count].min < 0)
236
+ raise MESSAGES[:bad_range_error] % [@options[:count].to_s] if count_is_range_but_no_min?
224
237
  rescue ArgumentError, "comparison of String with" # if @options[:count] == 'a'..'z'
225
238
  raise MESSAGES[:bad_range_error] % [@options[:count].to_s]
226
239
  end
227
240
  rescue TypeError # fix for 1.8.7 for 'rescue ArgumentError, "comparison of String with"' stroke
228
241
  raise MESSAGES[:bad_range_error] % [@options[:count].to_s]
229
242
  end
243
+ end
230
244
 
245
+ def count_is_range_but_no_min?
246
+ @options[:count] && @options[:count].is_a?(Range) &&
247
+ (@options[:count].min.nil? or @options[:count].min < 0)
248
+ end
249
+
250
+ def set_options
231
251
  @options[:minimum] ||= @options.delete(:min)
232
252
  @options[:maximum] ||= @options.delete(:max)
233
253
 
234
254
  @options[:text] = @options[:text].to_s if @options.has_key?(:text) && !@options[:text].is_a?(Regexp)
255
+
256
+ if @options.has_key?(:seen) && !@options[:seen].is_a?(Regexp)
257
+ @options[:text] = @options[:seen].to_s
258
+ @options[:squeeze_text] = true
259
+ end
235
260
  end
236
261
 
237
262
  end
@@ -278,7 +278,7 @@ describe 'have_tag' do
278
278
  end
279
279
  end
280
280
 
281
- context "with :text specified" do
281
+ context "with :text/:seen specified" do
282
282
  asset 'quotes'
283
283
 
284
284
  context 'using standard syntax' do
@@ -291,6 +291,10 @@ describe 'have_tag' do
291
291
  expect(rendered).to have_tag('span', :text => %Q{sample with 'single' and "double" quotes})
292
292
  expect(rendered).to have_tag('span', :text => /sample with 'single' and "double" quotes/)
293
293
 
294
+ expect(rendered).to have_tag('p', :seen => 'content with ignored spaces around')
295
+ expect(rendered).to have_tag('p', :seen => 'content with ignored spaces in')
296
+ expect(rendered).to have_tag('p', :seen => 'content with nbsp  and  spaces   around')
297
+
294
298
  expect(rendered).to have_tag('p', :text => 'content with nbsp')
295
299
  expect(rendered).to have_tag('pre', :text => " 1. bla \n 2. bla ")
296
300
  end
@@ -308,8 +312,14 @@ describe 'have_tag' do
308
312
  expect(rendered).to_not have_tag('strong', :text => 'text does not present')
309
313
  expect(rendered).to_not have_tag('p', :text => /text does not present/)
310
314
  expect(rendered).to_not have_tag('strong', :text => /text does not present/)
311
-
312
315
  expect(rendered).to_not have_tag('p', :text => 'contentwith nbsp')
316
+
317
+ expect(rendered).to_not have_tag('p', :seen => 'content with ignoredspaces around')
318
+ expect(rendered).to_not have_tag('p', :seen => 'content with ignored spaces around')
319
+ expect(rendered).to_not have_tag('p', :seen => 'content withignored spaces in')
320
+ expect(rendered).to_not have_tag('p', :seen => 'contentwith nbsp')
321
+ expect(rendered).to_not have_tag('p', :seen => 'content with nbsp and spaces around')
322
+
313
323
  expect(rendered).to_not have_tag('pre', :text => "1. bla\n2. bla")
314
324
  end
315
325
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-html-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kucaahbe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec