diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 10:17:15 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 10:17:15 -0700 |
commit | 75fdcc42bb2ce2abb206f01b8e90729dedc8286c (patch) | |
tree | 4c69fbc6f75718e6ab701fb8e9d886c36737d586 | |
parent | d2bf942ac38b1b6e91116a06855a00d32b95d27b (diff) | |
download | psych-75fdcc42bb2ce2abb206f01b8e90729dedc8286c.zip |
mapping tested
-rw-r--r-- | ext/psych/emitter.c | 39 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 6 | ||||
-rw-r--r-- | test/visitors/test_emitter.rb | 15 |
3 files changed, 60 insertions, 0 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index 7b8a43a..d6b13f9 100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c @@ -173,6 +173,43 @@ static VALUE end_sequence(VALUE self) return self; } +static VALUE start_mapping( + VALUE self, + VALUE anchor, + VALUE tag, + VALUE implicit, + VALUE style +) { + yaml_emitter_t * emitter; + Data_Get_Struct(self, yaml_emitter_t, emitter); + + yaml_event_t event; + yaml_mapping_start_event_initialize( + &event, + (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(anchor)), + (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(tag)), + Qtrue == implicit ? 1 : 0, + (yaml_sequence_style_t)NUM2INT(style) + ); + + emit(emitter, &event); + + return self; +} + +static VALUE end_mapping(VALUE self) +{ + yaml_emitter_t * emitter; + Data_Get_Struct(self, yaml_emitter_t, emitter); + + yaml_event_t event; + yaml_mapping_end_event_initialize(&event); + + emit(emitter, &event); + + return self; +} + void Init_psych_emitter() { VALUE psych = rb_define_module("Psych"); @@ -189,4 +226,6 @@ void Init_psych_emitter() rb_define_method(cPsychEmitter, "scalar", scalar, 6); rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4); rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0); + rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4); + rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0); } diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index a0ff9d9..71c33e3 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -26,6 +26,12 @@ module Psych o.children.each { |c| c.accept self } @handler.end_sequence end + + visitor_for(Nodes::Mapping) do |o| + @handler.start_mapping o.anchor, o.tag, o.implicit, o.style + o.children.each { |c| c.accept self } + @handler.end_mapping + end end end end diff --git a/test/visitors/test_emitter.rb b/test/visitors/test_emitter.rb index b7b9110..cdca130 100644 --- a/test/visitors/test_emitter.rb +++ b/test/visitors/test_emitter.rb @@ -58,6 +58,21 @@ module Psych assert_match(/- hello/, @io.string) assert_equal @io.string, s.to_yaml end + + def test_mapping + s = Nodes::Stream.new + doc = Nodes::Document.new + mapping = Nodes::Mapping.new + mapping.children << Nodes::Scalar.new('key') + mapping.children << Nodes::Scalar.new('value') + doc.children << mapping + s.children << doc + + @visitor.accept s + + assert_match(/key: value/, @io.string) + assert_equal @io.string, s.to_yaml + end end end end |