Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix spec covering the encoding of Array#inspect with 1.9

Array#inspect uses the encoding of the string returned by Object#inspect
ran against the first entry of the array. Object#inspect will call
String#inspect, which does _not_ take into consideration the encoding of
the String object but will rather rely on the default external encoding.

Hence the spec must be rewritten to take into consideration the default
external encoding.
  • Loading branch information...
commit 5512dec70564e647bc65e77ab88759b314489046 1 parent 99e8b67
@flavio authored
Showing with 24 additions and 17 deletions.
  1. +24 −17 core/array/shared/inspect.rb
View
41 core/array/shared/inspect.rb
@@ -51,20 +51,36 @@
end
ruby_version_is "1.9" do
+ before :each do
+ @default_external_encoding = Encoding.default_external
+ end
+
+ after :each do
+ Encoding.default_external = @default_external_encoding
+ end
+
it "returns a US-ASCII string for an empty Array" do
[].send(@method).encoding.should == Encoding::US_ASCII
end
- it "copies the ASCII-compatible encoding of the result of inspecting the first element" do
- euc_jp = mock("euc_jp")
- euc_jp.should_receive(:inspect).and_return("euc_jp".encode!(Encoding::EUC_JP))
+ it "use the default external encoding if it is ascii compatible" do
+ Encoding.default_external = Encoding.find('UTF-8')
- utf_8 = mock("utf_8")
- utf_8.should_receive(:inspect).and_return("utf_8".encode!(Encoding::UTF_8))
+ utf8 = "utf8".encode("UTF-8")
+ jp = "jp".encode("EUC-JP")
+ array = [jp, utf8]
- result = [euc_jp, utf_8].send(@method)
- result.encoding.should == Encoding::EUC_JP
- result.should == "[euc_jp, utf_8]".encode(Encoding::EUC_JP)
+ array.inspect.encoding.name.should == "UTF-8"
+ end
+
+ it "use US-ASCII encoding if the default external encoding is not ascii compatible" do
+ Encoding.default_external = Encoding.find('UTF-32')
+
+ utf8 = "utf8".encode("UTF-8")
+ jp = "jp".encode("EUC-JP")
+ array = [jp, utf8]
+
+ array.inspect.encoding.name.should == "US-ASCII"
end
ruby_bug "5848", "2.0" do
@@ -78,14 +94,5 @@
end
end
- it "raises if inspecting two elements produces incompatible encodings" do
- utf_8 = mock("utf_8")
- utf_8.should_receive(:inspect).and_return("utf_8".encode!(Encoding::UTF_8))
-
- utf_16be = mock("utf_16be")
- utf_16be.should_receive(:inspect).and_return("utf_16be".encode!(Encoding::UTF_16BE))
-
- lambda { [utf_8, utf_16be].send(@method) }.should raise_error(Encoding::CompatibilityError)
- end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.