diff options
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 13 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 5 | ||||
-rw-r--r-- | test/visitors/test_yast_builder.rb | 14 |
3 files changed, 24 insertions, 8 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 06ab5d1..9b5a253 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -10,12 +10,15 @@ module Psych def visit_Psych_Nodes_Scalar o @st[o.anchor] = o.value if o.anchor - case o.tag - when 'tag:yaml.org,2002:null' - nil - else - o.value + + return nil if o.tag == 'tag:yaml.org,2002:null' + + unless o.quoted + return nil if o.value =~ /^null$/i + return nil if o.value == '~' end + + o.value end def visit_Psych_Nodes_Sequence o diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 8508601..90dc8d0 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -21,7 +21,10 @@ module Psych end def visit_String o - @stack.last.children << Nodes::Scalar.new(o) + quote = !!(o =~ /^(null|~)$/i) + + scalar = Nodes::Scalar.new(o, nil, nil, !quote, quote) + @stack.last.children << scalar end def visit_Class o diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb index 36feb21..30374c1 100644 --- a/test/visitors/test_yast_builder.rb +++ b/test/visitors/test_yast_builder.rb @@ -7,6 +7,7 @@ module Psych def setup @v = Visitors::YASTBuilder.new end + def test_scalar @v.accept 'foo' @@ -37,13 +38,22 @@ module Psych assert_round_trip(%w{ a b }) end + # http://yaml.org/type/null.html def test_nil assert_round_trip nil + assert_equal nil, Psych.load('null') + assert_equal nil, Psych.load('Null') + assert_equal nil, Psych.load('NULL') + assert_equal nil, Psych.load('~') + + assert_round_trip 'null' + assert_round_trip '~' end def assert_round_trip obj - @v.accept(obj) - assert_equal(obj, Psych.load(@v.tree.to_yaml)) + v = Visitors::YASTBuilder.new + v.accept(obj) + assert_equal(obj, Psych.load(v.tree.to_yaml)) assert_equal(obj, Psych.load(obj.to_yaml)) assert_equal(obj, Psych.load(Psych.dump(obj))) end |