diff options
| author | Robert Schulze <robert@dotless.de> | 2023-02-23 13:38:54 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-01-18 17:32:34 +0000 |
| commit | d3b07b984545ce156e02e9f71404b652c6cb5284 (patch) | |
| tree | db319e0f29d7719d4d19ac4722ad09b049cf351b /test | |
| parent | 00814fd6724fff66a10966f5be10ea6dae06c616 (diff) | |
[ruby/psych] Add :stringify_names option to convert symbol keys to string for dumping
https://github.com/ruby/psych/commit/3d051d89aa
Diffstat (limited to 'test')
| -rw-r--r-- | test/psych/test_psych.rb | 26 | ||||
| -rw-r--r-- | test/psych/test_set.rb | 7 |
2 files changed, 33 insertions, 0 deletions
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_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 |
