summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/psych/emitter.c2
-rw-r--r--lib/psych/ruby.rb5
-rw-r--r--lib/psych/scalar_scanner.rb4
-rw-r--r--lib/psych/visitors/to_ruby.rb7
-rw-r--r--lib/psych/visitors/yast_builder.rb18
-rw-r--r--test/test_scalar_scanner.rb7
-rw-r--r--test/visitors/test_to_ruby.rb18
-rw-r--r--test/visitors/test_yast_builder.rb13
8 files changed, 70 insertions, 4 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c
index aa68e06..e5290e2 100644
--- a/ext/psych/emitter.c
+++ b/ext/psych/emitter.c
@@ -187,7 +187,7 @@ static VALUE start_mapping(
yaml_mapping_start_event_initialize(
&event,
(yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(anchor)),
- (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(tag)),
+ (yaml_char_t *)(Qnil == tag ? NULL : StringValuePtr(tag)),
Qtrue == implicit ? 1 : 0,
(yaml_sequence_style_t)NUM2INT(style)
);
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 02d5531..255f73c 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -1,4 +1,7 @@
-[Object, String, Class, Hash, Array, NilClass, Float].each do |klass|
+[
+ Object, String, Class, Hash, Array, NilClass, Float,
+ FalseClass, TrueClass
+].each do |klass|
klass.send(:remove_method, :to_yaml) rescue NameError
end
diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb
index 20f694d..71c75e3 100644
--- a/lib/psych/scalar_scanner.rb
+++ b/lib/psych/scalar_scanner.rb
@@ -20,6 +20,10 @@ module Psych
[:NAN, 0.0 / 0.0]
when /^(null|~)$/i
[:NULL, nil]
+ when /^(y|yes|true|on)$/i
+ [:BOOLEAN, true]
+ when /^(n|no|false|off)$/i
+ [:BOOLEAN, false]
when /^:/i
[:SYMBOL, @string.sub(/^:/, '').to_sym]
when /^[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+$/
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 34e0442..b05b34b 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -27,7 +27,12 @@ module Psych
end
def visit_Psych_Nodes_Mapping o
- Hash[*o.children.map { |c| c.accept self }]
+ hash = {}
+ @st[o.anchor] = hash if o.anchor
+ o.children.map { |c| c.accept self }.each_slice(2) { |k,v|
+ hash[k] = v
+ }
+ hash
end
def visit_Psych_Nodes_Document o
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index 5dd7dc5..25cfe34 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -25,6 +25,14 @@ module Psych
append Nodes::Scalar.new o.to_s
end
+ def visit_TrueClass o
+ append Nodes::Scalar.new o.to_s
+ end
+
+ def visit_FalseClass o
+ append Nodes::Scalar.new o.to_s
+ end
+
def visit_Float o
if o.nan?
append Nodes::Scalar.new '.nan'
@@ -47,7 +55,15 @@ module Psych
end
def visit_Hash o
- @stack.push append Nodes::Mapping.new
+ if node = @st[o.object_id]
+ node.anchor = o.object_id.to_s
+ return append Nodes::Alias.new o.object_id.to_s
+ end
+
+ map = Nodes::Mapping.new
+ @st[o.object_id] = map
+
+ @stack.push append map
o.each do |k,v|
k.accept self
diff --git a/test/test_scalar_scanner.rb b/test/test_scalar_scanner.rb
index 5311249..af4adc5 100644
--- a/test/test_scalar_scanner.rb
+++ b/test/test_scalar_scanner.rb
@@ -48,4 +48,11 @@ class TestScalarScanner < MiniTest::Unit::TestCase
ss = Psych::ScalarScanner.new('1.2')
assert_equal [:FLOAT, 1.2], ss.tokenize
end
+
+ def test_scan_true
+ ss = Psych::ScalarScanner.new('true')
+ assert_equal [:BOOLEAN, true], ss.tokenize
+ ss = Psych::ScalarScanner.new('y')
+ assert_equal [:BOOLEAN, true], ss.tokenize
+ end
end
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 0dafbd8..c252573 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -39,6 +39,24 @@ module Psych
end
end
+ # http://yaml.org/type/bool.html
+ def test_boolean_true
+ %w{ y Y yes Yes YES true True TRUE on On ON }.each do |t|
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
+ assert_equal true, i.to_ruby
+ assert_equal true, Nodes::Scalar.new(t).to_ruby
+ end
+ end
+
+ # http://yaml.org/type/bool.html
+ def test_boolean_false
+ %w{ n N no No NO false False FALSE off Off OFF }.each do |t|
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
+ assert_equal false, i.to_ruby
+ assert_equal false, Nodes::Scalar.new(t).to_ruby
+ end
+ end
+
def test_float
i = Nodes::Scalar.new('1.2', nil, 'tag:yaml.org,2002:float')
assert_equal 1.2, i.to_ruby
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index 9a074e1..9d2945b 100644
--- a/test/visitors/test_yast_builder.rb
+++ b/test/visitors/test_yast_builder.rb
@@ -14,12 +14,25 @@ module Psych
assert_equal a.inspect, Psych.load(a.to_yaml).inspect
end
+ def test_circular_map
+ a = {}
+ a[a] = a
+ assert_equal a.inspect, Psych.load(a.to_yaml).inspect
+ end
+
def test_scalar
assert_round_trip 'foo'
assert_round_trip ':foo'
assert_round_trip ''
end
+ def test_boolean
+ assert_round_trip true
+ assert_round_trip 'true'
+ assert_round_trip false
+ assert_round_trip 'false'
+ end
+
def test_binary
string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*')
assert_equal string, Psych.load(string.to_yaml)