Skip to content

Commit b810e9b

Browse files
committed
Make Regexps spouted in error message easier to read.
Becomes: ``` Expected at least 1 element matching "div:match('id', "/wups/")", found 0.. Expected 0 to be >= 1 ``` Instead of saying "\"(?-mix:wups)\"".
1 parent 3a2d936 commit b810e9b

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class HTMLSelector #:nodoc:
77
def initialize(values, previous_selection = nil, &root_fallback)
88
@values = values
99
@root = extract_root(previous_selection, root_fallback)
10-
@css_selector = @selector = extract_selector
10+
extract_selectors
1111
@tests = extract_equality_tests
1212
@message = @values.shift
1313

@@ -73,15 +73,15 @@ def extract_root(previous_selection, root_fallback)
7373
end
7474
end
7575

76-
def extract_selector
76+
def extract_selectors
7777
selector = @values.shift
7878

7979
unless selector.is_a? String
8080
raise ArgumentError, "Expecting a selector as the first argument"
8181
end
8282

83-
context.substitute!(selector, @values)
84-
selector
83+
@css_selector = context.substitute!(selector, @values.dup, true)
84+
@selector = context.substitute!(selector, @values)
8585
end
8686

8787
def extract_equality_tests

lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ def initialize
33
@substitute = '?'
44
end
55

6-
def substitute!(selector, values)
6+
def substitute!(selector, values, format_for_presentation = false)
7+
selector = selector.dup
8+
79
while !values.empty? && substitutable?(values.first) && selector.index(@substitute)
8-
selector.sub! @substitute, matcher_for(values.shift)
10+
selector.sub! @substitute, matcher_for(values.shift, format_for_presentation)
911
end
12+
13+
selector
1014
end
1115

1216
def match(matches, attribute, matcher)
1317
matches.find_all { |node| node[attribute] =~ Regexp.new(matcher) }
1418
end
1519

1620
private
17-
def matcher_for(value)
21+
def matcher_for(value, format_for_presentation)
22+
if format_for_presentation && value.is_a?(Regexp)
23+
value = value.inspect # Inspect Regexps for readability in error messages.
24+
end
25+
1826
value.to_s.inspect # Nokogiri doesn't like arbitrary values without quotes, hence inspect.
1927
end
2028

test/selector_assertions_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ def test_css_select_with_invalid_selector
232232
end
233233
end
234234

235+
def test_nested_assert_select_with_match_failure_shows_nice_regex
236+
render_html %Q{<div id="1">foo</div>}
237+
238+
error = assert_raises Minitest::Assertion do
239+
assert_select "div:match('id', ?)", /wups/
240+
end
241+
242+
assert_match %Q{div:match('id', "/wups/")}, error.message
243+
end
244+
235245
def test_feed_item_encoded
236246
render_xml <<-EOF
237247
<rss version="2.0">

0 commit comments

Comments
 (0)