diff options
Diffstat (limited to 'test/psych')
| -rw-r--r-- | test/psych/helper.rb | 1 | ||||
| -rw-r--r-- | test/psych/test_data.rb | 93 | ||||
| -rw-r--r-- | test/psych/test_date_time.rb | 16 | ||||
| -rw-r--r-- | test/psych/test_exception.rb | 13 | ||||
| -rw-r--r-- | test/psych/test_object_references.rb | 5 | ||||
| -rw-r--r-- | test/psych/test_parser.rb | 42 | ||||
| -rw-r--r-- | test/psych/test_psych.rb | 11 | ||||
| -rw-r--r-- | test/psych/test_psych_set.rb | 57 | ||||
| -rw-r--r-- | test/psych/test_ractor.rb | 6 | ||||
| -rw-r--r-- | test/psych/test_safe_load.rb | 32 | ||||
| -rw-r--r-- | test/psych/test_scalar_scanner.rb | 19 | ||||
| -rw-r--r-- | test/psych/test_serialize_subclasses.rb | 18 | ||||
| -rw-r--r-- | test/psych/test_set.rb | 61 | ||||
| -rw-r--r-- | test/psych/test_stream.rb | 8 | ||||
| -rw-r--r-- | test/psych/test_string.rb | 10 | ||||
| -rw-r--r-- | test/psych/test_stringio.rb | 14 | ||||
| -rw-r--r-- | test/psych/test_yaml.rb | 928 | ||||
| -rw-r--r-- | test/psych/test_yaml_special_cases.rb | 12 | ||||
| -rw-r--r-- | test/psych/test_yamlstore.rb | 16 | ||||
| -rw-r--r-- | test/psych/visitors/test_to_ruby.rb | 6 | ||||
| -rw-r--r-- | test/psych/visitors/test_yaml_tree.rb | 21 |
21 files changed, 896 insertions, 493 deletions
diff --git a/test/psych/helper.rb b/test/psych/helper.rb index 4e82887c6d..639f6055ff 100644 --- a/test/psych/helper.rb +++ b/test/psych/helper.rb @@ -2,7 +2,6 @@ require 'test/unit' require 'stringio' require 'tempfile' -require 'date' require 'psych' diff --git a/test/psych/test_data.rb b/test/psych/test_data.rb new file mode 100644 index 0000000000..5e340c580a --- /dev/null +++ b/test/psych/test_data.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true +require_relative 'helper' + +class PsychDataWithIvar < Data.define(:foo) + attr_reader :bar + def initialize(**) + @bar = 'hello' + super + end +end unless RUBY_VERSION < "3.2" + +module Psych + class TestData < TestCase + class SelfReferentialData < Data.define(:foo) + attr_accessor :ref + def initialize(foo:) + @ref = self + super + end + end unless RUBY_VERSION < "3.2" + + def setup + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + end + + # TODO: move to another test? + def test_dump_data + assert_equal <<~eoyml, Psych.dump(PsychDataWithIvar["bar"]) + --- !ruby/data-with-ivars:PsychDataWithIvar + members: + foo: bar + ivars: + "@bar": hello + eoyml + end + + def test_self_referential_data + circular = SelfReferentialData.new("foo") + + loaded = Psych.unsafe_load(Psych.dump(circular)) + assert_instance_of(SelfReferentialData, loaded.ref) + + assert_equal(circular, loaded) + assert_same(loaded, loaded.ref) + end + + def test_roundtrip + thing = PsychDataWithIvar.new("bar") + data = Psych.unsafe_load(Psych.dump(thing)) + + assert_equal "hello", data.bar + assert_equal "bar", data.foo + end + + def test_load + obj = Psych.unsafe_load(<<~eoyml) + --- !ruby/data-with-ivars:PsychDataWithIvar + members: + foo: bar + ivars: + "@bar": hello + eoyml + + assert_equal "hello", obj.bar + assert_equal "bar", obj.foo + end + + def test_members_must_be_identical + TestData.const_set :D, Data.define(:a, :b) + d = Psych.dump(TestData::D.new(1, 2)) + + # more members + TestData.send :remove_const, :D + TestData.const_set :D, Data.define(:a, :b, :c) + e = assert_raise(ArgumentError) { Psych.unsafe_load d } + assert_equal 'missing keyword: :c', e.message + + # less members + TestData.send :remove_const, :D + TestData.const_set :D, Data.define(:a) + e = assert_raise(ArgumentError) { Psych.unsafe_load d } + assert_equal 'unknown keyword: :b', e.message + + # completely different members + TestData.send :remove_const, :D + TestData.const_set :D, Data.define(:a, :c) + e = assert_raise(ArgumentError) { Psych.unsafe_load d } + assert_include e.message, 'keyword:' + ensure + TestData.send :remove_const, :D + end + end +end diff --git a/test/psych/test_date_time.rb b/test/psych/test_date_time.rb index 3379bd24bf..79a48e2472 100644 --- a/test/psych/test_date_time.rb +++ b/test/psych/test_date_time.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true require_relative 'helper' -require 'date' module Psych class TestDateTime < TestCase @@ -86,5 +85,20 @@ module Psych assert_match('&', yaml) assert_match('*', yaml) end + + def test_overwritten_to_s + pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/ + s = Psych.dump(Date.new(2023, 9, 2), permitted_classes: [Date]) + assert_separately(%W[-rpsych -rdate - #{s}], "#{<<~"begin;"}\n#{<<~'end;'}") + class Date + undef to_s + def to_s; strftime("%D"); end + end + expected = ARGV.shift + begin; + s = Psych.dump(Date.new(2023, 9, 2), permitted_classes: [Date]) + assert_equal(expected, s) + end; + end end end diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb index c1e69ab18d..6fd92abf9d 100644 --- a/test/psych/test_exception.rb +++ b/test/psych/test_exception.rb @@ -82,6 +82,19 @@ module Psych assert_equal 'omg!', ex.file end + def test_safe_load_stream_takes_file + ex = assert_raise(Psych::SyntaxError) do + Psych.safe_load_stream '--- `' + end + assert_nil ex.file + assert_match '(<unknown>)', ex.message + + ex = assert_raise(Psych::SyntaxError) do + Psych.safe_load_stream '--- `', filename: 'omg!' + end + assert_equal 'omg!', ex.file + end + def test_parse_file_exception Tempfile.create(['parsefile', 'yml']) {|t| t.binmode diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb index 86bb9034b9..0498d54eec 100644 --- a/test/psych/test_object_references.rb +++ b/test/psych/test_object_references.rb @@ -31,6 +31,11 @@ module Psych assert_reference_trip Struct.new(:foo).new(1) end + def test_data_has_references + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_reference_trip Data.define(:foo).new(1) + end + def assert_reference_trip obj yml = Psych.dump([obj, obj]) assert_match(/\*-?\d+/, yml) diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index c1e0abb89d..4ca4d63d80 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -198,6 +198,48 @@ module Psych assert_called :end_stream end + def test_parse_io_returns_more_bytes_than_requested + # An IO-like source whose #read returns more bytes than the size it was + # asked for must not overflow libyaml's read buffer. + io = Object.new + def io.external_encoding; Encoding::UTF_8 end + def io.read len + return nil if @done + @done = true + "--- a\n" + ("#" * (len + (1 << 20))) + end + + # CRuby clamps the over-read and parses; JRuby's parser rejects the + # over-reading IO with an IOError. Either way there is no overflow. + begin + @parser.parse io + rescue IOError + return + end + assert_called :start_stream + assert_called :scalar + assert_called :end_stream + end + + def test_parse_io_returns_more_bytes_than_requested_multibyte + # The over-read is rounded down to a character boundary so a multibyte + # character is never split when the copy is clamped. + io = Object.new + def io.external_encoding; Encoding::UTF_8 end + def io.read len + return nil if @done + @done = true + "--- a\n#" + ("あ" * (len + (1 << 20))) + end + + begin + @parser.parse io + rescue IOError + return + end + assert_called :scalar + end + def test_syntax_error assert_raise(Psych::SyntaxError) do @parser.parse("---\n\"foo\"\n\"bar\"\n") diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index 42586a8779..4455c471e7 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -89,6 +89,7 @@ class TestPsych < Psych::TestCase things = [22, "foo \n", {}] stream = Psych.dump_stream(*things) assert_equal things, Psych.load_stream(stream) + assert_equal things, Psych.safe_load_stream(stream) end def test_dump_file @@ -119,6 +120,8 @@ class TestPsych < Psych::TestCase def test_load_stream docs = Psych.load_stream("--- foo\n...\n--- bar\n...") assert_equal %w{ foo bar }, docs + safe_docs = Psych.safe_load_stream("--- foo\n...\n--- bar\n...") + assert_equal %w{ foo bar }, safe_docs end def test_load_stream_freeze @@ -138,10 +141,18 @@ class TestPsych < Psych::TestCase assert_equal [], Psych.load_stream("") end + def test_safe_load_stream_default_fallback + assert_equal [], Psych.safe_load_stream("") + end + def test_load_stream_raises_on_bad_input assert_raise(Psych::SyntaxError) { Psych.load_stream("--- `") } end + def test_safe_load_stream_raises_on_bad_input + assert_raise(Psych::SyntaxError) { Psych.safe_load_stream("--- `") } + end + def test_parse_stream docs = Psych.parse_stream("--- foo\n...\n--- bar\n...") assert_equal(%w[foo bar], docs.children.map(&:transform)) diff --git a/test/psych/test_psych_set.rb b/test/psych/test_psych_set.rb new file mode 100644 index 0000000000..c72cd73f18 --- /dev/null +++ b/test/psych/test_psych_set.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true +require_relative 'helper' + +module Psych + class TestPsychSet < TestCase + def setup + super + @set = Psych::Set.new + @set['foo'] = 'bar' + @set['bar'] = 'baz' + end + + def test_dump + assert_match(/!set/, Psych.dump(@set)) + end + + def test_roundtrip + assert_cycle(@set) + end + + ### + # FIXME: Syck should also support !!set as shorthand + def test_load_from_yaml + loaded = Psych.unsafe_load(<<-eoyml) +--- !set +foo: bar +bar: baz + eoyml + assert_equal(@set, loaded) + end + + def test_loaded_class + assert_instance_of(Psych::Set, Psych.unsafe_load(Psych.dump(@set))) + end + + def test_set_shorthand + loaded = Psych.unsafe_load(<<-eoyml) +--- !!set +foo: bar +bar: baz + eoyml + assert_instance_of(Psych::Set, loaded) + end + + def test_set_self_reference + @set['self'] = @set + assert_cycle(@set) + end + + def test_stringify_names + @set[:symbol] = :value + + assert_match(/^:symbol: :value/, Psych.dump(@set)) + assert_match(/^symbol: :value/, Psych.dump(@set, stringify_names: true)) + end + end +end diff --git a/test/psych/test_ractor.rb b/test/psych/test_ractor.rb index 1b0d810609..f1c8327aa3 100644 --- a/test/psych/test_ractor.rb +++ b/test/psych/test_ractor.rb @@ -7,7 +7,7 @@ class TestPsychRactor < Test::Unit::TestCase obj = {foo: [42]} obj2 = Ractor.new(obj) do |obj| Psych.unsafe_load(Psych.dump(obj)) - end.take + end.value assert_equal obj, obj2 RUBY end @@ -33,7 +33,7 @@ class TestPsychRactor < Test::Unit::TestCase val * 2 end Psych.load('--- !!omap hello') - end.take + end.value assert_equal 'hellohello', r assert_equal 'hello', Psych.load('--- !!omap hello') RUBY @@ -43,7 +43,7 @@ class TestPsychRactor < Test::Unit::TestCase assert_ractor(<<~RUBY, require_relative: 'helper') r = Ractor.new do Psych.libyaml_version.join('.') == Psych::LIBYAML_VERSION - end.take + end.value assert_equal true, r RUBY end diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb index a9ed737528..e6ca1e142b 100644 --- a/test/psych/test_safe_load.rb +++ b/test/psych/test_safe_load.rb @@ -114,6 +114,38 @@ module Psych end end + D = Data.define(:d) unless RUBY_VERSION < "3.2" + + def test_data_depends_on_sym + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol]) + assert_raise(Psych::DisallowedClass) do + cycle D.new(nil), permitted_classes: [D] + end + end + + def test_anon_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol]) +--- !ruby/data + foo: bar + eoyml + + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Data]) +--- !ruby/data + foo: bar + eoyml + end + + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Symbol]) +--- !ruby/data + foo: bar + eoyml + end + end + def test_safe_load_default_fallback assert_nil Psych.safe_load("") end diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb index 02b923afe2..bc6a74ad8b 100644 --- a/test/psych/test_scalar_scanner.rb +++ b/test/psych/test_scalar_scanner.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true require_relative 'helper' -require 'date' module Psych class TestScalarScanner < TestCase @@ -126,6 +125,24 @@ module Psych assert_equal '100_', ss.tokenize('100_') end + def test_scan_strings_with_legacy_int_delimiters + assert_equal '0x_,_', ss.tokenize('0x_,_') + assert_equal '+0__,,', ss.tokenize('+0__,,') + assert_equal '-0b,_,', ss.tokenize('-0b,_,') + end + + def test_scan_strings_with_strict_int_delimiters + scanner = Psych::ScalarScanner.new ClassLoader.new, strict_integer: true + assert_equal '0x___', scanner.tokenize('0x___') + assert_equal '+0____', scanner.tokenize('+0____') + assert_equal '-0b___', scanner.tokenize('-0b___') + end + + def test_scan_without_parse_symbols + scanner = Psych::ScalarScanner.new ClassLoader.new, parse_symbols: false + assert_equal ':foo', scanner.tokenize(':foo') + end + def test_scan_int_commas_and_underscores # NB: This test is to ensure backward compatibility with prior Psych versions, # not to test against any actual YAML specification. diff --git a/test/psych/test_serialize_subclasses.rb b/test/psych/test_serialize_subclasses.rb index 344c79b3ef..640c331337 100644 --- a/test/psych/test_serialize_subclasses.rb +++ b/test/psych/test_serialize_subclasses.rb @@ -35,5 +35,23 @@ module Psych so = StructSubclass.new('foo', [1,2,3]) assert_equal so, Psych.unsafe_load(Psych.dump(so)) end + + class DataSubclass < Data.define(:foo) + def initialize(foo:) + @bar = "hello #{foo}" + super(foo: foo) + end + + def == other + super(other) && @bar == other.instance_eval{ @bar } + end + end unless RUBY_VERSION < "3.2" + + def test_data_subclass + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + so = DataSubclass.new('foo') + assert_equal so, Psych.unsafe_load(Psych.dump(so)) + end + end end diff --git a/test/psych/test_set.rb b/test/psych/test_set.rb index b4968d3425..ccd591c626 100644 --- a/test/psych/test_set.rb +++ b/test/psych/test_set.rb @@ -1,57 +1,36 @@ +# encoding: UTF-8 # frozen_string_literal: true require_relative 'helper' +require 'set' unless defined?(Set) module Psych class TestSet < TestCase def setup - super - @set = Psych::Set.new - @set['foo'] = 'bar' - @set['bar'] = 'baz' + @set = ::Set.new([1, 2, 3]) end def test_dump - assert_match(/!set/, Psych.dump(@set)) + assert_equal <<~YAML, Psych.dump(@set) + --- !ruby/object:Set + hash: + 1: true + 2: true + 3: true + YAML end - def test_roundtrip - assert_cycle(@set) - end - - ### - # FIXME: Syck should also support !!set as shorthand - def test_load_from_yaml - loaded = Psych.unsafe_load(<<-eoyml) ---- !set -foo: bar -bar: baz - eoyml - assert_equal(@set, loaded) + def test_load + assert_equal @set, Psych.load(<<~YAML, permitted_classes: [::Set]) + --- !ruby/object:Set + hash: + 1: true + 2: true + 3: true + YAML end - def test_loaded_class - assert_instance_of(Psych::Set, Psych.unsafe_load(Psych.dump(@set))) - end - - def test_set_shorthand - loaded = Psych.unsafe_load(<<-eoyml) ---- !!set -foo: bar -bar: baz - eoyml - assert_instance_of(Psych::Set, loaded) - end - - def test_set_self_reference - @set['self'] = @set - assert_cycle(@set) - end - - def test_stringify_names - @set[:symbol] = :value - - assert_match(/^:symbol: :value/, Psych.dump(@set)) - assert_match(/^symbol: :value/, Psych.dump(@set, stringify_names: true)) + def test_roundtrip + assert_equal @set, Psych.load(Psych.dump(@set), permitted_classes: [::Set]) end end end diff --git a/test/psych/test_stream.rb b/test/psych/test_stream.rb index 9b71c6d996..ae940d1ee4 100644 --- a/test/psych/test_stream.rb +++ b/test/psych/test_stream.rb @@ -54,6 +54,14 @@ module Psych assert_equal %w{ foo bar }, list end + def test_safe_load_stream_yields_documents + list = [] + Psych.safe_load_stream("--- foo\n...\n--- bar") do |ruby| + list << ruby + end + assert_equal %w{ foo bar }, list + end + def test_load_stream_break list = [] Psych.load_stream("--- foo\n...\n--- `") do |ruby| diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 84ae5cbb45..cfd235a519 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -50,6 +50,16 @@ module Psych assert_equal str, Psych.load(yaml) end + def test_single_quote_when_matching_date + pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/ + + lib = File.expand_path("../../../lib", __FILE__) + assert_separately(["-I", lib, "-r", "psych"], __FILE__, __LINE__ + 1, <<~'RUBY') + yml = Psych.dump('2024-11-19') + assert_equal '2024-11-19', Psych.load(yml) + RUBY + end + def test_plain_when_shorten_than_line_width_and_no_final_line_break str = "Lorem ipsum" yaml = Psych.dump str, line_width: 12 diff --git a/test/psych/test_stringio.rb b/test/psych/test_stringio.rb new file mode 100644 index 0000000000..7fef1402a0 --- /dev/null +++ b/test/psych/test_stringio.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +require_relative 'helper' + +module Psych + class TestStringIO < TestCase + # The superclass of StringIO before Ruby 3.0 was `Data`, + # which can interfere with the Ruby 3.2+ `Data` dumping. + def test_stringio + assert_nothing_raised do + Psych.dump(StringIO.new("foo")) + end + end + end +end diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb index cedec46cc7..134c346c90 100644 --- a/test/psych/test_yaml.rb +++ b/test/psych/test_yaml.rb @@ -1,14 +1,12 @@ # -*- coding: us-ascii; mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- # frozen_string_literal: true -# vim:sw=4:ts=4 -# $Id$ -# + require_relative 'helper' -require 'ostruct' # [ruby-core:01946] module Psych_Tests StructTest = Struct::new( :c ) + DataTest = Data.define( :c ) unless RUBY_VERSION < "3.2" end class Psych_Unit_Tests < Psych::TestCase @@ -17,8 +15,14 @@ class Psych_Unit_Tests < Psych::TestCase end def test_y_method - assert_raise(NoMethodError) do - OpenStruct.new.y 1 + begin + require 'ostruct' + + assert_raise(NoMethodError) do + OpenStruct.new.y 1 + end + rescue LoadError + omit("OpenStruct is not available") end end @@ -32,34 +36,38 @@ class Psych_Unit_Tests < Psych::TestCase assert_cycle(Regexp.new("foo\nbar")) end + def test_regexp_with_slash + assert_cycle(Regexp.new('/')) + end + # [ruby-core:34969] def test_regexp_with_n assert_cycle(Regexp.new('',Regexp::NOENCODING)) end - # - # Tests modified from 00basic.t in Psych.pm - # - def test_basic_map - # Simple map - assert_parse_only( - { 'one' => 'foo', 'three' => 'baz', 'two' => 'bar' }, <<EOY + # + # Tests modified from 00basic.t in Psych.pm + # + def test_basic_map + # Simple map + assert_parse_only( + { 'one' => 'foo', 'three' => 'baz', 'two' => 'bar' }, <<EOY one: foo two: bar three: baz EOY - ) - end - - def test_basic_strings - # Common string types - assert_cycle("x") - assert_cycle(":x") - assert_cycle(":") - assert_parse_only( - { 1 => 'simple string', 2 => 42, 3 => '1 Single Quoted String', - 4 => 'Psych\'s Double "Quoted" String', 5 => "A block\n with several\n lines.\n", - 6 => "A \"chomped\" block", 7 => "A folded\n string\n", 8 => ": started string" }, - <<EOY + ) + end + + def test_basic_strings + # Common string types + assert_cycle("x") + assert_cycle(":x") + assert_cycle(":") + assert_parse_only( + { 1 => 'simple string', 2 => 42, 3 => '1 Single Quoted String', + 4 => 'Psych\'s Double "Quoted" String', 5 => "A block\n with several\n lines.\n", + 6 => "A \"chomped\" block", 7 => "A folded\n string\n", 8 => ": started string" }, + <<EOY 1: simple string 2: 42 3: '1 Single Quoted String' @@ -76,44 +84,44 @@ EOY string 8: ": started string" EOY - ) - end - - # - # Test the specification examples - # - Many examples have been changes because of whitespace problems that - # caused the two to be inequivalent, or keys to be sorted wrong - # - - def test_spec_simple_implicit_sequence - # Simple implicit sequence - assert_to_yaml( - [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ], <<EOY + ) + end + + # + # Test the specification examples + # - Many examples have been changes because of whitespace problems that + # caused the two to be inequivalent, or keys to be sorted wrong + # + + def test_spec_simple_implicit_sequence + # Simple implicit sequence + assert_to_yaml( + [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ], <<EOY - Mark McGwire - Sammy Sosa - Ken Griffey EOY - ) - end + ) + end - def test_spec_simple_implicit_map - # Simple implicit map - assert_to_yaml( - { 'hr' => 65, 'avg' => 0.278, 'rbi' => 147 }, <<EOY + def test_spec_simple_implicit_map + # Simple implicit map + assert_to_yaml( + { 'hr' => 65, 'avg' => 0.278, 'rbi' => 147 }, <<EOY avg: 0.278 hr: 65 rbi: 147 EOY - ) - end - - def test_spec_simple_map_with_nested_sequences - # Simple mapping with nested sequences - assert_to_yaml( - { 'american' => - [ 'Boston Red Sox', 'Detroit Tigers', 'New York Yankees' ], - 'national' => - [ 'New York Mets', 'Chicago Cubs', 'Atlanta Braves' ] }, <<EOY + ) + end + + def test_spec_simple_map_with_nested_sequences + # Simple mapping with nested sequences + assert_to_yaml( + { 'american' => + [ 'Boston Red Sox', 'Detroit Tigers', 'New York Yankees' ], + 'national' => + [ 'New York Mets', 'Chicago Cubs', 'Atlanta Braves' ] }, <<EOY american: - Boston Red Sox - Detroit Tigers @@ -123,16 +131,16 @@ national: - Chicago Cubs - Atlanta Braves EOY - ) - end - - def test_spec_simple_sequence_with_nested_map - # Simple sequence with nested map - assert_to_yaml( - [ - {'name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278}, - {'name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288} - ], <<EOY + ) + end + + def test_spec_simple_sequence_with_nested_map + # Simple sequence with nested map + assert_to_yaml( + [ + {'name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278}, + {'name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288} + ], <<EOY - avg: 0.278 hr: 65 @@ -142,38 +150,38 @@ EOY hr: 63 name: Sammy Sosa EOY - ) - end - - def test_spec_sequence_of_sequences - # Simple sequence with inline sequences - assert_parse_only( - [ - [ 'name', 'hr', 'avg' ], - [ 'Mark McGwire', 65, 0.278 ], - [ 'Sammy Sosa', 63, 0.288 ] - ], <<EOY + ) + end + + def test_spec_sequence_of_sequences + # Simple sequence with inline sequences + assert_parse_only( + [ + [ 'name', 'hr', 'avg' ], + [ 'Mark McGwire', 65, 0.278 ], + [ 'Sammy Sosa', 63, 0.288 ] + ], <<EOY - [ name , hr , avg ] - [ Mark McGwire , 65 , 0.278 ] - [ Sammy Sosa , 63 , 0.288 ] EOY - ) - end - - def test_spec_mapping_of_mappings - # Simple map with inline maps - assert_parse_only( - { 'Mark McGwire' => - { 'hr' => 65, 'avg' => 0.278 }, - 'Sammy Sosa' => - { 'hr' => 63, 'avg' => 0.288 } - }, <<EOY + ) + end + + def test_spec_mapping_of_mappings + # Simple map with inline maps + assert_parse_only( + { 'Mark McGwire' => + { 'hr' => 65, 'avg' => 0.278 }, + 'Sammy Sosa' => + { 'hr' => 63, 'avg' => 0.288 } + }, <<EOY Mark McGwire: {hr: 65, avg: 0.278} Sammy Sosa: {hr: 63, avg: 0.288} EOY - ) - end + ) + end def test_ambiguous_comments # [ruby-talk:88012] @@ -182,11 +190,11 @@ EOY EOY end - def test_spec_nested_comments - # Map and sequences with comments - assert_parse_only( - { 'hr' => [ 'Mark McGwire', 'Sammy Sosa' ], - 'rbi' => [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY + def test_spec_nested_comments + # Map and sequences with comments + assert_parse_only( + { 'hr' => [ 'Mark McGwire', 'Sammy Sosa' ], + 'rbi' => [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY hr: # 1998 hr ranking - Mark McGwire - Sammy Sosa @@ -195,16 +203,16 @@ rbi: - Sammy Sosa - Ken Griffey EOY - ) - end - - def test_spec_anchors_and_aliases - # Anchors and aliases - assert_parse_only( - { 'hr' => - [ 'Mark McGwire', 'Sammy Sosa' ], - 'rbi' => - [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY + ) + end + + def test_spec_anchors_and_aliases + # Anchors and aliases + assert_parse_only( + { 'hr' => + [ 'Mark McGwire', 'Sammy Sosa' ], + 'rbi' => + [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY hr: - Mark McGwire # Name "Sammy Sosa" scalar SS @@ -214,7 +222,7 @@ rbi: - *SS - Ken Griffey EOY - ) + ) assert_to_yaml( [{"arrival"=>"EDI", "departure"=>"LAX", "fareref"=>"DOGMA", "currency"=>"GBP"}, {"arrival"=>"MEL", "departure"=>"SYD", "fareref"=>"MADF", "currency"=>"AUD"}, {"arrival"=>"MCO", "departure"=>"JFK", "fareref"=>"DFSF", "currency"=>"USD"}], <<EOY @@ -251,13 +259,13 @@ FARES: EOY ) - end + end - def test_spec_mapping_between_sequences - # Complex key #1 - assert_parse_only( - { [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ], - [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ] }, <<EOY + def test_spec_mapping_between_sequences + # Complex key #1 + assert_parse_only( + { [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ], + [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ] }, <<EOY ? # PLAY SCHEDULE - Detroit Tigers - Chicago Cubs @@ -269,16 +277,16 @@ EOY : [ 2001-07-02, 2001-08-12, 2001-08-14 ] EOY - ) - - # Complex key #2 - assert_parse_only( - { [ 'New York Yankees', 'Atlanta Braves' ] => - [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), - Date.new( 2001, 8, 14 ) ], - [ 'Detroit Tigers', 'Chicago Cubs' ] => - [ Date.new( 2001, 7, 23 ) ] - }, <<EOY + ) + + # Complex key #2 + assert_parse_only( + { [ 'New York Yankees', 'Atlanta Braves' ] => + [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), + Date.new( 2001, 8, 14 ) ], + [ 'Detroit Tigers', 'Chicago Cubs' ] => + [ Date.new( 2001, 7, 23 ) ] + }, <<EOY ? - New York Yankees - Atlanta Braves @@ -292,17 +300,17 @@ EOY : - 2001-07-23 EOY - ) - end - - def test_spec_sequence_key_shortcut - # Shortcut sequence map - assert_parse_only( - { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), - 'bill-to' => 'Chris Dumars', 'product' => - [ { 'item' => 'Super Hoop', 'quantity' => 1 }, - { 'item' => 'Basketball', 'quantity' => 4 }, - { 'item' => 'Big Shoes', 'quantity' => 1 } ] }, <<EOY + ) + end + + def test_spec_sequence_key_shortcut + # Shortcut sequence map + assert_parse_only( + { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), + 'bill-to' => 'Chris Dumars', 'product' => + [ { 'item' => 'Super Hoop', 'quantity' => 1 }, + { 'item' => 'Basketball', 'quantity' => 4 }, + { 'item' => 'Big Shoes', 'quantity' => 1 } ] }, <<EOY invoice: 34843 date : 2001-01-23 bill-to: Chris Dumars @@ -314,8 +322,8 @@ product: - item : Big Shoes quantity: 1 EOY - ) - end + ) + end def test_spec_sequence_in_sequence_shortcut # Seq-in-seq @@ -351,31 +359,31 @@ EOY EOY end - def test_spec_single_literal - # Literal scalar block - assert_parse_only( [ "\\/|\\/|\n/ | |_\n" ], <<EOY ) + def test_spec_single_literal + # Literal scalar block + assert_parse_only( [ "\\/|\\/|\n/ | |_\n" ], <<EOY ) - | \\/|\\/| / | |_ EOY - end + end - def test_spec_single_folded - # Folded scalar block - assert_parse_only( - [ "Mark McGwire's year was crippled by a knee injury.\n" ], <<EOY + def test_spec_single_folded + # Folded scalar block + assert_parse_only( + [ "Mark McGwire's year was crippled by a knee injury.\n" ], <<EOY - > Mark McGwire\'s year was crippled by a knee injury. EOY - ) - end + ) + end - def test_spec_preserve_indent - # Preserve indented spaces - assert_parse_only( - "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n", <<EOY + def test_spec_preserve_indent + # Preserve indented spaces + assert_parse_only( + "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n", <<EOY --- > Sammy Sosa completed another fine season with great stats. @@ -385,13 +393,13 @@ EOY What a year! EOY - ) - end + ) + end - def test_spec_indentation_determines_scope - assert_parse_only( - { 'name' => 'Mark McGwire', 'accomplishment' => "Mark set a major league home run record in 1998.\n", - 'stats' => "65 Home Runs\n0.278 Batting Average\n" }, <<EOY + def test_spec_indentation_determines_scope + assert_parse_only( + { 'name' => 'Mark McGwire', 'accomplishment' => "Mark set a major league home run record in 1998.\n", + 'stats' => "65 Home Runs\n0.278 Batting Average\n" }, <<EOY name: Mark McGwire accomplishment: > Mark set a major league @@ -400,14 +408,14 @@ stats: | 65 Home Runs 0.278 Batting Average EOY - ) - end - - def test_spec_multiline_scalars - # Multiline flow scalars - assert_parse_only( - { 'plain' => 'This unquoted scalar spans many lines.', - 'quoted' => "So does this quoted scalar.\n" }, <<EOY + ) + end + + def test_spec_multiline_scalars + # Multiline flow scalars + assert_parse_only( + { 'plain' => 'This unquoted scalar spans many lines.', + 'quoted' => "So does this quoted scalar.\n" }, <<EOY plain: This unquoted scalar spans many lines. @@ -415,19 +423,19 @@ quoted: "\\ So does this quoted scalar.\\n" EOY - ) - end + ) + end - def test_spec_type_int - assert_parse_only( - { 'canonical' => 12345, 'decimal' => 12345, 'octal' => '014'.oct, 'hexadecimal' => '0xC'.hex }, <<EOY + def test_spec_type_int + assert_parse_only( + { 'canonical' => 12345, 'decimal' => 12345, 'octal' => '014'.oct, 'hexadecimal' => '0xC'.hex }, <<EOY canonical: 12345 decimal: +12,345 octal: 014 hexadecimal: 0xC EOY - ) - assert_parse_only( + ) + assert_parse_only( { 'canonical' => 685230, 'decimal' => 685230, 'octal' => 02472256, 'hexadecimal' => 0x0A74AE, 'sexagesimal' => 685230 }, <<EOY) canonical: 685230 decimal: +685,230 @@ -435,48 +443,48 @@ octal: 02472256 hexadecimal: 0x0A,74,AE sexagesimal: 190:20:30 EOY - end + end - def test_spec_type_float - assert_parse_only( - { 'canonical' => 1230.15, 'exponential' => 1230.15, 'fixed' => 1230.15, - 'negative infinity' => -1.0/0.0 }, <<EOY) + def test_spec_type_float + assert_parse_only( + { 'canonical' => 1230.15, 'exponential' => 1230.15, 'fixed' => 1230.15, + 'negative infinity' => -1.0/0.0 }, <<EOY) canonical: 1.23015e+3 exponential: 12.3015e+02 fixed: 1,230.15 negative infinity: -.inf EOY - nan = Psych::load( <<EOY ) + nan = Psych::load( <<EOY ) not a number: .NaN EOY - assert( nan['not a number'].nan? ) - end + assert( nan['not a number'].nan? ) + end - def test_spec_type_misc - assert_parse_only( - { nil => nil, true => true, false => false, 'string' => '12345' }, <<EOY + def test_spec_type_misc + assert_parse_only( + { nil => nil, true => true, false => false, 'string' => '12345' }, <<EOY null: ~ true: yes false: no string: '12345' EOY - ) - end - - def test_spec_complex_invoice - # Complex invoice type - id001 = { 'given' => 'Chris', 'family' => 'Dumars', 'address' => - { 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak', - 'state' => 'MI', 'postal' => 48046 } } - assert_parse_only( - { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), - 'bill-to' => id001, 'ship-to' => id001, 'product' => - [ { 'sku' => 'BL394D', 'quantity' => 4, - 'description' => 'Basketball', 'price' => 450.00 }, - { 'sku' => 'BL4438H', 'quantity' => 1, - 'description' => 'Super Hoop', 'price' => 2392.00 } ], - 'tax' => 251.42, 'total' => 4443.52, - 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n" }, <<EOY + ) + end + + def test_spec_complex_invoice + # Complex invoice type + id001 = { 'given' => 'Chris', 'family' => 'Dumars', 'address' => + { 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak', + 'state' => 'MI', 'postal' => 48046 } } + assert_parse_only( + { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), + 'bill-to' => id001, 'ship-to' => id001, 'product' => + [ { 'sku' => 'BL394D', 'quantity' => 4, + 'description' => 'Basketball', 'price' => 450.00 }, + { 'sku' => 'BL4438H', 'quantity' => 1, + 'description' => 'Super Hoop', 'price' => 2392.00 } ], + 'tax' => 251.42, 'total' => 4443.52, + 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n" }, <<EOY invoice: 34843 date : 2001-01-23 bill-to: &id001 @@ -507,12 +515,12 @@ comments: > Backup contact is Nancy Billsmer @ 338-4338. EOY - ) - end + ) + end - def test_spec_log_file - doc_ct = 0 - Psych::load_stream( <<EOY + def test_spec_log_file + doc_ct = 0 + Psych::load_stream( <<EOY --- Time: 2001-11-23 15:01:42 -05:00 User: ed @@ -540,52 +548,52 @@ Stack: code: |- foo = bar EOY - ) { |doc| - case doc_ct - when 0 - assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 01, 42, 00, "-05:00" ), - 'User' => 'ed', 'Warning' => "This is an error message for the log file\n" } ) - when 1 - assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 02, 31, 00, "-05:00" ), - 'User' => 'ed', 'Warning' => "A slightly different error message.\n" } ) - when 2 - assert_equal( doc, { 'Date' => mktime( 2001, 11, 23, 15, 03, 17, 00, "-05:00" ), - 'User' => 'ed', 'Fatal' => "Unknown variable \"bar\"\n", - 'Stack' => [ - { 'file' => 'TopClass.py', 'line' => 23, 'code' => "x = MoreObject(\"345\\n\")\n" }, - { 'file' => 'MoreClass.py', 'line' => 58, 'code' => "foo = bar" } ] } ) - end - doc_ct += 1 - } - assert_equal( doc_ct, 3 ) - end - - def test_spec_root_fold - y = Psych::load( <<EOY + ) { |doc| + case doc_ct + when 0 + assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 01, 42, 00, "-05:00" ), + 'User' => 'ed', 'Warning' => "This is an error message for the log file\n" } ) + when 1 + assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 02, 31, 00, "-05:00" ), + 'User' => 'ed', 'Warning' => "A slightly different error message.\n" } ) + when 2 + assert_equal( doc, { 'Date' => mktime( 2001, 11, 23, 15, 03, 17, 00, "-05:00" ), + 'User' => 'ed', 'Fatal' => "Unknown variable \"bar\"\n", + 'Stack' => [ + { 'file' => 'TopClass.py', 'line' => 23, 'code' => "x = MoreObject(\"345\\n\")\n" }, + { 'file' => 'MoreClass.py', 'line' => 58, 'code' => "foo = bar" } ] } ) + end + doc_ct += 1 + } + assert_equal( doc_ct, 3 ) + end + + def test_spec_root_fold + y = Psych::load( <<EOY --- This Psych stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end. EOY - ) - assert_equal( y, "This Psych stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end." ) - end + ) + assert_equal( y, "This Psych stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end." ) + end - def test_spec_root_mapping - y = Psych::unsafe_load( <<EOY + def test_spec_root_mapping + y = Psych::unsafe_load( <<EOY # This stream is an example of a top-level mapping. invoice : 34843 date : 2001-01-23 total : 4443.52 EOY - ) - assert_equal( y, { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), 'total' => 4443.52 } ) - end + ) + assert_equal( y, { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), 'total' => 4443.52 } ) + end - def test_spec_oneline_docs - doc_ct = 0 - Psych::load_stream( <<EOY + def test_spec_oneline_docs + doc_ct = 0 + Psych::load_stream( <<EOY # The following is a sequence of three documents. # The first contains an empty mapping, the second # an empty sequence, and the last an empty string. @@ -593,21 +601,21 @@ EOY --- [ ] --- '' EOY - ) { |doc| - case doc_ct - when 0 - assert_equal( doc, {} ) - when 1 - assert_equal( doc, [] ) - when 2 - assert_equal( doc, '' ) - end - doc_ct += 1 - } - assert_equal( doc_ct, 3 ) - end - - def test_spec_domain_prefix + ) { |doc| + case doc_ct + when 0 + assert_equal( doc, {} ) + when 1 + assert_equal( doc, [] ) + when 2 + assert_equal( doc, '' ) + end + doc_ct += 1 + } + assert_equal( doc_ct, 3 ) + end + + def test_spec_domain_prefix customer_proc = proc { |type, val| if Hash === val _, _, type = type.split( ':', 3 ) @@ -619,7 +627,7 @@ EOY } Psych.add_domain_type( "domain.tld/2002", 'invoice', &customer_proc ) Psych.add_domain_type( "domain.tld/2002", 'customer', &customer_proc ) - assert_parse_only( { "invoice"=> { "customers"=> [ { "given"=>"Chris", "type"=>"domain customer", "family"=>"Dumars" } ], "type"=>"domain invoice" } }, <<EOY + assert_parse_only( { "invoice"=> { "customers"=> [ { "given"=>"Chris", "type"=>"domain customer", "family"=>"Dumars" } ], "type"=>"domain invoice" } }, <<EOY # 'http://domain.tld,2002/invoice' is some type family. invoice: !domain.tld/2002:invoice # 'seq' is shorthand for 'http://yaml.org/seq'. @@ -632,12 +640,12 @@ invoice: !domain.tld/2002:invoice given : Chris family : Dumars EOY - ) - end + ) + end - def test_spec_throwaway - assert_parse_only( - {"this"=>"contains three lines of text.\nThe third one starts with a\n# character. This isn't a comment.\n"}, <<EOY + def test_spec_throwaway + assert_parse_only( + {"this"=>"contains three lines of text.\nThe third one starts with a\n# character. This isn't a comment.\n"}, <<EOY ### These are four throwaway comment ### ### lines (the second line is empty). ### @@ -649,19 +657,19 @@ this: | # Comments may trail lines. # These are three throwaway comment # lines (the first line is empty). EOY - ) - end + ) + end - def test_spec_force_implicit - # Force implicit - assert_parse_only( - { 'integer' => 12, 'also int' => 12, 'string' => '12' }, <<EOY + def test_spec_force_implicit + # Force implicit + assert_parse_only( + { 'integer' => 12, 'also int' => 12, 'string' => '12' }, <<EOY integer: 12 also int: ! "12" string: !str 12 EOY - ) - end + ) + end ### # Commenting out this test. This line: @@ -672,44 +680,44 @@ EOY # # http://yaml.org/spec/1.1/#id896876 # -# def test_spec_url_escaping -# Psych.add_domain_type( "domain.tld,2002", "type0" ) { |type, val| -# "ONE: #{val}" -# } -# Psych.add_domain_type( "domain.tld,2002", "type%30" ) { |type, val| -# "TWO: #{val}" -# } -# assert_parse_only( -# { 'same' => [ 'ONE: value', 'ONE: value' ], 'different' => [ 'TWO: value' ] }, <<EOY +# def test_spec_url_escaping +# Psych.add_domain_type( "domain.tld,2002", "type0" ) { |type, val| +# "ONE: #{val}" +# } +# Psych.add_domain_type( "domain.tld,2002", "type%30" ) { |type, val| +# "TWO: #{val}" +# } +# assert_parse_only( +# { 'same' => [ 'ONE: value', 'ONE: value' ], 'different' => [ 'TWO: value' ] }, <<EOY #same: # - !domain.tld,2002/type\\x30 value # - !domain.tld,2002/type0 value #different: # As far as the Psych parser is concerned # - !domain.tld,2002/type%30 value #EOY -# ) -# end - - def test_spec_override_anchor - # Override anchor - a001 = "The alias node below is a repeated use of this value.\n" - assert_parse_only( - { 'anchor' => 'This scalar has an anchor.', 'override' => a001, 'alias' => a001 }, <<EOY +# ) +# end + + def test_spec_override_anchor + # Override anchor + a001 = "The alias node below is a repeated use of this value.\n" + assert_parse_only( + { 'anchor' => 'This scalar has an anchor.', 'override' => a001, 'alias' => a001 }, <<EOY anchor : &A001 This scalar has an anchor. override : &A001 > The alias node below is a repeated use of this value. alias : *A001 EOY - ) - end + ) + end - def test_spec_explicit_families + def test_spec_explicit_families Psych.add_domain_type( "somewhere.com/2002", 'type' ) { |type, val| "SOMEWHERE: #{val}" } - assert_parse_only( - { 'not-date' => '2002-04-28', 'picture' => "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236i^\020' \202\n\001\000;", 'hmm' => "SOMEWHERE: family above is short for\nhttp://somewhere.com/type\n" }, <<EOY + assert_parse_only( + { 'not-date' => '2002-04-28', 'picture' => "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236i^\020' \202\n\001\000;", 'hmm' => "SOMEWHERE: family above is short for\nhttp://somewhere.com/type\n" }, <<EOY not-date: !str 2002-04-28 picture: !binary | R0lGODlhDAAMAIQAAP//9/X @@ -721,34 +729,34 @@ hmm: !somewhere.com/2002:type | family above is short for http://somewhere.com/type EOY - ) - end - - def test_spec_application_family - # Testing the clarkevans.com graphs - Psych.add_domain_type( "clarkevans.com/2002", 'graph/shape' ) { |type, val| - if Array === val - val << "Shape Container" - val - else - raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect - end - } - one_shape_proc = Proc.new { |type, val| - if Hash === val + ) + end + + def test_spec_application_family + # Testing the clarkevans.com graphs + Psych.add_domain_type( "clarkevans.com/2002", 'graph/shape' ) { |type, val| + if Array === val + val << "Shape Container" + val + else + raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect + end + } + one_shape_proc = Proc.new { |type, val| + if Hash === val type = type.split( /:/ ) - val['TYPE'] = "Shape: #{type[2]}" - val - else - raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect - end - } - Psych.add_domain_type( "clarkevans.com/2002", 'graph/circle', &one_shape_proc ) - Psych.add_domain_type( "clarkevans.com/2002", 'graph/line', &one_shape_proc ) - Psych.add_domain_type( "clarkevans.com/2002", 'graph/text', &one_shape_proc ) + val['TYPE'] = "Shape: #{type[2]}" + val + else + raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect + end + } + Psych.add_domain_type( "clarkevans.com/2002", 'graph/circle', &one_shape_proc ) + Psych.add_domain_type( "clarkevans.com/2002", 'graph/line', &one_shape_proc ) + Psych.add_domain_type( "clarkevans.com/2002", 'graph/text', &one_shape_proc ) # MODIFIED to remove invalid Psych - assert_parse_only( - [[{"radius"=>7, "center"=>{"x"=>73, "y"=>129}, "TYPE"=>"Shape: graph/circle"}, {"finish"=>{"x"=>89, "y"=>102}, "TYPE"=>"Shape: graph/line", "start"=>{"x"=>73, "y"=>129}}, {"TYPE"=>"Shape: graph/text", "value"=>"Pretty vector drawing.", "start"=>{"x"=>73, "y"=>129}, "color"=>16772795}, "Shape Container"]], <<EOY + assert_parse_only( + [[{"radius"=>7, "center"=>{"x"=>73, "y"=>129}, "TYPE"=>"Shape: graph/circle"}, {"finish"=>{"x"=>89, "y"=>102}, "TYPE"=>"Shape: graph/line", "start"=>{"x"=>73, "y"=>129}}, {"TYPE"=>"Shape: graph/text", "value"=>"Pretty vector drawing.", "start"=>{"x"=>73, "y"=>129}, "color"=>16772795}, "Shape Container"]], <<EOY - !clarkevans.com/2002:graph/shape - !/graph/circle center: &ORIGIN {x: 73, y: 129} @@ -761,12 +769,12 @@ EOY color: 0xFFEEBB value: Pretty vector drawing. EOY - ) - end + ) + end - def test_spec_float_explicit - assert_parse_only( - [ 10.0, 10.0, 10.0, 10.0 ], <<EOY + def test_spec_float_explicit + assert_parse_only( + [ 10.0, 10.0, 10.0, 10.0 ], <<EOY # All entries in the sequence # have the same type and value. - 10.0 @@ -776,15 +784,15 @@ EOY 1\\ 0" EOY - ) - end - - def test_spec_builtin_seq - # Assortment of sequences - assert_parse_only( - { 'empty' => [], 'in-line' => [ 'one', 'two', 'three', 'four', 'five' ], - 'nested' => [ 'First item in top sequence', [ 'Subordinate sequence entry' ], - "A multi-line sequence entry\n", 'Sixth item in top sequence' ] }, <<EOY + ) + end + + def test_spec_builtin_seq + # Assortment of sequences + assert_parse_only( + { 'empty' => [], 'in-line' => [ 'one', 'two', 'three', 'four', 'five' ], + 'nested' => [ 'First item in top sequence', [ 'Subordinate sequence entry' ], + "A multi-line sequence entry\n", 'Sixth item in top sequence' ] }, <<EOY empty: [] in-line: [ one, two, three # May span lines, , four, # indentation is @@ -798,24 +806,24 @@ nested: sequence entry - Sixth item in top sequence EOY - ) - end - - def test_spec_builtin_map - # Assortment of mappings - assert_parse_only( - { 'empty' => {}, 'in-line' => { 'one' => 1, 'two' => 2 }, - 'spanning' => { 'one' => 1, 'two' => 2 }, - 'nested' => { 'first' => 'First entry', 'second' => - { 'key' => 'Subordinate mapping' }, 'third' => - [ 'Subordinate sequence', {}, 'Previous mapping is empty.', - { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' }, - 'The previous entry is equal to the following one.', - { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' } ], - 12.0 => 'This key is a float.', "?\n" => 'This key had to be protected.', - "\a" => 'This key had to be escaped.', - "This is a multi-line folded key\n" => "Whose value is also multi-line.\n", - [ 'This key', 'is a sequence' ] => [ 'With a sequence value.' ] } }, <<EOY + ) + end + + def test_spec_builtin_map + # Assortment of mappings + assert_parse_only( + { 'empty' => {}, 'in-line' => { 'one' => 1, 'two' => 2 }, + 'spanning' => { 'one' => 1, 'two' => 2 }, + 'nested' => { 'first' => 'First entry', 'second' => + { 'key' => 'Subordinate mapping' }, 'third' => + [ 'Subordinate sequence', {}, 'Previous mapping is empty.', + { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' }, + 'The previous entry is equal to the following one.', + { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' } ], + 12.0 => 'This key is a float.', "?\n" => 'This key had to be protected.', + "\a" => 'This key had to be escaped.', + "This is a multi-line folded key\n" => "Whose value is also multi-line.\n", + [ 'This key', 'is a sequence' ] => [ 'With a sequence value.' ] } }, <<EOY empty: {} in-line: { one: 1, two: 2 } @@ -860,13 +868,13 @@ nested: # : # with a: mapping value. EOY - ) - end + ) + end - def test_spec_builtin_literal_blocks - # Assortment of literal scalar blocks - assert_parse_only( - {"both are equal to"=>" This has no newline.", "is equal to"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n", "also written as"=>" This has no newline.", "indented and chomped"=>" This has no newline.", "empty"=>"", "literal"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n"}, <<EOY + def test_spec_builtin_literal_blocks + # Assortment of literal scalar blocks + assert_parse_only( + {"both are equal to"=>" This has no newline.", "is equal to"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n", "also written as"=>" This has no newline.", "indented and chomped"=>" This has no newline.", "empty"=>"", "literal"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n"}, <<EOY empty: | literal: | @@ -900,15 +908,15 @@ also written as: |-2 both are equal to: " This has no newline." EOY - ) - - str1 = "This has one newline.\n" - str2 = "This has no newline." - str3 = "This has two newlines.\n\n" - assert_parse_only( - { 'clipped' => str1, 'same as "clipped" above' => str1, - 'stripped' => str2, 'same as "stripped" above' => str2, - 'kept' => str3, 'same as "kept" above' => str3 }, <<EOY + ) + + str1 = "This has one newline.\n" + str2 = "This has no newline." + str3 = "This has two newlines.\n\n" + assert_parse_only( + { 'clipped' => str1, 'same as "clipped" above' => str1, + 'stripped' => str2, 'same as "stripped" above' => str2, + 'kept' => str3, 'same as "kept" above' => str3 }, <<EOY clipped: | This has one newline. @@ -925,11 +933,11 @@ kept: |+ same as "kept" above: "This has two newlines.\\n\\n" EOY - ) - end + ) + end - def test_spec_span_single_quote - assert_parse_only( {"third"=>"a single quote ' must be escaped.", "second"=>"! : \\ etc. can be used freely.", "is same as"=>"this contains six spaces\nand one line break", "empty"=>"", "span"=>"this contains six spaces\nand one line break"}, <<EOY + def test_spec_span_single_quote + assert_parse_only( {"third"=>"a single quote ' must be escaped.", "second"=>"! : \\ etc. can be used freely.", "is same as"=>"this contains six spaces\nand one line break", "empty"=>"", "span"=>"this contains six spaces\nand one line break"}, <<EOY empty: '' second: '! : \\ etc. can be used freely.' third: 'a single quote '' must be escaped.' @@ -940,11 +948,11 @@ span: 'this contains line break' is same as: "this contains six spaces\\nand one line break" EOY - ) - end + ) + end - def test_spec_span_double_quote - assert_parse_only( {"is equal to"=>"this contains four spaces", "third"=>"a \" or a \\ must be escaped.", "second"=>"! : etc. can be used freely.", "empty"=>"", "fourth"=>"this value ends with an LF.\n", "span"=>"this contains four spaces"}, <<EOY + def test_spec_span_double_quote + assert_parse_only( {"is equal to"=>"this contains four spaces", "third"=>"a \" or a \\ must be escaped.", "second"=>"! : etc. can be used freely.", "empty"=>"", "fourth"=>"this value ends with an LF.\n", "span"=>"this contains four spaces"}, <<EOY empty: "" second: "! : etc. can be used freely." third: "a \\\" or a \\\\ must be escaped." @@ -954,29 +962,29 @@ span: "this contains spaces" is equal to: "this contains four spaces" EOY - ) - end - - def test_spec_builtin_time - # Time - assert_parse_only( - { "space separated" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ), - "canonical" => mktime( 2001, 12, 15, 2, 59, 43, ".10" ), - "date (noon UTC)" => Date.new( 2002, 12, 14), - "valid iso8601" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ) }, <<EOY + ) + end + + def test_spec_builtin_time + # Time + assert_parse_only( + { "space separated" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ), + "canonical" => mktime( 2001, 12, 15, 2, 59, 43, ".10" ), + "date (noon UTC)" => Date.new( 2002, 12, 14), + "valid iso8601" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ) }, <<EOY canonical: 2001-12-15T02:59:43.1Z valid iso8601: 2001-12-14t21:59:43.10-05:00 space separated: 2001-12-14 21:59:43.10 -05:00 date (noon UTC): 2002-12-14 EOY - ) - end - - def test_spec_builtin_binary - arrow_gif = "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236iiiccc\243\243\243\204\204\204\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371!\376\016Made with GIMP\000,\000\000\000\000\f\000\f\000\000\005, \216\2010\236\343@\024\350i\020\304\321\212\010\034\317\200M$z\357\3770\205p\270\2601f\r\e\316\001\303\001\036\020' \202\n\001\000;" - assert_parse_only( - { 'canonical' => arrow_gif, 'base64' => arrow_gif, - 'description' => "The binary value above is a tiny arrow encoded as a gif image.\n" }, <<EOY + ) + end + + def test_spec_builtin_binary + arrow_gif = "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236iiiccc\243\243\243\204\204\204\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371!\376\016Made with GIMP\000,\000\000\000\000\f\000\f\000\000\005, \216\2010\236\343@\024\350i\020\304\321\212\010\034\317\200M$z\357\3770\205p\270\2601f\r\e\316\001\303\001\036\020' \202\n\001\000;" + assert_parse_only( + { 'canonical' => arrow_gif, 'base64' => arrow_gif, + 'description' => "The binary value above is a tiny arrow encoded as a gif image.\n" }, <<EOY canonical: !binary "\\ R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOf\\ n515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaW\\ @@ -997,19 +1005,19 @@ description: > The binary value above is a tiny arrow encoded as a gif image. EOY - ) - end - def test_ruby_regexp - # Test Ruby regular expressions - assert_to_yaml( - { 'simple' => /a.b/, 'complex' => %r'\A"((?:[^"]|\")+)"', - 'case-insensitive' => /George McFly/i }, <<EOY + ) + end + def test_ruby_regexp + # Test Ruby regular expressions + assert_to_yaml( + { 'simple' => /a.b/, 'complex' => %r'\A"((?:[^"]|\")+)"', + 'case-insensitive' => /George McFly/i }, <<EOY case-insensitive: !ruby/regexp "/George McFly/i" complex: !ruby/regexp "/\\\\A\\"((?:[^\\"]|\\\\\\")+)\\"/" simple: !ruby/regexp "/a.b/" EOY - ) - end + ) + end # # Test of Ranges @@ -1033,15 +1041,14 @@ EOY end - def test_ruby_struct - Struct.send(:remove_const, :MyBookStruct) if Struct.const_defined?(:MyBookStruct) - # Ruby structures - book_struct = Struct::new( "MyBookStruct", :author, :title, :year, :isbn ) - assert_to_yaml( - [ book_struct.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ), - book_struct.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002, - book_struct.new( "This should be the ISBN", "but I have another struct here", 2002, "None" ) - ) ], <<EOY + def test_ruby_struct + # Ruby structures + book_struct = Struct::new( "MyBookStruct", :author, :title, :year, :isbn ) + assert_to_yaml( + [ book_struct.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ), + book_struct.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002, + book_struct.new( "This should be the ISBN", "but I have another struct here", 2002, "None" ) + ) ], <<EOY - !ruby/struct:MyBookStruct author: Yukihiro Matsumoto title: Ruby in a Nutshell @@ -1059,64 +1066,105 @@ EOY year: 2002 isbn: None EOY - ) + ) assert_to_yaml( Psych_Tests::StructTest.new( 123 ), <<EOY ) --- !ruby/struct:Psych_Tests::StructTest c: 123 EOY - end + ensure + Struct.__send__(:remove_const, :MyBookStruct) if book_struct + end + + def test_ruby_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + # Ruby Data value objects + book_class = Data.define(:author, :title, :year, :isbn) + Object.const_set(:MyBookData, book_class) + assert_to_yaml( + [ book_class.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ), + book_class.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002, + book_class.new( "This should be the ISBN", "but I have more data here", 2002, "None" ) + ) + ], <<EOY +- !ruby/data:MyBookData + author: Yukihiro Matsumoto + title: Ruby in a Nutshell + year: 2002 + isbn: 0-596-00214-9 +- !ruby/data:MyBookData + author: + - Dave Thomas + - Andy Hunt + title: The Pickaxe + year: 2002 + isbn: !ruby/data:MyBookData + author: This should be the ISBN + title: but I have more data here + year: 2002 + isbn: None +EOY + ) + + assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY ) +--- !ruby/data:Psych_Tests::DataTest +c: 123 +EOY + + ensure + Object.__send__(:remove_const, :MyBookData) if book_class + end - def test_ruby_rational - assert_to_yaml( Rational(1, 2), <<EOY ) + def test_ruby_rational + assert_to_yaml( Rational(1, 2), <<EOY ) --- !ruby/object:Rational numerator: 1 denominator: 2 EOY - # Read Psych dumped by the ruby 1.8.3. - assert_to_yaml( Rational(1, 2), "!ruby/object:Rational 1/2\n" ) - assert_raise( ArgumentError ) { Psych.unsafe_load("!ruby/object:Rational INVALID/RATIONAL\n") } - end + # Read Psych dumped by the ruby 1.8.3. + assert_to_yaml( Rational(1, 2), "!ruby/object:Rational 1/2\n" ) + assert_raise( ArgumentError ) { Psych.unsafe_load("!ruby/object:Rational INVALID/RATIONAL\n") } + end - def test_ruby_complex - assert_to_yaml( Complex(3, 4), <<EOY ) + def test_ruby_complex + assert_to_yaml( Complex(3, 4), <<EOY ) --- !ruby/object:Complex image: 4 real: 3 EOY - # Read Psych dumped by the ruby 1.8.3. - assert_to_yaml( Complex(3, 4), "!ruby/object:Complex 3+4i\n" ) - assert_raise( ArgumentError ) { Psych.unsafe_load("!ruby/object:Complex INVALID+COMPLEXi\n") } - end + # Read Psych dumped by the ruby 1.8.3. + assert_to_yaml( Complex(3, 4), "!ruby/object:Complex 3+4i\n" ) + assert_raise( ArgumentError ) { Psych.unsafe_load("!ruby/object:Complex INVALID+COMPLEXi\n") } + end - def test_emitting_indicators - assert_to_yaml( "Hi, from Object 1. You passed: please, pretty please", <<EOY + def test_emitting_indicators + assert_to_yaml( "Hi, from Object 1. You passed: please, pretty please", <<EOY --- "Hi, from Object 1. You passed: please, pretty please" EOY - ) - end - - ## - ## Test the Psych::Stream class -- INACTIVE at the moment - ## - #def test_document - # y = Psych::Stream.new( :Indent => 2, :UseVersion => 0 ) - # y.add( - # { 'hi' => 'hello', 'map' => - # { 'good' => 'two' }, - # 'time' => Time.now, - # 'try' => /^po(.*)$/, - # 'bye' => 'goodbye' - # } - # ) - # y.add( { 'po' => 'nil', 'oper' => 90 } ) - # y.add( { 'hi' => 'wow!', 'bye' => 'wow!' } ) - # y.add( { [ 'Red Socks', 'Boston' ] => [ 'One', 'Two', 'Three' ] } ) - # y.add( [ true, false, false ] ) - #end + ) + end + + ## + ## Test the Psych::Stream class -- INACTIVE at the moment + ## + #def test_document + # y = Psych::Stream.new( :Indent => 2, :UseVersion => 0 ) + # y.add( + # { 'hi' => 'hello', 'map' => + # { 'good' => 'two' }, + # 'time' => Time.now, + # 'try' => /^po(.*)$/, + # 'bye' => 'goodbye' + # } + # ) + # y.add( { 'po' => 'nil', 'oper' => 90 } ) + # y.add( { 'hi' => 'wow!', 'bye' => 'wow!' } ) + # y.add( { [ 'Red Socks', 'Boston' ] => [ 'One', 'Two', 'Three' ] } ) + # y.add( [ true, false, false ] ) + #end # # Test YPath choices parsing diff --git a/test/psych/test_yaml_special_cases.rb b/test/psych/test_yaml_special_cases.rb index 205457bcae..f1a607783e 100644 --- a/test/psych/test_yaml_special_cases.rb +++ b/test/psych/test_yaml_special_cases.rb @@ -15,6 +15,7 @@ module Psych s = "" assert_equal false, Psych.unsafe_load(s) assert_equal [], Psych.load_stream(s) + assert_equal [], Psych.safe_load_stream(s) assert_equal false, Psych.parse(s) assert_equal [], Psych.parse_stream(s).transform assert_nil Psych.safe_load(s) @@ -24,6 +25,7 @@ module Psych s = "false" assert_equal false, Psych.load(s) assert_equal [false], Psych.load_stream(s) + assert_equal [false], Psych.safe_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) @@ -33,6 +35,7 @@ module Psych s = "n" assert_equal "n", Psych.load(s) assert_equal ["n"], Psych.load_stream(s) + assert_equal ["n"], Psych.safe_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) @@ -42,6 +45,7 @@ module Psych s = "off" assert_equal false, Psych.load(s) assert_equal [false], Psych.load_stream(s) + assert_equal [false], Psych.safe_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) @@ -51,6 +55,7 @@ module Psych s = "-.inf" assert_equal(-Float::INFINITY, Psych.load(s)) assert_equal([-Float::INFINITY], Psych.load_stream(s)) + assert_equal([-Float::INFINITY], Psych.safe_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)) @@ -60,6 +65,7 @@ module Psych s = ".NaN" assert Psych.load(s).nan? assert Psych.load_stream(s).first.nan? + assert Psych.safe_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? @@ -69,6 +75,7 @@ module Psych s = "0xC" assert_equal 12, Psych.load(s) assert_equal [12], Psych.load_stream(s) + assert_equal [12], Psych.safe_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) @@ -78,6 +85,7 @@ module Psych s = "<<" assert_equal "<<", Psych.load(s) assert_equal ["<<"], Psych.load_stream(s) + assert_equal ["<<"], Psych.safe_load_stream(s) assert_equal "<<", Psych.parse(s).transform assert_equal ["<<"], Psych.parse_stream(s).transform assert_equal "<<", Psych.safe_load(s) @@ -87,6 +95,7 @@ module Psych s = "<<: {}" assert_equal({}, Psych.load(s)) assert_equal [{}], Psych.load_stream(s) + assert_equal [{}], Psych.safe_load_stream(s) assert_equal({}, Psych.parse(s).transform) assert_equal [{}], Psych.parse_stream(s).transform assert_equal({}, Psych.safe_load(s)) @@ -96,6 +105,7 @@ module Psych 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.safe_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) @@ -105,6 +115,7 @@ module Psych 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.safe_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) @@ -114,6 +125,7 @@ module Psych s = "null" assert_nil Psych.load(s) assert_equal [nil], Psych.load_stream(s) + assert_equal [nil], Psych.safe_load_stream(s) assert_nil Psych.parse(s).transform assert_equal [nil], Psych.parse_stream(s).transform assert_nil Psych.safe_load(s) diff --git a/test/psych/test_yamlstore.rb b/test/psych/test_yamlstore.rb index 1a1be3700e..c2721c41ea 100644 --- a/test/psych/test_yamlstore.rb +++ b/test/psych/test_yamlstore.rb @@ -23,14 +23,18 @@ module Psych class YAMLStoreTest < TestCase def setup - @dir = Dir.mktmpdir("rubytest-file") - File.chown(-1, Process.gid, @dir) - @yamlstore_file = make_tmp_filename("yamlstore") - @yamlstore = YAML::Store.new(@yamlstore_file) + if defined?(::PStore) + @dir = Dir.mktmpdir("rubytest-file") + File.chown(-1, Process.gid, @dir) + @yamlstore_file = make_tmp_filename("yamlstore") + @yamlstore = YAML::Store.new(@yamlstore_file) + else + omit "PStore is not available" + end end def teardown - FileUtils.remove_entry_secure @dir + FileUtils.remove_entry_secure(@dir) if @dir end def make_tmp_filename(prefix) @@ -97,5 +101,5 @@ module Psych end end end - end + end if defined?(::PStore) end if defined?(Psych) diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb index 89c3676651..c9b501dfa2 100644 --- a/test/psych/visitors/test_to_ruby.rb +++ b/test/psych/visitors/test_to_ruby.rb @@ -328,6 +328,12 @@ description: mapping.children << Nodes::Scalar.new('bar') assert_equal({'foo' => 'bar'}, mapping.to_ruby) end + + def test_parse_symbols + node = Nodes::Scalar.new(':foo') + assert_equal :foo, node.to_ruby + assert_equal ':foo', node.to_ruby(parse_symbols: false) + end end end end diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index 01e685134a..bd3919f83d 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -73,6 +73,27 @@ module Psych assert_equal s.method, obj.method end + D = Data.define(:foo) unless RUBY_VERSION < "3.2" + + def test_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_cycle D.new('bar') + end + + def test_data_anon + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + d = Data.define(:foo).new('bar') + obj = Psych.unsafe_load(Psych.dump(d)) + assert_equal d.foo, obj.foo + end + + def test_data_override_method + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + d = Data.define(:method).new('override') + obj = Psych.unsafe_load(Psych.dump(d)) + assert_equal d.method, obj.method + end + def test_exception ex = Exception.new 'foo' loaded = Psych.unsafe_load(Psych.dump(ex)) |
