This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Add RubyFeatures.ripper_supported? #245
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'benchmark/ips' | ||
require 'ripper' | ||
|
||
ruby_version = defined?(JRUBY_VERSION) ? JRUBY_VERSION : RUBY_VERSION | ||
puts "#{RUBY_ENGINE} #{ruby_version}" | ||
|
||
source = File.read(__FILE__) | ||
|
||
Benchmark.ips do |x| | ||
x.report("Ripper") do | ||
Ripper.sexp(source) | ||
Ripper.lex(source) | ||
end | ||
end | ||
|
||
__END__ | ||
|
||
ruby 1.9.3 | ||
Calculating ------------------------------------- | ||
Ripper 284.000 i/100ms | ||
|
||
ruby 2.2.3 | ||
Calculating ------------------------------------- | ||
Ripper 320.000 i/100ms | ||
|
||
jruby 1.7.5 | ||
Calculating ------------------------------------- | ||
Ripper 24.000 i/100ms | ||
|
||
jruby 1.7.13 | ||
Calculating ------------------------------------- | ||
Ripper 25.000 i/100ms | ||
|
||
jruby 1.7.14 | ||
Calculating ------------------------------------- | ||
Ripper 239.000 i/100ms | ||
|
||
jruby 1.7.22 | ||
Calculating ------------------------------------- | ||
Ripper 231.000 i/100ms | ||
|
||
jruby 9.0.1.0 | ||
Calculating ------------------------------------- | ||
Ripper 218.000 i/100ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module RSpec | ||
module Support | ||
# @private | ||
class ComparableVersion | ||
include Comparable | ||
|
||
attr_reader :string | ||
|
||
def initialize(string) | ||
@string = string | ||
end | ||
|
||
def <=>(other) | ||
other = self.class.new(other) unless other.is_a?(self.class) | ||
|
||
return 0 if string == other.string | ||
|
||
longer_segment_count = [self, other].map { |version| version.segments.count }.max | ||
|
||
longer_segment_count.times do |index| | ||
self_segment = segments[index] || 0 | ||
other_segment = other.segments[index] || 0 | ||
|
||
if self_segment.class == other_segment.class | ||
result = self_segment <=> other_segment | ||
return result unless result == 0 | ||
else | ||
return self_segment.is_a?(String) ? -1 : 1 | ||
end | ||
end | ||
|
||
0 | ||
end | ||
|
||
def segments | ||
@segments ||= string.scan(/[a-z]+|\d+/i).map do |segment| | ||
if segment =~ /\A\d+\z/ | ||
segment.to_i | ||
else | ||
segment | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require 'rbconfig' | ||
RSpec::Support.require_rspec_support "comparable_version" | ||
|
||
module RSpec | ||
module Support | ||
|
@@ -65,6 +66,24 @@ def supports_exception_cause? | |
end | ||
end | ||
|
||
ripper_requirements = [ComparableVersion.new(RUBY_VERSION) >= '1.9.2'] | ||
|
||
if Ruby.jruby? | ||
ripper_requirements.push(ComparableVersion.new(JRUBY_VERSION) >= '1.7.5') | ||
# Ripper on JRuby 9.0.0.0.rc1 or later reports wrong line number. | ||
ripper_requirements.push(ComparableVersion.new(JRUBY_VERSION) < '9.0.0.0.rc1') | ||
end | ||
|
||
if ripper_requirements.all? | ||
def ripper_supported? | ||
true | ||
end | ||
else | ||
def ripper_supported? | ||
false | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! The |
||
|
||
if Ruby.mri? | ||
def kw_args_supported? | ||
RUBY_VERSION >= '2.0.0' | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'rspec/support/comparable_version' | ||
|
||
module RSpec::Support | ||
describe ComparableVersion do | ||
describe '#<=>' do | ||
[ | ||
['1.2.3', '1.2.3', 0], | ||
['1.2.4', '1.2.3', 1], | ||
['1.3.0', '1.2.3', 1], | ||
['1.2.3', '1.2.4', -1], | ||
['1.2.3', '1.3.0', -1], | ||
['1.2.10', '1.2.3', 1], | ||
['1.2.3', '1.2.10', -1], | ||
['1.2.3.0', '1.2.3', 0], | ||
['1.2.3', '1.2.3.0', 0], | ||
['1.2.3.1', '1.2.3', 1], | ||
['1.2.3.1', '1.2.3.0', 1], | ||
['1.2.3', '1.2.3.1', -1], | ||
['1.2.3.0', '1.2.3.1', -1], | ||
['1.2.3.rc1', '1.2.3', -1], | ||
['1.2.3.rc1', '1.2.3.rc2', -1], | ||
['1.2.3.rc2', '1.2.3.rc10', -1], | ||
['1.2.3.alpha2', '1.2.3.beta1', -1], | ||
['1.2.3', '1.2.3.rc1', 1], | ||
['1.2.3.rc2', '1.2.3.rc1', 1], | ||
['1.2.3.rc10', '1.2.3.rc2', 1], | ||
['1.2.3.beta1', '1.2.3.alpha2', 1] | ||
].each do |subject_string, other_string, expected| | ||
context "with #{subject_string.inspect} and #{other_string.inspect}" do | ||
subject do | ||
ComparableVersion.new(subject_string) <=> ComparableVersion.new(other_string) | ||
end | ||
|
||
it { should eq(expected) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class looks nice and simple, but will it handle version numbers with
.pre
,.rc1
,.beta
, etc in the name? Thesegments
method usesto_i
which will silently convert those to0
. Looking at the JRuby downloads page, I see version numbers like9.0.0.0.pre1
and9.0.0.0.rc1
. Since we pass theJRUBY_VERSION
to this I think we need to handle those versions properly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.