forked from cloudhead/toto
/
test_helper.rb
44 lines (35 loc) · 1.12 KB
/
test_helper.rb
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
require 'rubygems'
require 'hpricot'
require 'riot'
$:.unshift File.dirname(__FILE__)
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'toto'
module Toto
class IncludesHTMLMacro < Riot::AssertionMacro
register :includes_html
def evaluate(actual, expected)
doc = Hpricot.parse(actual)
expected = expected.to_a.flatten
if (doc/expected.first).empty?
fail("expected #{actual} to contain a <#{expected.first}>")
elsif !(doc/expected.first).inner_html.match(expected.last)
fail("expected <#{expected.first}> to contain #{expected.last}")
else
pass
end
end
end
class IncludesElementsMacro < Riot::AssertionMacro
register :includes_elements
def evaluate(actual, selector, count)
doc = Hpricot.parse(actual)
(doc/selector).size == count ? pass : fail("expected #{actual} to contain #{count} #{selector}(s)")
end
end
class WithinMacro < Riot::AssertionMacro
register :within
def evaluate(actual, expected)
expected.include?(actual) ? pass : fail("expected #{actual} to be within #{expected}")
end
end
end