rspec-parameterized 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Parameterized
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -6,6 +6,15 @@ module RSpec
6
6
  module Parameterized
7
7
  module ExampleGroupMethods
8
8
 
9
+ # capsulize parameter attributes
10
+ class Parameter
11
+ attr_reader :arg_names, :table_format, :block
12
+
13
+ def initialize(arg_names, table_format, &block)
14
+ @arg_names, @table_format, @block = arg_names, table_format, block
15
+ end
16
+ end
17
+
9
18
  # Set parameters to be bound in specs under this example group.
10
19
  #
11
20
  # ## Example
@@ -19,7 +28,7 @@ module RSpec
19
28
  # end
20
29
  #
21
30
  def where(*args, &b)
22
- set_parameters(args, b.call)
31
+ set_parameters(args, false, &b)
23
32
  end
24
33
 
25
34
  # Set parameters to be bound in specs under this example group.
@@ -34,7 +43,7 @@ module RSpec
34
43
  # end
35
44
  #
36
45
  def where_table(*args, &b)
37
- set_parameters(args, separate_table_like_block(b))
46
+ set_parameters(args, true, &b)
38
47
  end
39
48
 
40
49
  # Use parameters to execute the block.
@@ -48,27 +57,21 @@ module RSpec
48
57
  # end
49
58
  #
50
59
  def with_them(*args, &b)
51
- if @arg_names.nil? || @param_sets.nil?
60
+ if @parameter.nil?
52
61
  @parameterized_pending_cases ||= []
53
62
  @parameterized_pending_cases << [args, b]
54
63
  else
55
- define_cases(@arg_names, @param_sets, *args, &b)
64
+ define_cases(@parameter, *args, &b)
56
65
  end
57
66
  end
58
67
 
59
68
  private
60
- def set_parameters(arg_names, param_sets)
61
- @arg_names = arg_names
62
-
63
- @param_sets = if arg_names.count == 1 && !param_sets[0].is_a?(Array)
64
- param_sets.map { |x| Array[x] }
65
- else
66
- param_sets
67
- end
69
+ def set_parameters(arg_names, table_format, &b)
70
+ @parameter = Parameter.new(arg_names, table_format, &b)
68
71
 
69
72
  if @parameterized_pending_cases
70
73
  @parameterized_pending_cases.each { |e|
71
- define_cases(arg_names, param_sets, *e[0], &e[1])
74
+ define_cases(@parameter, *e[0], &e[1])
72
75
  }
73
76
  end
74
77
  end
@@ -94,16 +97,28 @@ module RSpec
94
97
  end
95
98
 
96
99
  def eval_sexp(sexp)
97
- self.instance_eval(ruby2ruby.process(sexp))
100
+ instance = new # for evaluate let methods.
101
+ instance.instance_eval(ruby2ruby.process(sexp))
98
102
  end
99
103
 
100
104
  def ruby2ruby
101
105
  @ruby2ruby ||= Ruby2Ruby.new
102
106
  end
103
107
 
104
- def define_cases(arg_names, param_sets, *args, &block)
108
+ def define_cases(parameter, *args, &block)
109
+ instance = new # for evaluate let methods.
110
+
111
+ if parameter.table_format
112
+ param_sets = separate_table_like_block(parameter.block)
113
+ else
114
+ param_sets = instance.instance_eval(&parameter.block)
115
+ end
116
+
117
+ # for only one parameters
118
+ param_sets = param_sets.map { |x| Array[x] } if !param_sets[0].is_a?(Array)
119
+
105
120
  param_sets.each do |params|
106
- pairs = [arg_names, params].transpose
121
+ pairs = [parameter.arg_names, params].transpose
107
122
  pretty_params = pairs.map {|t| "#{t[0]}: #{params_inspect(t[1])}"}.join(", ")
108
123
  describe(pretty_params, *args) do
109
124
  pairs.each do |n|
@@ -130,4 +130,53 @@ describe RSpec::Parameterized do
130
130
  end
131
131
  end
132
132
  end
133
+
134
+ context "when the where has let variables, defined by parent example group" do
135
+ describe "parent (define let)" do
136
+ let(:five) { 5 }
137
+ let(:eight) { 8 }
138
+
139
+ describe "child 1" do
140
+ where(:a, :b, :answer) do
141
+ [
142
+ [1 , 2 , 3],
143
+ [five , eight , 13],
144
+ ]
145
+ end
146
+
147
+ with_them do
148
+ it "a plus b is answer" do
149
+ (a + b).should == answer
150
+ end
151
+ end
152
+ end
153
+
154
+ describe "child 2 (where_table)" do
155
+ where_table(:a, :b, :answer) do
156
+ 1 | 2 | 3
157
+ five | eight | 13
158
+ end
159
+
160
+ with_them do
161
+ it "a plus b is answer" do
162
+ (a + b).should == answer
163
+ end
164
+ end
165
+ end
166
+
167
+ let(:eq_matcher) { eq(13) }
168
+ describe "child 3 (use matcher)" do
169
+ where_table(:a, :b, :answer) do
170
+ 1 | 2 | eq(3)
171
+ five | eight | eq_matcher
172
+ end
173
+
174
+ with_them do
175
+ it "a plus b is answer" do
176
+ (a + b).should answer
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
133
182
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-parameterized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  segments:
110
110
  - 0
111
- hash: 847540897
111
+ hash: 960420339
112
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements:
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  segments:
119
119
  - 0
120
- hash: 847540897
120
+ hash: 960420339
121
121
  requirements: []
122
122
  rubyforge_project:
123
123
  rubygems_version: 1.8.24