1require 'psych/helper'
2
3module Psych
4  class TestToYamlProperties < MiniTest::Unit::TestCase
5    class Foo
6      attr_accessor :a, :b, :c
7      def initialize
8        @a = 1
9        @b = 2
10        @c = 3
11      end
12
13      def to_yaml_properties
14        [:@a, :@b]
15      end
16    end
17
18    def test_object_dump_yaml_properties
19      foo = Psych.load(Psych.dump(Foo.new))
20      assert_equal 1, foo.a
21      assert_equal 2, foo.b
22      assert_nil foo.c
23    end
24
25    class Bar < Struct.new(:foo, :bar)
26      attr_reader :baz
27      def initialize *args
28        super
29        @baz = 'hello'
30      end
31
32      def to_yaml_properties
33        []
34      end
35    end
36
37    def test_struct_dump_yaml_properties
38      bar = Psych.load(Psych.dump(Bar.new('a', 'b')))
39      assert_equal 'a', bar.foo
40      assert_equal 'b', bar.bar
41      assert_nil bar.baz
42    end
43
44    def test_string_dump
45      string = "okonomiyaki"
46      class << string
47        def to_yaml_properties
48          [:@tastes]
49        end
50      end
51
52      string.instance_variable_set(:@tastes, 'delicious')
53      v = Psych.load Psych.dump string
54      assert_equal 'delicious', v.instance_variable_get(:@tastes)
55    end
56
57    def test_string_load_syck
58      str = Psych.load("--- !str \nstr: okonomiyaki\n:@tastes: delicious\n")
59      assert_equal 'okonomiyaki', str
60      assert_equal 'delicious', str.instance_variable_get(:@tastes)
61    end
62  end
63end
64