1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
begin
require 'rubygems'
rescue
end
require 'pathname'
$LOAD_PATH.unshift Pathname.new(__FILE__).dirname.parent.parent + 'lib/'
require 'bluefeather'
require 'nokogiri'
module NokogiriMatcher
class HaveElement
def initialize(selector)
@selector = selector
end
def description
"have element"
end
def matches?(target)
@target = target
return !(@target.search(@selector).empty?)
end
def failure_message
%Q|expected an element selected by '#{@selector}', got no element\n(html: #{@target.to_s.inspect})|
end
def negative_failure_message
%Q|expected no element selected by '#{@selector}', got an element\n(html: #{@target.to_s.inspect})|
end
end
class HaveElements
def initialize(number, selector)
@number, @selector = number, selector
end
def description
"have elements"
end
def matches?(target)
@target = target
@found_number = @target.search(@selector).size
return @found_number == @number
end
def failure_message
%Q|expected #{@number} elements selected by '#{@selector}', got #{@found_number} elements\n(html: #{@target.to_s.inspect})|
end
def negative_failure_message
%Q|not expected #{@number} elements selected by '#{@selector}', got #{@found_number} elements\n(html: #{@target.to_s.inspect})|
end
end
end
# ex) doc.should have_element('div table')
def have_element(selector)
NokogiriMatcher::HaveElement.new(selector)
end
alias have_element_as have_element
# ex) doc.should have_elements(3, 'p')
def have_elements(number, selector)
NokogiriMatcher::HaveElements.new(number, selector)
end
alias have_elements_as have_elements
module CUIMatcher
class WriteToIOBase
def initialize(expected = nil)
# if expected is nil, allow any outputs.
@expected = expected
end
def description
"write to IO"
end
def matches?(cui)
@cui = cui
if @expected then
return get_written_string(@cui) == @expected
else
return !(get_written_string(@cui).empty?)
end
end
def get_written_string(cui)
# not implemented
end
def failure_message
if @expected then
%Q|expected: #{@expected.inspect}\n got: #{get_written_string(@cui).inspect})|
else
%Q|expected: any\n got: #{get_written_string(@cui).inspect})|
end
end
def negative_failure_message
if @expected then
%Q|not expected: #{@expected.inspect}\n got: #{get_written_string(@cui).inspect})|
else
%Q|not expected: any\n got: #{get_written_string(@cui).inspect})|
end
end
end
class WriteToStdout < WriteToIOBase
def description
"write to stdout"
end
def get_written_string(cui)
cui.stdout.string
end
end
class WriteToStderr < WriteToIOBase
def description
"write to stderr"
end
def get_written_string(cui)
cui.stderr.string
end
end
end
def write_to_stdout(expected = nil)
CUIMatcher::WriteToStdout.new(expected)
end
alias wrote_to_stdout write_to_stdout
def write_to_stderr(expected = nil)
CUIMatcher::WriteToStderr.new(expected)
end
alias wrote_to_stderr write_to_stderr
=begin
# new matcher
class String
def to_xml
REXML::Document.new("<xml>#{self}</xml>")
end
def xml_as?(other)
a = REXML::Document.new("<xml>#{self}</xml>")
b = REXML::Document.new("<xml>#{other}</xml>")
a == b
end
end
# comparable
module REXML
class Element
def ==(other)
text == other.text and attributes == other.attributes and children == other.children
end
end
end
=end
|