rspec-forward 0.1.6 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60586a2b506f34e4fe653293535ece8b206d47923a3fef6b0c2adfa5b82005ce
4
- data.tar.gz: 6c3384494e394a33996bc1580f15d3a3ae6d7a1976b196e98fd2ad1f73672c2a
3
+ metadata.gz: 6b4f8c6e7d8124ea73c31bf830dd1ee274b381e402f9f72075d42c605705dc1e
4
+ data.tar.gz: a222768f7062e6e05cfcbe3bf8cc23914a7e5f6f5a57457a7f56f89848604791
5
5
  SHA512:
6
- metadata.gz: 39019b0a6c054365e0f5e26d39f3081e07d91db3a42bcbf60d57f3907cd92761cf530e905916227f5919c2959a6e3f7c984960e3fc068ddc216159a2f324a6b2
7
- data.tar.gz: 46d07febe0734cc50532a5cf8c3772541153c47e98391c7f8a2b30f9b97670a8e98c633d02e22edacda53ace9b828f80cf6a4b243e5f2c14f703b66b3d479408
6
+ metadata.gz: 373959bf21c98b2788d98a10974b612c3ef0e625771329b97ba7d4097fc48f0c99a89220ec509beb135b5f3c513d0f0a90ecc1a6162f5274438791fb3fe4ad43
7
+ data.tar.gz: 9e4a8d5afa6d5d94f278e7613094ed2bf6a04f72297ca84fbf415e8e44cc29433cf3f86be3fd44bf340a485a1d664e25705589f3e35c79fd840c34c0b4054816
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.1.2
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in rspec-forward.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
6
+ gem "rake"
7
7
  gem "rspec", "~> 3.0"
data/Gemfile.lock CHANGED
@@ -1,23 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-forward (0.1.6)
5
- rspec-mocks (~> 3.9)
4
+ rspec-forward (0.3.1)
5
+ rspec-mocks (~> 3)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  coderay (1.1.3)
11
- diff-lcs (1.4.4)
12
- ffi (1.15.3)
13
- formatador (0.3.0)
14
- guard (2.17.0)
11
+ diff-lcs (1.5.0)
12
+ ffi (1.15.5)
13
+ formatador (1.1.0)
14
+ guard (2.18.0)
15
15
  formatador (>= 0.2.4)
16
16
  listen (>= 2.7, < 4.0)
17
17
  lumberjack (>= 1.0.12, < 2.0)
18
18
  nenv (~> 0.1)
19
19
  notiffany (~> 0.0)
20
- pry (>= 0.9.12)
20
+ pry (>= 0.13.0)
21
21
  shellany (~> 0.0)
22
22
  thor (>= 0.18.1)
23
23
  guard-compat (1.2.1)
@@ -25,7 +25,7 @@ GEM
25
25
  guard (~> 2.1)
26
26
  guard-compat (~> 1.1)
27
27
  rspec (>= 2.99.0, < 4.0)
28
- listen (3.5.1)
28
+ listen (3.7.1)
29
29
  rb-fsevent (~> 0.10, >= 0.10.3)
30
30
  rb-inotify (~> 0.9, >= 0.9.10)
31
31
  lumberjack (1.2.8)
@@ -37,25 +37,25 @@ GEM
37
37
  pry (0.14.1)
38
38
  coderay (~> 1.1)
39
39
  method_source (~> 1.0)
40
- rake (12.3.3)
41
- rb-fsevent (0.11.0)
40
+ rake (13.0.6)
41
+ rb-fsevent (0.11.1)
42
42
  rb-inotify (0.10.1)
43
43
  ffi (~> 1.0)
44
- rspec (3.10.0)
45
- rspec-core (~> 3.10.0)
46
- rspec-expectations (~> 3.10.0)
47
- rspec-mocks (~> 3.10.0)
48
- rspec-core (3.10.1)
49
- rspec-support (~> 3.10.0)
50
- rspec-expectations (3.10.1)
44
+ rspec (3.11.0)
45
+ rspec-core (~> 3.11.0)
46
+ rspec-expectations (~> 3.11.0)
47
+ rspec-mocks (~> 3.11.0)
48
+ rspec-core (3.11.0)
49
+ rspec-support (~> 3.11.0)
50
+ rspec-expectations (3.11.0)
51
51
  diff-lcs (>= 1.2.0, < 2.0)
52
- rspec-support (~> 3.10.0)
53
- rspec-mocks (3.10.2)
52
+ rspec-support (~> 3.11.0)
53
+ rspec-mocks (3.11.1)
54
54
  diff-lcs (>= 1.2.0, < 2.0)
