Skip to content

Commit

Permalink
use new versions of ruby_parser and ruby2ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
alexch committed Sep 18, 2012
1 parent 639f8bb commit dbb95a9
Show file tree
Hide file tree
Showing 52 changed files with 166 additions and 113 deletions.
11 changes: 4 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
source :gemcutter

gem "ruby_parser"
gem "ruby2ruby"
gem "ruby_parser", ">= 3.0.0.a6"
gem "ruby2ruby", ">= 2.0.0.b1"
gem "sexp_processor"
gem "predicated", '~> 0.2.1'
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

platforms :ruby_18 do
gem "ParseTree"
end

group :development, :test do
gem "rvm"
gem "bundler"
gem "rake"
gem "minitest", "~> 1.7.2"
Expand Down
5 changes: 3 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ And don't miss the [slideshare presentation](http://www.slideshare.net/alexchaff

## Helper methods

All these helper methods are provided if you do `include Wrong`.

### rescuing

There's also a convenience method for catching errors:
Expand Down Expand Up @@ -248,7 +250,6 @@ Wrong is compatible with RSpec and MiniTest::Spec, and probably Cucumber too, so

Here's an RSpec example:

require "wrong"
require "wrong/adapters/rspec"
Wrong.config.alias_assert :expect_that

Expand Down Expand Up @@ -276,7 +277,7 @@ So wait a second. How do we do it? Doesn't Ruby have [poor support for AST intro

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:

