Class: Arbre::RSpec::BeScriptedAsMatcher
- Inherits:
-
Object
- Object
- Arbre::RSpec::BeScriptedAsMatcher
- Defined in:
- lib/arbre/rspec/be_scripted_as_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.find_first('javascript')).to be_scripted_as('Flux.Application.initialize()')
expect(document.body.find_first('javascript')).to be_scripted_as('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) ⇒ BeScriptedAsMatcher
constructor
A new instance of BeScriptedAsMatcher.
- #matches?(actual) ⇒ Boolean
Constructor Details
#initialize(expected) ⇒ BeScriptedAsMatcher
Returns a new instance of BeScriptedAsMatcher.
14 15 16 |
# File 'lib/arbre/rspec/be_scripted_as_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/be_scripted_as_matcher.rb', line 18 def expected @expected end |
Instance Method Details
#canonize_js(js) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/arbre/rspec/be_scripted_as_matcher.rb', line 35 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/be_scripted_as_matcher.rb', line 20 def description "be scripted as #{expected}" end |
#failure_message_for_should ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/arbre/rspec/be_scripted_as_matcher.rb', line 47 def <<-MSG.gsub(/^\s{10}/, '') expected that element of type #{@actual.class} would be scripted differently: expected: #{expected.is_a?(Regexp) ? '/' + canonize_js(expected.source) + '/' : canonize_js(expected)} (#{expected.class}) got: #{@actual.nil? ? 'nil' : canonize_js(@actual.content)} MSG end |
#failure_message_for_should_not ⇒ Object
55 56 57 |
# File 'lib/arbre/rspec/be_scripted_as_matcher.rb', line 55 def "expected that element of type #{@actual.class} would be not scripted as #{expected}" end |
#matches?(actual) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/arbre/rspec/be_scripted_as_matcher.rb', line 24 def matches?(actual) @actual = actual regexp = case expected when Regexp then Regexp.new(canonize_js(expected.source)) else Regexp.new('^' + Regexp.escape(canonize_js(expected)).gsub('\(\.\.\.\)', '.+') + '$') end canonize_js(actual.content) =~ regexp end |