diff options
Diffstat (limited to 'test/psych')
-rw-r--r-- | test/psych/test_numeric.rb | 9 | ||||
-rw-r--r-- | test/psych/test_object_references.rb | 8 | ||||
-rw-r--r-- | test/psych/test_parser.rb | 19 | ||||
-rw-r--r-- | test/psych/test_psych.rb | 26 | ||||
-rw-r--r-- | test/psych/test_scalar_scanner.rb | 2 | ||||
-rw-r--r-- | test/psych/test_set.rb | 7 | ||||
-rw-r--r-- | test/psych/test_string.rb | 12 | ||||
-rw-r--r-- | test/psych/visitors/test_emitter.rb | 16 | ||||
-rw-r--r-- | test/psych/visitors/test_to_ruby.rb | 2 | ||||
-rw-r--r-- | test/psych/visitors/test_yaml_tree.rb | 8 |
10 files changed, 82 insertions, 27 deletions
diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb index 9c75c016cd..4c012e4562 100644 --- a/test/psych/test_numeric.rb +++ b/test/psych/test_numeric.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true require_relative 'helper' -require 'bigdecimal' +begin + require 'bigdecimal' +rescue LoadError +end module Psych ### @@ -29,13 +32,13 @@ module Psych def test_big_decimal_tag decimal = BigDecimal("12.34") assert_match "!ruby/object:BigDecimal", Psych.dump(decimal) - end + end if defined?(BigDecimal) def test_big_decimal_round_trip decimal = BigDecimal("12.34") $DEBUG = false assert_cycle decimal - end + end if defined?(BigDecimal) def test_does_not_attempt_numeric str = Psych.load('--- 4 roses') diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb index 269d72242e..86bb9034b9 100644 --- a/test/psych/test_object_references.rb +++ b/test/psych/test_object_references.rb @@ -39,7 +39,7 @@ module Psych rescue Psych::DisallowedClass data = Psych.unsafe_load yml end - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_float_references @@ -49,7 +49,7 @@ module Psych - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_binary_references @@ -60,7 +60,7 @@ module Psych - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_regexp_references @@ -70,7 +70,7 @@ module Psych - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end end end diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index 3b67a8eab1..c1e0abb89d 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -384,6 +384,25 @@ module Psych [:end_stream, [2, 0, 2, 0]]], events end + if Psych::Parser.method_defined?(:code_point_limit) + def test_code_point_limit + yaml = "foo: bar\n" * 500_000 + assert_raise(org.snakeyaml.engine.v2.exceptions.YamlEngineException) do + Psych.load(yaml) + end + + assert_nothing_raised do + begin + old_code_point_limit, Psych::Parser.code_point_limit = Psych::Parser::code_point_limit, 5_000_000 + + Psych.load(yaml) + ensure + Psych::Parser.code_point_limit = old_code_point_limit + end + end + end + end + def assert_called call, with = nil, parser = @parser if with call = parser.handler.calls.find { |x| diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index c977e799e3..42586a8779 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -430,6 +430,32 @@ eoyml assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_symbols: [:foo])) end + def test_safe_dump_stringify_names + yaml = <<-eoyml +--- +foo: + bar: bar + 'no': special escapes + 123: number +eoyml + + payload = Psych.safe_dump({ + foo: { + bar: "bar", + no: "special escapes", + 123 => "number" + } + }, stringify_names: true) + assert_equal yaml, payload + + assert_equal("---\nfoo: :bar\n", Psych.safe_dump({foo: :bar}, stringify_names: true, permitted_symbols: [:bar])) + + error = assert_raise Psych::DisallowedClass do + Psych.safe_dump({foo: :bar}, stringify_names: true) + end + assert_equal "Tried to dump unspecified class: Symbol(:bar)", error.message + end + def test_safe_dump_aliases x = [] x << x diff --git a/test/psych/test_scalar_scanner.rb b/test/psych/test_scalar_scanner.rb index 145db58fd9..02b923afe2 100644 --- a/test/psych/test_scalar_scanner.rb +++ b/test/psych/test_scalar_scanner.rb @@ -150,7 +150,7 @@ module Psych end def test_scan_strict_int_commas_and_underscores - # this test is to ensure adherance to YML spec using the 'strict_integer' option + # this test is to ensure adherence to YML spec using the 'strict_integer' option scanner = Psych::ScalarScanner.new ClassLoader.new, strict_integer: true assert_equal 123_456_789, scanner.tokenize('123_456_789') assert_equal '123,456,789', scanner.tokenize('123,456,789') diff --git a/test/psych/test_set.rb b/test/psych/test_set.rb index 87944d839e..b4968d3425 100644 --- a/test/psych/test_set.rb +++ b/test/psych/test_set.rb @@ -46,5 +46,12 @@ bar: baz @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_string.rb b/test/psych/test_string.rb index 0dc34b3083..84ae5cbb45 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -17,17 +17,17 @@ module Psych end end - # 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in + # 'y', 'Y', 'n', 'N' are kind of ambiguous. Syck treated those literals in # YAML documents as strings. But this is not what the YAML 1.1 spec says. # YAML 1.1 says they should be treated as booleans. When we're dumping # documents, we know it's a string, so adding quotes will eliminate the # "ambiguity" in the emitted document - def test_y_is_quoted - assert_match(/"y"/, Psych.dump("y")) - end - def test_n_is_quoted - assert_match(/"n"/, Psych.dump("n")) + def test_all_yaml_1_1_booleans_are_quoted + yaml_1_1_booleans = %w[y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF] # from https://yaml.org/type/bool.html + yaml_1_1_booleans.each do |boolean| + assert_match(/"#{boolean}"|'#{boolean}'/, Psych.dump(boolean)) + end end def test_string_with_newline diff --git a/test/psych/visitors/test_emitter.rb b/test/psych/visitors/test_emitter.rb index 70adbb9ca0..8bcf5491ca 100644 --- a/test/psych/visitors/test_emitter.rb +++ b/test/psych/visitors/test_emitter.rb @@ -61,9 +61,9 @@ module Psych @visitor.accept s - assert_match(/key: value/, @io.string) + assert_include(@io.string, "key: value") assert_equal @io.string, s.yaml - assert(/\.\.\./ !~ s.yaml) + assert_not_include(s.yaml, "...") end def test_scalar @@ -76,7 +76,7 @@ module Psych @visitor.accept s - assert_match(/hello/, @io.string) + assert_include(@io.string, "hello") assert_equal @io.string, s.yaml end @@ -90,8 +90,8 @@ module Psych @visitor.accept s - assert_match(/str/, @io.string) - assert_match(/hello/, @io.string) + assert_include(@io.string, "str") + assert_include(@io.string, "hello") assert_equal @io.string, s.yaml end @@ -107,7 +107,7 @@ module Psych @visitor.accept s - assert_match(/- hello/, @io.string) + assert_include(@io.string, "- hello") assert_equal @io.string, s.yaml end @@ -122,7 +122,7 @@ module Psych @visitor.accept s - assert_match(/key: value/, @io.string) + assert_include(@io.string, "key: value") assert_equal @io.string, s.yaml end @@ -137,7 +137,7 @@ module Psych @visitor.accept s - assert_match(/&A key: \*A/, @io.string) + assert_include(@io.string, "&A key: \*A") assert_equal @io.string, s.yaml end end diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb index 3d4608b903..89c3676651 100644 --- a/test/psych/visitors/test_to_ruby.rb +++ b/test/psych/visitors/test_to_ruby.rb @@ -319,7 +319,7 @@ description: list = seq.to_ruby assert_equal %w{ foo foo }, list - assert_equal list[0].object_id, list[1].object_id + assert_same list[0], list[1] end def test_mapping_with_str_tag diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index 4c48670f2f..01e685134a 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -34,7 +34,7 @@ module Psych v << "hello world" v.finish - assert_match "hello world", io.string + assert_include io.string, "hello world" end def test_binary_formatting @@ -167,9 +167,9 @@ module Psych end def test_string - assert_match(/'017'/, Psych.dump({'a' => '017'})) - assert_match(/'019'/, Psych.dump({'a' => '019'})) - assert_match(/'01818'/, Psych.dump({'a' => '01818'})) + assert_include(Psych.dump({'a' => '017'}), "'017'") + assert_include(Psych.dump({'a' => '019'}), "'019'") + assert_include(Psych.dump({'a' => '01818'}), "'01818'") end # http://yaml.org/type/null.html |