rspec-parameterized 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4ec74624713209b381c818242bdc31bed08c769
4
- data.tar.gz: a369308c5fa930c420763d32dbdc7d3f5e856002
3
+ metadata.gz: 2e6be52e986b05c5db18dc2355e2ee393b0ee954
4
+ data.tar.gz: 191bb660e0afdc89f466f1213a4ce756ba9566e1
5
5
  SHA512:
6
- metadata.gz: 21671b32125d34ef3efb84f11bbdb4a4a0f501f110c367e0b56b8999fa70254d7cd6dab31b33d0ac8ae485a2e7bcbbdb6345e262787cccc258c27940949799bb
7
- data.tar.gz: 2142247c46e8093a87bfab1deb178185952be853068e7503c35861c990123dad2816a5eccaf6bc3698bec93ba443653b90ec1ce68698423fc020d1efec799a78
6
+ metadata.gz: 7cce6711e56fb75d458b75b1fc4a9d47f7908395040bfc0eec9b809b66b5cbe0f5ec8663747b1b40e053286aafccfd9884eb4d2c97358414cc09191cfe74cef7
7
+ data.tar.gz: 9a5e59b1d28cd1bf826346c61d0953daba8bb6f198966d03024c98f1570a02e50b79f6ed5a89032fb805b7a50131ff5ceea8159965d59dc5c8cfd94ccf8c183f
@@ -1,11 +1,12 @@
1
1
  # Change Log
2
2
 
3
- ## [Unreleased](https://github.com/tomykaira/rspec-parameterized/tree/HEAD)
4
-
5
- [Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.3.1...HEAD)
3
+ ## [v0.3.2](https://github.com/tomykaira/rspec-parameterized/tree/v0.3.2) (2016-12-26)
4
+ [Full Changelog](https://github.com/tomykaira/rspec-parameterized/compare/v0.3.1...v0.3.2)
6
5
 
7
6
  **Merged pull requests:**
8
7
 
8
+ - Fix deprecation warning on ruby 2.4.0 [\#40](https://github.com/tomykaira/rspec-parameterized/pull/40) ([sue445](https://github.com/sue445))
9
+ - Add release note for v0.3.1 [\#39](https://github.com/tomykaira/rspec-parameterized/pull/39) ([sue445](https://github.com/sue445))
9
10
  - Add rubygems version badge [\#38](https://github.com/tomykaira/rspec-parameterized/pull/38) ([sue445](https://github.com/sue445))
10
11
 
11
12
  ## [v0.3.1](https://github.com/tomykaira/rspec-parameterized/tree/v0.3.1) (2016-08-17)
data/README.md CHANGED
@@ -72,6 +72,81 @@ describe "plus" do
72
72
  end
73
73
  end
74
74
  end
75
+
76
+ # Verbose Syntax
77
+ # For complex inputs or if you just want to be super explicit
78
+ describe "Verbose syntax" do
79
+ where do
80
+ {
81
+ "positive integers" => {
82
+ a: 1,
83
+ b: 2,
84
+ answer: 3,
85
+ },
86
+ "negative_integers" => {
87
+ a: -1,
88
+ b: -2,
89
+ answer: -3,
90
+ },
91
+ "mixed_integers" => {
92
+ a: 3,
93
+ b: -3,
94
+ answer: 0,
95
+ },
96
+ }
97
+ end
98
+
99
+ with_them do
100
+ it "should do additions" do
101
+ expect(a + b).to eq answer
102
+ end
103
+ end
104
+ end
105
+
106
+ # It's also possible to override each combination name using magic variable :case_name
107
+ # Output:
108
+ # Custom test case name
109
+ # positive integers
110
+ # should do additions
111
+ # negative integers
112
+ # should do additions
113
+ # mixed integers
114
+ # should do additions
115
+ describe "Custom names for regular syntax" do
116
+ where(:case_name, :a, :b, :answer) do
117
+ [
118
+ ["positive integers", 6, 2, 8],
119
+ ["negative integers", -1, -2, -3],
120
+ [ "mixed integers", -5, 3, -2],
121
+ ]
122
+ end
123
+
124
+ with_them do
125
+ it "should do additions" do
126
+ expect(a + b).to eq answer
127
+ end
128
+ end
129
+
130
+ # Or :case_names lambda for hash syntax
131
+ # Output:
132
+ # when hash arguments
133
+ # 1 + 5 + 2
134
+ # sum is even
135
+ # 1 + 5 + 4
136
+ # sum is even
137
+ # 1 + 7 + 2
138
+ # sum is even
139
+ # ...
140
+ describe "Custom naming for hash syntax" do
141
+ where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])
142
+
143
+ with_them do
144
+ it "sum is even" do
145
+ expect(a + b + c).to be_even
146
+ end
147
+ end
148
+ end
149
+
75
150
  ```
76
151
 
77
152
  I was inspired by [udzura's mock](https://gist.github.com/1881139).
@@ -40,12 +40,22 @@ module RSpec
40
40
  def where(*args, &b)
41
41
 
42
42
  if args.size == 1 && args[0].instance_of?(Hash)
43
+ naming_func = args.first.delete(:case_names)
43
44
  params = args[0]
44
45
  first, *rest = params.values
46
+ arg_names = params.keys
47
+ arg_values = first.product(*rest)
45
48
 
46
- set_parameters(params.keys) {
47
- first.product(*rest)
49
+ if naming_func && naming_func.respond_to?(:call)
50
+ arg_names << :case_name
51
+ arg_values.map! { |row| row << naming_func.call(*row) }
52
+ end
53
+
54
+ set_parameters(arg_names) {
55
+ arg_values
48
56
  }
57
+ elsif args.size == 0
58
+ set_verbose_parameters(&b)
49
59
  else
50
60
  set_parameters(args, &b)
51
61
  end
@@ -99,7 +109,7 @@ module RSpec
99
109
 
100
110
  param_sets.each do |param_set|
101
111
  pairs = [parameter.arg_names, param_set].transpose.to_h
102
- pretty_params = pairs.map {|name, val| "#{name}: #{params_inspect(val)}"}.join(", ")
112
+ pretty_params = pairs.has_key?(:case_name) ? pairs[:case_name] : pairs.map {|name, val| "#{name}: #{params_inspect(val)}"}.join(", ")
103
113
  describe(pretty_params, *args) do
104
114
  pairs.each do |name, val|
105
115
  let(name) { val }
@@ -135,6 +145,23 @@ module RSpec
135
145
  return obj.inspect
136
146
  end
137
147
  end
148
+
149
+ def set_verbose_parameters(&block)
150
+ arguments_hash = yield
151
+ arg_names = arguments_hash.values.reduce(Set.new) { |memo, pairs| memo | pairs.keys }.to_a
152
+ arg_values = []
153
+ arguments_hash.each do |name, values_hash|
154
+ row = [name]
155
+ arg_names.each do |argument_name|
156
+ row << values_hash[argument_name]
157
+ end
158
+ arg_values << row
159
+ end
160
+ arg_names.unshift(:case_name)
161
+ set_parameters(arg_names) {
162
+ arg_values
163
+ }
164
+ end
138
165
  end
139
166
  end
140
167
 
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Parameterized
3
- VERSION = "0.3.2"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -66,6 +66,131 @@ describe RSpec::Parameterized do
66
66
  end
67
67
  end
68
68
 
69
+ describe "Verbose syntax" do
70
+ where do
71
+ {
72
+ "positive integers" => {
73
+ a: 1,
74
+ b: 2,
75
+ answer: 3,
76
+ },
77
+ "negative_integers" => {
78
+ a: -1,
79
+ b: -2,
80
+ answer: -3,
81
+ },
82
+ "mixed_integers" => {
83
+ a: 3,
84
+ b: -3,
85
+ answer: 0,
86
+ },
87
+ }
88
+ end
89
+
90
+ with_them do
91
+ it "should do additions" do
92
+ expect(a + b).to eq answer
93
+ end
94
+
95
+ it "should have custom name" do |example|
96
+ expect(example.metadata[:example_group][:description]).to eq case_name
97
+ end
98
+ end
99
+
100
+ context "lambda parameter" do
101
+ where do
102
+ {
103
+ "integers" => {
104
+ a: 1,
105
+ b: 2,
106
+ answer: -> {expect(subject).to eq 3},
107
+ },
108
+ "strings" => {
109
+ a: "hello ",
110
+ b: "world",
111
+ answer: -> {expect(subject).to include "lo wo"},
112
+ },
113
+ "arrays" => {
114
+ a: [1, 2, 3],
115
+ b: [4, 5, 6],
116
+ answer: -> {expect(subject.size).to eq 6}
117
+ }
118
+ }
119
+ end
120
+
121
+ with_them do
122
+ subject {a + b}
123
+ it "should do additions" do
124
+ self.instance_exec(&answer)
125
+ end
126
+
127
+ it "should have custom name" do |example|
128
+ expect(example.metadata[:example_group][:description]).to eq(case_name)
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "Custom test case name" do
135
+ context "when regular arguments" do
136
+ where(:case_name, :a, :b, :answer) do
137
+ [
138
+ ["positive integers", 6, 2, 8],
139
+ ["negative integers", -1, -2, -3],
140
+ [ "mixed integers", -5, 3, -2],
141
+ ]
142
+ end
143
+
144
+ with_them do
145
+ it "should do additions" do
146
+ expect(a + b).to eq answer
147
+ end
148
+
149
+ it "should have custom test name" do |example|
150
+ expect(example.metadata[:example_group][:description]).to eq case_name
151
+ end
152
+ end
153
+ end
154
+
155
+ context "when hash arguments" do
156
+ where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])
157
+
158
+ with_them do
159
+ it "sum is even" do
160
+ expect(a + b + c).to be_even
161
+ end
162
+
163
+ it "should have custom names" do |example|
164
+ expect(example.metadata[:example_group][:description]).to include "+"
165
+ end
166
+ end
167
+ end
168
+
169
+ if RUBY_VERSION >= "2,1"
170
+ context "when arguments are separated with pipe (using TableSyntax)" do
171
+ using RSpec::Parameterized::TableSyntax
172
+
173
+ where(:case_name, :a, :b, :answer) do
174
+ "integers" | 1 | 2 | 3
175
+ "strings" | "hello " | "world" | "hello world"
176
+ "arrays" | [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
177
+ "giant numbers" | 100000000000000000000 | 100000000000000000000 | 200000000000000000000
178
+ end
179
+
180
+ with_them do
181
+ it "a plus b is answer" do
182
+ expect(a + b).to eq answer
183
+ end
184
+
185
+ it "should have custom test name" do |example|
186
+ expect(example.metadata[:example_group][:description]).to eq case_name
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ end
193
+
69
194
  if RUBY_VERSION >= "2.1"
70
195
  describe "table separated with pipe (using TableSyntax)" do
71
196
  using RSpec::Parameterized::TableSyntax
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-parameterized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomykaira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-26 00:00:00.000000000 Z
11
+ date: 2017-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.6.8
148
+ rubygems_version: 2.6.12
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: RSpec::Parameterized supports simple parameterized test syntax in rspec.