Skip to content

Commit d4cdd9d

Browse files
committed
remove ParseTree and sourcify dependencies in favor of new ruby2ruby/ruby_parser
1 parent e622ae7 commit d4cdd9d

15 files changed

+64
-120
lines changed

Gemfile

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
source :gemcutter
1+
source :rubygems
22

3-
gem "ruby_parser", ">= 3.0.0.a6"
4-
gem "ruby2ruby", ">= 2.0.0.b1"
5-
gem "sexp_processor"
6-
gem "predicated", '~> 0.2.6'
7-
gem "diff-lcs"
8-
9-
platforms :ruby do
10-
gem "sourcify", '~> 0.4'
11-
gem "file-tail", '~> 1.0' # Sourcify requires this but doesn't declare it
12-
end
3+
gemspec name: "wrong"
134

145
group :development, :test do
156
gem "rvm"

README.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ Warning: currently the use of `alias_assert :expect` is **not** compatible with
275275

276276
## Algorithm ##
277277

278-
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.
278+
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.
279279

280280
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:
281281

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

465465
## Bugs ##
466466

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

469469
## todo ##
470470

lib/wrong/assert.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
require "wrong/chunk"
66
require "wrong/config"
77
require "wrong/failure_message"
8-
require "wrong/ruby2ruby_patch" # need to patch it after some other stuff loads
98
require "wrong/rainbow"
109

1110
module Wrong
@@ -68,7 +67,7 @@ def aver(valence, explanation = nil, depth = 0, &block)
6867
value = !value if valence == :deny
6968
if value
7069
if Wrong.config[:verbose]
71-
code = Wrong::Chunk.from_block(block, depth + 2).code
70+
code = Wrong::Chunk.from_block(block, depth + 2).code
7271
if Wrong.config[:color]
7372
explanation = explanation.color(:blue) if explanation
7473
code = code.color(:green)

lib/wrong/chunk.rb

+7-18
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ def require_optionally(library)
1111
end
1212
end
1313

14-
require_optionally "ParseTree"
15-
require_optionally "sourcify"
16-
1714
require "wrong/config"
1815
require "wrong/sexp_ext"
1916
require "wrong/capturing"
@@ -60,21 +57,11 @@ def sexp
6057
end
6158

6259
def build_sexp
63-
sexp = begin
64-
unless @block.nil? or @block.is_a?(String) or !Object.const_defined?(:Sourcify)
65-
# first try sourcify
66-
@block.to_sexp[3] # the [3] is to strip out the "proc {" sourcify adds to everything
67-
end
68-
rescue Exception => e
69-
# sourcify failed, so fall through
70-
end
71-
72-
# next try glomming
73-
sexp ||= glom(if @file == "(irb)"
74-
IRB.CurrentContext.all_lines
75-
else
76-
read_source_file(@file)
77-
end)
60+
glom(if @file == "(irb)"
61+
IRB.CurrentContext.all_lines
62+
else
63+
read_source_file(@file)
64+
end)
7865
end
7966

8067
def read_source_file(file)
@@ -190,6 +177,8 @@ def build_details
190177
require "wrong/rainbow" if Wrong.config[:color]
191178
s = ""
192179
parts = self.parts
180+
181+
parts.shift while parts.first == "()" # the parser adds this sometimes
193182
parts.shift # remove the first part, since it's the same as the code
194183

195184
details = []

lib/wrong/config.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def self.load_config
1010
end
1111
Config.new settings
1212
end
13-
13+
1414
def self.config
1515
@config ||= load_config
1616
end
@@ -72,8 +72,8 @@ def deny_method_names
7272
self[:aliases][:deny]
7373
end
7474

75-
def assert_methods
76-
assert_method_names + deny_method_names
75+
def hidden_methods
76+
assert_method_names + deny_method_names + [:eventually]
7777
end
7878
end
7979
end

lib/wrong/d.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def d(*args, &block)
2121

2222
# look for a "d" inside the block
2323
sexp.each_subexp do |subexp|
24+
#sexp.deep_each do |subexp| # todo: try to use deep_each
2425
if subexp.d?
2526
sexp = subexp[3] # swap in the block part of the nested d call
2627
end

lib/wrong/ruby2ruby_patch.rb

-37
This file was deleted.

lib/wrong/sexp_ext.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def to_ruby
1212

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

3233
def assertion

test/adapters/rspec1/Gemfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
here = File.expand_path(File.dirname(__FILE__))
22

33
gem "rspec", "~> 1.3", :require => "spec"
4+
gem "rvm"
5+
gem "bundler"
6+
gem "rake"
7+
8+
gemspec path: "#{here}/../../..", name: "wrong"
49

5-
eval File.read("#{here}/../../../Gemfile")

test/adapters/rspec2/Gemfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ gem "rspec", "~> 2.0"
44
gem "rspec-core", "~> 2.0"
55
gem "rspec-expectations", "~> 2.0"
66

7-
eval File.read("#{here}/../../../Gemfile")
7+
gem "rvm"
8+
gem "bundler"
9+
gem "rake"
10+
11+
gemspec path: "#{here}/../../..", name: "wrong"

test/assert_advanced_test.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def assert_later(&p)
1616
assert_later { x > 10 }
1717
end
1818

