From 7f9b75d4edb035f12b50aab5067d19064f61d0c8 Mon Sep 17 00:00:00 2001 From: aliaksandr-martsinovich Date: Fri, 2 Jun 2017 13:59:15 +0200 Subject: [PATCH 1/2] Verbose syntax --- README.md | 30 ++++++++++++++++++ lib/rspec/parameterized.rb | 19 +++++++++++ spec/parametarized_spec.rb | 65 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/README.md b/README.md index 4c8272b..58a4202 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rspec/parameterized.rb b/lib/rspec/parameterized.rb index 25bb6c3..9dcb31f 100644 --- a/lib/rspec/parameterized.rb +++ b/lib/rspec/parameterized.rb @@ -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 @@ -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 diff --git a/spec/parametarized_spec.rb b/spec/parametarized_spec.rb index 2e3f7f1..6bc1f47 100644 --- a/spec/parametarized_spec.rb +++ b/spec/parametarized_spec.rb @@ -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 From 8c6abdf30dc8c3e763c69f0179fa76cc8a7a4090 Mon Sep 17 00:00:00 2001 From: aliaksandr-martsinovich Date: Tue, 13 Jun 2017 09:42:57 +0200 Subject: [PATCH 2/2] Fixed to 2 spaces indent in spec and readme --- README.md | 30 +++++++++---------- spec/parametarized_spec.rb | 60 +++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 58a4202..a8e5218 100644 --- a/README.md +++ b/README.md @@ -78,21 +78,21 @@ 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, - }, + "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 diff --git a/spec/parametarized_spec.rb b/spec/parametarized_spec.rb index 6bc1f47..69a4d73 100644 --- a/spec/parametarized_spec.rb +++ b/spec/parametarized_spec.rb @@ -69,21 +69,21 @@ 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, - }, + "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 @@ -100,21 +100,21 @@ 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} - } + "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