active_mocker 2.4.4 → 2.5.0
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +2 -2
- data/lib/active_mocker/mock_creator/defined_methods.rb +6 -18
- data/lib/active_mocker/mock_creator/safe_methods.rb +24 -0
- data/lib/active_mocker/mock_creator/scopes.rb +30 -2
- data/lib/active_mocker/mock_template/_scopes.erb +1 -1
- data/lib/active_mocker/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9471f85889f657625b27471d107dacf1f2a1e356
|
4
|
+
data.tar.gz: 36ca7ce095b75166f8148a11a39ab8f0ce502dd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cf121a9c56ed305c01c1b5445cb855f95e807f8475f1993e6f6e664da8b1cbfa9c648c553476aa1cca7bcc2ae6bd7e9277d13d6388ceec4379e21f8aed3f09a
|
7
|
+
data.tar.gz: f9d5c52020ae381bab78685780d0eeefa784de91348643196e5f5053539b0e7d59679e1c786372e1e6d0058b275dcb83f7d652c48f29adf97805692fc210dae2
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
## 2.5.0 - 2017-03-17
|
5
|
+
### Feature
|
6
|
+
- In comment Macro `ActiveMocker.safe_methods(*instance_methods, scopes: [], instance_methods: [])` Now accepts scope methods
|
7
|
+
|
3
8
|
## 2.4.4 - 2017-03-10
|
4
9
|
### Fix
|
5
10
|
- Really fix: scope methods returned `undefined method `call_mock_method' for nil:NilClass` for Mocks nested in modules.
|
data/README.md
CHANGED
@@ -221,10 +221,10 @@ ActiveMocker::LoadedMocks.features.enable(:stub_active_record_exceptions)
|
|
221
221
|
Be careful that it does not contain anything that ActiveMocker cannot run.
|
222
222
|
|
223
223
|
```ruby
|
224
|
-
# ActiveMocker.safe_methods :full_name
|
224
|
+
# ActiveMocker.safe_methods(scopes: [], instance_methods: [:full_name])
|
225
225
|
class User
|
226
226
|
def full_name
|
227
|
-
"#{first_name} + #{last_name}
|
227
|
+
"#{first_name} + #{last_name}"
|
228
228
|
end
|
229
229
|
end
|
230
230
|
```
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require_relative "safe_methods"
|
2
3
|
module ActiveMocker
|
3
4
|
class MockCreator
|
4
5
|
module DefinedMethods
|
6
|
+
include SafeMethods
|
5
7
|
Method = Struct.new(:name, :arguments, :body)
|
6
8
|
|
7
9
|
def instance_methods
|
8
10
|
meths = class_introspector.get_class.public_instance_methods(false).sort
|
9
|
-
if safe_methods.include?(:initialize)
|
10
|
-
meths << :initialize
|
11
|
-
end
|
11
|
+
meths << :initialize if safe_methods[:instance_methods].include?(:initialize)
|
12
12
|
meths.map { |m| create_method(m, :instance_method) }
|
13
13
|
end
|
14
14
|
|
@@ -22,22 +22,10 @@ module ActiveMocker
|
|
22
22
|
|
23
23
|
private
|
24
24
|
|
25
|
-
def safe_methods
|
26
|
-
@safe_methods ||= class_introspector.parsed_source.comments.flat_map do |comment|
|
27
|
-
if comment.text.include?("ActiveMocker.safe_methods")
|
28
|
-
ActiveMocker.module_eval(comment.text.delete("#"))
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module ActiveMocker
|
34
|
-
def self.safe_methods(*methods)
|
35
|
-
methods
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
25
|
def create_method(m, type)
|
40
|
-
|
26
|
+
plural_type = (type.to_s + "s").to_sym
|
27
|
+
if safe_methods[plural_type].include?(m)
|
28
|
+
raise "ActiveMocker.safe_methods(class_methods: []) is currently unsupported." if type == :method
|
41
29
|
def_method = class_introspector.parsed_source.defs.detect { |meth| meth.name == m }
|
42
30
|
Method.new(
|
43
31
|
m,
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module ActiveMocker
|
3
|
+
class MockCreator
|
4
|
+
module SafeMethods
|
5
|
+
BASE = { instance_methods: [], scopes: [], methods: [] }.freeze
|
6
|
+
|
7
|
+
def safe_methods
|
8
|
+
@safe_methods ||= class_introspector.parsed_source.comments.each_with_object(BASE.dup) do |comment, hash|
|
9
|
+
if comment.text.include?("ActiveMocker.safe_methods")
|
10
|
+
hash.merge!(ActiveMocker.module_eval(comment.text.delete("#")))
|
11
|
+
else
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ActiveMocker
|
18
|
+
def self.safe_methods(*arg_methods, scopes: [], instance_methods: [], class_methods: [])
|
19
|
+
{ instance_methods: arg_methods.concat(instance_methods), scopes: scopes, methods: class_methods }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,15 +1,43 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require_relative "safe_methods"
|
2
3
|
module ActiveMocker
|
3
4
|
class MockCreator
|
4
5
|
module Scopes
|
5
6
|
Method = Struct.new(:name, :arguments, :body)
|
7
|
+
include SafeMethods
|
6
8
|
|
7
9
|
def scope_methods
|
8
10
|
class_introspector.class_macros.select { |h| h.keys.first == :scope }.map do |h|
|
9
|
-
|
10
|
-
|
11
|
+
name, args = h.values.first.first
|
12
|
+
arguments = ReverseParameters.new(args, blocks_as_values: true)
|
13
|
+
body = scope_body(arguments, name)
|
14
|
+
Method.new(name, arguments, body)
|
11
15
|
end
|
12
16
|
end
|
17
|
+
|
18
|
+
def scope_body(arguments, name)
|
19
|
+
if safe_methods[:scopes].include?(name)
|
20
|
+
find_scope_body_from_ast(name)
|
21
|
+
else
|
22
|
+
"#{class_name}.send(:call_mock_method, " \
|
23
|
+
"method: '#{name}', " \
|
24
|
+
"caller: Kernel.caller, " \
|
25
|
+
"arguments: [#{arguments.arguments}])"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def ast_scopes
|
30
|
+
@ast_scopes ||= class_introspector
|
31
|
+
.parsed_source
|
32
|
+
.class_begin
|
33
|
+
.children
|
34
|
+
.select { |n| n.try(:type) == :send && n.try { children[1] == :scope } }
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_scope_body_from_ast(name)
|
38
|
+
scope = ast_scopes.detect { |n| n.children[2].children.first == name }
|
39
|
+
DissociatedIntrospection::RubyCode.build_from_ast(scope.children[3].children[2]).source
|
40
|
+
end
|
13
41
|
end
|
14
42
|
end
|
15
43
|
end
|
@@ -4,7 +4,7 @@ include <%= parent_class_inspector.parent_mock_name %>::Scopes
|
|
4
4
|
|
5
5
|
<% scope_methods.each do |method| -%>
|
6
6
|
def <%= method.name %><%= "(#{method.arguments.parameters})" unless method.arguments.parameters.to_a.empty? %>
|
7
|
-
<%=
|
7
|
+
<%= method.body %>
|
8
8
|
end
|
9
9
|
|
10
10
|
<% end -%>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mocker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -233,6 +233,7 @@ files:
|
|
233
233
|
- lib/active_mocker/mock_creator/defined_methods.rb
|
234
234
|
- lib/active_mocker/mock_creator/modules_constants.rb
|
235
235
|
- lib/active_mocker/mock_creator/recreate_class_method_calls.rb
|
236
|
+
- lib/active_mocker/mock_creator/safe_methods.rb
|
236
237
|
- lib/active_mocker/mock_creator/scopes.rb
|
237
238
|
- lib/active_mocker/mock_template.erb
|
238
239
|
- lib/active_mocker/mock_template/_associations.erb
|