2010-03-28 17:49:37 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestEncoding < TestCase
|
|
|
|
class EncodingCatcher < Handler
|
|
|
|
attr_reader :strings
|
|
|
|
def initialize
|
|
|
|
@strings = []
|
|
|
|
end
|
|
|
|
|
|
|
|
(Handler.instance_methods(true) -
|
|
|
|
Object.instance_methods).each do |m|
|
|
|
|
class_eval %{
|
|
|
|
def #{m} *args
|
|
|
|
@strings += args.flatten.find_all { |a|
|
|
|
|
String === a
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2010-05-19 13:15:27 -04:00
|
|
|
@buffer = StringIO.new
|
2010-03-28 17:49:37 -04:00
|
|
|
@handler = EncodingCatcher.new
|
|
|
|
@parser = Psych::Parser.new @handler
|
|
|
|
@utf8 = Encoding.find('UTF-8')
|
2010-05-19 13:15:27 -04:00
|
|
|
@emitter = Psych::Emitter.new @buffer
|
|
|
|
end
|
|
|
|
|
2014-02-28 21:09:53 -05:00
|
|
|
def test_dump_load_encoding_object
|
|
|
|
assert_cycle Encoding::US_ASCII
|
|
|
|
assert_cycle Encoding::UTF_8
|
|
|
|
end
|
|
|
|
|
2012-02-23 18:12:57 -05:00
|
|
|
def test_transcode_shiftjis
|
|
|
|
str = "こんにちは!"
|
|
|
|
loaded = Psych.load("--- こんにちは!".encode('SHIFT_JIS'))
|
|
|
|
assert_equal str, loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transcode_utf16le
|
|
|
|
str = "こんにちは!"
|
|
|
|
loaded = Psych.load("--- こんにちは!".encode('UTF-16LE'))
|
|
|
|
assert_equal str, loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transcode_utf16be
|
|
|
|
str = "こんにちは!"
|
|
|
|
loaded = Psych.load("--- こんにちは!".encode('UTF-16BE'))
|
|
|
|
assert_equal str, loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_io_shiftjis
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['shiftjis', 'yml'], :encoding => 'SHIFT_JIS') {|t|
|
|
|
|
t.write '--- こんにちは!'
|
|
|
|
t.close
|
|
|
|
|
|
|
|
# If the external encoding isn't utf8, utf16le, or utf16be, we cannot
|
|
|
|
# process the file.
|
|
|
|
File.open(t.path, 'r', :encoding => 'SHIFT_JIS') do |f|
|
|
|
|
assert_raises Psych::SyntaxError do
|
|
|
|
Psych.load(f)
|
|
|
|
end
|
2012-02-23 18:12:57 -05:00
|
|
|
end
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2012-02-23 18:12:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_io_utf16le
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['utf16le', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write '--- こんにちは!'.encode('UTF-16LE')
|
|
|
|
t.close
|
2012-02-23 18:12:57 -05:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
File.open(t.path, 'rb', :encoding => 'UTF-16LE') do |f|
|
|
|
|
assert_equal "こんにちは!", Psych.load(f)
|
|
|
|
end
|
|
|
|
}
|
2012-02-23 18:12:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_io_utf16be
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['utf16be', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write '--- こんにちは!'.encode('UTF-16BE')
|
|
|
|
t.close
|
2012-02-23 18:12:57 -05:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
File.open(t.path, 'rb', :encoding => 'UTF-16BE') do |f|
|
|
|
|
assert_equal "こんにちは!", Psych.load(f)
|
|
|
|
end
|
|
|
|
}
|
2012-02-23 18:12:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_io_utf8
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['utf8', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write '--- こんにちは!'.encode('UTF-8')
|
|
|
|
t.close
|
2012-02-23 18:12:57 -05:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
File.open(t.path, 'rb', :encoding => 'UTF-8') do |f|
|
|
|
|
assert_equal "こんにちは!", Psych.load(f)
|
|
|
|
end
|
|
|
|
}
|
2012-02-23 18:12:57 -05:00
|
|
|
end
|
|
|
|
|
2010-05-19 13:15:27 -04:00
|
|
|
def test_emit_alias
|
|
|
|
@emitter.start_stream Psych::Parser::UTF8
|
|
|
|
@emitter.start_document [], [], true
|
|
|
|
e = assert_raises(RuntimeError) do
|
|
|
|
@emitter.alias 'ドラえもん'.encode('EUC-JP')
|
|
|
|
end
|
|
|
|
assert_match(/alias value/, e.message)
|
|
|
|
end
|
|
|
|
|
2011-08-24 17:14:44 -04:00
|
|
|
def test_to_yaml_is_valid
|
2013-05-14 13:07:51 -04:00
|
|
|
with_default_external(Encoding::US_ASCII) do
|
|
|
|
with_default_internal(nil) do
|
2013-04-01 22:14:21 -04:00
|
|
|
s = "こんにちは!"
|
|
|
|
# If no encoding is specified, use UTF-8
|
|
|
|
assert_equal Encoding::UTF_8, Psych.dump(s).encoding
|
|
|
|
assert_equal s, Psych.load(Psych.dump(s))
|
|
|
|
end
|
|
|
|
end
|
2011-08-24 17:14:44 -04:00
|
|
|
end
|
|
|
|
|
2010-05-19 13:15:27 -04:00
|
|
|
def test_start_mapping
|
|
|
|
foo = 'foo'
|
|
|
|
bar = 'バー'
|
|
|
|
|
|
|
|
@emitter.start_stream Psych::Parser::UTF8
|
|
|
|
@emitter.start_document [], [], true
|
|
|
|
@emitter.start_mapping(
|
|
|
|
foo.encode('Shift_JIS'),
|
|
|
|
bar.encode('UTF-16LE'),
|
|
|
|
false, Nodes::Sequence::ANY)
|
|
|
|
@emitter.end_mapping
|
|
|
|
@emitter.end_document false
|
|
|
|
@emitter.end_stream
|
|
|
|
|
|
|
|
@parser.parse @buffer.string
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
assert_equal [foo, bar], @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_start_sequence
|
|
|
|
foo = 'foo'
|
|
|
|
bar = 'バー'
|
|
|
|
|
|
|
|
@emitter.start_stream Psych::Parser::UTF8
|
|
|
|
@emitter.start_document [], [], true
|
|
|
|
@emitter.start_sequence(
|
|
|
|
foo.encode('Shift_JIS'),
|
|
|
|
bar.encode('UTF-16LE'),
|
|
|
|
false, Nodes::Sequence::ANY)
|
|
|
|
@emitter.end_sequence
|
|
|
|
@emitter.end_document false
|
|
|
|
@emitter.end_stream
|
|
|
|
|
|
|
|
@parser.parse @buffer.string
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
assert_equal [foo, bar], @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_doc_tag_encoding
|
|
|
|
key = '鍵'
|
|
|
|
@emitter.start_stream Psych::Parser::UTF8
|
|
|
|
@emitter.start_document(
|
|
|
|
[1, 1],
|
|
|
|
[['!'.encode('EUC-JP'), key.encode('EUC-JP')]],
|
|
|
|
true
|
|
|
|
)
|
|
|
|
@emitter.scalar 'foo', nil, nil, true, false, Nodes::Scalar::ANY
|
|
|
|
@emitter.end_document false
|
|
|
|
@emitter.end_stream
|
|
|
|
|
|
|
|
@parser.parse @buffer.string
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
assert_equal key, @handler.strings[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_emitter_encoding
|
|
|
|
str = "壁に耳あり、障子に目あり"
|
|
|
|
thing = Psych.load Psych.dump str.encode('EUC-JP')
|
|
|
|
assert_equal str, thing
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
2010-05-10 12:22:52 -04:00
|
|
|
def test_default_internal
|
2013-05-14 13:07:51 -04:00
|
|
|
with_default_internal(Encoding::EUC_JP) do
|
2013-04-01 22:14:21 -04:00
|
|
|
str = "壁に耳あり、障子に目あり"
|
|
|
|
assert_equal @utf8, str.encoding
|
2010-05-10 12:22:52 -04:00
|
|
|
|
2013-04-01 22:14:21 -04:00
|
|
|
@parser.parse str
|
|
|
|
assert_encodings Encoding::EUC_JP, @handler.strings
|
|
|
|
assert_equal str, @handler.strings.first.encode('UTF-8')
|
|
|
|
end
|
2010-05-10 12:22:52 -04:00
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_scalar
|
|
|
|
@parser.parse("--- a")
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alias
|
|
|
|
@parser.parse(<<-eoyml)
|
|
|
|
%YAML 1.1
|
|
|
|
---
|
|
|
|
!!seq [
|
|
|
|
!!str "Without properties",
|
|
|
|
&A !!str "Anchored",
|
|
|
|
!!str "Tagged",
|
|
|
|
*A,
|
|
|
|
!!str "",
|
|
|
|
]
|
|
|
|
eoyml
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_list_anchor
|
|
|
|
list = %w{ a b }
|
|
|
|
list << list
|
|
|
|
@parser.parse(Psych.dump(list))
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_anchor
|
|
|
|
h = {}
|
|
|
|
h['a'] = h
|
|
|
|
@parser.parse(Psych.dump(h))
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map_tag
|
|
|
|
@parser.parse(<<-eoyml)
|
|
|
|
%YAML 1.1
|
|
|
|
---
|
|
|
|
!!map { a : b }
|
|
|
|
eoyml
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_doc_tag
|
|
|
|
@parser.parse(<<-eoyml)
|
|
|
|
%YAML 1.1
|
|
|
|
%TAG ! tag:tenderlovemaking.com,2009:
|
|
|
|
--- !fun
|
|
|
|
eoyml
|
|
|
|
assert_encodings @utf8, @handler.strings
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def assert_encodings encoding, strings
|
|
|
|
strings.each do |str|
|
|
|
|
assert_equal encoding, str.encoding, str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|