Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix spec covering the encoding of Array#inspect with 1.9 #147

Merged
merged 1 commit into from

3 participants

Flavio Castelli NARUSE, Yui Brian Shirai
Flavio Castelli

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.

NARUSE, Yui

You should use before and after :each to set and recover Encoding.default_external.

Flavio Castelli flavio 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.
5512dec
Flavio Castelli

@nurse fixed

Brian Shirai brixen merged commit a6e90ee into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 27, 2012
  1. Flavio Castelli

    Fix spec covering the encoding of Array#inspect with 1.9

    flavio authored
    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.
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 17 deletions.
  1. +24 −17 core/array/shared/inspect.rb
41 core/array/shared/inspect.rb
View
@@ -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
Something went wrong with that request. Please try again.