* ext/psych/parser.c (parse): add the file name to the exception when

parse errors occur.
* test/psych/test_parser.rb: test for parse error file name

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30626 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tenderlove 2011-01-22 01:26:40 +00:00
Родитель 0331314d27
Коммит 8dd3a4af66
3 изменённых файлов: 34 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Sat Jan 22 10:25:19 2011 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/psych/parser.c (parse): add the file name to the exception when
parse errors occur.
* test/psych/test_parser.rb: test for parse error file name
Sat Jan 22 10:12:30 2011 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/psych/parser.c (parse): fix assertion error when reusing a

Просмотреть файл

@ -4,6 +4,7 @@ VALUE cPsychParser;
VALUE ePsychSyntaxError;
static ID id_read;
static ID id_path;
static ID id_empty;
static ID id_start_stream;
static ID id_end_stream;
@ -93,13 +94,20 @@ static VALUE parse(VALUE self, VALUE yaml)
while(!done) {
if(!yaml_parser_parse(parser, &event)) {
VALUE path;
size_t line = parser->mark.line;
size_t column = parser->mark.column;
if(rb_respond_to(yaml, id_path))
path = rb_funcall(yaml, id_path, 0);
else
path = rb_str_new2("<unknown>");
yaml_parser_delete(parser);
yaml_parser_initialize(parser);
rb_raise(ePsychSyntaxError, "couldn't parse YAML at line %d column %d",
rb_raise(ePsychSyntaxError, "(%s): couldn't parse YAML at line %d column %d",
StringValuePtr(path),
(int)line, (int)column);
}
@ -360,6 +368,7 @@ void Init_psych_parser()
rb_define_method(cPsychParser, "external_encoding=", set_external_encoding, 1);
id_read = rb_intern("read");
id_path = rb_intern("path");
id_empty = rb_intern("empty");
id_start_stream = rb_intern("start_stream");
id_end_stream = rb_intern("end_stream");

Просмотреть файл

@ -138,6 +138,23 @@ module Psych
end
end
def test_syntax_error_has_path_for_string
e = assert_raises(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
end
assert_match '(<unknown>):', e.message
end
def test_syntax_error_has_path_for_io
io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
def io.path; "hello!"; end
e = assert_raises(Psych::SyntaxError) do
@parser.parse(io)
end
assert_match "(#{io.path}):", e.message
end
def test_mapping_end
@parser.parse("---\n!!map { key: value }")
assert_called :end_mapping