| 7 | | class BeRedirect |
|---|
| 8 | | def matches?(target) |
|---|
| 9 | | @target = target |
|---|
| 10 | | target == 302 |
|---|
| 11 | | end |
|---|
| 12 | | def failure_message |
|---|
| 13 | | "expected to redirect" |
|---|
| 14 | | end |
|---|
| 15 | | def negative_failure_message |
|---|
| 16 | | "expected not to redirect" |
|---|
| 17 | | end |
|---|
| 18 | | end |
|---|
| 19 | | |
|---|
| 20 | | class Redirect |
|---|
| 21 | | def matches?(target) |
|---|
| 22 | | @target = target |
|---|
| 23 | | BeRedirect.new.matches?(target.status) |
|---|
| 24 | | end |
|---|
| 25 | | def failure_message |
|---|
| 26 | | "expected #{@target.inspect} to redirect" |
|---|
| 27 | | end |
|---|
| 28 | | def negative_failure_message |
|---|
| 29 | | "expected #{@target.inspect} not to redirect" |
|---|
| 30 | | end |
|---|
| 31 | | end |
|---|
| 32 | | |
|---|
| 33 | | class RedirectTo |
|---|
| 34 | | def initialize(expected) |
|---|
| 35 | | @expected = expected |
|---|
| 36 | | end |
|---|
| 37 | | |
|---|
| 38 | | def matches?(target) |
|---|
| 39 | | @target = target.headers['Location'] |
|---|
| 40 | | @redirected = BeRedirect.new.matches?(target.status) |
|---|
| 41 | | @target == @expected |
|---|
| 42 | | end |
|---|
| 43 | | |
|---|
| 44 | | def failure_message |
|---|
| 45 | | msg = "expected a redirect to <#{@expected}>, but " |
|---|
| 46 | | if @redirected |
|---|
| 47 | | msg << "found one to <#{@target}>" |
|---|
| 48 | | else |
|---|
| 49 | | msg << "there was no redirect" |
|---|
| 50 | | end |
|---|
| 51 | | end |
|---|
| 52 | | |
|---|
| 53 | | def negative_failure_message |
|---|
| 54 | | "expected not to redirect to <#{@expected}>, but did anyway" |
|---|
| 55 | | end |
|---|
| 56 | | end |
|---|
| 57 | | |
|---|
| 58 | | def be_redirect |
|---|
| 59 | | BeRedirect.new |
|---|
| 60 | | end |
|---|
| 61 | | |
|---|
| 62 | | def redirect |
|---|
| 63 | | Redirect.new |
|---|
| 64 | | end |
|---|
| 65 | | |
|---|
| 66 | | def redirect_to(expected) |
|---|
| 67 | | RedirectTo.new(expected) |
|---|
| 68 | | end |
|---|
| 69 | | |
|---|
| 70 | | |
|---|
| 71 | | class HaveSelector |
|---|
| 72 | | def initialize(expected) |
|---|
| 73 | | @expected = expected |
|---|
| 74 | | end |
|---|
| 75 | | |
|---|
| 76 | | def matches?(stringlike) |
|---|
| 77 | | @document = case stringlike |
|---|
| 78 | | when Hpricot::Elem |
|---|
| 79 | | stringlike |
|---|
| 80 | | when StringIO |
|---|
| 81 | | Hpricot.parse(stringlike.string) |
|---|
| 82 | | else |
|---|
| 83 | | Hpricot.parse(stringlike) |
|---|
| 84 | | end |
|---|
| 85 | | !@document.search(@expected).empty? |
|---|
| 86 | | end |
|---|
| 87 | | |
|---|
| 88 | | def failure_message |
|---|
| 89 | | "expected following text to match selector #{@expected}:\n#{@document}" |
|---|
| 90 | | end |
|---|
| 91 | | |
|---|
| 92 | | def negative_failure_message |
|---|
| 93 | | "expected following text to not match selector #{@expected}:\n#{@document}" |
|---|
| 94 | | end |
|---|
| 95 | | end |
|---|
| 96 | | |
|---|
| 97 | | class MatchTag |
|---|
| 98 | | def initialize(name, attrs) |
|---|
| 99 | | @name, @attrs = name, attrs |
|---|
| 100 | | @content = @attrs.delete(:content) |
|---|
| 101 | | end |
|---|
| 102 | | |
|---|
| 103 | | def matches?(target) |
|---|
| 104 | | @errors = [] |
|---|
| 105 | | unless target.include?("<#{@name}") |
|---|
| 106 | | @errors << "Expected a <#{@name}>, but was #{target}" |
|---|
| 107 | | end |
|---|
| 108 | | @attrs.each do |attr, val| |
|---|
| 109 | | unless target.include?("#{attr}=\"#{val}\"") |
|---|
| 110 | | @errors << "Expected #{attr}=\"#{val}\", but was #{target}" |
|---|
| 111 | | end |
|---|
| 112 | | end |
|---|
| 113 | | if @content |
|---|
| 114 | | unless target.include?(">#{@content}<") |
|---|
| 115 | | @errors << "Expected #{target} to include #{@content}" |
|---|
| 116 | | end |
|---|
| 117 | | end |
|---|
| 118 | | @errors.size == 0 |
|---|
| 119 | | end |
|---|
| 120 | | |
|---|
| 121 | | def failure_message |
|---|
| 122 | | @errors[0] |
|---|
| 123 | | end |
|---|
| 124 | | |
|---|
| 125 | | def negative_failure_message |
|---|
| 126 | | "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" |
|---|
| 127 | | end |
|---|
| 128 | | end |
|---|
| 129 | | |
|---|
| 130 | | class NotMatchTag |
|---|
| 131 | | def initialize(attrs) |
|---|
| 132 | | @attrs = attrs |
|---|
| 133 | | end |
|---|
| 134 | | |
|---|
| 135 | | def matches?(target) |
|---|
| 136 | | @errors = [] |
|---|
| 137 | | @attrs.each do |attr, val| |
|---|
| 138 | | if target.include?("#{attr}=\"#{val}\"") |
|---|
| 139 | | @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" |
|---|
| 140 | | end |
|---|
| 141 | | end |
|---|
| 142 | | @errors.size == 0 |
|---|
| 143 | | end |
|---|
| 144 | | |
|---|
| 145 | | def failure_message |
|---|
| 146 | | @errors[0] |
|---|
| 147 | | end |
|---|
| 148 | | end |
|---|
| 149 | | |
|---|
| 150 | | def match_tag(name, attrs) |
|---|
| 151 | | MatchTag.new(name, attrs) |
|---|
| 152 | | end |
|---|
| 153 | | def not_match_tag(attrs) |
|---|
| 154 | | NotMatchTag.new(attrs) |
|---|
| 155 | | end |
|---|
| 156 | | |
|---|
| 157 | | def have_selector(expected) |
|---|
| 158 | | HaveSelector.new(expected) |
|---|
| 159 | | end |
|---|
| 160 | | alias_method :match_selector, :have_selector |
|---|
| 161 | | # alias_method :match_regex, :match |
|---|