From f15eb2945281d9c6ae79adab0bee01574d91f5e8 Mon Sep 17 00:00:00 2001 From: aliaksandr-martsinovich Date: Fri, 2 Jun 2017 12:20:06 +0200 Subject: [PATCH 1/3] Added support for custom naming strategies. :case_name -> "name" for regular syntax :case_names -> lambda for hash syntax --- README.md | 44 ++++++++++++++++++++++++++++ lib/rspec/parameterized.rb | 14 +++++++-- spec/parametarized_spec.rb | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2631b3a..7ba5949 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,50 @@ describe "plus" do end end end + +# It's also possible to override each combination name using magic variable :case_name +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 +# Output: +# Custom test case name +# positive integers +# should do additions +# negative integers +# should do additions +# mixed integers +# should do additions + +# Or :case_names lambda for hash syntax +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 + +# Output: +# when hash arguments +# 1 + 5 + 2 +# sum is even +# 1 + 5 + 4 +# sum is even +# 1 + 7 + 2 +# sum is even +# ... ``` I was inspired by [udzura's mock](https://gist.github.com/1881139). diff --git a/lib/rspec/parameterized.rb b/lib/rspec/parameterized.rb index 62b8cc4..25bb6c3 100644 --- a/lib/rspec/parameterized.rb +++ b/lib/rspec/parameterized.rb @@ -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) @@ -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 } diff --git a/spec/parametarized_spec.rb b/spec/parametarized_spec.rb index ccd2176..39b838d 100644 --- a/spec/parametarized_spec.rb +++ b/spec/parametarized_spec.rb @@ -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 From ca8400f24d74021509d6464c939b2950ac2473b0 Mon Sep 17 00:00:00 2001 From: aliaksandr-martsinovich Date: Fri, 2 Jun 2017 12:28:05 +0200 Subject: [PATCH 2/3] Reformatting readme a bit --- README.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7ba5949..5e044f7 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,14 @@ describe "plus" do 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 [ @@ -87,16 +95,18 @@ describe "Custom names for regular syntax" do it "should do additions" do expect(a + b).to eq answer end -# Output: -# Custom test case name -# positive integers -# should do additions -# negative integers -# should do additions -# mixed integers -# should do additions +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]) @@ -107,15 +117,6 @@ describe "Custom naming for hash syntax" do end end -# Output: -# when hash arguments -# 1 + 5 + 2 -# sum is even -# 1 + 5 + 4 -# sum is even -# 1 + 7 + 2 -# sum is even -# ... ``` I was inspired by [udzura's mock](https://gist.github.com/1881139). From 3ef237ae268bea8671164eaa2a86f36afdad5500 Mon Sep 17 00:00:00 2001 From: aliaksandr-martsinovich Date: Mon, 12 Jun 2017 09:40:06 +0200 Subject: [PATCH 3/3] Fixed to 2 spaces indent in spec and readme --- README.md | 6 +++--- spec/parametarized_spec.rb | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5e044f7..4c8272b 100644 --- a/README.md +++ b/README.md @@ -85,9 +85,9 @@ end 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], + ["positive integers", 6, 2, 8], + ["negative integers", -1, -2, -3], + [ "mixed integers", -5, 3, -2], ] end diff --git a/spec/parametarized_spec.rb b/spec/parametarized_spec.rb index 39b838d..2e3f7f1 100644 --- a/spec/parametarized_spec.rb +++ b/spec/parametarized_spec.rb @@ -70,9 +70,9 @@ 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], + ["positive integers", 6, 2, 8], + ["negative integers", -1, -2, -3], + [ "mixed integers", -5, 3, -2], ] end