55
- rspec-support (~> 3.10.0)
56
- rspec-support (3.10.2)
55
+ rspec-support (~> 3.11.0)
56
+ rspec-support (3.11.0)
57
57
  shellany (0.0.1)
58
- thor (1.1.0)
58
+ thor (1.2.1)
59
59
 
60
60
  PLATFORMS
61
61
  ruby
@@ -63,9 +63,9 @@ PLATFORMS
63
63
  DEPENDENCIES
64
64
  guard (~> 2.16)
65
65
  guard-rspec (~> 4.7)
66
- rake (~> 12.0)
66
+ rake
67
67
  rspec (~> 3.0)
68
68
  rspec-forward!
69
69
 
70
70
  BUNDLED WITH
71
- 2.2.3
71
+ 2.3.20
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Aleksander Długopolski
3
+ Copyright (c) 2022 Marz Drel
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -37,6 +37,8 @@ complete: `spring stop`.
37
37
 
38
38
  ## Usage
39
39
 
40
+ ### Forwarding from class method to instance of the same class
41
+
40
42
  Assume you have a Method Object with `#call` method defined like this:
41
43
 
42
44
  ```ruby
@@ -75,42 +77,54 @@ RSpec.describe Add do
75
77
  end
76
78
 
77
79
  describe ".call" do
78
- subject { described_class }
80
+ let(:klass) { described_class }
79
81
 
80
- it { should forward_to_instance(:call).with_2_args }
82
+ it "passes arguments to instance" do
83
+ expect(klass)
84
+ .to forward_to_instance(:call)
85
+ .with_2_args
86
+ end
81
87
  end
82
88
  end
83
89
  ```
84
90
 
85
- If you would rather not use the old, deprecated
86
- `should` syntax in RSpec, replace the above exaple with the following:
91
+ Possible calls to the matcher include the following:
87
92
 
88
93
  ```ruby
89
- describe ".call" do
90
- let(:klass) { described_class }
94
+ it { is_expected.to forward_to_instance(:call).with_no_args }
95
+ it { is_expected.to forward_to_instance(:call).with_1_arg }
96
+ it { is_expected.to forward_to_instance(:call).with_2_args }
97
+ it { is_expected.to forward_to_instance(:call).with_3_args }
98
+ it { is_expected.to forward_to_instance(:call).with_named(:foo, :bar) }
99
+ it { is_expected.to forward_to_instance(:call).with_1_arg_and_named(:foo) }
100
+ it { is_expected.to forward_to_instance(:call).with_2_args_and_named(:foo) }
101
+ it { is_expected.to forward_to_instance(:call).with_3_args_and_named(:foo, :bar) }
102
+ ```
91
103
 
92
- it "passes arguments to instance" do
93
- expect(klass)
94
- .to forward_to_instance(:call)
95
- .with_2_args
104
+ **TODO**: Explain `forward_to_instance_build(...)`
105
+
106
+ ### Forwarding from method to another class
107
+
108
+ Assume you have a method which just passes all args to another class.
109
+
110
+ ```ruby
111
+ class Add
112
+ def self.call(addend1, addend2)
113
+ @addend1 + @addend2
96
114
  end
97
115
  end
98
- ```
99
116
 
100
- Possible calls to the matcher include the following:
117
+ class Other
118
+ def add(...)
119
+ Add.call(...)
120
+ end
121
+ end
101
122
 
102
- ```ruby
103
- it { should forward_to_instance(:call).with_no_args }
104
- it { should forward_to_instance(:call).with_1_arg }
105
- it { should forward_to_instance(:call).with_2_args }
106
- it { should forward_to_instance(:call).with_3_args }
107
- it { should forward_to_instance(:call).with_named(:foo, :bar) }
108
- it { should forward_to_instance(:call).with_1_arg_and_named(:foo) }
109
- it { should forward_to_instance(:call).with_2_args_and_named(:foo) }
110
- it { should forward_to_instance(:call).with_3_args_and_named(:foo, :bar) }
123
+ Add.call(3, 5) # => 8
111
124
  ```
125
+ [...]
112
126
 
113
- **TODO**: Explain `forward_to_instance_build(...)`
127
+ **TODO**: Explain usage.
114
128
 
115
129
  ## Development
116
130
 
@@ -0,0 +1,21 @@
1
+ module ::RSpec
2
+ module Forward
3
+ module CoreExtensions
4
+ refine String do
5
+ def camelize(uppercase_first_letter = true)
6
+ string = self
7
+
8
+ if uppercase_first_letter
9
+ string = string.sub(/^[a-z\d]*/, &:capitalize)
10
+ else
11
+ string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
12
+ end
13
+
14
+ string
15
+ .gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
16
+ .gsub("/", "::")
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -12,7 +12,8 @@ module ::RSpec
12
12
 
