Class: Suhyo::ViewMatchers::HaveOrderedContent

Inherits:
Webrat::Matchers::HasContent
  • Object
show all
Defined in:
lib/suhyo/view_matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(content, options = {}) ⇒ HaveOrderedContent

Returns a new instance of HaveOrderedContent.



15
16
17
18
# File 'lib/suhyo/view_matchers.rb', line 15

def initialize(content, options = {})
	@content = content
	@options = options
end

Instance Method Details

#failure_messageObject



47
48
49
# File 'lib/suhyo/view_matchers.rb', line 47

def failure_message
	"expected the following element's content to #{content_message}#{order_message}:\n\n#{squeeze_space(@element)}"
end

#matches?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/suhyo/view_matchers.rb', line 20

def matches?(stringlike)
	@document = Webrat::XML.document(stringlike)
	@element = @document.inner_text
	compacted = @element.gsub(/\s+/, ' ')
	if @main_content_index = compacted.index(@content)
		if @options.has_key?(:before)
			@other_content_index = compacted.index(@options[:before])
			if @other_content_index.nil?
				return false
			else
				return @main_content_index < @other_content_index
			end
		elsif @options.has_key?(:after)
			@other_content_index = compacted.index(@options[:after])
			if @other_content_index.nil?
				return false
			else
				return @main_content_index > @other_content_index
			end
		else
			return true
		end
	else
		return nil
	end
end

#negative_failure_messageObject



51
52
53
# File 'lib/suhyo/view_matchers.rb', line 51

def negative_failure_message
	"expected the following element's content to not #{content_message}#{order_message}:\n\n#{squeeze_space(@element)}"
end

#order_messageObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/suhyo/view_matchers.rb', line 55

def order_message
	if @options.has_key?(:before)
		str = %Q{ before "#{@options[:before]}"}
		if !@main_content_index.nil? and @other_content_index.nil?
			str << %Q{ but "#{@options[:before]}" was not found}
		end
		return str
	elsif @options.has_key?(:after)
		str = %Q{ after "#{@options[:after]}"}
		if !@main_content_index.nil? and @other_content_index.nil?
			str << %Q{ but "#{@options[:after]}" was not found}
		end
		return str
	else
		return nil
	end
end