Class: Arbre::RSpec::ContainScriptMatcher
- Inherits:
-
Object
- Object
- Arbre::RSpec::ContainScriptMatcher
- Defined in:
- lib/arbre/rspec/contain_script_matcher.rb
Overview
Used to match JS snippets in HTML content. The JS is canonized as much as possible, so that you don’t have to worry about whitespace. Use ‘(…)’ as a wildcard. You may even place text in the wildcard for readability: ‘(… some wildcard …)’.
Examples
expect(document.body).to contain_script('Flux.Application.initialize()')
expect(document.body).to contain_script('Flux.Application.initialize((... args ...))')
Instance Attribute Summary collapse
-
#expected ⇒ Object
readonly
Returns the value of attribute expected.
Instance Method Summary collapse
- #canonize_js(js) ⇒ Object
- #description ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#initialize(expected) ⇒ ContainScriptMatcher
constructor
A new instance of ContainScriptMatcher.
- #matches?(actual) ⇒ Boolean
Constructor Details
#initialize(expected) ⇒ ContainScriptMatcher
Returns a new instance of ContainScriptMatcher.
14 15 16 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 14 def initialize(expected) @expected = expected end |
Instance Attribute Details
#expected ⇒ Object (readonly)
Returns the value of attribute expected.
18 19 20 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 18 def expected @expected end |
Instance Method Details
#canonize_js(js) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 29 def canonize_js(js) js = js.dup js.gsub! %r|//\s+<!\[CDATA\[[\n\r]+|, '' js.gsub! %r|[\n\r]+//\s+\]\]>|, '' js.gsub! /(\s*[\n\r]\s*)+/, ' ' js.gsub! /^\s+|\s+$/, '' js end |
#description ⇒ Object
20 21 22 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 20 def description "contain script #{expected}" end |
#failure_message_for_should ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 41 def <<-MSG.gsub(/^\s{10}/, '') expected that element of type #{@actual.class} contained script: expected: #{expected.is_a?(Regexp) ? '/' + canonize_js(expected.source) + '/' : canonize_js(expected)} (#{expected.class}) got: #{@actual.nil? ? 'nil' : canonize_js(@actual.to_s)} MSG end |
#failure_message_for_should_not ⇒ Object
49 50 51 52 53 54 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 49 def <<-MSG.gsub(/^\s{10}/, '') expected that element of type #{actual.class} would not contain a script: script: #{expected.is_a?(Regexp) ? '/' + canonize_js(expected.source) + '/' : canonize_js(expected)} (#{expected.class}) MSG end |
#matches?(actual) ⇒ Boolean
24 25 26 27 |
# File 'lib/arbre/rspec/contain_script_matcher.rb', line 24 def matches?(actual) @actual = actual canonize_js(actual.to_s).include?(canonize_js(expected)) end |