aboutsummaryrefslogtreecommitdiffstats
path: root/test/psych/test_encoding.rb
diff options
context:
space:
mode:
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