summaryrefslogtreecommitdiff
path: root/test/psych/test_object_references.rb
blob: 51d8085fa04909c60112a28cc57dbdbc4d18b96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'psych/helper'

module Psych
  class TestObjectReferences < TestCase
    def test_range_has_references
      assert_reference_trip 1..2
    end

    def test_module_has_references
      assert_reference_trip Psych
    end

    def test_class_has_references
      assert_reference_trip TestObjectReferences
    end

    def test_rational_has_references
      assert_reference_trip Rational('1.2')
    end

    def test_complex_has_references
      assert_reference_trip Complex(1, 2)
    end

    def test_datetime_has_references
      assert_reference_trip DateTime.now
    end

    def assert_reference_trip obj
      yml = Psych.dump([obj, obj])
      assert_match(/\*-?\d+/, yml)
      data = Psych.load yml
      assert_equal data.first.object_id, data.last.object_id
    end

    def test_float_references
      data = Psych.load <<-eoyml
---\s
- &name 1.2
- *name
      eoyml
      assert_equal data.first, data.last
      assert_equal data.first.object_id, data.last.object_id
    end

    def test_binary_references
      data = Psych.load <<-eoyml
---
- &name !binary |-
  aGVsbG8gd29ybGQh
- *name
      eoyml
      assert_equal data.first, data.last
      assert_equal data.first.object_id, data.last.object_id
    end

    def test_regexp_references
      data = Psych.load <<-eoyml
---\s
- &name !ruby/regexp /pattern/i
- *name
      eoyml
      assert_equal data.first, data.last
      assert_equal data.first.object_id, data.last.object_id
    end
  end
end