rspec-with-args 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 363d298854a03c2ec3babea22fcaa61f31652744
4
+ data.tar.gz: 2e2de1d3f2325526ce3d2b3556e0934e1843f667
5
+ SHA512:
6
+ metadata.gz: 64130abd4d1d03f683d36815190896fdf330333f27c129d52b0cd1ba537408e5a403183923a6207e6cea57f83f6941c6c5559393e078072596465327c49ab0da
7
+ data.tar.gz: b5a2daf772518389537c75c0e0ead6995a10f2063d73a5df8d222e21e87aafcaad1d308b5951c48d95871bcab9013b50e8720f370f7ea3d2bc8db3582ab315aa
@@ -0,0 +1 @@
1
+ require File.expand_path('../with_args.rb', __FILE__)
@@ -0,0 +1,159 @@
1
+ module RSpec::WithArgs
2
+ module CommonClassMethods
3
+ def descriptee(m)
4
+ m[:example_group][:description_args].first
5
+ end
6
+ end
7
+
8
+ module CommonInstanceMethods
9
+ def subject_args
10
+ metadata.fetch(:with_args,[])
11
+ end
12
+
13
+ def metadata
14
+ self.class.metadata
15
+ end
16
+ end
17
+
18
+ module ClassExampleGroup
19
+ extend CommonClassMethods
20
+ include CommonInstanceMethods
21
+ RSpec.configure do |c|
22
+ c.include self, {with_args:
23
+ proc do |a, m|
24
+ descriptee(m).is_a?(Class)
25
+ end
26
+ }
27
+ end
28
+
29
+ def self.included(base)
30
+ set_subject_class_proc(base.metadata)
31
+ set_initialization_args(base.metadata)
32
+
33
+ base.subject do
34
+ instance_eval &subject_class_proc
35
+ end
36
+ end
37
+
38
+ def self.set_subject_class_proc(metadata)
39
+ metadata[:subject_class_proc] ||= proc do |x|
40
+ described_class.new(*initialization_args.map{|a| send(a)})
41
+ end
42
+ end
43
+
44
+ def self.set_initialization_args(metadata)
45
+ metadata[:initialization_args] = metadata.fetch(:with_args,[])
46
+ end
47
+
48
+ def subject_class_proc
49
+ metadata[:subject_class_proc]
50
+ end
51
+
52
+ def initialization_args
53
+ metadata[:initialization_args]
54
+ end
55
+ end
56
+
57
+ module ClassMethodExampleGroup
58
+ extend CommonClassMethods
59
+ include CommonInstanceMethods
60
+ RSpec.configure do |c|
61
+ c.include self, {with_args:
62
+ lambda do |a, m|
63
+ descriptee(m).to_s.match(/^\./)
64
+ end
65
+ }
66
+ end
67
+
68
+ def self.included(base)
69
+ set_subject_proc(base.metadata)
70
+ set_subject_method_name(base.metadata)
71
+
72
+ base.subject do
73
+ instance_eval &subject_proc
74
+ end
75
+ end
76
+
77
+ def self.set_subject_proc(metadata)
78
+ metadata[:subject_proc] = proc do |x|
79
+ described_class.send(
80
+ subject_method_name,
81
+ *subject_args.map{|a| send(a)}
82
+ )
83
+ end
84
+ end
85
+
86
+ def self.set_subject_method_name(metadata)
87
+ case d = metadata[:example_group][:description_args].first
88
+ when is_a?(Class)
89
+ metadata[:subject_method_name] = d
90
+ when /^[.#]/
91
+ metadata[:subject_method_name] = d.gsub /^[.#]/, ""
92
+ end
93
+ end
94
+
95
+ def subject_proc
96
+ metadata[:subject_proc]
97
+ end
98
+
99
+ def subject_method_name
100
+ metadata[:subject_method_name]
101
+ end
102
+ end
103
+
104
+ module InstanceMethodExampleGroup
105
+ extend CommonClassMethods
106
+ include CommonInstanceMethods
107
+ RSpec.configure do |c|
108
+ c.include self, {with_args:
109
+ lambda do |a, m|
110
+ descriptee(m).to_s.match(/^#/)
111
+ end
112
+ }
113
+ end
114
+
115
+ def self.included(base)
116
+ set_subject_method_proc(base.metadata)
117
+ set_subject_method_name(base.metadata)
118
+
119
+ base.subject do
120
+ callee
121
+ end
122
+ end
123
+
124
+ def self.set_subject_method_proc(metadata)
125
+ metadata[:subject_method_proc] = proc do |x|
126
+ x.send(
127
+ subject_method_name,
128
+ *subject_args.map{|a| send(a)}
129
+ )
130
+ end
131
+ end
132
+
133
+ def self.set_subject_method_name(metadata)
134
+ case d = metadata[:example_group][:description_args].first
135
+ when is_a?(Class)
136
+ metadata[:subject_method_name] = d
137
+ when /^[.#]/
138
+ metadata[:subject_method_name] = d.gsub /^[.#]/, ""
139
+ end
140
+ end
141
+
142
+ def callee
143
+ instance_exec parent_callee, &subject_method_proc
144
+ end
145
+
146
+ def parent_callee
147
+ instance_eval &metadata[:subject_class_proc]
148
+ end
149
+
150
+ def subject_method_proc
151
+ metadata[:subject_method_proc]
152
+ end
153
+
154
+ def subject_method_name
155
+ metadata[:subject_method_name]
156
+ end
157
+ end
158
+
159
+ end
@@ -0,0 +1,68 @@
1
+ require 'rspec-with-args'
2
+
3
+ class Foo < Struct.new(:arg1, :arg2)
4
+ def foo(m_arg1)
5
+ m_arg1 + arg1
6
+ end
7
+
8
+ def self.class_method(c_arg1, c_arg2)
9
+ c_arg1 + c_arg2
10
+ end
11
+ end
12
+
13
+ describe Foo, with_args: [:bar, :baz] do
14
+ let(:bar) { "bar" }
15
+ let(:baz) { "baz" }
16
+
17
+ context "subject initialization" do
18
+ it { should eq(Foo.new(bar, baz)) }
19
+
20
+ context "with changed arguments" do
21
+ let(:bar) { "bee" }
22
+ let(:baz) { "boo" }
23
+
24
+ it { should eq(Foo.new(bar, baz)) }
25
+ end
26
+ end
27
+
28
+ context "explicitly setting the subject" do
29
+ subject { described_class.new(fuu) }
30
+ let(:fuu) { "fuu" }
31
+
32
+ it "overrides with-args default subject" do
33
+ subject.arg1.should eq(fuu)
34
+ end
35
+ end
36
+
37
+ context "method arguments" do
38
+ describe "#foo", with_args: [:zoo] do
39
+ let(:zoo) { "zoo" }
40
+ context "has access to initialization arguments" do
41
+ it { should eq(zoo + bar) }
42
+ end
43
+
44
+ context "with changed arguments" do
45
+ let(:zoo) { "zuu" }
46
+ let(:bar) { "baar" }
47
+
48
+ it { should eq(zoo + bar) }
49
+ end
50
+ end
51
+ end
52
+
53
+ context "class method arguments" do
54
+ describe ".class_method", with_args: [:zoo, :bar] do
55
+ let(:zoo) { "zoo" }
56
+ let(:bar) { "bar" }
57
+
58
+ it { should eq(zoo + bar) }
59
+
60
+ context "with changed arguments" do
61
+ let(:zoo) { "zuu" }
62
+ let(:bar) { "baar" }
63
+
64
+ it { should eq(zoo + bar) }
65
+ end
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-with-args
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Stannard
8
+ - Kelly Stannard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: |
15
+ rspec-with-args attempts to cut out having to explicitly write the subject of your example group when trying to call methods or add arguments to methods. It works by reading the conventional description syntax to determine what the subject is. Then allowing you to explicitly state what variables will be passed in as arguments.
16
+
17
+ Currently it supports initialization, class methods, and instance methods.
18
+ email: kwstannard@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/rspec-with-args.rb
24
+ - lib/with_args.rb
25
+ - spec/with_args_spec.rb
26
+ homepage: https://github.com/kwstannard/rspec-with-args
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: adding arguments to the subject
50
+ test_files:
51
+ - spec/with_args_spec.rb