diff --git a/lib/psych/comments/node_ext.rb b/lib/psych/comments/node_ext.rb index e79ccd2..968334b 100644 --- a/lib/psych/comments/node_ext.rb +++ b/lib/psych/comments/node_ext.rb @@ -8,4 +8,12 @@ def leading_comments def trailing_comments @trailing_comments ||= [] end + + def leading_comments=(comments) + @leading_comments = Array(comments) + end + + def trailing_comments=(comments) + @trailing_comments = Array(comments) + end end diff --git a/spec/node_ext_spec.rb b/spec/node_ext_spec.rb index 36b37f8..ef37052 100644 --- a/spec/node_ext_spec.rb +++ b/spec/node_ext_spec.rb @@ -8,6 +8,18 @@ node = Psych::Nodes::Scalar.new("foo") expect(node.leading_comments).to eq([]) end + + it "can be set with an array of comments" do + node = Psych::Nodes::Scalar.new("foo") + node.leading_comments = ["# Comment 1", "# Comment 2"] + expect(node.leading_comments).to eq(["# Comment 1", "# Comment 2"]) + end + + it "wraps non-array values in an array" do + node = Psych::Nodes::Scalar.new("foo") + node.leading_comments = "# Single comment" + expect(node.leading_comments).to eq(["# Single comment"]) + end end describe "#trailing_comments" do @@ -15,5 +27,17 @@ node = Psych::Nodes::Scalar.new("foo") expect(node.trailing_comments).to eq([]) end + + it "can be set with an array of comments" do + node = Psych::Nodes::Scalar.new("foo") + node.trailing_comments = ["# Comment 1", "# Comment 2"] + expect(node.trailing_comments).to eq(["# Comment 1", "# Comment 2"]) + end + + it "wraps non-array values in an array" do + node = Psych::Nodes::Scalar.new("foo") + node.trailing_comments = "# Single comment" + expect(node.trailing_comments).to eq(["# Single comment"]) + end end end