@@ -39,51 +39,85 @@ def test_that_it_has_a_version_number
3939
4040 URLPATTERNTESTDATA . each_with_index do |entry , i |
4141 define_method ( "test_urlpattern_#{ i } " ) do
42- skip if [ [ { pathname : "*{}**?" } ] , [ "((?R)):" ] ] . include? ( entry [ :pattern ] )
42+ skip_if_unsupported entry
4343
44- if entry [ :expected_obj ] == "error"
45- assert_raises ( StandardError ) { URLPattern ::URLPattern . new ( *entry [ :pattern ] ) }
46- return
47- end
44+ return if assert_expected_obj_error entry
4845
49- begin
50- pattern = URLPattern ::URLPattern . new ( *entry [ :pattern ] )
51- rescue EncodingError
52- skip
53- end
46+ pattern = new_urlpattern entry
5447
55- entry [ :expected_obj ] &.each do |key , value |
56- assert_equal pattern . send ( key ) , value
57- end
48+ assert_expected_obj pattern , entry
5849
59- if entry [ :expected_match ] == "error"
60- assert_raises ( StandardError ) { pattern . test? ( *entry [ :inputs ] ) }
50+ return if assert_expected_match pattern , entry
6151
62- assert_raises ( StandardError ) { pattern . exec ( *entry [ :inputs ] ) }
52+ assert_exactly_empty_components pattern , entry
53+ end
54+ end
6355
64- return
56+ private
6557
66- elsif entry [ :expected_match ] . is_a? ( Hash )
67- assert pattern . test? ( *entry [ :inputs ] )
58+ def skip_if_unsupported ( entry )
59+ skip if [ [ { pathname : "*{}**?" } ] , [ "((?R)):" ] ] . include? ( entry [ :pattern ] )
60+ end
6861
69- result = pattern . exec ( *entry [ :inputs ] )
70- refute_nil result
71- entry [ :expected_match ] . each do |key , expected |
72- assert_equal result [ key ] , expected
73- end
62+ def assert_expected_obj_error ( entry )
63+ return unless entry [ :expected_obj ] == "error"
7464
75- else
76- refute pattern . test? ( *entry [ :inputs ] )
65+ assert_raises ( StandardError ) { URLPattern ::URLPattern . new ( *entry [ :pattern ] ) }
66+ true
67+ end
7768
78- assert_nil pattern . exec ( *entry [ :inputs ] )
79- end
69+ def new_urlpattern ( entry )
70+ URLPattern ::URLPattern . new ( *entry [ :pattern ] )
71+ rescue EncodingError
72+ skip
73+ end
8074
81- return unless entry . key? ( :exactly_empty_components )
75+ def assert_expected_obj ( pattern , entry )
76+ entry [ :expected_obj ] &.each do |key , value |
77+ assert_equal pattern . send ( key ) , value
78+ end
79+ end
8280
83- result = pattern . exec ( *entry [ :inputs ] )
84- entry [ :exactly_empty_components ] . each do |component |
85- assert_equal ( result [ component ] [ :groups ] , { } ) if result
86- end
81+ def assert_expected_match ( pattern , entry )
82+ if entry [ :expected_match ] == "error"
83+ assert_expected_match_error pattern , entry
84+ elsif entry [ :expected_match ] . is_a? ( Hash )
85+ assert_expected_match_hash pattern , entry
86+ else
87+ assert_expected_match_nil pattern , entry
88+ end
89+ end
90+
91+ def assert_expected_match_error ( pattern , entry )
92+ assert_raises ( StandardError ) { pattern . test? ( *entry [ :inputs ] ) }
93+
94+ assert_raises ( StandardError ) { pattern . exec ( *entry [ :inputs ] ) }
95+
96+ true
97+ end
98+
99+ def assert_expected_match_hash ( pattern , entry )
100+ assert pattern . test? ( *entry [ :inputs ] )
101+
102+ result = pattern . exec ( *entry [ :inputs ] )
103+ refute_nil result
104+ entry [ :expected_match ] . each do |key , expected |
105+ assert_equal result [ key ] , expected
106+ end
107+ end
108+
109+ def assert_expected_match_nil ( pattern , entry )
110+ refute pattern . test? ( *entry [ :inputs ] )
111+
112+ assert_nil pattern . exec ( *entry [ :inputs ] )
113+ end
114+
115+ def assert_exactly_empty_components ( pattern , entry )
116+ return unless entry . key? ( :exactly_empty_components )
117+
118+ result = pattern . exec ( *entry [ :inputs ] )
119+ entry [ :exactly_empty_components ] . each do |component |
120+ assert_equal ( result [ component ] [ :groups ] , { } ) if result
87121 end
88122 end
89123end
0 commit comments