13
13
  define_method name do |*kwargs|
14
14
  @args = Array.new(index, :arg)
15
- @kwargs = Hash[kwargs.map { [_1, _1] }]
15
+ @kwargs = Hash[kwargs.map { |name| [name, name] }]
16
+
16
17
  self
17
18
  end
18
19
  end
@@ -22,7 +23,11 @@ module ::RSpec
22
23
  end
23
24
 
24
25
  def with_1_arg_and_named(*args, **kwargs)
25
- with_1_args_and_named(*args, **kwargs)
26
+ if kwargs.empty?
27
+ with_1_args_and_named(*args)
28
+ else
29
+ with_1_args_and_named(*args, **kwargs)
30
+ end
26
31
  end
27
32
 
28
33
  def with_no_args
@@ -37,7 +42,7 @@ module ::RSpec
37
42
 
38
43
  def with_named(*kwargs)
39
44
  @args = []
40
- @kwargs = Hash[kwargs.map { [_1, _1] }]
45
+ @kwargs = Hash[kwargs.map { |name| [name, name] }]
41
46
  self
42
47
  end
43
48
 
@@ -73,16 +78,29 @@ module ::RSpec
73
78
  def matches_for?(actual, return_value)
74
79
  assign_actual(actual)
75
80
 
76
- allow(@actual)
77
- .to receive(:new)
78
- .with(*exp_args, **@kwargs)
79
- .and_return(instance)
81
+ if @kwargs.any?
82
+ allow(@actual)
83
+ .to receive(:new)
84
+ .with(*exp_args, **@kwargs)
85
+ .and_return(instance)
80
86
 
81
- result = @actual.send(@expected, *@args, **@kwargs) == return_value
87
+ result = @actual.send(@expected, *@args, **@kwargs) == return_value
82
88
 
83
- expect(@actual)
84
- .to have_received(:new)
85
- .with(*exp_args, **@kwargs)
89
+ expect(@actual)
90
+ .to have_received(:new)
91
+ .with(*exp_args, **@kwargs)
92
+ else
93
+ allow(@actual)
94
+ .to receive(:new)
95
+ .with(*exp_args)
96
+ .and_return(instance)
97
+
98
+ result = @actual.send(@expected, *@args) == return_value
99
+
100
+ expect(@actual)
101
+ .to have_received(:new)
102
+ .with(*exp_args)
103
+ end
86
104
 
87
105
  result
88
106
  end
@@ -0,0 +1,77 @@
1
+ module ::RSpec
2
+ module Forward
3
+ module ForwardScopeMethods
4
+ using CoreExtensions
5
+
6
+ def exp_args(actual)
7
+ return [actual, *@args] if @with_parent_arg
8
+ return [no_args] if @args.size.zero? && @kwargs.size.zero?
9
+
10
+ @args
11
+ end
12
+
13
+ def find_actual(klass_or_instance)
14
+ if klass_or_instance.is_a? Class
15
+ klass_or_instance
16
+ else
17
+ klass_or_instance.class
18
+ end
19
+ end
20
+
21
+ def matches_for?(klass_or_instance)
22
+ actual = find_actual(klass_or_instance)
23
+
24
+ method_name = @expected
25
+ base_klass = format("%<method>s_scope", method: method_name.to_s).camelize
26
+ @scope_klass_name ||= format("%s::%s", actual.to_s, base_klass)
27
+
28
+
29
+ begin
30
+ @scope_klass = Object.const_get(@scope_klass_name)
31
+ rescue NameError => e
32
+ failure_scope_klass_name_not_defined(@scope_klass_name)
33
+ return false
34
+ end
35
+
36
+ allow(@scope_klass)
37
+ .to receive(@target_method_name)
38
+ .and_return(:result)
39
+
40
+ if @kwargs.any?
41
+ result = klass_or_instance.public_send(method_name, *@args, **@kwargs) == :result
42
+
43
+ expect(@scope_klass)
44
+ .to have_received(@target_method_name)
45
+ .with(*exp_args(actual), **@kwargs)
46
+ else
47
+ result = klass_or_instance.public_send(method_name, *@args) == :result
48
+
49
+ expect(@scope_klass)
50
+ .to have_received(@target_method_name)
51
+ .with(*exp_args(actual))
52
+ end
53
+
54
+ result
55
+ end
56
+
57
+ def failure_scope_klass_name_not_defined(name)
58
+ @failure_message = "#{name} should be defined"
59
+ @failure_message_when_negated = "#{name} should be defined"
60
+ end
61
+
62
+ def failure_message
63
+ @failure_message ||
64
+ %Q(expected "#{@target}.call" to be invoked)
65
+ end
66
+
67
+ def failure_message_when_negated
68
+ @failure_message_when_negated ||
69
+ %Q(expected "#{@target}.call" not to be invoked)
70
+ end
71
+
72
+ def self.included(base)
73
+ private :matches_for?, :find_actual
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,32 @@
1
+ module ::RSpec
2
+ module Forward
3
+ module ForwardScopeParentMethods
4
+ using CoreExtensions
5
+
6
+ def using_method(target_method_name)
7
+ @target_method_name = target_method_name
8
+ self
9
+ end
10
+
11
+ def using_class_name(target_class_name)
12
+ @scope_klass_name = target_class_name
13
+ self
14
+ end
15
+
16
+ def using_method_args(*args, **kwargs)
17
+ @args = args
18
+ @kwargs = kwargs
19
+ self
20
+ end
21
+
22
+ def with_parent_arg
23
+ # Name of the parent class is not available here if the default
24
+ # class is used, because defaualt class needs the "actual"
25
+ # parameter from matchers_for?(actual)
26
+
27
+ @with_parent_arg = true
28
+ self
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module ::RSpec
2
+ module Forward
3
+ class ForwardToScope
4
+ include ForwardScopeMethods
5
+ include ForwardScopeParentMethods
6
+ include Mocks::ExampleMethods
7
+
8
+ def initialize(expected)
9
+ @expected = expected
10
+ @kwargs ||= {}
11
+ @args ||= []
12
+ @target_method_name ||= :call
13
+ end
14
+
15
+ def matches?(actual)
16
+ matches_for?(actual)
17
+ end
18
+
19
+ def description
20
+ <<~TXT.gsub(/\n+/, " ")
21
+ to pass the arguments to the constructor of instance and return
22
+ the value returned by the instance method
23
+ TXT
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Forward
3
- VERSION = "0.1.6"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
data/lib/rspec/forward.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  require "rspec"
2
2
  require "rspec/mocks"
