rspec-advanced_subject 0.0.1 → 0.0.2.1

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: 722cf7491396807cf442c184f6d94431218ea4ff
4
- data.tar.gz: 3f203f3370aae6b2a6504c836bfd8e213a876c03
3
+ metadata.gz: e98db86f51ac4f784eef4d14e18fd0452adeeee2
4
+ data.tar.gz: c658fb02bc1b28a986b7d25d20bcb64dcd286d28
5
5
  SHA512:
6
- metadata.gz: a44a957f9366e20f87a5145aaff09cebcc0649b21d549b3226ff6362eb83727b6c302eaff11e14e51f12de56cf230fcf5b8fc65a4a746c05e63f7af79152e8f0
7
- data.tar.gz: 1da7e0499723a02967fb459b75b376f07a1315944752c23d4dc2e477d971678812c85302d126c5f69a566e8a2e7e7a1fcebfca76f49dc6b1657167bf0e108ca2
6
+ metadata.gz: b859a3376f312d18f9fc5f2cc5477b64cbad00a7e9210f36233928080af8f2b40e8acfb7cf2b130cae20059d1c961b1cf720606ee878e391715496eb039eaef2
7
+ data.tar.gz: 90681ff5a62c75f81ee61d6f82c1dca575193b4a984d821e8690ab5addcd5e592401b9d0e0fe65a7217e1e4202a0a2d1e3ef7abef0a06469b3ee7a387702f6d0
@@ -0,0 +1,43 @@
1
+ require_relative 'common_methods'
2
+
3
+ module RSpec::AdvancedSubject
4
+ module ClassExampleGroup
5
+ extend CommonClassMethods
6
+ include CommonInstanceMethods
7
+
8
+ RSpec.configure do |c|
9
+ c.include self, {advanced_subject:
10
+ proc do |a, m|
11
+ descriptee(m).is_a?(Class)
12
+ end
13
+ }
14
+ end
15
+
16
+ def self.included(base)
17
+ set_subject_class_proc(base.metadata)
18
+ set_initialization_args(base.metadata)
19
+
20
+ base.subject do
21
+ instance_eval &subject_class_proc
22
+ end
23
+ end
24
+
25
+ def self.set_subject_class_proc(metadata)
26
+ metadata[:subject_class_proc] ||= proc do |x|
27
+ described_class.new(*initialization_args)
28
+ end
29
+ end
30
+
31
+ def self.set_initialization_args(metadata)
32
+ metadata[:initialization_args] = metadata.fetch(:with_args,[])
33
+ end
34
+
35
+ def subject_class_proc
36
+ metadata[:subject_class_proc]
37
+ end
38
+
39
+ def initialization_args
40
+ metadata.fetch(:initialization_args, Array.new)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ module RSpec::AdvancedSubject::ClassMethodExampleGroup
2
+ extend RSpec::AdvancedSubject::CommonClassMethods
3
+ include RSpec::AdvancedSubject::CommonInstanceMethods
4
+ RSpec.configure do |c|
5
+ c.include self, {advanced_subject:
6
+ lambda do |a, m|
7
+ descriptee(m).to_s.match(class_regex)
8
+ end
9
+ }
10
+ end
11
+
12
+ def self.included(base)
13
+ set_subject_proc(base.metadata)
14
+ set_subject_method_name(base.metadata)
15
+
16
+ base.subject do
17
+ raise NotImplementedError if metadata[:with_args].nil?
18
+ instance_eval &subject_proc
19
+ end
20
+ end
21
+
22
+ def self.set_subject_proc(metadata)
23
+ metadata[:subject_proc] = proc do |x|
24
+ described_class.send(
25
+ subject_method_name,
26
+ *subject_args
27
+ )
28
+ end
29
+ end
30
+
31
+ def self.set_subject_method_name(metadata)
32
+ case d = metadata[:example_group][:description_args].first
33
+ when is_a?(Class)
34
+ metadata[:subject_method_name] = d
35
+ when class_regex
36
+ metadata[:subject_method_name] = d.gsub class_regex, ""
37
+ end
38
+ end
39
+
40
+ def self.class_regex
41
+ /^\./
42
+ end
43
+
44
+ def subject_proc
45
+ metadata[:subject_proc]
46
+ end
47
+
48
+ def subject_method_name
49
+ metadata[:subject_method_name]
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ module RSpec::AdvancedSubject
2
+ module CommonClassMethods
3
+ def descriptee(m)
4
+ m[:example_group][:description_args].first
5
+ end
6
+ end
7
+
8
+ module RSpec::AdvancedSubject::CommonInstanceMethods
9
+
10
+ def subject_args
11
+ metadata.fetch(:with_args, Array.new)
12
+ end
13
+
14
+ def subject_class_proc
15
+ metadata[:subject_class_proc]
16
+ end
17
+
18
+ def metadata
19
+ self.class.metadata
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,59 @@
1
+ module RSpec::AdvancedSubject::InstanceMethodExampleGroup
2
+ extend RSpec::AdvancedSubject::CommonClassMethods
3
+ include RSpec::AdvancedSubject::CommonInstanceMethods
4
+
5
+ RSpec.configure do |c|
6
+ c.include self, {advanced_subject:
7
+ ->(a,m) do
8
+ descriptee(m).to_s.match(instance_regex)
9
+ end
10
+ }
11
+ end
12
+
13
+ def self.included(base)
14
+ set_subject_method_proc(base.metadata)
15
+ set_subject_method_name(base.metadata)
16
+
17
+ base.subject do
18
+ raise NotImplementedError if metadata[:with_args].nil?
19
+ callee
20
+ end
21
+ end
22
+
23
+ def self.set_subject_method_proc(metadata)
24
+ metadata[:subject_method_proc] = Proc.new do |x|
25
+ x.send(
26
+ subject_method_name, *subject_args
27
+ )
28
+ end
29
+ end
30
+
31
+ def self.set_subject_method_name(metadata)
32
+ case d = metadata[:example_group][:description_args].first
33
+ when is_a?(Class)
34
+ metadata[:subject_method_name] = d
35
+ when instance_regex
36
+ metadata[:subject_method_name] = d.gsub instance_regex, ""
37
+ end
38
+ end
39
+
40
+ def self.instance_regex
41
+ /^#/
42
+ end
43
+
44
+ def callee
45
+ instance_exec parent_callee, &subject_method_proc
46
+ end
47
+
48
+ def parent_callee
49
+ instance_eval &subject_class_proc
50
+ end
51
+
52
+ def subject_method_proc
53
+ metadata[:subject_method_proc]
54
+ end
55
+
56
+ def subject_method_name
57
+ metadata[:subject_method_name]
58
+ end
59
+ end
@@ -0,0 +1,39 @@
1
+ class RSpec::Core::ExampleGroup
2
+ def self.when_passed(*args, &example_group_block)
3
+ ::RSpec::AdvancedSubject::Core.when_passed(*args, self, &example_group_block)
4
+ end
5
+
6
+ def self.when_initialized_with(*args, &example_group_block)
7
+ ::RSpec::AdvancedSubject::Core.when_initialized_with(*args, self, &example_group_block)
8
+ end
9
+
10
+ define_example_group_method :describe, advanced_subject: true
11
+ end
12
+
13
+ module RSpec::AdvancedSubject::Core
14
+ def self.when_passed(*args, example_groups, &example_group_block)
15
+ example_groups.context(method_describe_string(*args), with_args: args, &example_group_block)
16
+ end
17
+
18
+ def self.when_initialized_with(*args, example_groups, &example_group_block)
19
+ example_groups.context(initialization_describe_string(*args), initialization_args: args, &example_group_block)
20
+ end
21
+
22
+ private
23
+
24
+ def self.method_describe_string(*args)
25
+ "when passed #{argument_joiner(args)}"
26
+ end
27
+
28
+ def self.initialization_describe_string(*args)
29
+ "when passed #{argument_joiner(args)}"
30
+ end
31
+
32
+ def self.argument_joiner(args)
33
+ if args.empty?
34
+ "nothing"
35
+ else
36
+ args.map(&:inspect).join(", ")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,57 @@
1
+ require 'rspec-advanced_subject'
2
+
3
+ class Comparator
4
+ def ==(other)
5
+ other.instance_variables == self.instance_variables
6
+ end
7
+ end
8
+
9
+ class NoArgs < Comparator; end
10
+
11
+ describe NoArgs do
12
+ when_initialized_with do
13
+ it { should eq(NoArgs.new) }
14
+ end
15
+ end
16
+
17
+ class InitializationMock < Comparator
18
+ def initialize(foo); @foo = foo; end
19
+ end
20
+
21
+ describe InitializationMock do
22
+ when_initialized_with 5 do
23
+ it { should eq(InitializationMock.new(5)) }
24
+ end
25
+ end
26
+
27
+ class MultiArgsInitializationMock < Comparator
28
+ def initialize(*args); @foo,@bar = *args; end
29
+ end
30
+
31
+ describe MultiArgsInitializationMock do
32
+ when_initialized_with do
33
+ it { should eq(MultiArgsInitializationMock.new) }
34
+ end
35
+
36
+ when_initialized_with 5, 10 do
37
+ it { should eq(MultiArgsInitializationMock.new(5, 10)) }
38
+ end
39
+ end
40
+
41
+ class InitializeAndMethodCall
42
+ def initialize(foo); @foo = foo; end
43
+
44
+ def instance_method(arg)
45
+ [@foo, arg]
46
+ end
47
+ end
48
+
49
+ describe InitializeAndMethodCall do
50
+ when_initialized_with 5 do
51
+ describe "#instance_method" do
52
+ when_passed 10 do
53
+ it { should eq([5,10]) }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,53 @@
1
+ require 'rspec-advanced_subject'
2
+
3
+ class ClassMock
4
+ def self.single_argument(arg)
5
+ arg
6
+ end
7
+
8
+ def self.multiple_arguments(*args)
9
+ args
10
+ end
11
+ end
12
+
13
+ describe ClassMock do
14
+ describe ".single_argument" do
15
+ when_passed 5 do
16
+ it { should eq(5) }
17
+ end
18
+
19
+ when_passed 1 do
20
+ it { should eq(1) }
21
+ end
22
+
23
+ context "when explicitly setting subject" do
24
+ subject { described_class.single_argument(5) }
25
+ it { should eq(5) }
26
+ end
27
+
28
+ context "when using default subject without passing arguments" do
29
+ it "raises NotImplementedError" do
30
+ expect{subject.single_argument(5)}.to raise_error(NotImplementedError)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe ".multiple_arguments" do
36
+ when_passed do
37
+ it { should eq([]) }
38
+ end
39
+
40
+ when_passed 5, 5 do
41
+ it { should eq([5, 5]) }
42
+ end
43
+
44
+ when_passed *[1,2,3,4] do
45
+ it { should eq([1,2,3,4]) }
46
+ end
47
+
48
+ context "when explicitly setting subject" do
49
+ subject { described_class.multiple_arguments(5) }
50
+ it { should eq([5]) }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ require 'rspec-advanced_subject'
2
+
3
+ class InstanceMock
4
+ def single_argument(arg)
5
+ arg
6
+ end
7
+
8
+ def multiple_arguments(*args)
9
+ args
10
+ end
11
+ end
12
+
13
+ describe InstanceMock do
14
+ describe "#single_argument" do
15
+ when_passed 5 do
16
+ it { is_expected.to eq(5) }
17
+ end
18
+
19
+ when_passed 1 do
20
+ it { is_expected.to eq(1) }
21
+ end
22
+
23
+ context "when explicitly setting subject" do
24
+ subject { described_class.new.single_argument(5) }
25
+ it { is_expected.to eq(5) }
26
+ end
27
+
28
+ context "when using default subject without passing arguments" do
29
+ it "raises NotImplementedError" do
30
+ expect{subject.single_argument(5)}.to raise_error(NotImplementedError)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#multiple_arguments" do
36
+ when_passed do
37
+ it { is_expected.to eq([]) }
38
+ end
39
+
40
+ when_passed 5, 5 do
41
+ it { is_expected.to eq([5, 5]) }
42
+ end
43
+
44
+ when_passed *[1,2,3,4] do
45
+ it { is_expected.to eq([1,2,3,4]) }
46
+ end
47
+
48
+ context "when explicitly setting subject" do
49
+ subject { described_class.new.multiple_arguments(5) }
50
+ it { is_expected.to eq([5]) }
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,15 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-advanced_subject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Stannard
8
8
  - Kelly Stannard
