Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new parameter type: ref and lazy. #62

Merged
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/rspec/parameterized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'parser'
require 'unparser'
require 'proc_to_ast'
require 'rspec/parameterized/helper_methods'

module RSpec
module Parameterized
Expand Down Expand Up @@ -97,6 +98,7 @@ def set_parameters(arg_names, &b)

def define_cases(parameter, *args, &block)
instance = new # for evaluate let methods.
instance.extend HelperMethods
if defined?(self.superclass::LetDefinitions)
instance.extend self.superclass::LetDefinitions
end
Expand All @@ -112,7 +114,11 @@ def define_cases(parameter, *args, &block)
pretty_params = pairs.has_key?(:case_name) ? pairs[:case_name] : pairs.map {|name, val| "#{name}: #{params_inspect(val)}"}.join(", ")
describe(pretty_params, *args) do
pairs.each do |name, val|
let(name) { val }
if HelperMethods.applicable? val
let(name) { val.apply(self) }
else
let(name) { val }
end
end

singleton_class.module_eval do
Expand Down
7 changes: 7 additions & 0 deletions lib/rspec/parameterized/arg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module RSpec
module Parameterized
class Arg
def apply(obj) ; end
end
end
end
20 changes: 20 additions & 0 deletions lib/rspec/parameterized/helper_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rspec/parameterized/ref_arg'
require 'rspec/parameterized/lazy_arg'
module RSpec
module Parameterized
module HelperMethods
def ref(symbol)
RefArg.new(symbol)
end

def lazy(&block)
LazyArg.new(&block)
end

def self.applicable?(arg)
arg.is_a? Arg
end
end
end
end

13 changes: 13 additions & 0 deletions lib/rspec/parameterized/lazy_arg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module RSpec
module Parameterized
class LazyArg < Arg
def initialize(&block)
@block = block
end

def apply(obj)
obj.instance_eval(&@block)
end
end
end
end
15 changes: 15 additions & 0 deletions lib/rspec/parameterized/ref_arg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rspec/parameterized/arg'
module RSpec
module Parameterized
class RefArg < Arg
def initialize(symbol)
@symbol = symbol
end

def apply(obj)
obj.send(@symbol)
end
end
end
end

46 changes: 46 additions & 0 deletions spec/parametarized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,52 @@

end

describe "ref" do
let(:foo) { 1 }

where(:value, :answer) do
[
[ref(:foo), 1],
]
end

with_them do
it "let variable in same example group can be used" do
expect(value).to eq answer
end

context "override let varibale" do
let(:foo) { 3 }

it "can override let variable" do
expect(value).to eq 3
end
end
end
end

describe "lazy" do
let(:one) { 1 }
let(:four) { 4 }

where(:a, :b, :answer) do
[
[ref(:one), ref(:four), lazy { two + three }],
]
end

with_them do
context "define two and three after where block" do
let(:two) { 2 }
let(:three) { 3 }

it "should do additions" do
expect(a + b).to eq answer
end
end
end
end

if RUBY_VERSION >= "2.1"
describe "table separated with pipe (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax
Expand Down