2018-08-26 20:44:04 -04:00
|
|
|
# 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.load(s)
|
|
|
|
assert_equal [], Psych.load_stream(s)
|
|
|
|
assert_equal false, Psych.parse(s)
|
|
|
|
assert_equal [], Psych.parse_stream(s).transform
|
2018-10-20 00:25:04 -04:00
|
|
|
assert_nil Psych.safe_load(s)
|
2018-08-26 20:44:04 -04:00
|
|
|
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"
|
2018-10-20 00:25:04 -04:00
|
|
|
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))
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_NaN
|
|
|
|
s = ".NaN"
|
2021-05-10 04:46:58 -04:00
|
|
|
assert Psych.load(s).nan?
|
|
|
|
assert Psych.load_stream(s).first.nan?
|
2018-08-26 20:44:04 -04:00
|
|
|
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"
|
2018-10-20 00:25:04 -04:00
|
|
|
assert_nil Psych.load(s)
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_equal [nil], Psych.load_stream(s)
|
2018-10-20 00:25:04 -04:00
|
|
|
assert_nil Psych.parse(s).transform
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_equal [nil], Psych.parse_stream(s).transform
|
2018-10-20 00:25:04 -04:00
|
|
|
assert_nil Psych.safe_load(s)
|
2018-08-26 20:44:04 -04:00
|
|
|
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
|