Skip to content

Commit e80982a

Browse files
committed
Spec exception messages for Regexp.union
1 parent 49ae17c commit e80982a

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

spec/ruby/core/regexp/union_spec.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,83 +72,83 @@
7272
it "raises ArgumentError if the arguments include conflicting ASCII-incompatible Strings" do
7373
-> {
7474
Regexp.union("a".encode("UTF-16LE"), "b".encode("UTF-16BE"))
75-
}.should raise_error(ArgumentError)
75+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and UTF-16BE')
7676
end
7777

7878
it "raises ArgumentError if the arguments include conflicting ASCII-incompatible Regexps" do
7979
-> {
8080
Regexp.union(Regexp.new("a".encode("UTF-16LE")),
8181
Regexp.new("b".encode("UTF-16BE")))
82-
}.should raise_error(ArgumentError)
82+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and UTF-16BE')
8383
end
8484

8585
it "raises ArgumentError if the arguments include conflicting fixed encoding Regexps" do
8686
-> {
8787
Regexp.union(Regexp.new("a".encode("UTF-8"), Regexp::FIXEDENCODING),
8888
Regexp.new("b".encode("US-ASCII"), Regexp::FIXEDENCODING))
89-
}.should raise_error(ArgumentError)
89+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-8 and US-ASCII')
9090
end
9191

9292
it "raises ArgumentError if the arguments include a fixed encoding Regexp and a String containing non-ASCII-compatible characters in a different encoding" do
9393
-> {
9494
Regexp.union(Regexp.new("a".encode("UTF-8"), Regexp::FIXEDENCODING),
9595
"\u00A9".encode("ISO-8859-1"))
96-
}.should raise_error(ArgumentError)
96+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-8 and ISO-8859-1')
9797
end
9898

9999
it "raises ArgumentError if the arguments include a String containing non-ASCII-compatible characters and a fixed encoding Regexp in a different encoding" do
100100
-> {
101101
Regexp.union("\u00A9".encode("ISO-8859-1"),
102102
Regexp.new("a".encode("UTF-8"), Regexp::FIXEDENCODING))
103-
}.should raise_error(ArgumentError)
103+
}.should raise_error(ArgumentError, 'incompatible encodings: ISO-8859-1 and UTF-8')
104104
end
105105

106106
it "raises ArgumentError if the arguments include an ASCII-incompatible String and an ASCII-only String" do
107107
-> {
108108
Regexp.union("a".encode("UTF-16LE"), "b".encode("UTF-8"))
109-
}.should raise_error(ArgumentError)
109+
}.should raise_error(ArgumentError, /ASCII incompatible encoding: UTF-16LE|incompatible encodings: UTF-16LE and US-ASCII/)
110110
end
111111

112112
it "raises ArgumentError if the arguments include an ASCII-incompatible Regexp and an ASCII-only String" do
113113
-> {
114114
Regexp.union(Regexp.new("a".encode("UTF-16LE")), "b".encode("UTF-8"))
115-
}.should raise_error(ArgumentError)
115+
}.should raise_error(ArgumentError, /ASCII incompatible encoding: UTF-16LE|incompatible encodings: UTF-16LE and US-ASCII/)
116116
end
117117

118118
it "raises ArgumentError if the arguments include an ASCII-incompatible String and an ASCII-only Regexp" do
119119
-> {
120120
Regexp.union("a".encode("UTF-16LE"), Regexp.new("b".encode("UTF-8")))
121-
}.should raise_error(ArgumentError)
121+
}.should raise_error(ArgumentError, /ASCII incompatible encoding: UTF-16LE|incompatible encodings: UTF-16LE and US-ASCII/)
122122
end
123123

124124
it "raises ArgumentError if the arguments include an ASCII-incompatible Regexp and an ASCII-only Regexp" do
125125
-> {
126126
Regexp.union(Regexp.new("a".encode("UTF-16LE")), Regexp.new("b".encode("UTF-8")))
127-
}.should raise_error(ArgumentError)
127+
}.should raise_error(ArgumentError, /ASCII incompatible encoding: UTF-16LE|incompatible encodings: UTF-16LE and US-ASCII/)
128128
end
129129

130130
it "raises ArgumentError if the arguments include an ASCII-incompatible String and a String containing non-ASCII-compatible characters in a different encoding" do
131131
-> {
132132
Regexp.union("a".encode("UTF-16LE"), "\u00A9".encode("ISO-8859-1"))
133-
}.should raise_error(ArgumentError)
133+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and ISO-8859-1')
134134
end
135135

136136
it "raises ArgumentError if the arguments include an ASCII-incompatible Regexp and a String containing non-ASCII-compatible characters in a different encoding" do
137137
-> {
138138
Regexp.union(Regexp.new("a".encode("UTF-16LE")), "\u00A9".encode("ISO-8859-1"))
139-
}.should raise_error(ArgumentError)
139+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and ISO-8859-1')
140140
end
141141

142142
it "raises ArgumentError if the arguments include an ASCII-incompatible String and a Regexp containing non-ASCII-compatible characters in a different encoding" do
143143
-> {
144144
Regexp.union("a".encode("UTF-16LE"), Regexp.new("\u00A9".encode("ISO-8859-1")))
145-
}.should raise_error(ArgumentError)
145+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and ISO-8859-1')
146146
end
147147

148148
it "raises ArgumentError if the arguments include an ASCII-incompatible Regexp and a Regexp containing non-ASCII-compatible characters in a different encoding" do
149149
-> {
150150
Regexp.union(Regexp.new("a".encode("UTF-16LE")), Regexp.new("\u00A9".encode("ISO-8859-1")))
151-
}.should raise_error(ArgumentError)
151+
}.should raise_error(ArgumentError, 'incompatible encodings: UTF-16LE and ISO-8859-1')
152152
end
153153

154154
it "uses to_str to convert arguments (if not Regexp)" do
@@ -172,6 +172,8 @@
172172
not_supported_on :opal do
173173
Regexp.union([/dogs/, /cats/i]).should == /(?-mix:dogs)|(?i-mx:cats)/
174174
end
175-
->{Regexp.union(["skiing", "sledding"], [/dogs/, /cats/i])}.should raise_error(TypeError)
175+
-> {
176+
Regexp.union(["skiing", "sledding"], [/dogs/, /cats/i])
177+
}.should raise_error(TypeError, 'no implicit conversion of Array into String')
176178
end
177179
end

src/main/ruby/truffleruby/core/regexp.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,26 @@ def self.try_convert(obj)
5656
end
5757

5858
def self.negotiate_union_encoding(*patterns)
59-
res = nil
59+
compatible_enc = nil
6060

6161
patterns.each do |pattern|
6262
converted = Primitive.is_a?(pattern, Regexp) ? pattern : Regexp.quote(pattern)
6363

6464
enc = converted.encoding
6565

66-
if Primitive.nil?(res)
67-
res = enc
66+
if Primitive.nil?(compatible_enc)
67+
compatible_enc = enc
6868
else
69-
res = Primitive.encoding_compatible?(enc, res)
70-
raise ArgumentError, "incompatible encodings: #{enc} and #{res}" unless res
69+
if test = Primitive.encoding_compatible?(enc, compatible_enc)
70+
compatible_enc = test
71+
else
72+
raise ArgumentError, "incompatible encodings: #{compatible_enc} and #{enc}"
73+
end
74+
7175
end
7276
end
7377

74-
res
78+
compatible_enc
7579
end
7680

7781
def self.last_match(index = nil)

0 commit comments

Comments
 (0)