[前][次][番号順一覧][スレッド一覧]

ruby-changes:66218

From: Jeremy <ko1@a...>
Date: Mon, 17 May 2021 11:21:06 +0900 (JST)
Subject: [ruby-changes:66218] 830778db95 (master): [ruby/psych] feat: allow scalars and sequences to be styled when dumped

https://git.ruby-lang.org/ruby.git/commit/?id=830778db95

From 830778db95c1dca5dfad591eae5176d3133bf7ee Mon Sep 17 00:00:00 2001
From: Jeremy Ebler <jebler@g...>
Date: Sun, 7 Feb 2021 18:39:07 -0800
Subject: [ruby/psych] feat: allow scalars and sequences to be styled when
 dumped

https://github.com/ruby/psych/commit/546154ddb7
---
 ext/psych/lib/psych/visitors/yaml_tree.rb |   4 +-
 test/psych/test_coder.rb                  | 121 ++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 2 deletions(-)

diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index ac6777a..bf7c0bb 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -509,9 +509,9 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L509
       def emit_coder c, o
         case c.type
         when :scalar
-          @emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, Nodes::Scalar::ANY
+          @emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, c.style
         when :seq
-          @emitter.start_sequence nil, c.tag, c.tag.nil?, Nodes::Sequence::BLOCK
+          @emitter.start_sequence nil, c.tag, c.tag.nil?, c.style
           c.seq.each do |thing|
             accept thing
           end
diff --git a/test/psych/test_coder.rb b/test/psych/test_coder.rb
index 5ea8cab..aaf66fc 100644
--- a/test/psych/test_coder.rb
+++ b/test/psych/test_coder.rb
@@ -112,6 +112,16 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_coder.rb#L112
       end
     end
 
+    class CustomEncode
+      def initialize(**opts)
+        @opts = opts
+      end
+
+      def encode_with(coder)
+        @opts.each { |k,v| coder.public_send :"#{k}=", v }
+      end
+    end
+
     def test_self_referential
       x = Referential.new
       copy = Psych.load Psych.dump x
@@ -203,5 +213,116 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_coder.rb#L213
       assert_equal foo.b, bar.b
       assert_nil bar.c
     end
+
+    def test_coder_style_map_default
+      foo = Psych.dump a: 1, b: 2
+      assert_equal foo, "---\n:a: 1\n:b: 2\n"
+    end
+
+    def test_coder_style_map_any
+      foo = Psych.dump CustomEncode.new \
+        map: {a: 1, b: 2},
+        style: Psych::Nodes::Mapping::ANY,
+        tag: nil
+      assert_equal foo, "---\n:a: 1\n:b: 2\n"
+    end
+
+    def test_coder_style_map_block
+      foo = Psych.dump CustomEncode.new \
+        map: {a: 1, b: 2},
+        style: Psych::Nodes::Mapping::BLOCK,
+        tag: nil
+      assert_equal foo, "---\n:a: 1\n:b: 2\n"
+    end
+
+    def test_coder_style_map_flow
+      foo = Psych.dump CustomEncode.new \
+        map: { a: 1, b: 2 },
+        style: Psych::Nodes::Mapping::FLOW,
+        tag: nil
+      assert_equal foo, "--- {! ':a': 1, ! ':b': 2}\n"
+    end
+
+    def test_coder_style_seq_default
+      foo = Psych.dump [ 1, 2, 3 ]
+      assert_equal foo, "---\n- 1\n- 2\n- 3\n"
+    end
+
+    def test_coder_style_seq_any
+      foo = Psych.dump CustomEncode.new \
+        seq: [ 1, 2, 3 ],
+        style: Psych::Nodes::Sequence::ANY,
+        tag: nil
+      assert_equal foo, "---\n- 1\n- 2\n- 3\n"
+    end
+
+    def test_coder_style_seq_block
+      foo = Psych.dump CustomEncode.new \
+        seq: [ 1, 2, 3 ],
+        style: Psych::Nodes::Sequence::BLOCK,
+        tag: nil
+      assert_equal foo, "---\n- 1\n- 2\n- 3\n"
+    end
+
+    def test_coder_style_seq_flow
+      foo = Psych.dump CustomEncode.new \
+        seq: [ 1, 2, 3 ],
+        style: Psych::Nodes::Sequence::FLOW,
+        tag: nil
+      assert_equal foo, "--- [1, 2, 3]\n"
+    end
+
+    def test_coder_style_scalar_default
+      foo = Psych.dump 'some scalar'
+      assert_equal foo, "--- some scalar\n"
+    end
+
+    def test_coder_style_scalar_any
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::ANY,
+        tag: nil
+      assert_equal foo, "--- some scalar\n"
+    end
+
+    def test_coder_style_scalar_plain
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::PLAIN,
+        tag: nil
+      assert_equal foo, "--- some scalar\n"
+    end
+
+    def test_coder_style_scalar_single_quoted
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::SINGLE_QUOTED,
+        tag: nil
+      assert_equal foo, "--- ! 'some scalar'\n"
+    end
+
+    def test_coder_style_scalar_double_quoted
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::DOUBLE_QUOTED,
+        tag: nil
+      assert_equal foo, %Q'--- ! "some scalar"\n'
+    end
+
+    def test_coder_style_scalar_literal
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::LITERAL,
+        tag: nil
+      assert_equal foo, "--- ! |-\n  some scalar\n"
+    end
+
+    def test_coder_style_scalar_folded
+      foo = Psych.dump CustomEncode.new \
+        scalar: 'some scalar',
+        style: Psych::Nodes::Scalar::FOLDED,
+        tag: nil
+      assert_equal foo, "--- ! >-\n  some scalar\n"
+    end
   end
 end
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]