summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConrad Irwin <conrad.irwin@gmail.com>2012-07-02 00:31:32 -0700
committerConrad Irwin <conrad.irwin@gmail.com>2012-07-02 00:33:45 -0700
commit0dbc9344dfa5e94d52ebb3d79d3d43b35434a74e (patch)
treeb8e12136c55e71dc44ac5a3f5a1fc422934fd8d9
parentd2b25cdbbed2f8539fe5f11e5dcf180fc1f3fce8 (diff)
downloadmethod_source-0dbc9344dfa5e94d52ebb3d79d3d43b35434a74e.tar.gz
Allow embedded documents in incomplete expressions [Fixes pry#622]
Also import complete_expression? tests from Pry.
-rw-r--r--Rakefile2
-rw-r--r--lib/method_source/code_helpers.rb2
-rw-r--r--lib/method_source/version.rb2
-rw-r--r--test/test_code_helpers.rb41
4 files changed, 45 insertions, 2 deletions
diff --git a/Rakefile b/Rakefile
index 4268722..687d3e1 100644
--- a/Rakefile
+++ b/Rakefile
@@ -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"
diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb
index a3d2a71..b3c33de 100644
--- a/lib/method_source/code_helpers.rb
+++ b/lib/method_source/code_helpers.rb
@@ -123,8 +123,10 @@ module MethodSource
# 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
diff --git a/lib/method_source/version.rb b/lib/method_source/version.rb
index f699eb0..cd55cf3 100644
--- a/lib/method_source/version.rb
+++ b/lib/method_source/version.rb
@@ -1,3 +1,3 @@
module MethodSource
- VERSION = "0.8.pre.3"
+ VERSION = "0.8.pre.4"
end
diff --git a/test/test_code_helpers.rb b/test/test_code_helpers.rb
new file mode 100644
index 0000000..ba83a63
--- /dev/null
+++ b/test/test_code_helpers.rb
@@ -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