banister / method_source

return the sourcecode for a method
MIT License
361 stars 43 forks source link

SourceNotFoundError on blocks defined in larger expressions #22

Open searls opened 11 years ago

searls commented 11 years ago

I had low expectations of method_source helping me, so I might be way out of bounds here.

I'm writing a little gem called arg-that which provides a method that takes a block. To give better test messages, I was hoping to use method_source (or similar) to print the content of the arg_that blocks.

Here's a little example:

    expect(
      :a => 1,
      :b => 99
    ).to eqish(
      :a => 1,
      :b => arg_that {|arg| arg > 98 && arg < 100 }
    )

In the above, calling #inspect on the block passed to arg_that will raise the following:

Failure/Error: Unable to find matching line from backtrace
     MethodSource::SourceNotFoundError:
       Could not parse source for #<Proc:0x007ffc8bc73898@/Volumes/Macintosh HD/Users/justin/code/vagrants/oss/projects/ruby/arg_that/spec/arg_that_spec.rb:42>: (eval):2: syntax error, unexpected tASSOC, expecting $end
                 :b => arg_that {|arg| arg > 98 && arg < 100 }
                      ^
     # ./lib/arg_that/that_arg.rb:13:in `inspect'
     # ./lib/arg_that/eqish.rb:19:in `block (2 levels) in <top (required)>'