|
4 | 4 |
|
5 | 5 | require_relative "substitution_context"
|
6 | 6 |
|
7 |
| -class HTMLSelector # :nodoc: |
8 |
| - attr_reader :css_selector, :tests, :message |
9 |
| - |
10 |
| - include Minitest::Assertions |
11 |
| - |
12 |
| - def initialize(values, previous_selection = nil, &root_fallback) |
13 |
| - @values = values |
14 |
| - @root = extract_root(previous_selection, root_fallback) |
15 |
| - extract_selectors |
16 |
| - @tests = extract_equality_tests |
17 |
| - @message = @values.shift |
18 |
| - |
19 |
| - if @message.is_a?(Hash) |
20 |
| - raise ArgumentError, "Last argument was a Hash, which would be used for the assertion message. You probably want this to be a String, or you have the wrong type of arguments." |
21 |
| - end |
22 |
| - |
23 |
| - if @values.shift |
24 |
| - raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type" |
25 |
| - end |
26 |
| - end |
27 |
| - |
28 |
| - def selecting_no_body? # :nodoc: |
29 |
| - # Nokogiri gives the document a body element. Which means we can't |
30 |
| - # run an assertion expecting there to not be a body. |
31 |
| - @selector == "body" && @tests[:count] == 0 |
32 |
| - end |
33 |
| - |
34 |
| - def select |
35 |
| - filter @root.css(@selector, context) |
36 |
| - end |
37 |
| - |
38 |
| - private |
39 |
| - NO_STRIP = %w{pre script style textarea} |
40 |
| - |
41 |
| - mattr_reader(:context) { SubstitutionContext.new } |
42 |
| - |
43 |
| - def filter(matches) |
44 |
| - match_with = tests[:text] || tests[:html] |
45 |
| - return matches if matches.empty? || !match_with |
46 |
| - |
47 |
| - content_mismatch = nil |
48 |
| - text_matches = tests.has_key?(:text) |
49 |
| - regex_matching = match_with.is_a?(Regexp) |
50 |
| - |
51 |
| - remaining = matches.reject do |match| |
52 |
| - # Preserve markup with to_s for html elements |
53 |
| - content = text_matches ? match.text : match.children.to_s |
54 |
| - |
55 |
| - content.strip! unless NO_STRIP.include?(match.name) |
56 |
| - content.delete_prefix!("\n") if text_matches && match.name == "textarea" |
57 |
| - |
58 |
| - next if regex_matching ? (content =~ match_with) : (content == match_with) |
59 |
| - content_mismatch ||= diff(match_with, content) |
60 |
| - true |
61 |
| - end |
62 |
| - |
63 |
| - @message ||= content_mismatch if remaining.empty? |
64 |
| - Nokogiri::XML::NodeSet.new(matches.document, remaining) |
65 |
| - end |
66 |
| - |
67 |
| - def extract_root(previous_selection, root_fallback) |
68 |
| - possible_root = @values.first |
69 |
| - |
70 |
| - if possible_root == nil |
71 |
| - raise ArgumentError, "First argument is either selector or element " \ |
72 |
| - "to select, but nil found. Perhaps you called assert_dom with " \ |
73 |
| - "an element that does not exist?" |
74 |
| - elsif possible_root.respond_to?(:css) |
75 |
| - @values.shift # remove the root, so selector is the first argument |
76 |
| - possible_root |
77 |
| - elsif previous_selection |
78 |
| - previous_selection |
79 |
| - else |
80 |
| - root_fallback.call |
81 |
| - end |
82 |
| - end |
83 |
| - |
84 |
| - def extract_selectors |
85 |
| - selector = @values.shift |
86 |
| - |
87 |
| - unless selector.is_a? String |
88 |
| - raise ArgumentError, "Expecting a selector as the first argument" |
89 |
| - end |
90 |
| - |
91 |
| - @css_selector = context.substitute!(selector, @values.dup, true) |
92 |
| - @selector = context.substitute!(selector, @values) |
93 |
| - end |
94 |
| - |
95 |
| - def extract_equality_tests |
96 |
| - comparisons = {} |
97 |
| - case comparator = @values.shift |
98 |
| - when Hash |
99 |
| - comparisons = comparator |
100 |
| - when String, Regexp |
101 |
| - comparisons[:text] = comparator |
102 |
| - when Integer |
103 |
| - comparisons[:count] = comparator |
104 |
| - when Range |
105 |
| - comparisons[:minimum] = comparator.begin |
106 |
| - comparisons[:maximum] = comparator.end |
107 |
| - when FalseClass |
108 |
| - comparisons[:count] = 0 |
109 |
| - when NilClass, TrueClass |
110 |
| - comparisons[:minimum] = 1 |
111 |
| - else raise ArgumentError, "I don't understand what you're trying to match" |
112 |
| - end |
113 |
| - |
114 |
| - # By default we're looking for at least one match. |
115 |
| - if comparisons[:count] |
116 |
| - comparisons[:minimum] = comparisons[:maximum] = comparisons[:count] |
117 |
| - else |
118 |
| - comparisons[:minimum] ||= 1 |
| 7 | +module Rails |
| 8 | + module Dom |
| 9 | + module Testing |
| 10 | + module Assertions |
| 11 | + module SelectorAssertions |
| 12 | + class HTMLSelector # :nodoc: |
| 13 | + attr_reader :css_selector, :tests, :message |
| 14 | + |
| 15 | + include Minitest::Assertions |
| 16 | + |
| 17 | + def initialize(values, previous_selection = nil, &root_fallback) |
| 18 | + @values = values |
| 19 | + @root = extract_root(previous_selection, root_fallback) |
| 20 | + extract_selectors |
| 21 | + @tests = extract_equality_tests |
| 22 | + @message = @values.shift |
| 23 | + |
| 24 | + if @message.is_a?(Hash) |
| 25 | + raise ArgumentError, "Last argument was a Hash, which would be used for the assertion message. You probably want this to be a String, or you have the wrong type of arguments." |
| 26 | + end |
| 27 | + |
| 28 | + if @values.shift |
| 29 | + raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type" |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def selecting_no_body? # :nodoc: |
| 34 | + # Nokogiri gives the document a body element. Which means we can't |
| 35 | + # run an assertion expecting there to not be a body. |
| 36 | + @selector == "body" && @tests[:count] == 0 |
| 37 | + end |
| 38 | + |
| 39 | + def select |
| 40 | + filter @root.css(@selector, context) |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + NO_STRIP = %w{pre script style textarea} |
| 45 | + |
| 46 | + mattr_reader(:context) { SubstitutionContext.new } |
| 47 | + |
| 48 | + def filter(matches) |
| 49 | + match_with = tests[:text] || tests[:html] |
| 50 | + return matches if matches.empty? || !match_with |
| 51 | + |
| 52 | + content_mismatch = nil |
| 53 | + text_matches = tests.has_key?(:text) |
| 54 | + regex_matching = match_with.is_a?(Regexp) |
| 55 | + |
| 56 | + remaining = matches.reject do |match| |
| 57 | + # Preserve markup with to_s for html elements |
| 58 | + content = text_matches ? match.text : match.children.to_s |
| 59 | + |
| 60 | + content.strip! unless NO_STRIP.include?(match.name) |
| 61 | + content.delete_prefix!("\n") if text_matches && match.name == "textarea" |
| 62 | + |
| 63 | + next if regex_matching ? (content =~ match_with) : (content == match_with) |
| 64 | + content_mismatch ||= diff(match_with, content) |
| 65 | + true |
| 66 | + end |
| 67 | + |
| 68 | + @message ||= content_mismatch if remaining.empty? |
| 69 | + Nokogiri::XML::NodeSet.new(matches.document, remaining) |
| 70 | + end |
| 71 | + |
| 72 | + def extract_root(previous_selection, root_fallback) |
| 73 | + possible_root = @values.first |
| 74 | + |
| 75 | + if possible_root == nil |
| 76 | + raise ArgumentError, "First argument is either selector or element " \ |
| 77 | + "to select, but nil found. Perhaps you called assert_dom with " \ |
| 78 | + "an element that does not exist?" |
| 79 | + elsif possible_root.respond_to?(:css) |
| 80 | + @values.shift # remove the root, so selector is the first argument |
| 81 | + possible_root |
| 82 | + elsif previous_selection |
| 83 | + previous_selection |
| 84 | + else |
| 85 | + root_fallback.call |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def extract_selectors |
| 90 | + selector = @values.shift |
| 91 | + |
| 92 | + unless selector.is_a? String |
| 93 | + raise ArgumentError, "Expecting a selector as the first argument" |
| 94 | + end |
| 95 | + |
| 96 | + @css_selector = context.substitute!(selector, @values.dup, true) |
| 97 | + @selector = context.substitute!(selector, @values) |
| 98 | + end |
| 99 | + |
| 100 | + def extract_equality_tests |
| 101 | + comparisons = {} |
| 102 | + case comparator = @values.shift |
| 103 | + when Hash |
| 104 | + comparisons = comparator |
| 105 | + when String, Regexp |
| 106 | + comparisons[:text] = comparator |
| 107 | + when Integer |
| 108 | + comparisons[:count] = comparator |
| 109 | + when Range |
| 110 | + comparisons[:minimum] = comparator.begin |
| 111 | + comparisons[:maximum] = comparator.end |
| 112 | + when FalseClass |
| 113 | + comparisons[:count] = 0 |
| 114 | + when NilClass, TrueClass |
| 115 | + comparisons[:minimum] = 1 |
| 116 | + else raise ArgumentError, "I don't understand what you're trying to match" |
| 117 | + end |
| 118 | + |
| 119 | + # By default we're looking for at least one match. |
| 120 | + if comparisons[:count] |
| 121 | + comparisons[:minimum] = comparisons[:maximum] = comparisons[:count] |
| 122 | + else |
| 123 | + comparisons[:minimum] ||= 1 |
| 124 | + end |
| 125 | + comparisons |
| 126 | + end |
| 127 | + end |
| 128 | + end |
119 | 129 | end
|
120 |
| - comparisons |
121 | 130 | end
|
| 131 | + end |
122 | 132 | end
|
0 commit comments