|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'helper' |
| 4 | + |
| 5 | +require 'stringio' |
| 6 | +require 'tempfile' |
| 7 | + |
| 8 | +module Psych |
| 9 | + class TestYamlSpecialCases < TestCase |
| 10 | + def setup |
| 11 | + super |
| 12 | + end |
| 13 | + |
| 14 | + def test_empty_string |
| 15 | + s = "" |
| 16 | + assert_equal false, Psych.load(s) |
| 17 | + assert_equal [], Psych.load_stream(s) |
| 18 | + assert_equal false, Psych.parse(s) |
| 19 | + assert_equal [], Psych.parse_stream(s).transform |
| 20 | + assert_equal nil, Psych.safe_load(s) |
| 21 | + end |
| 22 | + |
| 23 | + def test_false |
| 24 | + s = "false" |
| 25 | + assert_equal false, Psych.load(s) |
| 26 | + assert_equal [false], Psych.load_stream(s) |
| 27 | + assert_equal false, Psych.parse(s).transform |
| 28 | + assert_equal [false], Psych.parse_stream(s).transform |
| 29 | + assert_equal false, Psych.safe_load(s) |
| 30 | + end |
| 31 | + |
| 32 | + def test_n |
| 33 | + s = "n" |
| 34 | + assert_equal "n", Psych.load(s) |
| 35 | + assert_equal ["n"], Psych.load_stream(s) |
| 36 | + assert_equal "n", Psych.parse(s).transform |
| 37 | + assert_equal ["n"], Psych.parse_stream(s).transform |
| 38 | + assert_equal "n", Psych.safe_load(s) |
| 39 | + end |
| 40 | + |
| 41 | + def test_off |
| 42 | + s = "off" |
| 43 | + assert_equal false, Psych.load(s) |
| 44 | + assert_equal [false], Psych.load_stream(s) |
| 45 | + assert_equal false, Psych.parse(s).transform |
| 46 | + assert_equal [false], Psych.parse_stream(s).transform |
| 47 | + assert_equal false, Psych.safe_load(s) |
| 48 | + end |
| 49 | + |
| 50 | + def test_inf |
| 51 | + s = "-.inf" |
| 52 | + assert_equal -Float::INFINITY, Psych.load(s) |
| 53 | + assert_equal [-Float::INFINITY], Psych.load_stream(s) |
| 54 | + assert_equal -Float::INFINITY, Psych.parse(s).transform |
| 55 | + assert_equal [-Float::INFINITY], Psych.parse_stream(s).transform |
| 56 | + assert_equal -Float::INFINITY, Psych.safe_load(s) |
| 57 | + end |
| 58 | + |
| 59 | + def test_NaN |
| 60 | + s = ".NaN" |
| 61 | + assert Float::NAN, Psych.load(s).nan? |
| 62 | + assert [Float::NAN], Psych.load_stream(s).first.nan? |
| 63 | + assert Psych.parse(s).transform.nan? |
| 64 | + assert Psych.parse_stream(s).transform.first.nan? |
| 65 | + assert Psych.safe_load(s).nan? |
| 66 | + end |
| 67 | + |
| 68 | + def test_0xC |
| 69 | + s = "0xC" |
| 70 | + assert_equal 12, Psych.load(s) |
| 71 | + assert_equal [12], Psych.load_stream(s) |
| 72 | + assert_equal 12, Psych.parse(s).transform |
| 73 | + assert_equal [12], Psych.parse_stream(s).transform |
| 74 | + assert_equal 12, Psych.safe_load(s) |
| 75 | + end |
| 76 | + |
| 77 | + def test_arrows |
| 78 | + s = "<<" |
| 79 | + assert_equal "<<", Psych.load(s) |
| 80 | + assert_equal ["<<"], Psych.load_stream(s) |
| 81 | + assert_equal "<<", Psych.parse(s).transform |
| 82 | + assert_equal ["<<"], Psych.parse_stream(s).transform |
| 83 | + assert_equal "<<", Psych.safe_load(s) |
| 84 | + end |
| 85 | + |
| 86 | + def test_arrows_hash |
| 87 | + s = "<<: {}" |
| 88 | + assert_equal({}, Psych.load(s)) |
| 89 | + assert_equal [{}], Psych.load_stream(s) |
| 90 | + assert_equal({}, Psych.parse(s).transform) |
| 91 | + assert_equal [{}], Psych.parse_stream(s).transform |
| 92 | + assert_equal({}, Psych.safe_load(s)) |
| 93 | + end |
| 94 | + |
| 95 | + def test_thousand |
| 96 | + s = "- 1000\n- +1000\n- 1_000" |
| 97 | + assert_equal [1000, 1000, 1000], Psych.load(s) |
| 98 | + assert_equal [[1000, 1000, 1000]], Psych.load_stream(s) |
| 99 | + assert_equal [1000, 1000, 1000], Psych.parse(s).transform |
| 100 | + assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform |
| 101 | + assert_equal [1000, 1000, 1000], Psych.safe_load(s) |
| 102 | + end |
| 103 | + |
| 104 | + def test_8 |
| 105 | + s = "[8, 08, 0o10, 010]" |
| 106 | + assert_equal [8, "08", "0o10", 8], Psych.load(s) |
| 107 | + assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s) |
| 108 | + assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform |
| 109 | + assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform |
| 110 | + assert_equal [8, "08", "0o10", 8], Psych.safe_load(s) |
| 111 | + end |
| 112 | + |
| 113 | + def test_null |
| 114 | + s = "null" |
| 115 | + assert_equal nil, Psych.load(s) |
| 116 | + assert_equal [nil], Psych.load_stream(s) |
| 117 | + assert_equal nil, Psych.parse(s).transform |
| 118 | + assert_equal [nil], Psych.parse_stream(s).transform |
| 119 | + assert_equal nil, Psych.safe_load(s) |
| 120 | + end |
| 121 | + |
| 122 | + private |
| 123 | + |
| 124 | + def special_case_cycle(object) |
| 125 | + %w[load load_stream parse parse_stream safe_load].map do |m| |
| 126 | + Psych.public_send(m, object) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments