[前][次][番号順一覧][スレッド一覧]

ruby-changes:13709

From: kazu <ko1@a...>
Date: Tue, 27 Oct 2009 08:12:09 +0900 (JST)
Subject: [ruby-changes:13709] Ruby:r25499 (trunk): * parse.y (ripper_filename): add Ripper#filename. * test/ripper/test_filter.rb: add more tests.

kazu	2009-10-27 08:11:59 +0900 (Tue, 27 Oct 2009)

  New Revision: 25499

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25499

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

  Modified files:
    trunk/parse.y
    trunk/test/ripper/test_filter.rb

Index: parse.y
===================================================================
--- parse.y	(revision 25498)
+++ parse.y	(revision 25499)
@@ -10388,6 +10388,24 @@
 
 /*
  *  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
  *
  *  Return line number of current parsing line.
@@ -10437,6 +10455,7 @@
     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);
Index: test/ripper/test_filter.rb
===================================================================
--- test/ripper/test_filter.rb	(revision 25498)
+++ test/ripper/test_filter.rb	(revision 25499)
@@ -23,31 +23,60 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]