Permalink
Browse files

Allow embedded documents in incomplete expressions [Fixes pry#622]

Also import complete_expression? tests from Pry.
1 parent d2b25cd commit 0dbc9344dfa5e94d52ebb3d79d3d43b35434a74e @ConradIrwin ConradIrwin committed Jul 2, 2012
Showing with 45 additions and 2 deletions.
  1. +1 −1 Rakefile
  2. +2 −0 lib/method_source/code_helpers.rb
  3. +1 −1 lib/method_source/version.rb
  4. +41 −0 test/test_code_helpers.rb
View
@@ -29,7 +29,7 @@ def apply_spec_defaults(s)
end
task :test do
- sh "bacon -q #{direc}/test/test.rb"
+ sh "bacon -q #{direc}/test/test.rb #{direc}/test/test_code_helpers.rb"
end
desc "reinstall gem"
@@ -123,8 +123,10 @@ def extract_last_comment(lines)
# fixed by adding more input to the buffer.
module IncompleteExpression
def self.===(ex)
+ return false unless SyntaxError === ex
case ex.message
when /unexpected (\$end|end-of-file|END_OF_FILE)/, # mri, jruby, ironruby
+ /embedded document meets end of file/, # =begin
/unterminated (quoted string|string|regexp) meets end of file/, # "quoted string" is ironruby
/missing 'end' for/, /: expecting '[})\]]'$/, /can't find string ".*" anywhere before EOF/, /: expecting keyword_end/, /expecting kWHEN/ # rbx
true
@@ -1,3 +1,3 @@
module MethodSource
- VERSION = "0.8.pre.3"
+ VERSION = "0.8.pre.4"
end
View
@@ -0,0 +1,41 @@
+describe MethodSource::CodeHelpers do
+ before do
+ @tester = Object.new.extend(MethodSource::CodeHelpers)
+ end
+
+ [
+ ["p = '", "'"],
+ ["def", "a", "(); end"],
+ ["p = <<FOO", "lots", "and", "lots of", "foo", "FOO"],
+ ["[", ":lets,", "'list',", "[/nested/", "], things ]"],
+ ["abc =~ /hello", "/"],
+ ["issue = %W/", "343/"],
+ ["pouts(<<HI, 'foo", "bar", "HI", "baz')"],
+ ["=begin", "no-one uses this syntax anymore...", "=end"],
+ ["puts 1, 2,", "3"],
+ ["puts 'hello'\\", "'world'"]
+ ].each do |lines|
+ it "should not raise an error on broken lines: #{lines.join("\\n")}" do
+ 1.upto(lines.size - 1) do |i|
+ @tester.complete_expression?(lines[0...i].join("\n") + "\n").should == false
+ end
+ @tester.complete_expression?(lines.join("\n")).should == true
+ end
+ end
+
+ [
+ ["end"],
+ ["puts )("],
+ ["1 1"],
+ ["puts :"]
+ ] + (RbConfig::CONFIG['ruby_install_name'] == 'rbx' ? [] : [
+ ["def", "method(1"], # in this case the syntax error is "expecting ')'".
+ ["o = Object.new.tap{ def o.render;","'MEH'", "}"] # in this case the syntax error is "expecting keyword_end".
+ ]).compact.each do |foo|
+ it "should raise an error on invalid syntax like #{foo.inspect}" do
+ lambda{
+ @tester.complete_expression?(foo.join("\n"))
+ }.should.raise(SyntaxError)
+ end
+ end
+end

0 comments on commit 0dbc934

Please sign in to comment.