| 9 | | def matches?(stringlike) |
|---|
| 10 | | @document = case stringlike |
|---|
| 11 | | when Hpricot::Elem |
|---|
| 12 | | stringlike |
|---|
| 13 | | when StringIO |
|---|
| 14 | | Hpricot.parse(stringlike.string) |
|---|
| 15 | | else |
|---|
| 16 | | Hpricot.parse(stringlike) |
|---|
| | 11 | def matches?(stringlike) |
|---|
| | 12 | @document = case stringlike |
|---|
| | 13 | when Hpricot::Elem |
|---|
| | 14 | stringlike |
|---|
| | 15 | when StringIO |
|---|
| | 16 | Hpricot.parse(stringlike.string) |
|---|
| | 17 | else |
|---|
| | 18 | Hpricot.parse(stringlike) |
|---|
| | 19 | end |
|---|
| | 20 | !@document.search(@expected).empty? |
|---|
| | 21 | end |
|---|
| | 22 | |
|---|
| | 23 | def failure_message |
|---|
| | 24 | "expected following text to match selector #{@expected}:\n#{@document}" |
|---|
| | 25 | end |
|---|
| | 26 | |
|---|
| | 27 | def negative_failure_message |
|---|
| | 28 | "expected following text to not match selector #{@expected}:\n#{@document}" |
|---|
| | 29 | end |
|---|
| 18 | | !@document.search(@expected).empty? |
|---|
| 19 | | end |
|---|
| | 31 | |
|---|
| | 32 | class MatchTag |
|---|
| | 33 | def initialize(name, attrs) |
|---|
| | 34 | @name, @attrs = name, attrs |
|---|
| | 35 | @content = @attrs.delete(:content) |
|---|
| | 36 | end |
|---|
| | 37 | |
|---|
| | 38 | def matches?(target) |
|---|
| | 39 | @errors = [] |
|---|
| | 40 | unless target.include?("<#{@name}") |
|---|
| | 41 | @errors << "Expected a <#{@name}>, but was #{target}" |
|---|
| | 42 | end |
|---|
| | 43 | @attrs.each do |attr, val| |
|---|
| | 44 | unless target.include?("#{attr}=\"#{val}\"") |
|---|
| | 45 | @errors << "Expected #{attr}=\"#{val}\", but was #{target}" |
|---|
| | 46 | end |
|---|
| | 47 | end |
|---|
| | 48 | if @content |
|---|
| | 49 | unless target.include?(">#{@content}<") |
|---|
| | 50 | @errors << "Expected #{target} to include #{@content}" |
|---|
| | 51 | end |
|---|
| | 52 | end |
|---|
| | 53 | @errors.size == 0 |
|---|
| | 54 | end |
|---|
| 21 | | def failure_message |
|---|
| 22 | | "expected following text to match selector #{@expected}:\n#{@document}" |
|---|
| 23 | | end |
|---|
| 24 | | |
|---|
| 25 | | def negative_failure_message |
|---|
| 26 | | "expected following text to not match selector #{@expected}:\n#{@document}" |
|---|
| | 56 | def failure_message |
|---|
| | 57 | @errors[0] |
|---|
| | 58 | end |
|---|
| | 59 | |
|---|
| | 60 | def negative_failure_message |
|---|
| | 61 | "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" |
|---|
| | 62 | end |
|---|
| | 63 | end |
|---|
| | 64 | |
|---|
| | 65 | class NotMatchTag |
|---|
| | 66 | def initialize(attrs) |
|---|
| | 67 | @attrs = attrs |
|---|
| | 68 | end |
|---|
| | 69 | |
|---|
| | 70 | def matches?(target) |
|---|
| | 71 | @errors = [] |
|---|
| | 72 | @attrs.each do |attr, val| |
|---|
| | 73 | if target.include?("#{attr}=\"#{val}\"") |
|---|
| | 74 | @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" |
|---|
| | 75 | end |
|---|
| | 76 | end |
|---|
| | 77 | @errors.size == 0 |
|---|
| | 78 | end |
|---|
| | 79 | |
|---|
| | 80 | def failure_message |
|---|
| | 81 | @errors[0] |
|---|
| | 82 | end |
|---|
| | 83 | end |
|---|
| | 84 | |
|---|
| | 85 | def match_tag(name, attrs) |
|---|
| | 86 | MatchTag.new(name, attrs) |
|---|
| | 87 | end |
|---|
| | 88 | def not_match_tag(attrs) |
|---|
| | 89 | NotMatchTag.new(attrs) |
|---|
| | 90 | end |
|---|
| | 91 | |
|---|
| | 92 | def have_selector(expected) |
|---|
| | 93 | HaveSelector.new(expected) |
|---|
| | 94 | end |
|---|
| | 95 | alias_method :match_selector, :have_selector |
|---|
| | 96 | # alias_method :match_regex, :match |
|---|
| 29 | | |
|---|
| 30 | | class MatchTag |
|---|
| 31 | | def initialize(name, attrs) |
|---|
| 32 | | @name, @attrs = name, attrs |
|---|
| 33 | | @content = @attrs.delete(:content) |
|---|
| 34 | | end |
|---|
| 35 | | |
|---|
| 36 | | def matches?(target) |
|---|
| 37 | | @errors = [] |
|---|
| 38 | | unless target.include?("<#{@name}") |
|---|
| 39 | | @errors << "Expected a <#{@name}>, but was #{target}" |
|---|
| 40 | | end |
|---|
| 41 | | @attrs.each do |attr, val| |
|---|
| 42 | | unless target.include?("#{attr}=\"#{val}\"") |
|---|
| 43 | | @errors << "Expected #{attr}=\"#{val}\", but was #{target}" |
|---|
| 44 | | end |
|---|
| 45 | | end |
|---|
| 46 | | if @content |
|---|
| 47 | | unless target.include?(">#{@content}<") |
|---|
| 48 | | @errors << "Expected #{target} to include #{@content}" |
|---|
| 49 | | end |
|---|
| 50 | | end |
|---|
| 51 | | @errors.size == 0 |
|---|
| 52 | | end |
|---|
| 53 | | |
|---|
| 54 | | def failure_message |
|---|
| 55 | | @errors[0] |
|---|
| 56 | | end |
|---|
| 57 | | |
|---|
| 58 | | def negative_failure_message |
|---|
| 59 | | "Expected not to match against <#{@name} #{@attrs.map{ |a,v| "#{a}=\"#{v}\"" }.join(" ")}> tag, but it matched" |
|---|
| 60 | | end |
|---|
| 61 | | end |
|---|
| 62 | | |
|---|
| 63 | | class NotMatchTag |
|---|
| 64 | | def initialize(attrs) |
|---|
| 65 | | @attrs = attrs |
|---|
| 66 | | end |
|---|
| 67 | | |
|---|
| 68 | | def matches?(target) |
|---|
| 69 | | @errors = [] |
|---|
| 70 | | @attrs.each do |attr, val| |
|---|
| 71 | | if target.include?("#{attr}=\"#{val}\"") |
|---|
| 72 | | @errors << "Should not include #{attr}=\"#{val}\", but was #{target}" |
|---|
| 73 | | end |
|---|
| 74 | | end |
|---|
| 75 | | @errors.size == 0 |
|---|
| 76 | | end |
|---|
| 77 | | |
|---|
| 78 | | def failure_message |
|---|
| 79 | | @errors[0] |
|---|
| 80 | | end |
|---|
| 81 | | end |
|---|
| 82 | | |
|---|
| 83 | | def match_tag(name, attrs) |
|---|
| 84 | | MatchTag.new(name, attrs) |
|---|
| 85 | | end |
|---|
| 86 | | def not_match_tag(attrs) |
|---|
| 87 | | NotMatchTag.new(attrs) |
|---|
| 88 | | end |
|---|
| 89 | | |
|---|
| 90 | | def have_selector(expected) |
|---|
| 91 | | HaveSelector.new(expected) |
|---|
| 92 | | end |
|---|
| 93 | | alias_method :match_selector, :have_selector |
|---|
| 94 | | # alias_method :match_regex, :match |
|---|