Skip to content

Commit 62f0b65

Browse files
committed
Move to_raw_source to CompositeParser
1 parent ed84c58 commit 62f0b65

File tree

5 files changed

+100
-10
lines changed

5 files changed

+100
-10
lines changed

lib/rspec/parameterized/core.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
require "rspec/parameterized/core/version"
2-
require 'parser'
3-
require 'unparser'
4-
require 'proc_to_ast'
52
require 'rspec/parameterized/core/helper_methods'
63
require 'rspec/parameterized/core/example_helper_methods'
4+
require 'rspec/parameterized/core/errors'
5+
require 'rspec/parameterized/core/composite_parser'
76

87
module RSpec
98
module Parameterized
@@ -143,11 +142,9 @@ def define_cases(parameter, *args, &block)
143142
end
144143

145144
def params_inspect(obj)
146-
begin
147-
obj.is_a?(Proc) ? obj.to_raw_source : obj.inspect
148-
rescue Parser::SyntaxError
149-
return obj.inspect
150-
end
145+
RSpec::Parameterized::Core::CompositeParser.to_raw_source(obj)
146+
rescue ParserSyntaxError
147+
return obj.inspect
151148
end
152149

153150
def set_verbose_parameters(&block)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module RSpec
2+
module Parameterized
3+
module Core
4+
# Proxy class for parser and prism
5+
module CompositeParser
6+
# @param obj [Object]
7+
# @return [String]
8+
# @raise [RSpec::Parameterized::Core::ParserSyntaxError]
9+
def self.to_raw_source(obj)
10+
return to_raw_source_with_prism(obj) if use_prism?
11+
12+
to_raw_source_with_parser(obj)
13+
end
14+
15+
# Whether use parser or prism
16+
#
17+
# @return [true] Use prism
18+
# @return [false] Use parser
19+
def self.use_prism?
20+
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
21+
end
22+
23+
# @param obj [Object]
24+
# @return [String]
25+
# @raise [RSpec::Parameterized::Core::ParserSyntaxError]
26+
def self.to_raw_source_with_parser(obj)
27+
obj.is_a?(Proc) ? obj.to_raw_source : obj.inspect
28+
rescue Parser::SyntaxError => e
29+
raise ParserSyntaxError
30+
end
31+
private_class_method :to_raw_source_with_parser
32+
33+
# @param obj [Object]
34+
# @return [String]
35+
def self.to_raw_source_with_prism(obj)
36+
# TODO: Impl
37+
raise "TODO: Impl"
38+
end
39+
private_class_method :to_raw_source_with_prism
40+
end
41+
end
42+
end
43+
end
44+
45+
if RSpec::Parameterized::Core::CompositeParser.use_prism?
46+
require 'prism'
47+
else
48+
require 'parser'
49+
require 'unparser'
50+
require 'proc_to_ast'
51+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module RSpec
2+
module Parameterized
3+
module Core
4+
class Error < StandardError; end
5+
6+
class ParserSyntaxError < Error; end
7+
end
8+
end
9+
end

lib/rspec/parameterized/core/lazy_arg.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def apply(obj)
1111
end
1212

1313
def inspect
14-
"#{@block.to_raw_source}"
15-
rescue Parser::SyntaxError
14+
CompositeParser.to_raw_source(@block)
15+
rescue ParserSyntaxError
1616
super.inspect
1717
end
1818
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe RSpec::Parameterized::Core::CompositeParser do
2+
describe ".to_raw_source" do
3+
subject { RSpec::Parameterized::Core::CompositeParser.to_raw_source(arg) }
4+
5+
context "arg is not proc" do
6+
let(:arg) do
7+
123
8+
end
9+
10+
it { should eq "123" }
11+
end
12+
13+
context "arg is proc" do
14+
context "simple case" do
15+
let(:arg) do
16+
->(a) { a + 1 }
17+
end
18+
19+
it { should eq "->(a) { a + 1 }" }
20+
its(:encoding) { should eq Encoding::UTF_8 }
21+
end
22+
23+
context "arg is multibyte characters" do
24+
let(:arg) do
25+
->(a) { a + "ほげほげ" }
26+
end
27+
28+
it { should eq '->(a) { a + "ほげほげ" }' }
29+
its(:encoding) { should eq Encoding::UTF_8 }
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)