summaryrefslogtreecommitdiff
path: root/ruby/test
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-08-31 06:30:16 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-08-31 06:30:16 +0900
commitb5c78de2ddf82783a6f80a199b68927d1a1747ca (patch)
treeaf0605ac128cf76442f81bb17599e1950c7676cd /ruby/test
parenta1bd14e516a0baef6f96b441da70e29e5be7d175 (diff)
downloadmsgpack-python-b5c78de2ddf82783a6f80a199b68927d1a1747ca.tar.gz
ruby: converts encodings into UTF-8 on Ruby 1.9
Diffstat (limited to 'ruby/test')
-rw-r--r--ruby/test/test_encoding.rb68
-rw-r--r--ruby/test/test_helper.rb4
2 files changed, 71 insertions, 1 deletions
diff --git a/ruby/test/test_encoding.rb b/ruby/test/test_encoding.rb
new file mode 100644
index 0000000..2cf0767
--- /dev/null
+++ b/ruby/test/test_encoding.rb
@@ -0,0 +1,68 @@
+#!/usr/bin/env ruby
+require File.dirname(__FILE__)+'/test_helper'
+
+if RUBY_VERSION < "1.9"
+ exit
+end
+
+class MessagePackTestEncoding < Test::Unit::TestCase
+ def self.it(name, &block)
+ define_method("test_#{name}", &block)
+ end
+
+ it "US-ASCII" do
+ check_unpack "abc".force_encoding("US-ASCII")
+ end
+
+ it "UTF-8 ascii" do
+ check_unpack "abc".force_encoding("UTF-8")
+ end
+
+ it "UTF-8 mbstr" do
+ check_unpack "\xE3\x81\x82".force_encoding("UTF-8")
+ end
+
+ it "UTF-8 invalid" do
+ check_unpack "\xD0".force_encoding("UTF-8")
+ end
+
+ it "ASCII-8BIT" do
+ check_unpack "\xD0".force_encoding("ASCII-8BIT")
+ end
+
+ it "EUC-JP" do
+ x = "\xA4\xA2".force_encoding("EUC-JP")
+ check_unpack(x)
+ end
+
+ it "EUC-JP invalid" do
+ begin
+ "\xD0".force_encoding("EUC-JP").to_msgpack
+ assert(false)
+ rescue Encoding::InvalidByteSequenceError
+ assert(true)
+ end
+ end
+
+ private
+ def check_unpack(str)
+ if str.encoding.to_s == "ASCII-8BIT"
+ should_str = str.dup.force_encoding("UTF-8")
+ else
+ should_str = str.encode("UTF-8")
+ end
+
+ raw = str.to_msgpack
+ r = MessagePack.unpack(str.to_msgpack)
+ assert_equal(r.encoding.to_s, "UTF-8")
+ assert_equal(r, should_str.force_encoding("UTF-8"))
+
+ if str.valid_encoding?
+ sym = str.to_sym
+ r = MessagePack.unpack(sym.to_msgpack)
+ assert_equal(r.encoding.to_s, "UTF-8")
+ assert_equal(r, should_str.force_encoding("UTF-8"))
+ end
+ end
+end
+
diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb
index 80d7806..4def861 100644
--- a/ruby/test/test_helper.rb
+++ b/ruby/test/test_helper.rb
@@ -5,4 +5,6 @@ rescue LoadError
require File.dirname(__FILE__) + '/../lib/msgpack'
end
-#GC.stress = true
+if ENV["GC_STRESS"]
+ GC.stress = true
+end