Skip to content

Commit

Permalink
Merge pull request #41 from aliaksandr-martsinovich/case-name-logging
Browse files Browse the repository at this point in the history
Customizable description
  • Loading branch information
joker1007 authored Jun 13, 2017
2 parents f77596d + 3ef237a commit 11566ee
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ describe "plus" do
end
end
end

# It's also possible to override each combination name using magic variable :case_name
# Output:
# Custom test case name
# positive integers
# should do additions
# negative integers
# should do additions
# mixed integers
# should do additions
describe "Custom names for regular syntax" do
where(:case_name, :a, :b, :answer) do
[
["positive integers", 6, 2, 8],
["negative integers", -1, -2, -3],
[ "mixed integers", -5, 3, -2],
]
end

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

# Or :case_names lambda for hash syntax
# Output:
# when hash arguments
# 1 + 5 + 2
# sum is even
# 1 + 5 + 4
# sum is even
# 1 + 7 + 2
# sum is even
# ...
describe "Custom naming for hash syntax" do
where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])

with_them do
it "sum is even" do
expect(a + b + c).to be_even
end
end
end

```

I was inspired by [udzura's mock](https://gist.github.com/1881139).
Expand Down
14 changes: 11 additions & 3 deletions lib/rspec/parameterized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ def initialize(arg_names, &block)
def where(*args, &b)

if args.size == 1 && args[0].instance_of?(Hash)
naming_func = args.first.delete(:case_names)
params = args[0]
first, *rest = params.values
arg_names = params.keys
arg_values = first.product(*rest)

set_parameters(params.keys) {
first.product(*rest)
if naming_func && naming_func.respond_to?(:call)
arg_names << :case_name
arg_values.map! { |row| row << naming_func.call(*row) }
end

set_parameters(arg_names) {
arg_values
}
else
set_parameters(args, &b)
Expand Down Expand Up @@ -99,7 +107,7 @@ def define_cases(parameter, *args, &block)

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(", ")
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 }
Expand Down
60 changes: 60 additions & 0 deletions spec/parametarized_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,66 @@
end
end

describe "Custom test case name" do
context "when regular arguments" do
where(:case_name, :a, :b, :answer) do
[
["positive integers", 6, 2, 8],
["negative integers", -1, -2, -3],
[ "mixed integers", -5, 3, -2],
]
end

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

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

context "when hash arguments" do
where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])

with_them do
it "sum is even" do
expect(a + b + c).to be_even
end

it "should have custom names" do |example|
expect(example.metadata[:example_group][:description]).to include "+"
end
end
end

if RUBY_VERSION >= "2,1"
context "when arguments are separated with pipe (using TableSyntax)" do
using RSpec::Parameterized::TableSyntax

where(:case_name, :a, :b, :answer) do
"integers" | 1 | 2 | 3
"strings" | "hello " | "world" | "hello world"
"arrays" | [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
"giant numbers" | 100000000000000000000 | 100000000000000000000 | 200000000000000000000
end

with_them do
it "a plus b is answer" do
expect(a + b).to eq answer
end

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

end

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

0 comments on commit 11566ee

Please sign in to comment.