Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 4ba2e05

Browse files
committed
Add RubyFeatures.ripper_supported?
1 parent 9bdb4ab commit 4ba2e05

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Enhancements:
44

55
* Improve formatting of `Delegator` based objects (e.g. `SimpleDelgator`) in
66
failure messages and diffs. (Andrew Horner, #215)
7+
* Add `Ripper` support detection. (Yuji Nakayama, #245)
78

89
Bug Fixes:
910

benchmarks/ripper.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'benchmark/ips'
2+
require 'ripper'
3+
4+
ruby_version = defined?(JRUBY_VERSION) ? JRUBY_VERSION : RUBY_VERSION
5+
puts "#{RUBY_ENGINE} #{ruby_version}"
6+
7+
source = File.read(__FILE__)
8+
9+
Benchmark.ips do |x|
10+
x.report("Ripper") do
11+
Ripper.sexp(source)
12+
Ripper.lex(source)
13+
end
14+
end
15+
16+
__END__
17+
18+
ruby 1.9.3
19+
Calculating -------------------------------------
20+
Ripper 284.000 i/100ms
21+
22+
ruby 2.2.3
23+
Calculating -------------------------------------
24+
Ripper 320.000 i/100ms
25+
26+
jruby 1.7.22
27+
Calculating -------------------------------------
28+
Ripper 230.000 i/100ms
29+
30+
jruby 1.7.5
31+
Calculating -------------------------------------
32+
Ripper 24.000 i/100ms
33+
34+
jruby 1.7.13
35+
Calculating -------------------------------------
36+
Ripper 25.000 i/100ms
37+
38+
jruby 1.7.14
39+
Calculating -------------------------------------
40+
Ripper 239.000 i/100ms
41+
42+
jruby 1.7.22
43+
Calculating -------------------------------------
44+
Ripper 231.000 i/100ms
45+
46+
jruby 9.0.1.0
47+
Calculating -------------------------------------
48+
Ripper 218.000 i/100ms

lib/rspec/support/ruby_features.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ def supports_exception_cause?
6565
end
6666
end
6767

68+
if Ruby.mri? && RUBY_VERSION >= '1.9.0'
69+
def ripper_supported?
70+
true
71+
end
72+
elsif Ruby.jruby? &&
73+
defined?(Gem::Version) &&
74+
Gem::Version.new(JRUBY_VERSION) >= Gem::Version.new('1.7.14') &&
75+
!JRUBY_VERSION.start_with?('9.')
76+
# JRuby 1.7.5 - 1.7.13 actually supports Ripper but it's too slow.
77+
# Also, Ripper on JRuby 9.0.0.0 reports wrong line number.
78+
def ripper_supported?
79+
true
80+
end
81+
else
82+
def ripper_supported?
83+
false
84+
end
85+
end
86+
6887
if Ruby.mri?
6988
def kw_args_supported?
7089
RUBY_VERSION >= '2.0.0'

spec/rspec/support/ruby_features_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,57 @@ module Support
8181
expect(RubyFeatures.caller_locations_supported?).to eq(RUBY_VERSION >= '2.0.0')
8282
end
8383
end
84+
85+
describe "#ripper_supported?" do
86+
it 'does not load Ripper' do
87+
expect { RubyFeatures.ripper_supported? }.not_to change { defined?(Ripper) }
88+
end
89+
90+
describe 'Ripper' do
91+
let(:line_number) do
92+
token = tokens.first
93+
location = token.first
94+
location.first
95+
end
96+
97+
let(:tokens) do
98+
require 'ripper'
99+
Ripper.lex('foo')
100+
end
101+
102+
if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby'
103+
context 'on MRI' do
104+
context '1.8.x', :if => RUBY_VERSION.start_with?('1.8.') do
105+
it 'is not supported' do
106+
expect { tokens }.to raise_error(LoadError)
107+
end
108+
end
109+
110+
context '1.9.x or later', :if => RUBY_VERSION >= '1.9' do
111+
it 'is supported' do
112+
expect(line_number).to eq(1)
113+
end
114+
end
115+
end
116+
end
117+
118+
if defined?(JRUBY_VERSION)
119+
context 'on JRuby' do
120+
context '1.7.x', :if => JRUBY_VERSION.start_with?('1.7.') do
121+
it 'is supported' do
122+
expect(line_number).to eq(1)
123+
end
124+
end
125+
126+
context '9.x.x.x', :if => JRUBY_VERSION.start_with?('9.') do
127+
it 'reports wrong line number' do
128+
expect(line_number).to eq(2)
129+
end
130+
end
131+
end
132+
end
133+
end
134+
end
84135
end
85136
end
86137
end

0 commit comments

Comments
 (0)