From a5dfe2354312f54065cd7767da3a3cccb5a67267 Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Fri, 5 Jan 2024 15:26:12 +1000 Subject: [PATCH] Raise an error when provided an invalid Range, or invalid :minimum and :maximum --- .../selector_assertions/html_selector.rb | 5 +++++ test/selector_assertions_test.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb index ee13833..b44d655 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -131,6 +131,11 @@ def extract_equality_tests(refute) else comparisons[:minimum] ||= 1 end + + if comparisons[:minimum] && comparisons[:maximum] && comparisons[:minimum] > comparisons[:maximum] + raise ArgumentError, "Range begin or :minimum cannot be greater than Range end or :maximum" + end + comparisons end end diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index e158414..26b81c2 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -210,6 +210,20 @@ def test_assert_select_text_match end end + def test_assert_select_with_invalid_range + render_html "
foo
" + error = assert_raises(ArgumentError) { assert_select("div", 2..1) { nil } } + assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message + end + + def test_assert_select_with_invalid_minimum_and_maximum + render_html "
foo
" + error = assert_raises(ArgumentError) { assert_select("div", maximum: 0) { nil } } + assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message + error = assert_raises(ArgumentError) { assert_select("div", minimum: 2, maximum: 1) { nil } } + assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message + end + # # Test assert_not_select. #