Skip to content

Commit

Permalink
remove ParseTree and sourcify dependencies in favor of new ruby2ruby/…
Browse files Browse the repository at this point in the history
…ruby_parser
  • Loading branch information
alexch committed Nov 16, 2012
1 parent e622ae7 commit d4cdd9d
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 120 deletions.
13 changes: 2 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
source :gemcutter
source :rubygems

gem "ruby_parser", ">= 3.0.0.a6"
gem "ruby2ruby", ">= 2.0.0.b1"
gem "sexp_processor"
gem "predicated", '~> 0.2.6'
gem "diff-lcs"

platforms :ruby do
gem "sourcify", '~> 0.4'
gem "file-tail", '~> 1.0' # Sourcify requires this but doesn't declare it
end
gemspec name: "wrong"

group :development, :test do
gem "rvm"
Expand Down
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Warning: currently the use of `alias_assert :expect` is **not** compatible with

## Algorithm ##

So wait a second. How do we do it? Doesn't Ruby have [poor support for AST introspection](http://blog.zenspider.com/2009/04/parsetree-eol.html)? Well, yes, it does, so we cheat: we figure out what file and line the assert block is defined in, then open the file, read the code, and parse it directly using Ryan Davis' amazing [RubyParser](http://parsetree.rubyforge.org/ruby_parser/) and [Ruby2Ruby](http://seattlerb.rubyforge.org/ruby2ruby/). You can bask in the kludge by examining `chunk.rb` and `assert.rb`. If you find some code it can't parse, please send it our way. As a failsafe we also use Sourcify, which has yet another home baked RACC parser, so we have many chances to parse your code.
So wait a second. How do we do it? Doesn't Ruby have [poor support for AST introspection](http://blog.zenspider.com/2009/04/parsetree-eol.html)? Well, yes, it does, so we cheat: we figure out what file and line the assert block is defined in, then open the file, read the code, and parse it directly using Ryan Davis' amazing [RubyParser](http://parsetree.rubyforge.org/ruby_parser/) and [Ruby2Ruby](http://seattlerb.rubyforge.org/ruby2ruby/). You can bask in the kludge by examining `chunk.rb` and `assert.rb`. If you find some code it can't parse, please send it our way.

Before you get your knickers in a twist about how this is totally unacceptable because it doesn't support this or that use case, here are our caveats and excuses:

Expand Down Expand Up @@ -464,7 +464,7 @@ If you're in Ruby 1.8, you **really** shouldn't do it! But if you do, you can us

## Bugs ##

* assert doesn't work (can't find the source code) from inside a "Dir.chdir" block
* see Github Issues <http://github.com/sconover/wrong/issues>

## todo ##

Expand Down
3 changes: 1 addition & 2 deletions lib/wrong/assert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "wrong/chunk"
require "wrong/config"
require "wrong/failure_message"
require "wrong/ruby2ruby_patch" # need to patch it after some other stuff loads
require "wrong/rainbow"

module Wrong
Expand Down Expand Up @@ -68,7 +67,7 @@ def aver(valence, explanation = nil, depth = 0, &block)
value = !value if valence == :deny
if value
if Wrong.config[:verbose]
code = Wrong::Chunk.from_block(block, depth + 2).code
code = Wrong::Chunk.from_block(block, depth + 2).code
if Wrong.config[:color]
explanation = explanation.color(:blue) if explanation
code = code.color(:green)
Expand Down
25 changes: 7 additions & 18 deletions lib/wrong/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ def require_optionally(library)
end
end

require_optionally "ParseTree"
require_optionally "sourcify"

require "wrong/config"
require "wrong/sexp_ext"
require "wrong/capturing"
Expand Down Expand Up @@ -60,21 +57,11 @@ def sexp
end

def build_sexp
sexp = begin
unless @block.nil? or @block.is_a?(String) or !Object.const_defined?(:Sourcify)
# first try sourcify
@block.to_sexp[3] # the [3] is to strip out the "proc {" sourcify adds to everything
end
rescue Exception => e
# sourcify failed, so fall through
end

# next try glomming
sexp ||= glom(if @file == "(irb)"
IRB.CurrentContext.all_lines
else
read_source_file(@file)
end)
glom(if @file == "(irb)"
IRB.CurrentContext.all_lines
else
read_source_file(@file)
end)
end

def read_source_file(file)
Expand Down Expand Up @@ -190,6 +177,8 @@ def build_details
require "wrong/rainbow" if Wrong.config[:color]
s = ""
parts = self.parts

parts.shift while parts.first == "()" # the parser adds this sometimes
parts.shift # remove the first part, since it's the same as the code

details = []
Expand Down
6 changes: 3 additions & 3 deletions lib/wrong/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.load_config
end
Config.new settings
end

def self.config
@config ||= load_config
end
Expand Down Expand Up @@ -72,8 +72,8 @@ def deny_method_names
self[:aliases][:deny]
end

def assert_methods
assert_method_names + deny_method_names
def hidden_methods
assert_method_names + deny_method_names + [:eventually]
end
end
end
1 change: 1 addition & 0 deletions lib/wrong/d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def d(*args, &block)

# look for a "d" inside the block
sexp.each_subexp do |subexp|
#sexp.deep_each do |subexp| # todo: try to use deep_each
if subexp.d?
sexp = subexp[3] # swap in the block part of the nested d call
end
Expand Down
37 changes: 0 additions & 37 deletions lib/wrong/ruby2ruby_patch.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/wrong/sexp_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def to_ruby

# visit every node in the tree, including the root, that is an Sexp
# todo: test
# todo: use deep_each instead
def each_subexp(include_root = true, &block)
yield self if include_root
each do |child|
Expand All @@ -26,7 +27,7 @@ def assertion?
self[0] == :iter and
self[1].is_a? Sexp and
self[1][0] == :call and
Wrong.config.assert_methods.include? self[1][2]
Wrong.config.hidden_methods.include? self[1][2]
end

def assertion
Expand Down
6 changes: 5 additions & 1 deletion test/adapters/rspec1/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
here = File.expand_path(File.dirname(__FILE__))

gem "rspec", "~> 1.3", :require => "spec"
gem "rvm"
gem "bundler"
gem "rake"

gemspec path: "#{here}/../../..", name: "wrong"

eval File.read("#{here}/../../../Gemfile")
6 changes: 5 additions & 1 deletion test/adapters/rspec2/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ gem "rspec", "~> 2.0"
gem "rspec-core", "~> 2.0"
gem "rspec-expectations", "~> 2.0"

eval File.read("#{here}/../../../Gemfile")
gem "rvm"
gem "bundler"
gem "rake"

gemspec path: "#{here}/../../..", name: "wrong"
6 changes: 5 additions & 1 deletion test/assert_advanced_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def assert_later(&p)
assert_later { x > 10 }
end

assert(e.message =~ /Expected \(x > 10\), but.*x is 10/m, e.message)
assert(e.message =~ /Expected assert_later { \(x > 10\) }, but.*x is 10/m, e.message)
end
end

Expand Down Expand Up @@ -48,4 +48,8 @@ def assert_later(&p)

# todo: test for finding it if you'd changed dirs into a parent or sibling or cousin dir

it "can compare two hashes" do
assert { {1=>2} == {1=>2} }
assert { {a:2} == {a:2} }
end
end
4 changes: 3 additions & 1 deletion test/chunk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def details(&block)

it "skips assignments" do
y = 14
d = details { x = 7; y }
d = details do
x = 7; y
end
assert d !~ /x = 7/
assert d =~ /y is 14/
end
Expand Down
61 changes: 27 additions & 34 deletions test/eventually_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# based on
# based on
# * https://gist.github.com/1228927
# * https://github.com/pivotal/selenium/blob/master/lib/selenium/wait_for.rb
# see
Expand Down Expand Up @@ -30,7 +30,7 @@ class << self
include Wrong::Helpers
include Wrong::D

# rolling our own mock clock and stubbing framework since we want these
# rolling our own mock clock and stubbing framework since we want these
# tests to run in MiniTest or in any version of RSpec

class ::Time
Expand All @@ -43,27 +43,27 @@ def now
def now= new_now
@now = new_now
end
end
end
end

def stub_it(receiver, method_name, &block)
receiver.singleton_class.send(:define_method, method_name, &block)
end

def unstub_it(receiver, method_name)
receiver.singleton_class.send(:remove_method, method_name)
end

before do
stub_it(self, :sleep) do |secs|
stub_it(self, :sleep) do |secs|
Time.now += secs
end
end

after do
unstub_it(self, :sleep)
end

it "requires a block" do
e = rescuing {
eventually
Expand All @@ -86,29 +86,25 @@ def unstub_it(receiver, method_name)
deny { e.nil? }
assert { Time.now == original_now + 5}
end

it "calls the block every 0.25 seconds" do
original_now = Time.now
original_now = Time.now
called_at = []
rescuing {
eventually {
eventually {
called_at << (Time.now - original_now)
false
}
}
assert { called_at.uniq == [
0.0, 0.25, 0.5, 0.75,
1.0, 1.25, 1.5, 1.75,
2.0, 2.25, 2.5, 2.75,
3.0, 3.25, 3.5, 3.75,
4.0, 4.25, 4.5, 4.75,
0.0, 0.25, 0.5, 0.75,
1.0, 1.25, 1.5, 1.75,
2.0, 2.25, 2.5, 2.75,
3.0, 3.25, 3.5, 3.75,
4.0, 4.25, 4.5, 4.75,
] }
end

it "puts the elapsed time in the exception message"
# assert { e.message =~ /\(after 5 sec\)$/}



it "returns after the condition is false for a while then true" do
original_now = Time.now
eventually {
Expand All @@ -121,19 +117,20 @@ def unstub_it(receiver, method_name)
end

it "raises a detailed Wrong exception if the result keeps being false" do
original_now = Time.now
e = rescuing do
eventually { false }
end
assert { e.message == "Expected false" }

x = 1
e = rescuing do
eventually { x + 2 == 4 }
eventually do
x + 2 == 4
end
end
assert { e.message == "Expected ((x + 2) == 4), but\n (x + 2) is 3\n x is 1\n" }
end

describe "if the block raises an exception" do
it "for 5 seconds, it raises that exception" do
original_now = Time.now
Expand All @@ -160,27 +157,23 @@ def unstub_it(receiver, method_name)
}
end
end

describe "passes a context hash to the block" do
it "that influences the error message"
end


describe "takes an options hash" do
it "that can change the timeout" do
original_now = Time.now
original_now = Time.now
rescuing {
eventually(:timeout => 2) { false }
}
assert {
Time.now == original_now + 2
Time.now == original_now + 2
}
end

it "that can change the delay" do
original_now = Time.now
original_now = Time.now
called_at = []
rescuing {
eventually(:delay => 1.5) {
eventually(:delay => 1.5) {
called_at << (Time.now - original_now)
false
}
Expand Down
2 changes: 0 additions & 2 deletions wrong-java.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- encoding: utf-8 -*-
loaded_gemspec = eval(File.read(File.expand_path('../wrong.gemspec', __FILE__)))
#loaded_gemspec.name = "wrong"
loaded_gemspec.platform = "java"
loaded_gemspec.dependencies.delete_if {|item| ["ParseTree", "sourcify", "file-tail"].include? item.name}
loaded_gemspec
Loading

0 comments on commit d4cdd9d

Please sign in to comment.