summaryrefslogtreecommitdiff
path: root/test/psych/test_yaml_special_cases.rb
blob: 205457bcae58ecddfeaf00b54453be11777806a6 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require_relative 'helper'

require 'stringio'
require 'tempfile'

module Psych
  class TestYamlSpecialCases < TestCase
    def setup
      super
    end

    def test_empty_string
      s = ""
      assert_equal false, Psych.unsafe_load(s)
      assert_equal [], Psych.load_stream(s)
      assert_equal false, Psych.parse(s)
      assert_equal [], Psych.parse_stream(s).transform
      assert_nil   Psych.safe_load(s)
    end

    def test_false
      s = "false"
      assert_equal false, Psych.load(s)
      assert_equal [false], Psych.load_stream(s)
      assert_equal false, Psych.parse(s).transform
      assert_equal [false], Psych.parse_stream(s).transform
      assert_equal false, Psych.safe_load(s)
    end

    def test_n
      s = "n"
      assert_equal "n", Psych.load(s)
      assert_equal ["n"], Psych.load_stream(s)
      assert_equal "n", Psych.parse(s).transform
      assert_equal ["n"], Psych.parse_stream(s).transform
      assert_equal "n", Psych.safe_load(s)
    end

    def test_off
      s = "off"
      assert_equal false, Psych.load(s)
      assert_equal [false], Psych.load_stream(s)
      assert_equal false, Psych.parse(s).transform
      assert_equal [false], Psych.parse_stream(s).transform
      assert_equal false, Psych.safe_load(s)
    end

    def test_inf
      s = "-.inf"
      assert_equal(-Float::INFINITY, Psych.load(s))
      assert_equal([-Float::INFINITY], Psych.load_stream(s))
      assert_equal(-Float::INFINITY, Psych.parse(s).transform)
      assert_equal([-Float::INFINITY], Psych.parse_stream(s).transform)
      assert_equal(-Float::INFINITY, Psych.safe_load(s))
    end

    def test_NaN
      s = ".NaN"
      assert Psych.load(s).nan?
      assert Psych.load_stream(s).first.nan?
      assert Psych.parse(s).transform.nan?
      assert Psych.parse_stream(s).transform.first.nan?
      assert Psych.safe_load(s).nan?
    end

    def test_0xC
      s = "0xC"
      assert_equal 12, Psych.load(s)
      assert_equal [12], Psych.load_stream(s)
      assert_equal 12, Psych.parse(s).transform
      assert_equal [12], Psych.parse_stream(s).transform
      assert_equal 12, Psych.safe_load(s)
    end

    def test_arrows
      s = "<<"
      assert_equal "<<", Psych.load(s)
      assert_equal ["<<"], Psych.load_stream(s)
      assert_equal "<<", Psych.parse(s).transform
      assert_equal ["<<"], Psych.parse_stream(s).transform
      assert_equal "<<", Psych.safe_load(s)
    end

    def test_arrows_hash
      s = "<<: {}"
      assert_equal({}, Psych.load(s))
      assert_equal [{}], Psych.load_stream(s)
      assert_equal({}, Psych.parse(s).transform)
      assert_equal [{}], Psych.parse_stream(s).transform
      assert_equal({}, Psych.safe_load(s))
    end

    def test_thousand
      s = "- 1000\n- +1000\n- 1_000"
      assert_equal [1000, 1000, 1000], Psych.load(s)
      assert_equal [[1000, 1000, 1000]], Psych.load_stream(s)
      assert_equal [1000, 1000, 1000], Psych.parse(s).transform
      assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform
      assert_equal [1000, 1000, 1000], Psych.safe_load(s)
    end

    def test_8
      s = "[8, 08, 0o10, 010]"
      assert_equal [8, "08", "0o10", 8], Psych.load(s)
      assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s)
      assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform
      assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform
      assert_equal [8, "08", "0o10", 8], Psych.safe_load(s)
    end

    def test_null
      s = "null"
      assert_nil   Psych.load(s)
      assert_equal [nil], Psych.load_stream(s)
      assert_nil   Psych.parse(s).transform
      assert_equal [nil], Psych.parse_stream(s).transform
      assert_nil   Psych.safe_load(s)
    end

    private

    def special_case_cycle(object)
      %w[load load_stream parse parse_stream safe_load].map do |m|
        Psych.public_send(m, object)
      end
    end
  end
end