summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-07 10:58:55 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-07 10:58:55 -0700
commitd3bfc7a71256f84d4ce1b7e31b032739f02dfb55 (patch)
treedd9ff7addd7c5c2693b37790a676637768030263
parent4d047de2c59b71bbe6b4d438970b2c93012662b4 (diff)
downloadpsych-d3bfc7a71256f84d4ce1b7e31b032739f02dfb55.zip
exceptions move back and forth
-rw-r--r--lib/psych/ruby.rb3
-rw-r--r--lib/psych/visitors/to_ruby.rb4
-rw-r--r--lib/psych/visitors/yast_builder.rb8
-rw-r--r--test/visitors/test_to_ruby.rb13
-rw-r--r--test/visitors/test_yast_builder.rb8
5 files changed, 34 insertions, 2 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 8831811..f226c84 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -4,9 +4,8 @@ require 'date'
[
Object, String, Class, Hash, Array, NilClass, Float, FalseClass, TrueClass,
- Range, Complex, Rational, Date, Time, Regexp
+ Range, Complex, Rational, Date, Time, Regexp, Exception
# Struct
- # Exception
].each do |klass|
klass.send(:remove_method, :to_yaml) rescue NameError
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index ab8ef52..d00f72a 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -81,6 +81,10 @@ module Psych
h = Hash[*o.children.map { |c| accept c }]
Range.new(h['begin'], h['end'], h['excl'])
+ when "!ruby/exception"
+ h = Hash[*o.children.map { |c| accept c }]
+ Exception.new h['message']
+
when '!ruby/object:Complex'
h = Hash[*o.children.map { |c| accept c }]
Complex(h['real'], h['image'])
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index 1cfcd33..914a891 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -21,6 +21,14 @@ module Psych
raise TypeError, "Can't dump #{target.class}"
end
+ def visit_Exception o
+ @stack.push append Nodes::Mapping.new(nil, '!ruby/exception', false)
+ ['message', o.message].each do |m|
+ accept m
+ end
+ @stack.pop
+ end
+
def visit_Regexp o
append Nodes::Scalar.new(o.inspect, nil, '!ruby/regexp', false)
end
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index 7fd99a4..e8b7a6e 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -11,6 +11,19 @@ module Psych
@visitor = ToRuby.new
end
+ def test_exception
+ exc = Exception.new 'hello'
+
+ mapping = Nodes::Mapping.new nil, '!ruby/exception'
+ mapping.children << Nodes::Scalar.new('message')
+ mapping.children << Nodes::Scalar.new('hello')
+
+ ruby = mapping.to_ruby
+
+ assert_equal exc.class, ruby.class
+ assert_equal exc.message, ruby.message
+ end
+
def test_regexp
node = Nodes::Scalar.new('/foo/', nil, '!ruby/regexp')
assert_equal(/foo/, node.to_ruby)
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index dec8c6a..d35ff3d 100644
--- a/test/visitors/test_yast_builder.rb
+++ b/test/visitors/test_yast_builder.rb
@@ -8,6 +8,14 @@ module Psych
@v = Visitors::YASTBuilder.new
end
+ def test_exception
+ ex = Exception.new 'foo'
+ loaded = Psych.load(Psych.dump(ex))
+
+ assert_equal ex.message, loaded.message
+ assert_equal ex.class, loaded.class
+ end
+
def test_regexp
assert_round_trip(/foo/)
assert_round_trip(/foo/i)