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

Enable to browse parameter values in with_them block #30

Merged
merged 1 commit into from
Feb 22, 2016
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: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe "plus" do
expect(a + b).to eq answer
end
end

with_them do
# Can browse parameters via `params` method in with_them block
# Can browse all parameters via `all_params` method in with_them block
it "#{params[:a]} + #{params[:b]} == #{params[:answer]}" do
expect(a + b).to eq answer
end
end
end

# Hash and Array Style
Expand Down
28 changes: 23 additions & 5 deletions lib/rspec/parameterized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,30 @@ def define_cases(parameter, *args, &block)
# for only one parameters
param_sets = param_sets.map { |x| Array[x] } if !param_sets[0].is_a?(Array)

param_sets.each do |params|
pairs = [parameter.arg_names, params].transpose
pretty_params = pairs.map {|t| "#{t[0]}: #{params_inspect(t[1])}"}.join(", ")
param_sets.each do |param_set|
pairs = [parameter.arg_names, param_set].transpose.to_h
pretty_params = pairs.map {|name, val| "#{name}: #{params_inspect(val)}"}.join(", ")
describe(pretty_params, *args) do
pairs.each do |n|
let(n[0]) { n[1] }
pairs.each do |name, val|
let(name) { val }
end

singleton_class.module_eval do
if respond_to?(:params)
warn "ExampleGroup.params method is overrided."
end

define_method(:params) do
pairs
end

if respond_to?(:all_params)
warn "ExampleGroup.all_params method is overrided."
end

define_method(:all_params) do
param_sets
end
end

module_eval(&block)
Expand Down
6 changes: 6 additions & 0 deletions spec/parametarized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
end
end

with_them do
it "#{params[:a]} + #{params[:b]} == #{params[:answer]}" do
expect(a + b).to eq answer
end
end

with_them pending: "PENDING" do
it "should do additions" do
expect(a + b).to == answer
Expand Down