rspec-html-matchers 0.9.2 → 0.9.3

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
  SHA256:
3
- metadata.gz: 9562deaafdea89229b40132a80f29ba68f3010738105688ba29ee42a245755e8
4
- data.tar.gz: dd881bf03c6e5ab533eb9249d7d0450adf03ce7245f4e5ba430203cfbcb20b8f
3
+ metadata.gz: 0f037ee7285bbe655173850423adeba9e9825641718cc372e89669a5d02284d0
4
+ data.tar.gz: fc31002e7b48380897520cb09e0e08ee00151311c847b260d75cad9c2fab81f8
5
5
  SHA512:
6
- metadata.gz: 2abbd53202a5cd53d43e564fa4ea7d5663703d22d04af4dd7f9fdce45a3e3621a9766440968d686c879643e3035f1c4b537d107db631fa0961f968931155431f
7
- data.tar.gz: b17d9eb26651fb25389e024009369fa6cea40f36f7c0c911c8775e92cb96629a947308aada13dd427c99a22a462680ead185f36d80896bcc13dbf8229f84ca37
6
+ metadata.gz: a5b9f1f542091c493c69f402a46d54b6483a2f8c90e65c6fd8f7b038b14bc834cd76f8671e664323b37800f4a3e61afacf08d352963907c0873187521c8355cd
7
+ data.tar.gz: 5dd7980ad29b5ed66f3528366cc13b48199b809a0c025ed713cff6fb2f18f1a9323caa8c3bd6bc9145fea06d86c85c66b8763c560bb46377682c82d0dc7feff6
data/README.md CHANGED
@@ -41,7 +41,6 @@ describe "my view spec" do
41
41
  it "has tags" do
42
42
  expect(rendered).to have_tag('div')
43
43
  end
44
-
45
44
  end