9
+ - Kelly Stannard
10
+ - Kelly Stannard
11
+ - Kelly Stannard
9
12
  autorequire:
10
13
  bindir: bin
11
14
  cert_chain: []
12
- date: 2014-07-27 00:00:00.000000000 Z
15
+ date: 2014-07-28 00:00:00.000000000 Z
13
16
  dependencies:
14
17
  - !ruby/object:Gem::Dependency
15
18
  name: rspec
@@ -33,12 +36,24 @@ description: |
33
36
  describe Hash do
34
37
  when_initialized_with [:a, :b] do
35
38
  it { should eq({a: :b}) }
39
+
40
+ describe '#fetch' do
41
+ when_passed :a do
42
+ it { should eq(:b) }
43
+ end
44
+ end
36
45
  end
37
46
  end
38
47
  ```
39
48
 
40
49
  When you run `rspec -f d advanced_subject_spec.rb` it will output:
41
50
  ```
51
+ Hash
52
+ when initialized with [:a, :b]
53
+ should eq {:a => :b}
54
+ #fetch
55
+ when passed :a
56
+ should eq :b
42
57
  ```
43
58
  email: kwstannard@gmail.com
44
59
  executables: []
@@ -46,6 +61,14 @@ extensions: []
46
61
  extra_rdoc_files: []
47
62
  files:
48
63
  - lib/rspec-advanced_subject.rb
64
+ - lib/rspec-advanced_subject/class_example_group.rb
65
+ - lib/rspec-advanced_subject/class_method_example_group.rb
66
+ - lib/rspec-advanced_subject/common_methods.rb
67
+ - lib/rspec-advanced_subject/instance_method_example_group.rb
68
+ - lib/rspec-advanced_subject/when_passed.rb
69
+ - spec/advanced_subject/class_example_group_spec.rb
70
+ - spec/advanced_subject/class_method_example_group_spec.rb
71
+ - spec/advanced_subject/instance_method_spec.rb
49
72
  homepage: https://github.com/kwstannard/rspec-advanced_subject
50
73
  licenses:
51
74
  - MIT
@@ -70,4 +93,7 @@ rubygems_version: 2.2.2
70
93
  signing_key:
71
94
  specification_version: 4
72
95
  summary: adding arguments to the subject
73
- test_files: []
96
+ test_files:
97
+ - spec/advanced_subject/class_example_group_spec.rb
98
+ - spec/advanced_subject/class_method_example_group_spec.rb
99
+ - spec/advanced_subject/instance_method_spec.rb