19-
assert(e.message =~ /Expected \(x > 10\), but.*x is 10/m, e.message)
19+
assert(e.message =~ /Expected assert_later { \(x > 10\) }, but.*x is 10/m, e.message)
2020
end
2121
end
2222

@@ -48,4 +48,8 @@ def assert_later(&p)
4848

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

51+
it "can compare two hashes" do
52+
assert { {1=>2} == {1=>2} }
53+
assert { {a:2} == {a:2} }
54+
end
5155
end

test/chunk_test.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def details(&block)
273273

274274
it "skips assignments" do
275275
y = 14
276-
d = details { x = 7; y }
276+
d = details do
277+
x = 7; y
278+
end
277279
assert d !~ /x = 7/
278280
assert d =~ /y is 14/
279281
end

test/eventually_test.rb

+27-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# based on
1+
# based on
22
# * https://gist.github.com/1228927
33
# * https://github.com/pivotal/selenium/blob/master/lib/selenium/wait_for.rb
44
# see
@@ -30,7 +30,7 @@ class << self
3030
include Wrong::Helpers
3131
include Wrong::D
3232

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

3636
class ::Time
@@ -43,27 +43,27 @@ def now
4343
def now= new_now
4444
@now = new_now
4545
end
46-
end
46+
end
4747
end
4848

4949
def stub_it(receiver, method_name, &block)
5050
receiver.singleton_class.send(:define_method, method_name, &block)
5151
end
52-
52+
5353
def unstub_it(receiver, method_name)
5454
receiver.singleton_class.send(:remove_method, method_name)
5555
end
5656

5757
before do
58-
stub_it(self, :sleep) do |secs|
58+
stub_it(self, :sleep) do |secs|
5959
Time.now += secs
6060
end
6161
end
62-
62+
6363
after do
6464
unstub_it(self, :sleep)
6565
end
66-
66+
6767
it "requires a block" do
6868
e = rescuing {
6969
eventually
@@ -86,29 +86,25 @@ def unstub_it(receiver, method_name)
8686
deny { e.nil? }
8787
assert { Time.now == original_now + 5}
8888
end
89-
89+
9090
it "calls the block every 0.25 seconds" do
91-
original_now = Time.now
91+
original_now = Time.now
9292
called_at = []
9393
rescuing {
94-
eventually {
94+
eventually {
9595
called_at << (Time.now - original_now)
9696
false
9797
}
9898
}
9999
assert { called_at.uniq == [
100-
0.0, 0.25, 0.5, 0.75,
101-
1.0, 1.25, 1.5, 1.75,
102-
2.0, 2.25, 2.5, 2.75,
103-
3.0, 3.25, 3.5, 3.75,
104-
4.0, 4.25, 4.5, 4.75,
100+
0.0, 0.25, 0.5, 0.75,
101+
1.0, 1.25, 1.5, 1.75,
102+
2.0, 2.25, 2.5, 2.75,
103+
3.0, 3.25, 3.5, 3.75,
104+
4.0, 4.25, 4.5, 4.75,
105105
] }
106106
end
107-
108-
it "puts the elapsed time in the exception message"
109-
# assert { e.message =~ /\(after 5 sec\)$/}
110-
111-
107+
112108
it "returns after the condition is false for a while then true" do
113109
original_now = Time.now
114110
eventually {
@@ -121,19 +117,20 @@ def unstub_it(receiver, method_name)
121117
end
122118

123119
it "raises a detailed Wrong exception if the result keeps being false" do
124-
original_now = Time.now
125120
e = rescuing do
126121
eventually { false }
127122
end
128123
assert { e.message == "Expected false" }
129-
124+
130125
x = 1
131126
e = rescuing do
132-
eventually { x + 2 == 4 }
127+
eventually do
128+
x + 2 == 4
129+
end
133130
end
134131
assert { e.message == "Expected ((x + 2) == 4), but\n (x + 2) is 3\n x is 1\n" }
135132
end
136-
133+
137134
describe "if the block raises an exception" do
138135
it "for 5 seconds, it raises that exception" do
139136
original_now = Time.now
@@ -160,27 +157,23 @@ def unstub_it(receiver, method_name)
160157
}
161158
end
162159
end
163-
164-
describe "passes a context hash to the block" do
165-
it "that influences the error message"
166-
end
167-
160+
168161
describe "takes an options hash" do
169162
it "that can change the timeout" do
170-
original_now = Time.now
163+
original_now = Time.now
171164
rescuing {
172165
eventually(:timeout => 2) { false }
173166
}
174167
assert {
175-
Time.now == original_now + 2
168+
Time.now == original_now + 2
176169
}
177170
end
178-
171+
179172
it "that can change the delay" do
180-
original_now = Time.now
173+
original_now = Time.now
181174
called_at = []
182175
rescuing {
183-
eventually(:delay => 1.5) {
176+
eventually(:delay => 1.5) {
184177
called_at << (Time.now - original_now)
185178
false
186179
}

wrong-java.gemspec

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# -*- encoding: utf-8 -*-
22
loaded_gemspec = eval(File.read(File.expand_path('../wrong.gemspec', __FILE__)))
3-
#loaded_gemspec.name = "wrong"
43
loaded_gemspec.platform = "java"
5-
loaded_gemspec.dependencies.delete_if {|item| ["ParseTree", "sourcify", "file-tail"].include? item.name}
64
loaded_gemspec

0 commit comments

Comments
 (0)