46
45
  ```
47
46
 
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Given /^I have following template:$/ do |string|
2
- File.open($INDEX_HTML,'w+') do |file|
4
+ File.open($INDEX_HTML, 'w+') do |file| # rubocop:disable Style/GlobalVars
3
5
  file.write(string)
4
6
  end
5
7
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # setup rspec matchers
2
4
  require 'rspec/expectations'
3
5
  World(RSpec::Matchers)
@@ -9,8 +11,9 @@ require 'selenium-webdriver'
9
11
 
10
12
  World RSpecHtmlMatchers
11
13
 
12
- $ASSETS_DIR = File.expand_path('../tmp',__FILE__)
13
- $INDEX_HTML = File.join($ASSETS_DIR,'index.html')
14
+ # rubocop:disable Style/GlobalVars
15
+ $ASSETS_DIR = File.expand_path('../tmp', __FILE__)
16
+ $INDEX_HTML = File.join($ASSETS_DIR, 'index.html')
14
17
 
15
18
  class SimpleApp < Sinatra::Base
16
19
  set :public_folder, $ASSETS_DIR
@@ -39,3 +42,4 @@ end
39
42
  After do
40
43
  FileUtils.rm_rf $ASSETS_DIR
41
44
  end
45
+ # rubocop:enable Style/GlobalVars
@@ -1,15 +1,30 @@
1
- # frozen_string_literal: true
2
1
  # encoding: UTF-8
3
-
4
- require 'rspec'
5
- require 'nokogiri'
2
+ # frozen_string_literal: true
6
3
 
7
4
  require 'rspec-html-matchers/nokogiri_regexp_helper'
8
5
  require 'rspec-html-matchers/nokogiri_text_helper'
9
6
  require 'rspec-html-matchers/have_tag'
10
7
 
8
+ # RSpec global configuration:
9
+ #
10
+ # RSpec.configure do |config|
11
+ # config.include RSpecHtmlMatchers
12
+ # end
13
+ #
14
+ # RSpec per-test configuration
15
+ #
16
+ # RSpec.describe "my view spec" do
17
+ # include RSpecHtmlMatchers
18
+ #
19
+ # it "has tags" do
20
+ # expect(rendered).to have_tag('div')
21
+ # end
22
+ # end
23
+ #
24
+ # Cucumber configuration:
25
+ #
26
+ # World RSpecHtmlMatchers
11
27
  module RSpecHtmlMatchers
12
-
13
28
  # tag assertion, this is the core of functionality, other matchers are shortcuts to this matcher
14
29
  #
15
30
  # @yield block where you should put with_tag, without_tag and/or other matchers
@@ -44,9 +59,9 @@ module RSpecHtmlMatchers
44
59
  # </html>").to have_tag('body') { with_tag('h1', :text => 'some html document') }
45
60
  # expect('<div class="one two">').to have_tag('div', :with => { :class => ['two', 'one'] })
46
61
  # expect('<div class="one two">').to have_tag('div', :with => { :class => 'two one' })
47
- def have_tag tag, options={}, &block
62
+ def have_tag tag, options = {}, &block
48
63
  # for backwards compatibility with rpecs have tag:
49
- options = { :text => options } if options.kind_of?(String) || options.kind_of?(Regexp)
64
+ options = { :text => options } if options.is_a?(String) || options.is_a?(Regexp)
50
65
  @__current_scope_for_nokogiri_matcher = HaveTag.new(tag, options, &block)
51
66
  end
52
67
 
@@ -55,13 +70,14 @@ module RSpecHtmlMatchers
55
70
  # @example
56
71
  # expect('<div></div>').to have_empty_tag('div') # success
57
72
  # expect('<div>hi</div>').to have_empty_tag('div') # fail
58
- def have_empty_tag tag, options={}
73
+ def have_empty_tag tag, options = {}
59
74
  have_tag(tag, options.merge(:blank => true))
60
75
  end
61
76
 
62
77
  def with_text text
63
78
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
64
79
  raise ArgumentError, 'this matcher does not accept block' if block_given?
80
+
65
81
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
66
82
  within_nested_tag do
67
83
  expect(@__current_scope_for_nokogiri_matcher).to have_tag(tag, :text => text)
@@ -71,18 +87,19 @@ module RSpecHtmlMatchers
71
87
  def without_text text
72
88
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
73
89
  raise ArgumentError, 'this matcher does not accept block' if block_given?
90
+
74
91
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
75
92
  within_nested_tag do
76
93
  expect(@__current_scope_for_nokogiri_matcher).to_not have_tag(tag, :text => text)
77
94
  end
78
95
  end
79
- alias :but_without_text :without_text
96
+ alias but_without_text without_text
80
97
 
81
98
  # with_tag matcher
82
99
  # @yield block where you should put other with_tag or without_tag
83
100
  # @see #have_tag
84
101
  # @note this should be used within block of have_tag matcher
85
- def with_tag tag, options={}, &block
102
+ def with_tag tag, options = {}, &block
86
103
  within_nested_tag do
87
104
  expect(@__current_scope_for_nokogiri_matcher).to have_tag(tag, options, &block)
88
105
  end
@@ -92,7 +109,7 @@ module RSpecHtmlMatchers
92
109
  # @yield block where you should put other with_tag or without_tag
93
110
  # @see #have_tag
94
111
  # @note this should be used within block of have_tag matcher
95
- def without_tag tag, options={}, &block
112
+ def without_tag tag, options = {}, &block
96
113
  within_nested_tag do
97
114
  expect(@__current_scope_for_nokogiri_matcher).to_not have_tag(tag, options, &block)
98
115
  end
@@ -104,121 +121,123 @@ module RSpecHtmlMatchers
104
121
  # have_tag 'form', :with => { :action => action_url, :method => method ... }
105
122
  # @yield block with with_<field>, see below
106
123
  # @see #have_tag
107
- def have_form action_url, method, options={}, &block
124
+ def have_form action_url, method, options = {}, &block
108
125
  options[:with] ||= {}
109
126
  id = options[:with].delete(:id)
110
- tag = 'form'; tag += '#'+id if id
127
+ tag = 'form'; tag += '#' + id if id
111
128
  options[:with].merge!(:action => action_url)
112
129
  options[:with].merge!(:method => method.to_s)
113
130
  have_tag tag, options, &block
114
131
  end
115
132
 
116
- #@TODO fix code duplications
133
+ # @TODO fix code duplications
117
134
 
118
- def with_hidden_field name, value=nil
119
- options = form_tag_options('hidden',name,value)
135
+ def with_hidden_field name, value = nil
136
+ options = form_tag_options('hidden', name, value)
120
137
  should_have_input(options)
121
138
  end
122
139
 
123
- def without_hidden_field name, value=nil
124
- options = form_tag_options('hidden',name,value)
140
+ def without_hidden_field name, value = nil
141
+ options = form_tag_options('hidden', name, value)
125
142
  should_not_have_input(options)
126
143
  end
127
144
 
128
- def with_text_field name, value=nil
129
- options = form_tag_options('text',name,value)
145
+ def with_text_field name, value = nil
146
+ options = form_tag_options('text', name, value)
130
147
  should_have_input(options)
131
148
  end
132
149
 
133
- def without_text_field name, value=nil
134
- options = form_tag_options('text',name,value)
150
+ def without_text_field name, value = nil
151
+ options = form_tag_options('text', name, value)
135
152
  should_not_have_input(options)
136
153
  end
137
154
 
138
- def with_email_field name, value=nil
139
- options = form_tag_options('email',name,value)
155
+ def with_email_field name, value = nil
156
+ options = form_tag_options('email', name, value)
140
157
  should_have_input(options)
141
158
  end
142
159
 
143
- def without_email_field name, value=nil
144
- options = form_tag_options('email',name,value)
160
+ def without_email_field name, value = nil
161
+ options = form_tag_options('email', name, value)
145
162
  should_not_have_input(options)
146
163
  end
147
164
 
148
- def with_url_field name, value=nil
149
- options = form_tag_options('url',name,value)
165
+ def with_url_field name, value = nil
166
+ options = form_tag_options('url', name, value)
150
167
  should_have_input(options)
151
168
  end
152
169
 
153
- def without_url_field name, value=nil
154
- options = form_tag_options('url',name,value)
170
+ def without_url_field name, value = nil
171
+ options = form_tag_options('url', name, value)
155
172
  should_not_have_input(options)
156
173
  end
157
174
 
158
- def with_number_field name, value=nil
159
- options = form_tag_options('number',name,value)
175
+ def with_number_field name, value = nil
176
+ options = form_tag_options('number', name, value)
160
177
  should_have_input(options)
161
178
  end
162
179
 
163
- def without_number_field name, value=nil
164
- options = form_tag_options('number',name,value)
180
+ def without_number_field name, value = nil
181
+ options = form_tag_options('number', name, value)
165
182
  should_not_have_input(options)
166
183
  end
167
184
 
168
- def with_range_field name, min, max, options={}
169
- options = { :with => { :name => name, :type => 'range', :min => min.to_s, :max => max.to_s }.merge(options.delete(:with)||{}) }
185
+ def with_range_field name, min, max, options = {}
186
+ options = { :with => { :name => name, :type => 'range', :min => min.to_s, :max => max.to_s }.merge(options.delete(:with) || {}) }
170
187
  should_have_input(options)
171
188
  end
172
189
 
173
- def without_range_field name, min=nil, max=nil, options={}
174
- options = { :with => { :name => name, :type => 'range' }.merge(options.delete(:with)||{}) }
190
+ def without_range_field name, min = nil, max = nil, options = {}
191
+ options = { :with => { :name => name, :type => 'range' }.merge(options.delete(:with) || {}) }
175
192
  options[:with].merge!(:min => min.to_s) if min
176
193
  options[:with].merge!(:max => max.to_s) if max
177
194
  should_not_have_input(options)
178
195
  end
179
196
 
180
- DATE_FIELD_TYPES = %w( date month week time datetime datetime-local )
197
+ DATE_FIELD_TYPES = %w[date month week time datetime datetime-local].freeze
181
198
 
182
- def with_date_field date_field_type, name=nil, options={}
199
+ def with_date_field date_field_type, name = nil, options = {}
183
200
  date_field_type = date_field_type.to_s
184
201
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
185
- options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
202
+
203
+ options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with) || {}) }
186
204
  options[:with].merge!(:name => name.to_s) if name
187
205
  should_have_input(options)
188
206
  end
189
207
 
190
- def without_date_field date_field_type, name=nil, options={}
208
+ def without_date_field date_field_type, name = nil, options = {}
191
209
  date_field_type = date_field_type.to_s
192
210
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
193
- options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
211
+
212
+ options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with) || {}) }
194
213
  options[:with].merge!(:name => name.to_s) if name
195
214
  should_not_have_input(options)
196
215
  end
197
216
 
198
- def with_password_field name, value=nil
199
- # TODO add ability to explicitly say that value should be empty
200
- options = form_tag_options('password',name,value)
217
+ def with_password_field name, value = nil
218
+ # TODO: add ability to explicitly say that value should be empty
219
+ options = form_tag_options('password', name, value)
201
220
  should_have_input(options)
202
221
  end
203
222
 
204
- def without_password_field name, value=nil
205
- options = form_tag_options('password',name,value)
223
+ def without_password_field name, value = nil
224
+ options = form_tag_options('password', name, value)
206
225
  should_not_have_input(options)
207
226
  end
208
227
 
209
- def with_file_field name, value=nil
210
- options = form_tag_options('file',name,value)
228
+ def with_file_field name, value = nil
229
+ options = form_tag_options('file', name, value)
211
230
  should_have_input(options)
212
231
  end
213
232
 
214
- def without_file_field name, value=nil
215
- options = form_tag_options('file',name,value)
233
+ def without_file_field name, value = nil
234
+ options = form_tag_options('file', name, value)
216
235
  should_not_have_input(options)
217
236
  end
218
237
 
219
238
  def with_text_area name
220
239
  # TODO, should be: with_text_area name, text=nil
221
- #options = form_tag_options('text',name,value)
240
+ # options = form_tag_options('text',name,value)
222
241
  options = { :with => { :name => name } }
223
242
  within_nested_tag do
224
243
  expect(@__current_scope_for_nokogiri_matcher).to have_tag('textarea', options)
@@ -227,64 +246,62 @@ module RSpecHtmlMatchers
227
246
 
228
247
  def without_text_area name
229
248
  # TODO, should be: without_text_area name, text=nil
230
- #options = form_tag_options('text',name,value)
249
+ # options = form_tag_options('text',name,value)
231
250
  options = { :with => { :name => name } }
232
251
  within_nested_tag do
233
252
  expect(@__current_scope_for_nokogiri_matcher).to_not have_tag('textarea', options)
234
253
  end
235
254
  end
236
255
 
237
- def with_checkbox name, value=nil
238
- options = form_tag_options('checkbox',name,value)
256
+ def with_checkbox name, value = nil
257
+ options = form_tag_options('checkbox', name, value)
239
258
  should_have_input(options)
240
259
  end
241
260
 
242
- def without_checkbox name, value=nil
243
- options = form_tag_options('checkbox',name,value)
261
+ def without_checkbox name, value = nil
262
+ options = form_tag_options('checkbox', name, value)
244
263
  should_not_have_input(options)
245
264
  end
246
265
 
247
266
  def with_radio_button name, value
248
- options = form_tag_options('radio',name,value)
267
+ options = form_tag_options('radio', name, value)
249
268
  should_have_input(options)
250
269
  end
251
270
 
252
271
  def without_radio_button name, value
253
- options = form_tag_options('radio',name,value)
272
+ options = form_tag_options('radio', name, value)
254
273
  should_not_have_input(options)
255
274
  end
256
275
 
257
- def with_select name, options={}, &block
276
+ def with_select name, options = {}, &block
258
277
  options[:with] ||= {}
259
278
  id = options[:with].delete(:id)
260
- tag='select'; tag += '#'+id if id
279
+ tag = 'select'; tag += '#' + id if id
261
280
  options[:with].merge!(:name => name)
262
281
  within_nested_tag do
263
282
  expect(@__current_scope_for_nokogiri_matcher).to have_tag(tag, options, &block)
264
283
  end
265
284
  end
266
285
 
267
- def without_select name, options={}, &block
286
+ def without_select name, options = {}, &block
268
287
  options[:with] ||= {}
269
288
  id = options[:with].delete(:id)
270
- tag='select'; tag += '#'+id if id
289
+ tag = 'select'; tag += '#' + id if id
271
290
  options[:with].merge!(:name => name)
272
291
  within_nested_tag do
273
292
  expect(@__current_scope_for_nokogiri_matcher).to_not have_tag(tag, options, &block)
274
293
  end
275
294
  end
276
295
 
277
- def with_option text, value=nil, options={}
296
+ def with_option text, value = nil, options = {}
278
297
  options[:with] ||= {}
279
298
  if value.is_a?(Hash)
280
299
  options.merge!(value)
281
- value=nil
300
+ value = nil
282
301
  end
283
- tag='option'
302
+ tag = 'option'
284
303
  options[:with].merge!(:value => value.to_s) if value
285
- if options[:selected]
286
- options[:with].merge!(:selected => "selected")
287
- end
304
+ options[:with].merge!(:selected => 'selected') if options[:selected]
288
305
  options.delete(:selected)
289
306
  options.merge!(:text => text) if text
290
307
  within_nested_tag do
@@ -292,17 +309,15 @@ module RSpecHtmlMatchers
292
309
  end
293
310
  end
294
311
 
295
- def without_option text, value=nil, options={}
312
+ def without_option text, value = nil, options = {}
296
313
  options[:with] ||= {}
297
314
  if value.is_a?(Hash)
298
315
  options.merge!(value)
299
- value=nil
316
+ value = nil
300
317
  end
301
- tag='option'
318
+ tag = 'option'
302
319
  options[:with].merge!(:value => value.to_s) if value
303
- if options[:selected]
304
- options[:with].merge!(:selected => "selected")
305
- end
320
+ options[:with].merge!(:selected => 'selected') if options[:selected]
306
321
  options.delete(:selected)
307
322
  options.merge!(:text => text) if text
308
323
  within_nested_tag do
@@ -310,11 +325,11 @@ module RSpecHtmlMatchers
310
325
  end
311
326
  end
312
327
 
313
- def with_button text, value=nil, options={}
328
+ def with_button text, value = nil, options = {}
314
329
  options[:with] ||= {}
315
330
  if value.is_a?(Hash)
316
331
  options.merge!(value)
317
- value=nil
332
+ value = nil
318
333
  end
319
334
  options[:with].merge!(:value => value.to_s) if value
320
335
  options.merge!(:text => text) if text
@@ -323,11 +338,11 @@ module RSpecHtmlMatchers
323
338
  end
324
339
  end
325
340
 
326
- def without_button text, value=nil, options={}
341
+ def without_button text, value = nil, options = {}
327
342
  options[:with] ||= {}
328
343
  if value.is_a?(Hash)
329
344
  options.merge!(value)
330
- value=nil
345
+ value = nil
331
346
  end
332
347
  options[:with].merge!(:value => value.to_s) if value
333
348
  options.merge!(:text => text) if text
@@ -338,43 +353,43 @@ module RSpecHtmlMatchers
338
353
 
339
354
  def with_submit value
340
355
  options = { :with => { :type => 'submit', :value => value } }
341
- #options = form_tag_options('text',name,value)
356
+ # options = form_tag_options('text',name,value)
342
357
  should_have_input(options)
343
358
  end
344
359
 
345
360
  def without_submit value
346
- #options = form_tag_options('text',name,value)
361
+ # options = form_tag_options('text',name,value)
347
362
  options = { :with => { :type => 'submit', :value => value } }
348
363
  should_not_have_input(options)
349
364
  end
350
365
 
351
366
  private
352
367
 
353
- def should_have_input(options)
368
+ def should_have_input options
354
369
  within_nested_tag do
355
370
  expect(@__current_scope_for_nokogiri_matcher).to have_tag('input', options)
356
371
  end
357
372
  end
358
373
 
359
- def should_not_have_input(options)
374
+ def should_not_have_input options
360
375
  within_nested_tag do
361
376
  expect(@__current_scope_for_nokogiri_matcher).to_not have_tag('input', options)
362
377
  end
363
378
  end
364
379
 
365
380
  # form_tag in method name name mean smth. like input, submit, tags that should appear in a form
366
- def form_tag_options form_tag_type, form_tag_name, form_tag_value=nil
381
+ def form_tag_options form_tag_type, form_tag_name, form_tag_value = nil
367
382
  options = { :with => { :name => form_tag_name, :type => form_tag_type } }
368
383
  # .to_s if value is a digit or smth. else, see issue#10
369
384
  options[:with].merge!(:value => form_tag_value.to_s) if form_tag_value
370
- return options
385
+ options
371
386
  end
372
387
 
373
- def within_nested_tag(&block)
388
+ def within_nested_tag &block
374
389
  raise 'block needed' unless block_given?
390
+
375
391
  parent_scope = @__current_scope_for_nokogiri_matcher
376
392
  block.call
377
393
  @__current_scope_for_nokogiri_matcher = parent_scope
378
394
  end
379
-
380
395
  end