From 93c8fb443a746a49e5ad248a925f87c7c735264d Mon Sep 17 00:00:00 2001 From: Colin Kelley Date: Wed, 19 Jul 2023 12:32:12 -0700 Subject: [PATCH 1/2] issue #443: quote Y and N when dumping --- lib/psych/visitors/yaml_tree.rb | 2 +- test/psych/test_string.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 51491783..c32f115d 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -272,7 +272,7 @@ def visit_String o tag = 'tag:yaml.org,2002:str' plain = false quote = false - elsif o == 'y' || o == 'n' + elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N' style = Nodes::Scalar::DOUBLE_QUOTED elsif @line_width && o.length > @line_width style = Nodes::Scalar::FOLDED diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 0dc34b30..c996a8fc 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -24,10 +24,19 @@ def initialize # "ambiguity" in the emitted document def test_y_is_quoted assert_match(/"y"/, Psych.dump("y")) + assert_match(/"Y"/, Psych.dump("Y")) end def test_n_is_quoted assert_match(/"n"/, Psych.dump("n")) + assert_match(/"N"/, Psych.dump("N")) + end + + 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 From 6750b3540215e115c24bbeaaaca069b1508db72c Mon Sep 17 00:00:00 2001 From: Colin Kelley Date: Wed, 19 Jul 2023 13:14:10 -0700 Subject: [PATCH 2/2] issue #443: drop special tests for y, Y, n, N since they covered in the more general test --- test/psych/test_string.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index c996a8fc..84ae5cbb 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -17,20 +17,11 @@ def initialize 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")) - assert_match(/"Y"/, Psych.dump("Y")) - end - - def test_n_is_quoted - assert_match(/"n"/, Psych.dump("n")) - assert_match(/"N"/, Psych.dump("N")) - end 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