Skip to content

Commit

Permalink
duplicating erorrs works. yay! closes #148
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 14, 2009
1 parent cb102e9 commit 33922d7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* NodeSet#[] works with slices larger than NodeSet#length GH #131
* Reparented nodes maintain their namespace. GH #134
* Fixed SEGV when adding an XML::Document to NodeSet
* XML::SyntaxError can be duplicated. GH #148

=== 1.3.3 / 2009/07/26

Expand Down
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ test/xml/test_processing_instruction.rb
test/xml/test_reader_encoding.rb
test/xml/test_relax_ng.rb
test/xml/test_schema.rb
test/xml/test_syntax_error.rb
test/xml/test_text.rb
test/xml/test_unparented_node.rb
test/xml/test_xpath.rb
59 changes: 58 additions & 1 deletion ext/nokogiri/xml_syntax_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ static void dealloc(xmlErrorPtr ptr)
NOKOGIRI_DEBUG_END(ptr);
}

static VALUE allocate(VALUE klass)
{
xmlErrorPtr error = xmlMalloc(sizeof(xmlError));
memset(error, 0, sizeof(xmlError));
return Data_Wrap_Struct(klass, NULL, dealloc, error);
}

/*
* call-seq:
* column
Expand Down Expand Up @@ -153,7 +160,53 @@ static VALUE message(VALUE self)
{
xmlErrorPtr error;
Data_Get_Struct(self, xmlError, error);
return NOKOGIRI_STR_NEW2(error->message);
if(error->message) return NOKOGIRI_STR_NEW2(error->message);
return Qnil;
}

/*
* call-seq:
* message=
*
* Set the human readable message.
*/
static VALUE set_message(VALUE self, VALUE _message)
{
xmlErrorPtr error;
Data_Get_Struct(self, xmlError, error);

if(error->message) {
xmlFree(error->message);
error->message = 0;
}

if(RTEST(_message)) {
error->message = xmlMalloc(RSTRING_LEN(_message) + 1);
memset(error->message, 0, RSTRING_LEN(_message) + 1);
memcpy(error->message, StringValuePtr(_message), RSTRING_LEN(_message));
}

return message;
}

/*
* call-seq:
* initialize_copy(old_copy)
*
* Initialize a copy of the +old_copy+
*/
static VALUE initialize_copy(VALUE self, VALUE _old_copy)
{
if(!rb_obj_is_kind_of(_old_copy, cNokogiriXmlSyntaxError))
rb_raise(rb_eArgError, "node must be a Nokogiri::XML::SyntaxError");

xmlErrorPtr error, old_error;
Data_Get_Struct(self, xmlError, error);
Data_Get_Struct(_old_copy, xmlError, old_error);

xmlCopyError(old_error, error);

return message;
}

void Nokogiri_error_array_pusher(void * ctx, xmlErrorPtr error)
Expand Down Expand Up @@ -190,7 +243,11 @@ void init_xml_syntax_error()
VALUE klass = rb_define_class_under(xml, "SyntaxError", syntax_error_mommy);
cNokogiriXmlSyntaxError = klass;

rb_define_alloc_func(klass, allocate);

rb_define_method(klass, "message", message, 0);
rb_define_method(klass, "message=", set_message, 1);
rb_define_method(klass, "initialize_copy", initialize_copy, 1);
rb_define_method(klass, "domain", domain, 0);
rb_define_method(klass, "code", code, 0);
rb_define_method(klass, "level", level, 0);
Expand Down
4 changes: 4 additions & 0 deletions lib/nokogiri/xml/syntax_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module XML
# This class provides information about XML SyntaxErrors. These
# exceptions are typically stored on Nokogiri::XML::Document#errors.
class SyntaxError < ::Nokogiri::SyntaxError
def initialize message
self.message = message
end

###
# return true if this is a non error
def none?
Expand Down
27 changes: 27 additions & 0 deletions test/xml/test_syntax_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "helper"

module Nokogiri
module XML
class TestSyntaxError < Nokogiri::TestCase
def test_new
error = Nokogiri::XML::SyntaxError.new 'hello'
assert_equal 'hello', error.message
end

def test_exception
error = Nokogiri::XML::SyntaxError.new 'hello'
dup = error.exception 'world'
assert_equal 'hello', dup.message
assert_not_equal error.message.object_id, dup.message.object_id
end

def test_initialize_copy
error = Nokogiri::XML::SyntaxError.new 'hello'
dup = error.dup
assert_equal 'hello', dup.message
assert_not_equal error.object_id, dup.object_id
assert_not_equal error.message.object_id, dup.message.object_id
end
end
end
end

0 comments on commit 33922d7

Please sign in to comment.