* It works! Tested in MRI 1.8.6, 1.8.7, 1.9.1, 1.9.2, and JRuby 1.5.3. (Thank you, [rvm](http://rvm.beginrescueend.com/)!)
* It works! Tested in MRI 1.8.6, 1.8.7, 1.9.1, 1.9.2, 1.9.3, and JRuby 1.5.3. (Thank you, [rvm](http://rvm.io/)!)
* Your code needs to be in a file.
* If you're developing Ruby code without saving it to a mounted disk, then sorry, Wrong is not right for you.
* We monkey-patch IRB so if you do `irb -rwrong` it'll save off your session in memory where Wrong can read it.
Expand Down
20 changes: 13 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ end
namespace :rvm do

# todo: use https://gist.github.com/674648 technique instead
$: << ENV["HOME"] + "/.rvm/lib"
# $: << ENV["HOME"] + "/.rvm/lib"
require 'rvm'

@rubies='1.8.6,1.8.7,1.9.1,1.9.2,1.9.3,jruby'

@rubies=['1.8.6',
'1.8.7',
'1.9.1-p378', # we can't use p429 or p431, see http://bugs.ruby-lang.org/issues/show/3584
'1.9.2',
'1.9.3',
'jruby']
@rubies_str = @rubies.join(', ')

def rvm
rvm = `which rvm`.strip
Expand All @@ -73,7 +79,7 @@ namespace :rvm do

def rvm_run(cmd, options = {})
options = {:bundle_check => true}.merge(options)
@rubies.split(',').each do |version|
@rubies.each do |version|
puts "\n== Using #{version}"
using = `#{rvm} #{version} exec true`
if using =~ /not installed/
Expand All @@ -100,7 +106,7 @@ namespace :rvm do
end
end

desc "run all tests with rvm in #{@rubies}"
desc "run all tests with rvm in #{@rubies_str}"
task :test do
rvm_run "bundle exec rake test"
rvm_run "ruby ./test/suite.rb"
Expand All @@ -110,12 +116,12 @@ namespace :rvm do

task :install => [:install_bundler, :install_gems]

desc "run 'gem install bundler' with rvm in each of #{@rubies}"
desc "run 'gem install bundler' with rvm in each of #{@rubies_str}"
task :install_bundler do
rvm_run("gem install bundler", :bundle_check => false)
end

desc "run 'bundle install' with rvm in each of #{@rubies}"
desc "run 'bundle install' with rvm in each of #{@rubies_str}"
task :install_gems do
rvm_run("bundle install", :bundle_check => false)
rvm_run("bundle install --gemfile=#{File.dirname __FILE__}/test/adapters/rspec1/Gemfile", :bundle_check => false)
Expand Down
24 changes: 17 additions & 7 deletions lib/wrong.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,36 @@
require "wrong/message/string_comparison"
require "wrong/eventually"

# After doing "require 'wrong'",
# if you `include Wrong` you will get all of Wrong's asserts and helpers,
# available from both instance and class methods of the object you included it in.
#
# If you only want some of them, then don't "require 'wrong'", and instead
# `require` and `include` just what you want separately.
#
# For example, if you only want `eventually`, then do
# require "wrong/eventually"
# include Wrong::Eventually
#
module Wrong
include Wrong::Assert
extend Wrong::Assert
include Wrong::Helpers
extend Wrong::Helpers
include Wrong::Eventually
extend Wrong::Eventually
end

# this does some magic; if you don't like it...

# ...`require 'wrong/assert'` et al. individually and don't `require 'wrong/close_to'` or `require 'wrong'`
# This `require "wrong/close_to"` adds close_to? to Numeric.
# If you don't like that, then
# `require 'wrong/assert'` et al. individually and don't `require 'wrong/close_to'` or `require 'wrong'`
require "wrong/close_to"

# ...don't `require 'wrong'`, and `include Wrong::D` only in the modules you want to call `d` from
# This makes the `d` method available everywhere.
# If you don't like that, then
# don't `require 'wrong'`, and `include Wrong::D` only in the modules you want to call `d` from
class Object # should we add this to Kernel instead?
include Wrong::D
end

# ...don't `require 'wrong'`
# this part isn't working yet -- it's supposed to make 'assert' available at the top level but it breaks the minitest adapter
# include Wrong
# class Object
Expand Down
56 changes: 56 additions & 0 deletions lib/wrong/capturing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Wrong
module Capturing

# Usage:
# capturing { puts "hi" } => "hi\n"
# capturing(:stderr) { $stderr.puts "hi" } => "hi\n"
# out, err = capturing(:stdout, :stderr) { ... }
#
# see http://www.justskins.com/forums/closing-stderr-105096.html for more explanation
def capturing(*streams)
streams = [:stdout] if streams.empty?
original = {}
captured = {}

# reassign the $ variable (which is used by well-behaved code e.g. puts)
streams.each do |stream|
original[stream] = (stream == :stdout ? $stdout : $stderr)
captured[stream] = StringIO.new
reassign_stream(stream, captured)
end

yield

# return either one string, or an array of two strings
if streams.size == 1
captured[streams.first].string
else
[captured[streams[0]].string, captured[streams[1]].string]
end

ensure

streams.each do |stream|
# bail if stream was reassigned inside the block
if (stream == :stdout ? $stdout : $stderr) != captured[stream]
raise "#{stream} was reassigned while being captured"
end
# support nested calls to capturing
original[stream] << captured[stream].string if original[stream].is_a? StringIO
reassign_stream(stream, original)
end
end

private
def reassign_stream(which, streams)
case which
when :stdout
$stdout = streams[which]
when :stderr
$stderr = streams[which]
end
end

end

end
22 changes: 14 additions & 8 deletions lib/wrong/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def require_optionally(library)

require "wrong/config"
require "wrong/sexp_ext"
require "wrong/capturing"

module Wrong
class Chunk
Expand All @@ -35,6 +36,8 @@ def self.from_block(block, depth = 0)
new(file, line, &block)
end

include Capturing

attr_reader :file, :line_number, :block

# line parameter is 1-based
Expand Down Expand Up @@ -92,7 +95,10 @@ def glom(source)
while sexp.nil? && line_index + c < lines.size
begin
@chunk = lines[line_index..line_index+c].join("\n")
sexp = @parser.parse(@chunk)

capturing(:stderr) do # new RubyParser is too loud
sexp = @parser.parse(@chunk)
end
rescue Racc::ParseError => e
# loop and try again
c += 1
Expand Down Expand Up @@ -161,7 +167,7 @@ def parts(sexp = nil)
def details
@details ||= build_details
end

def pretty_value(value, starting_col = 0, indent_wrapped_lines = 6, width = Chunk.terminal_width)
# inspected = value.inspect

Expand All @@ -175,7 +181,7 @@ def pretty_value(value, starting_col = 0, indent_wrapped_lines = 6, width = Chun
else
indented
end
end
end

private

Expand Down Expand Up @@ -241,7 +247,7 @@ def newline(indent)
def indent_all(amount, s)
s.gsub("\n", "\n#{indent(amount)}")
end

def wrap_and_indent(indented, starting_col, indent_wrapped_lines, full_width)
first_line = true
width = full_width - starting_col # the first line is essentially shorter
Expand Down Expand Up @@ -277,22 +283,22 @@ def self.terminal_size
end
rescue
nil
end
end
end

def self.terminal_width
(@terminal_width ||= nil) || (terminal_size && terminal_size.first) || 80
end

def self.terminal_width= forced_with
@terminal_width = forced_with
end

# Determines if a shell command exists by searching for it in ENV['PATH'].
def self.command_exists?(command)
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
end


end

Expand Down
18 changes: 9 additions & 9 deletions lib/wrong/failure_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def match?
false
end
end


attr_accessor :chunk, :valence, :explanation

def initialize(chunk, valence, explanation)
Expand All @@ -50,32 +50,32 @@ def initialize(chunk, valence, explanation)
def basic
"#{valence == :deny ? "Didn't expect" : "Expected"} #{colored(:blue, chunk.code)}"
end

def full
message = ""
message << "#{explanation}: " if explanation
message << basic

formatted_message = if predicate && !(predicate.is_a? Predicated::Conjunction)
if formatter = FailureMessage.formatter_for(predicate)
colored(:bold, formatter.describe)
end
end

unless chunk.details.empty? and formatted_message.nil?
message << ", but"
end

message << formatted_message if formatted_message
message << chunk.details unless chunk.details.empty?
message
end

protected
protected
def code
@code ||= chunk.code
end

def predicate
@predicate ||= begin
Predicated::Predicate.from_ruby_code_string(code, chunk.block.binding)
Expand All @@ -85,7 +85,7 @@ def predicate
nil
end
end

def colored(color, text)
if Wrong.config[:color]
if color == :bold
Expand Down
Loading

0 comments on commit dbb95a9

Please sign in to comment.