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

Verbose test syntax #42

Merged
merged 2 commits into from
Jun 13, 2017
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ describe "plus" do
end
end

# Verbose Syntax
# For complex inputs or if you just want to be super explicit
describe "Verbose syntax" do
where do
{
"positive integers" => {
a: 1,
b: 2,
answer: 3,
},
"negative_integers" => {
a: -1,
b: -2,
answer: -3,
},
"mixed_integers" => {
a: 3,
b: -3,
answer: 0,
},
}
end

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

# It's also possible to override each combination name using magic variable :case_name
# Output:
# Custom test case name
Expand Down
19 changes: 19 additions & 0 deletions lib/rspec/parameterized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def where(*args, &b)
set_parameters(arg_names) {
arg_values
}
elsif args.size == 0
set_verbose_parameters(&b)
else
set_parameters(args, &b)
end
Expand Down Expand Up @@ -143,6 +145,23 @@ def params_inspect(obj)
return obj.inspect
end
end

def set_verbose_parameters(&block)
arguments_hash = yield
arg_names = arguments_hash.values.reduce(Set.new) { |memo, pairs| memo | pairs.keys }.to_a
arg_values = []
arguments_hash.each do |name, values_hash|
row = [name]
arg_names.each do |argument_name|
row << values_hash[argument_name]
end
arg_values << row
end
arg_names.unshift(:case_name)
set_parameters(arg_names) {
arg_values
}
end
end
end

Expand Down
65 changes: 65 additions & 0 deletions spec/parametarized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,71 @@
end
end

describe "Verbose syntax" do
where do
{
"positive integers" => {
a: 1,
b: 2,
answer: 3,
},
"negative_integers" => {
a: -1,
b: -2,
answer: -3,
},
"mixed_integers" => {
a: 3,
b: -3,
answer: 0,
},
}
end

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

it "should have custom name" do |example|
expect(example.metadata[:example_group][:description]).to eq case_name
end
end

context "lambda parameter" do
where do
{
"integers" => {
a: 1,
b: 2,
answer: -> {expect(subject).to eq 3},
},
"strings" => {
a: "hello ",
b: "world",
answer: -> {expect(subject).to include "lo wo"},
},
"arrays" => {
a: [1, 2, 3],
b: [4, 5, 6],
answer: -> {expect(subject.size).to eq 6}
}
}
end

with_them do
subject {a + b}
it "should do additions" do
self.instance_exec(&answer)
end

it "should have custom name" do |example|
expect(example.metadata[:example_group][:description]).to eq(case_name)
end
end
end
end

describe "Custom test case name" do
context "when regular arguments" do
where(:case_name, :a, :b, :answer) do
Expand Down