rspec-parameterized 0.0.1 → 0.0.2

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.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,4 +1,6 @@
1
1
  require "rspec/parameterized/version"
2
+ require 'ruby2ruby'
3
+ require 'sourcify'
2
4
 
3
5
  module RSpec
4
6
  module Parameterized
@@ -17,8 +19,22 @@ module RSpec
17
19
  # end
18
20
  #
19
21
  def where(*args, &b)
20
- @arg_names = args
21
- @param_sets = b.call
22
+ set_parameters(args, b.call)
23
+ end
24
+
25
+ # Set parameters to be bound in specs under this example group.
26
+ # You can separate fields with | like a cucumber table.
27
+ #
28
+ # ## Example
29
+ #
30
+ # where(:a, :b, :answer) do
31
+ # 1 | 2 | 3
32
+ # 5 | 8 | 13
33
+ # 0 | 0 | 0
34
+ # end
35
+ #
36
+ def where_table(*args, &b)
37
+ set_parameters(args, separate_table_like_block(b))
22
38
  end
23
39
 
24
40
  # Use parameters to execute the block.
@@ -31,9 +47,51 @@ module RSpec
31
47
  # end
32
48
  # end
33
49
  #
34
- def with_them(*args, &block)
35
- arg_names = @arg_names
36
- @param_sets.each do |params|
50
+ def with_them(*args, &b)
51
+ if @arg_names.nil? || @param_sets.nil?
52
+ @parameterized_pending_cases ||= []
53
+ @parameterized_pending_cases << [args, b]
54
+ else
55
+ define_cases(@arg_names, @param_sets, *args, &b)
56
+ end
57
+ end
58
+
59
+ private
60
+ def set_parameters(arg_names, param_sets)
61
+ @arg_names = arg_names
62
+ @param_sets = param_sets
63
+ if @parameterized_pending_cases
64
+ @parameterized_pending_cases.each { |e|
65
+ define_cases(arg_names, param_sets, *e[0], &e[1])
66
+ }
67
+ end
68
+ end
69
+
70
+ def separate_table_like_block(b)
71
+ sexp = b.to_sexp(:strip_enclosure => true)
72
+
73
+ lines = sexp.find_nodes(:call)
74
+ lines.map do |l|
75
+ rev_insts = []
76
+ while l.sexp_type == :call and l[2] == :|
77
+ rev_insts << eval_sexp(l[3][1])
78
+ l = l[1]
79
+ end
80
+ rev_insts << eval_sexp(l)
81
+ rev_insts.reverse
82
+ end
83
+ end
84
+
85
+ def eval_sexp(sexp)
86
+ self.instance_eval(ruby2ruby.process(sexp))
87
+ end
88
+
89
+ def ruby2ruby
90
+ @ruby2ruby ||= Ruby2Ruby.new
91
+ end
92
+
93
+ def define_cases(arg_names, param_sets, *args, &block)
94
+ param_sets.each do |params|
37
95
  describe(params.inspect, *args) do
38
96
  [arg_names, params].transpose.each do |n|
39
97
  let(n[0]) { n[1] }
@@ -10,6 +10,8 @@ I was inspired by [udzura's mock](https://gist.github.com/1881139).}
10
10
  gem.homepage = ""
11
11
 
12
12
  gem.add_dependency('rspec', '~>2.10.0')
13
+ gem.add_dependency('sourcify', '~> 0.5.0')
14
+ gem.add_dependency('ruby2ruby', '~> 1.3.1')
13
15
  gem.add_development_dependency('rake')
14
16
 
15
17
  gem.files = `git ls-files`.split($\)
@@ -11,27 +11,81 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
11
11
  # should do additions
12
12
 
13
13
  describe RSpec::Parameterized do
14
- describe "Sample" do
15
- describe "plus" do
16
- where(:a, :b, :answer) do
17
- [
18
- [1 , 2 , 3],
19
- [5 , 8 , 13],
20
- [0 , 0 , 0]
21
- ]
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
+ (a + b).should == answer
26
+ end
27
+ end
28
+
29
+ with_them :pending do
30
+ it "should do additions" do
31
+ (a + b).should == answer
22
32
  end
33
+ end
34
+ end
35
+
36
+ describe "table separated with pipe" do
37
+ where_table(:a, :b, :answer) do
38
+ 1 | 2 | 3
39
+ "hello " | "world" | "hello world"
40
+ [1, 2, 3] | [4, 5, 6] | [1, 2, 3, 4, 5, 6]
41
+ end
23
42
 
24
- with_them do
25
- it "should do additions" do
26
- (a + b).should == answer
27
- end
43
+ with_them do
44
+ it "a plus b is answer" do
45
+ (a + b).should == answer
28
46
  end
47
+ end
48
+ end
29
49
 
30
- with_them :pending do
31
- it "should do additions" do
32
- (a + b).should == answer
33
- end
50
+ context "when the where block is after with_them" do
51
+ with_them do
52
+ it "should do additions" do
53
+ (a + b).should == answer
34
54
  end
35
55
  end
56
+
57
+ with_them do
58
+ subject { a }
59
+ it { should be_is_a Numeric }
60
+ end
61
+
62
+ where(:a, :b, :answer) do
63
+ [
64
+ [1 , 2 , 3],
65
+ [5 , 8 , 13],
66
+ [0 , 0 , 0]
67
+ ]
68
+ end
69
+ end
70
+
71
+ context "when the where block is between with_thems" do
72
+ with_them do
73
+ it "should do additions" do
74
+ (a + b).should == answer
75
+ end
76
+ end
77
+
78
+ where(:a, :b, :answer) do
79
+ [
80
+ [1 , 2 , 3],
81
+ [5 , 8 , 13],
82
+ [0 , 0 , 0]
83
+ ]
84
+ end
85
+
86
+ with_them do
87
+ subject { a }
88
+ it { should be_is_a Numeric }
89
+ end
36
90
  end
37
91
  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.1
4
+ version: 0.0.2
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-05-20 00:00:00.000000000 Z
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 2.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: sourcify
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.5.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.5.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: ruby2ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: rake
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -76,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
108
  version: '0'
77
109
  segments:
78
110
  - 0
79
- hash: -316469805
111
+ hash: -875834415
80
112
  required_rubygems_version: !ruby/object:Gem::Requirement
81
113
  none: false
82
114
  requirements:
@@ -85,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
117
  version: '0'
86
118
  segments:
87
119
  - 0
88
- hash: -316469805
120
+ hash: -875834415
89
121
  requirements: []
90
122
  rubyforge_project:
91
123
  rubygems_version: 1.8.24