Created
April 10, 2012 12:28
-
-
Save bernerdschaefer/2351047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'minitest/spec' | |
require 'minitest/autorun' | |
puts RUBY_DESCRIPTION | |
describe "Encoding conversions" do | |
def serialize(string) | |
begin | |
data = string.encode('utf-8') | |
rescue EncodingError | |
data = string.dup | |
data.force_encoding 'utf-8' | |
raise unless data.valid_encoding? | |
end | |
data.force_encoding('binary') | |
end | |
def deserialize(data) | |
data.force_encoding('utf-8') | |
end | |
before do | |
@source = "europäischen" | |
end | |
it "losslessly serializes and deserializes utf-8 data" do | |
deserialize(serialize(@source)).must_equal @source | |
end | |
it "serializes and deserializes iso-8859-1 data as utf-8" do | |
deserialize(serialize(@source.encode('iso-8859-1'))).must_equal @source | |
end | |
it "serializes and deserializes utf-8 data in binary form" do | |
binary = @source.dup.force_encoding('binary') | |
deserialize(serialize(binary)).must_equal @source | |
end | |
it "raises an exception when the data cannot be converted to utf-8" do | |
binary = "\xFF".force_encoding('binary') | |
lambda { deserialize(serialize(binary)) }.must_raise Encoding::UndefinedConversionError | |
end | |
end | |
__END__ | |
$ ruby encoding_test.rb | |
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0] | |
Run options: --seed 44410 | |
# Running tests: | |
.... | |
Finished tests in 0.000923s, 4333.6945 tests/s, 4333.6945 assertions/s. | |
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips | |
$ jruby --1.9 encoding_test.rb | |
jruby 1.7.0.dev (ruby-1.9.3-p139) (2012-04-10 96f32d2) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java] | |
Run options: --seed 28683 | |
# Running tests: | |
.F.F | |
Finished tests in 0.116000s, 34.4828 tests/s, 34.4828 assertions/s. | |
1) Failure: | |
test_0004_raises_an_exception_when_the_data_cannot_be_converted_to_utf_8(Encoding conversions) [encoding_test.rb:44]: | |
Encoding::UndefinedConversionError expected but nothing was raised. | |
2) Failure: | |
test_0003_serializes_and_deserializes_utf_8_data_in_binary_form(Encoding conversions) [encoding_test.rb:39]: | |
Expected: "europäischen" | |
Actual: "europäischen" | |
4 tests, 4 assertions, 2 failures, 0 errors, 0 skips |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment