Skip to content

Commit b482003

Browse files
authored
Merge pull request #1 from rspec-parameterized/import
Import commits from tomykaira/rspec-parameterized
2 parents 0567a34 + 7ad6178 commit b482003

File tree

3 files changed

+384
-3
lines changed

3 files changed

+384
-3
lines changed

lib/rspec/parameterized/table.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module RSpec
2+
module Parameterized
3+
class Table
4+
attr_reader :last_row
5+
6+
def initialize
7+
@rows = []
8+
@last_row = nil
9+
end
10+
11+
def add_row(row)
12+
unless @rows.find {|r| r.object_id == row.object_id}
13+
@rows << row
14+
@last_row = row
15+
end
16+
self
17+
end
18+
19+
def add_param_to_last_row(param)
20+
last_row.add_param(param)
21+
self
22+
end
23+
alias :| :add_param_to_last_row
24+
25+
def to_a
26+
@rows.map(&:to_a)
27+
end
28+
alias :to_params :to_a
29+
30+
class Row
31+
def initialize(param)
32+
@params = [param]
33+
end
34+
35+
def add_param(param)
36+
@params << param
37+
end
38+
39+
def to_a
40+
@params
41+
end
42+
43+
def to_params
44+
[@params]
45+
end
46+
end
47+
end
48+
end
49+
end
Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
11
# frozen_string_literal: true
22

33
require_relative "table_syntax/version"
4+
require 'rspec/parameterized/table'
5+
require 'binding_of_caller'
46

5-
module Rspec
7+
module RSpec
68
module Parameterized
9+
module TableSyntaxImplement
10+
def |(other)
11+
where_binding = binding.of_caller(1) # get where block binding
12+
caller_instance = eval("self", where_binding) # get caller instance (ExampleGroup)
13+
14+
if caller_instance.instance_variable_defined?(:@__parameter_table)
15+
table = caller_instance.instance_variable_get(:@__parameter_table)
16+
else
17+
table = RSpec::Parameterized::Table.new
18+
caller_instance.instance_variable_set(:@__parameter_table, table)
19+
end
20+
21+
row = Table::Row.new(self)
22+
table.add_row(row)
23+
row.add_param(other)
24+
table
25+
end
26+
end
27+
728
module TableSyntax
8-
class Error < StandardError; end
9-
# Your code goes here...
29+
if Gem::Version.create(RUBY_VERSION) >= Gem::Version.create("3.2.0.rc1")
30+
refine Object do
31+
import_methods TableSyntaxImplement
32+
end
33+
34+
refine Integer do
35+
import_methods TableSyntaxImplement
36+
end
37+
38+
refine Array do
39+
import_methods TableSyntaxImplement
40+
end
41+
42+
refine NilClass do
43+
import_methods TableSyntaxImplement
44+
end
45+
46+
refine TrueClass do
47+
import_methods TableSyntaxImplement
48+
end
49+
50+
refine FalseClass do
51+
import_methods TableSyntaxImplement
52+
end
53+
else
54+
refine Object do
55+
include TableSyntaxImplement
56+
end
57+
58+
refine Integer do
59+
include TableSyntaxImplement
60+
end
61+
62+
refine Array do
63+
include TableSyntaxImplement
64+
end
65+
66+
refine NilClass do
67+
include TableSyntaxImplement
68+
end
69+
70+
refine TrueClass do
71+
include TableSyntaxImplement
72+
end
73+
74+
refine FalseClass do
75+
include TableSyntaxImplement
76+
end
77+
end
1078
end
1179
end
1280
end

spec/parametarized_spec.rb

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2+
3+
# RSpec::Parameterized
4+
# Sample
5+
# plus
6+
# [1, 2, 3]
7+
# should do additions
8+
# [5, 8, 13]
9+
# should do additions
10+
# [0, 0, 0]
11+
# should do additions
12+
13+
describe RSpec::Parameterized do
14+
describe "where and with_them" do
15+
where(:a, :b, :answer) do
16+
[
17+
[1 , 2 , 3],
18+
[5 , 8 , 13],
19+
[0 , 0 , 0]
20+
]
21+
end
22+
23+
with_them do
24+
it "should do additions" do
25+
expect(a + b).to eq answer
26+
end
27+
end
28+
29+
with_them pending: "PENDING" do
30+
it "should do additions" do
31+
expect(a + b).to == answer
32+
end
33+
end
34+
end
35+
36+
describe "lambda parameter" do
37+
where(:a, :b, :answer) do
38+
[
39+
[1 , 2 , -> {should == 3}],
40+
[5 , 8 , -> {should == 13}],
41+
[0 , 0 , -> {should == 0}]
42+
]
43+
end
44+
45+
with_them do
46+
subject {a + b}
47+
it "should do additions" do
48+
self.instance_exec(&answer)
49+
end
50+
end
51+
end
52+
53+
describe "table separated with pipe" do
54+
where_table(:a, :b, :answer) do
55+
1 | 2 | 3
56+
"hello " | "world" | "hello world"
57+
[1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
58+
end
59+
60+
with_them do
61+
it "a plus b is answer" do
62+
expect(a + b).to eq answer
63+
end
64+
end
65+
end
66+
67+
if RUBY_VERSION >= "2.1"
68+
describe "table separated with pipe (using TableSyntax)" do
69+
using RSpec::Parameterized::TableSyntax
70+
71+
where(:a, :b, :answer) do
72+
1 | 2 | 3
73+
"hello " | "world" | "hello world"
74+
[1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
75+
end
76+
77+
with_them do
78+
it "a plus b is answer" do
79+
expect(a + b).to eq answer
80+
end
81+
end
82+
end
83+
84+
describe "table separated with pipe and lambda parameter (using TableSyntax)" do
85+
using RSpec::Parameterized::TableSyntax
86+
87+
where(:a, :b, :matcher) do
88+
1 | 2 | -> { eq(3) }
89+
"hello " | "world" | -> { eq("hello world") }
90+
[1, 2, 3] | [4, 5, 6] | -> { be_a(Array) }
91+
end
92+
93+
with_them do
94+
it "a plus b is answer" do
95+
expect(a + b).to instance_exec(&matcher)
96+
end
97+
end
98+
end
99+
end
100+
101+
context "when the where block is after with_them" do
102+
with_them do
103+
it "should do additions" do
104+
expect(a + b).to eq answer
105+
end
106+
end
107+
108+
with_them do
109+
subject { a }
110+
it { should be_a Numeric }
111+
end
112+
113+
where(:a, :b, :answer) do
114+
[
115+
[1 , 2 , 3],
116+
[5 , 8 , 13],
117+
[0 , 0 , 0]
118+
]
119+
end
120+
end
121+
122+
context "when the where block is between with_thems" do
123+
with_them do
124+
it "should do additions" do
125+
expect(a + b).to eq answer
126+
end
127+
end
128+
129+
where(:a, :b, :answer) do
130+
[
131+
[1 , 2 , 3],
132+
[5 , 8 , 13],
133+
[0 , 0 , 0]
134+
]
135+
end
136+
137+
with_them do
138+
subject { a }
139+
it { should be_a Numeric }
140+
end
141+
end
142+
143+
context "when the where has only one parameter to be set" do
144+
where(:x) do
145+
[1, 2, 3]
146+
end
147+
148+
with_them do
149+
it 'can take an array of elements' do
150+
expect(x).to eq x
151+
end
152+
end
153+
end
154+
155+
context "when the table has only a row" do
156+
where_table(:a, :b, :answer) do
157+
1 | 2 | 3
158+
end
159+
160+
with_them do
161+
it "a plus b is answer" do
162+
expect(a + b).to eq answer
163+
end
164+
end
165+
end
166+
167+
if RUBY_VERSION >= "2.1"
168+
context "when the table has only a row (using TableSyntax)" do
169+
using RSpec::Parameterized::TableSyntax
170+
171+
where(:a, :b, :answer) do
172+
1 | 2 | 3
173+
end
174+
175+
with_them do
176+
it "a plus b is answer" do
177+
expect(a + b).to eq answer
178+
end
179+
end
180+
end
181+
context "when 1st column is nil or true or false" do
182+
using RSpec::Parameterized::TableSyntax
183+
where(:a, :result) do
184+
nil | nil
185+
false | false
186+
true | true
187+
end
188+
189+
with_them do
190+
it "a is result" do
191+
expect(a).to be result
192+
end
193+
end
194+
end
195+
end
196+
197+
context "when the where has let variables, defined by parent example group" do
198+
describe "parent (define let)" do
199+
let(:five) { 5 }
200+
let(:eight) { 8 }
201+
202+
describe "child 1" do
203+
where(:a, :b, :answer) do
204+
[
205+
[1 , 2 , 3],
206+
[five , eight , 13],
207+
]
208+
end
209+
210+
with_them do
211+
it "a plus b is answer" do
212+
expect(a + b).to eq answer
213+
end
214+
end
215+
end
216+
217+
describe "child 2 (where_table)" do
218+
where_table(:a, :b, :answer) do
219+
1 | 2 | 3
220+
five | eight | 13
221+
end
222+
223+
with_them do
224+
it "a plus b is answer" do
225+
expect(a + b).to eq answer
226+
end
227+
end
228+
end
229+
230+
if RUBY_VERSION >= "2.1"
231+
describe "child 3 (Using TableSyntax)" do
232+
using RSpec::Parameterized::TableSyntax
233+
234+
where(:a, :b, :answer) do
235+
1 | 2 | 3
236+
five | eight | 13
237+
end
238+
239+
with_them do
240+
it "a plus b is answer" do
241+
expect(a + b).to eq answer
242+
end
243+
end
244+
end
245+
end
246+
247+
let(:eq_matcher) { eq(13) }
248+
describe "child 3 (use matcher)" do
249+
where(:a, :b, :matcher) do
250+
[
251+
[1 , 2 , eq(3) ],
252+
[five , eight , eq_matcher],
253+
]
254+
end
255+
256+
with_them do
257+
it "a plus b is answer" do
258+
expect(a + b).to matcher
259+
end
260+
end
261+
end
262+
end
263+
end
264+
end

0 commit comments

Comments
 (0)