3
3
  require "rspec/forward/version"
4
+ require "rspec/forward/core_extensions"
4
5
  require "rspec/forward/forward_methods"
6
+ require "rspec/forward/forward_scope_methods"
7
+ require "rspec/forward/forward_scope_parent_methods"
5
8
  require "rspec/forward/forward_to_instance"
6
9
  require "rspec/forward/forward_to_instance_build"
10
+ require "rspec/forward/forward_to_scope"
7
11
 
8
12
  module RSpec
9
13
  module Forward
@@ -16,6 +20,10 @@ module RSpec
16
20
  def forward_to_instance_build(expected)
17
21
  ForwardToInstanceBuild.new(expected)
18
22
  end
23
+
24
+ def forward_to_scope(expected)
25
+ ForwardToScope.new(expected)
26
+ end
19
27
  end
20
28
  end
21
29
 
@@ -40,7 +40,7 @@ Gem::Specification.new do |spec|
40
40
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
41
  spec.require_paths = ["lib"]
42
42
 
43
- spec.add_runtime_dependency "rspec-mocks", "~> 3.9"
43
+ spec.add_runtime_dependency "rspec-mocks", "~> 3"
44
44
  spec.add_development_dependency "guard", "~> 2.16"
45
45
  spec.add_development_dependency "guard-rspec", "~> 4.7"
46
46
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-forward
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marz Drel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-20 00:00:00.000000000 Z
11
+ date: 2022-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-mocks
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.9'
19
+ version: '3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.9'
26
+ version: '3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: guard
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -73,9 +73,13 @@ files:
73
73
  - bin/console
74
74
  - bin/setup
75
75
  - lib/rspec/forward.rb
76
+ - lib/rspec/forward/core_extensions.rb
76
77
  - lib/rspec/forward/forward_methods.rb
78
+ - lib/rspec/forward/forward_scope_methods.rb
79
+ - lib/rspec/forward/forward_scope_parent_methods.rb
77
80
  - lib/rspec/forward/forward_to_instance.rb
78
81
  - lib/rspec/forward/forward_to_instance_build.rb
82
+ - lib/rspec/forward/forward_to_scope.rb
79
83
  - lib/rspec/forward/version.rb
80
84
  - rspec-forward.gemspec
81
85
  homepage:
@@ -99,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
103
  - !ruby/object:Gem::Version
100
104
  version: '0'
101
105
  requirements: []
102
- rubygems_version: 3.2.3
106
+ rubygems_version: 3.3.7
103
107
  signing_key:
104
108
  specification_version: 4
105
109
  summary: Set of RSpec matchers for checking class method to instance method forwarding.