Skip to content

Commit

Permalink
* parse.y (ripper_filename): add Ripper#filename. [ruby-dev:37856] * …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
znz committed Oct 26, 2009
1 parent 33c5abb commit ab77f00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
19 changes: 19 additions & 0 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
41 changes: 35 additions & 6 deletions test/ripper/test_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,60 @@ def filename
File.expand_path(__FILE__)
end

def test_filter_filename
def test_filter_filename_unset
data = {}
Filter.new(File.read(filename)).parse(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), 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

0 comments on commit ab77f00

Please sign in to comment.