summaryrefslogtreecommitdiff
path: root/test/psych/test_encoding.rb
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-28 21:49:37 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-28 21:49:37 +0000
commitb9b923ca942096ddb1062be2deb9e6de9a65f346 (patch)
treebc8466746efbe763c7e8a54390d9b34db1aa63c4 /test/psych/test_encoding.rb
parenta8a99a7379fa8e07f217fc7b24e3a9208a967898 (diff)
* ext/psych/*: importing Psych to trunk
* test/psych/*: ditto * lib/psych/*: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27079 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/psych/test_encoding.rb')
-rw-r--r--test/psych/test_encoding.rb91
1 files changed, 91 insertions, 0 deletions
diff --git a/test/psych/test_encoding.rb b/test/psych/test_encoding.rb
new file mode 100644
index 0000000000..4690621168
--- /dev/null
+++ b/test/psych/test_encoding.rb
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+
+require 'test/psych/helper'
+
+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
+ @handler = EncodingCatcher.new
+ @parser = Psych::Parser.new @handler
+ @utf8 = Encoding.find('UTF-8')
+ end
+
+ 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