1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* parse.y (ripper_filename): add Ripper#filename. [ruby-dev:37856] * test/ripper/test_filter.rb: add more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kazu 2009-10-26 23:11:59 +00:00
parent 33c5abbefa
commit ab77f00b04
2 changed files with 56 additions and 8 deletions

19
parse.y
View file

@ -10386,6 +10386,24 @@ ripper_column(VALUE self)
return LONG2NUM(col);
}
/*
* call-seq:
* ripper#filename -> String
*
* Return current parsing filename.
*/
static VALUE
ripper_filename(VALUE self)
{
struct parser_params *parser;
TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
if (!ripper_initialized_p(parser)) {
rb_raise(rb_eArgError, "method called for uninitialized object");
}
return parser->parser_ruby_sourcefile_string;
}
/*
* call-seq:
* ripper#lineno -> Integer
@ -10437,6 +10455,7 @@ Init_ripper(void)
rb_define_method(Ripper, "initialize", ripper_initialize, -1);
rb_define_method(Ripper, "parse", ripper_parse, 0);
rb_define_method(Ripper, "column", ripper_column, 0);
rb_define_method(Ripper, "filename", ripper_filename, 0);
rb_define_method(Ripper, "lineno", ripper_lineno, 0);
rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);

View file

@ -23,31 +23,60 @@ class TestRipper_Filter < Test::Unit::TestCase
File.expand_path(__FILE__)
end
def test_filter_filename_unset
data = {}
filter = Filter.new(File.read(filename))
filter.parse(data)
assert_equal('-', data[:filename], "[ruby-dev:37856]")
assert_equal('-', filter.filename)
end
def test_filter_filename
data = {}
Filter.new(File.read(filename)).parse(data)
assert_equal('-', data[:filename], "[ruby-dev:37856]")
data = {}
Filter.new(File.read(filename), filename).parse(data)
filter = Filter.new(File.read(filename), filename)
assert_equal(filename, filter.filename)
filter.parse(data)
assert_equal(filename, data[:filename])
assert_equal(filename, filter.filename)
end
def test_filter_lineno
data = {}
Filter.new(File.read(filename)).parse(data)
src = File.read(filename)
src_lines = src.count("\n")
filter = Filter.new(src)
assert_equal(nil, filter.lineno)
filter.parse(data)
assert_equal(1, data[:lineno])
assert_equal(src_lines, filter.lineno)
end
def test_filter_lineno_set
data = {}
src = File.read(filename)
src_lines = src.count("\n")
filter = Filter.new(src, '-', 100)
assert_equal(nil, filter.lineno)
filter.parse(data)
assert_equal(100, data[:lineno])
assert_equal(src_lines+100-1, filter.lineno)
end
def test_filter_column
data = {}
Filter.new(File.read(filename)).parse(data)
src = File.read(filename)
last_columns = src[/(.*)\Z/].size
filter = Filter.new(src)
assert_equal(nil, filter.column)
filter.parse(data)
assert_equal(0, data[:column])
assert_equal(last_columns, filter.column)
end
def test_filter_token
data = {}
Filter.new(File.read(filename)).parse(data)
filter = Filter.new(File.read(filename))
filter.parse(data)
assert_equal("begin", data[:token])
end
end if ripper_test