Permalink
Browse files

fix bug in Context

  • Loading branch information...
Sven Fuchs
Sven Fuchs committed Jul 5, 2009
1 parent 2b11ebe commit 5f73f6ba5220e39d401b1dd61edb2665b84b518e
Showing with 75 additions and 62 deletions.
  1. +6 −6 lib/ruby/node/text.rb
  2. +64 −54 test/context_test.rb
  3. +3 −1 test/nodes/block_test.rb
  4. +2 −1 test/test_helper.rb
View
@@ -10,15 +10,15 @@ def split(str)
end
class Context
- @@context_width = 2
+ @@width = 2
class << self
- def context_width
- @@context_width
+ def width
+ @@width
end
- def context_width=(context_width)
- @@context_width = context_width
+ def width=(width)
+ @@width = width
end
end
@@ -71,7 +71,7 @@ def head
end
def tail
- lines[self.end.row][self.end.col..-1] + lines[(self.end.row + 1)..-1].join
+ lines[self.end.row][self.end.col..-1].to_s + lines[(self.end.row + 1)..-1].join
end
def src
View
@@ -1,54 +1,64 @@
-# require File.dirname(__FILE__) + '/test_helper'
-# require 'highlighters/ansi'
-#
-# class ContextTest < Test::Unit::TestCase
-# def setup
-# Ruby.context_width = 2
-# end
-#
-# def build(code)
-# Ripper::RubyBuilder.build(code)
-# end
-#
-# def lines(range)
-# range.to_a.map { |i| " call_#{i}(:a)" }.join("\n")
-# end
-#
-# define_method :"test context returns 5 lines" do
-# program = build(lines(1..10))
-# line = program.statements.select { |s| s.to_ruby == 'call_4(:a)' }[0]
-# assert_equal lines(2..6), line.context
-# end
-#
-# define_method :"test context returns 3 lines when no preceeding lines present" do
-# program = build(lines(1..10))
-# line = program.statements.select { |s| s.to_ruby == 'call_1(:a)' }[0]
-# assert_equal lines(1..3), line.context
-# end
-#
-# define_method :"test context returns 4 lines when only one preceeding line present" do
-# program = build(lines(1..10))
-# line = program.statements.select { |s| s.to_ruby == 'call_2(:a)' }[0]
-# assert_equal lines(1..4), line.context
-# end
-#
-# define_method :"test context returns 4 lines when only one succeeding line present" do
-# program = build(lines(1..10))
-# line = program.statements.select { |s| s.to_ruby == 'call_9(:a)' }[0]
-# assert_equal lines(7..10), line.context
-# end
-#
-# define_method :"test context returns 3 lines when no succeeding lines present" do
-# program = build(lines(1..10))
-# line = program.statements.select { |s| s.to_ruby == 'call_10(:a)' }[0]
-# assert_equal lines(8..10), line.context
-# end
-#
-# define_method :"test context highlights elements" do
-# program = build(lines(1..10))
-# highlighter = Highlighters::Ansi.new(:red, :bold)
-# line = program.statements.select { |s| s.to_ruby == 'call_4(:a)' }[0]
-# assert_equal " \e[0;31;1mcall_4(:a)\e[0m", line.context(:highlight => highlighter, :width => 0)
-# end
-#
-# end
+require File.dirname(__FILE__) + '/test_helper'
+require 'highlighters/ansi'
+
+class ContextTest < Test::Unit::TestCase
+ def setup
+ Ruby::Node::Text::Context.width = 2
+ end
+
+ def build(code)
+ Ripper::RubyBuilder.build(code)
+ end
+
+ def lines(range)
+ range.to_a.map { |i| " call_#{i}(:a)" }.join("\n")
+ end
+
+ define_method :"test context returns 5 lines" do
+ code = build(lines(1..10))
+ line = code.statements.select { |s| s.to_ruby == 'call_4(:a)' }[0]
+ assert_equal lines(2..6), line.context
+ end
+
+ define_method :"test context returns 3 lines when no preceeding lines present" do
+ code = build(lines(1..10))
+ line = code.statements.select { |s| s.to_ruby == 'call_1(:a)' }[0]
+ assert_equal lines(1..3), line.context
+ end
+
+ define_method :"test context returns 4 lines when only one preceeding line present" do
+ code = build(lines(1..10))
+ line = code.statements.select { |s| s.to_ruby == 'call_2(:a)' }[0]
+ assert_equal lines(1..4), line.context
+ end
+
+ define_method :"test context returns 4 lines when only one succeeding line present" do
+ code = build(lines(1..10))
+ line = code.statements.select { |s| s.to_ruby == 'call_9(:a)' }[0]
+ assert_equal lines(7..10), line.context
+ end
+
+ define_method :"test context returns 3 lines when no succeeding lines present" do
+ code = build(lines(1..10))
+ line = code.statements.select { |s| s.to_ruby == 'call_10(:a)' }[0]
+ assert_equal lines(8..10), line.context
+ end
+
+ define_method :"test context highlights elements" do
+ code = build(lines(1..10))
+ highlighter = Highlighters::Ansi.new(:red, :bold)
+ line = code.statements.select { |s| s.to_ruby == 'call_4(:a)' }[0]
+ assert_equal " \e[0;31;1mcall_4(:a)\e[0m", line.context(:highlight => highlighter, :width => 0)
+ end
+
+ define_method :"test multiline method call" do
+ src = <<-src
+ t(:'adva.assets.upload_summary', :count => @site.assets.count,
+ :size => number_to_human_size(@site.assets.sum(:data_file_size) || 0))
+ src
+ call = build(src).select(Ruby::Call, :token => 't').first
+ assert_nothing_raised { call.context }
+ end
+
+
+end
View
@@ -75,7 +75,9 @@ class BlockTest < Test::Unit::TestCase
define_method :"test: a begin block with rescue without error_type" do
src = "begin\nfoo\nrescue\nbar\nend"
- assert_build(src)
+ assert_build(src) do |code|
+ assert code.select(Ruby::Assoc).empty?, "should not add an Assoc to the params list"
+ end
end
define_method :"test: a begin block with rescue without an error_type" do
View
@@ -1,4 +1,4 @@
-$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
+$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'ripper/ruby_builder'
require 'ripper/event_log'
@@ -25,6 +25,7 @@ def assert_build(src)
assert_equal src, expr.src(true)
# expr.all_nodes.each { |node| assert_equal Ruby::Program, node.root.class }
yield(expr) if block_given?
+ expr
end
def assert_node(node, assertions)

0 comments on commit 5f73f6b

Please sign in to comment.