evt-mimic 1.2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/mimic.rb +13 -0
- data/lib/mimic/build.rb +25 -0
- data/lib/mimic/class.rb +32 -0
- data/lib/mimic/controls.rb +11 -0
- data/lib/mimic/controls/invocation.rb +17 -0
- data/lib/mimic/controls/mimic.rb +9 -0
- data/lib/mimic/controls/recorder.rb +13 -0
- data/lib/mimic/controls/subject.rb +32 -0
- data/lib/mimic/controls/subject/block_parameter.rb +12 -0
- data/lib/mimic/controls/subject/mixed_parameters.rb +12 -0
- data/lib/mimic/controls/subject/multiple_assignment_named_parameter.rb +12 -0
- data/lib/mimic/controls/subject/multiple_assignment_parameter.rb +12 -0
- data/lib/mimic/controls/subject/named_parameters.rb +12 -0
- data/lib/mimic/controls/subject/no_parameters.rb +12 -0
- data/lib/mimic/controls/subject/positional_parameters.rb +12 -0
- data/lib/mimic/define_methods.rb +79 -0
- data/lib/mimic/mimic.rb +6 -0
- data/lib/mimic/preserved_methods.rb +5 -0
- data/lib/mimic/proofs.rb +1 -0
- data/lib/mimic/proofs/method/parameters.rb +28 -0
- data/lib/mimic/recorder.rb +53 -0
- data/lib/mimic/remove_methods.rb +13 -0
- data/lib/mimic/subject_methods.rb +10 -0
- data/lib/mimic/void.rb +9 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 531e81d3a7bf58ad75e07c0d24f12fe092dfea068455a99b03bdded840ae8cd9
|
4
|
+
data.tar.gz: e96368d98bec7c7153693dd1a0d52373bf57750ba18aec12d7a6ff87187f9cfc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b4055c9ce7f3c6caad3779ba0775f5b7704d97c410b0cd6230624122fbe8f84d23c1dd2154b675a43487c7f8f2659db464a1a7f35df1c8585519aa69741ef32
|
7
|
+
data.tar.gz: edeb321468a5bd20fc6d6f15aa8893136ac910ae016da04463b10d5d60ec52141b8d0835d551262f21fecdfe7de5a59a92dbb760f288991103d881c9bf0d993b
|
data/lib/mimic.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
require 'invocation'
|
4
|
+
|
5
|
+
require 'mimic/preserved_methods'
|
6
|
+
require 'mimic/subject_methods'
|
7
|
+
require 'mimic/class'
|
8
|
+
require 'mimic/build'
|
9
|
+
require 'mimic/void'
|
10
|
+
require 'mimic/remove_methods'
|
11
|
+
require 'mimic/define_methods'
|
12
|
+
require 'mimic/recorder'
|
13
|
+
require 'mimic/mimic'
|
data/lib/mimic/build.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Build
|
3
|
+
def self.call(subject_class, record: nil, &blk)
|
4
|
+
record ||= false
|
5
|
+
|
6
|
+
cls = Class.build(subject_class, &blk)
|
7
|
+
|
8
|
+
subject_methods = Mimic.subject_methods(cls)
|
9
|
+
|
10
|
+
RemoveMethods.(cls, subject_methods)
|
11
|
+
|
12
|
+
## TODO goes in Class.build
|
13
|
+
## It's about the class def
|
14
|
+
if record
|
15
|
+
cls.class_exec do
|
16
|
+
include Recorder
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
DefineMethods.(cls, subject_methods, record: record)
|
21
|
+
|
22
|
+
cls
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mimic/class.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Class
|
3
|
+
def self.build(subject_class, &blk)
|
4
|
+
define_class(subject_class, &blk)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.define_class(subject_class, &blk)
|
8
|
+
mimic_class = ::Class.new(subject_class, &blk)
|
9
|
+
set_constant(mimic_class, subject_class)
|
10
|
+
mimic_class
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.set_constant(mimic_class, subject_class)
|
14
|
+
class_id = mimic_class.object_id
|
15
|
+
class_name = class_name(subject_class, class_id)
|
16
|
+
|
17
|
+
unless self.const_defined?(class_name, false)
|
18
|
+
self.const_set(class_name, mimic_class)
|
19
|
+
end
|
20
|
+
|
21
|
+
class_name
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.class_name(cls, class_id)
|
25
|
+
if cls.name.nil?
|
26
|
+
return "C#{class_id}"
|
27
|
+
else
|
28
|
+
return "#{cls.name.gsub('::', '_')}_#{class_id}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'mimic/controls/subject'
|
2
|
+
require 'mimic/controls/subject/no_parameters'
|
3
|
+
require 'mimic/controls/subject/positional_parameters'
|
4
|
+
require 'mimic/controls/subject/named_parameters'
|
5
|
+
require 'mimic/controls/subject/block_parameter'
|
6
|
+
require 'mimic/controls/subject/multiple_assignment_parameter'
|
7
|
+
require 'mimic/controls/subject/multiple_assignment_named_parameter'
|
8
|
+
require 'mimic/controls/subject/mixed_parameters'
|
9
|
+
require 'mimic/controls/invocation'
|
10
|
+
require 'mimic/controls/recorder'
|
11
|
+
require 'mimic/controls/mimic'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Controls
|
3
|
+
module Invocation
|
4
|
+
def self.example
|
5
|
+
method_name = :some_method
|
6
|
+
|
7
|
+
parameters = {}
|
8
|
+
parameters[:some_parameter] = 1
|
9
|
+
parameters[:some_other_parameter] = 11
|
10
|
+
|
11
|
+
invocation = ::Invocation.new(method_name, parameters)
|
12
|
+
|
13
|
+
invocation
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Controls
|
3
|
+
module Subject
|
4
|
+
def self.example
|
5
|
+
Example
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.implemented_methods
|
9
|
+
Example.instance_methods(false)
|
10
|
+
end
|
11
|
+
|
12
|
+
class Example
|
13
|
+
def a_method
|
14
|
+
end
|
15
|
+
|
16
|
+
def another_method
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Anonymous
|
21
|
+
def self.example
|
22
|
+
::Class.new(Example)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MethodMissing
|
27
|
+
def method_missing(*)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Mimic
|
2
|
+
module DefineMethods
|
3
|
+
Error = ::Class.new(RuntimeError)
|
4
|
+
|
5
|
+
def self.call(cls, subject_methods=nil, record: nil)
|
6
|
+
subject_methods ||= Mimic.subject_methods(cls)
|
7
|
+
|
8
|
+
subject_methods.each do |mthd|
|
9
|
+
define_method(cls, mthd, record)
|
10
|
+
end
|
11
|
+
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.define_method(cls, mthd, record)
|
16
|
+
method_definition = method_definition(mthd, record)
|
17
|
+
cls.class_eval(method_definition)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.method_definition(mthd, record)
|
21
|
+
"
|
22
|
+
#{signature(mthd)}
|
23
|
+
#{body(record)}
|
24
|
+
end
|
25
|
+
"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.signature(mthd)
|
29
|
+
parameter_list = parameter_list(mthd.parameters)
|
30
|
+
|
31
|
+
"def #{mthd.name}(#{parameter_list})"
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.parameter_list(parameters)
|
35
|
+
parameter_list = ''
|
36
|
+
parameters.each do |parameter|
|
37
|
+
parameter_signature = parameter_signature(parameter)
|
38
|
+
parameter_list << "#{parameter_signature}, "
|
39
|
+
end
|
40
|
+
|
41
|
+
parameter_list[0...-2]
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.parameter_signature(parameter)
|
45
|
+
type = parameter[0]
|
46
|
+
name = parameter.fetch(1) { :args }
|
47
|
+
|
48
|
+
case type
|
49
|
+
when :req
|
50
|
+
return "#{name}"
|
51
|
+
when :keyreq
|
52
|
+
return "#{name}:"
|
53
|
+
when :rest
|
54
|
+
return "*#{name}"
|
55
|
+
when :keyrest
|
56
|
+
return "**#{name}"
|
57
|
+
when :block
|
58
|
+
return "&#{name}"
|
59
|
+
else
|
60
|
+
raise Error, "Unknown parameter type (Name: #{name.inspect}, Type: #{type.inspect})"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.body(record)
|
65
|
+
body = ''
|
66
|
+
if record
|
67
|
+
body << <<~RECORD
|
68
|
+
invocation = Invocation.build(binding)
|
69
|
+
__record(invocation)
|
70
|
+
RECORD
|
71
|
+
end
|
72
|
+
|
73
|
+
body << "Void.new"
|
74
|
+
|
75
|
+
body
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
data/lib/mimic/mimic.rb
ADDED
data/lib/mimic/proofs.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mimic/proofs/method/parameters'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Proofs
|
3
|
+
module Method
|
4
|
+
module Parameters
|
5
|
+
def self.equal?(compare, control, method_name)
|
6
|
+
control_method = control.method(method_name)
|
7
|
+
|
8
|
+
control_parameters = control_method.parameters
|
9
|
+
|
10
|
+
compare_method = compare.method(method_name)
|
11
|
+
compare_parameters = compare_method.parameters
|
12
|
+
|
13
|
+
if ENV['VERBOSE'] == 'on'
|
14
|
+
puts "Control: #{control.class}"
|
15
|
+
pp control_parameters
|
16
|
+
|
17
|
+
puts
|
18
|
+
|
19
|
+
puts "Compare: #{compare.class}"
|
20
|
+
pp compare_parameters
|
21
|
+
end
|
22
|
+
|
23
|
+
compare_parameters == control_parameters
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Mimic
|
2
|
+
module Recorder
|
3
|
+
attr_writer :__records
|
4
|
+
def __records
|
5
|
+
@__records ||= []
|
6
|
+
end
|
7
|
+
alias :records :__records
|
8
|
+
alias :records= :__records=
|
9
|
+
|
10
|
+
def __record(invocation)
|
11
|
+
__records << invocation
|
12
|
+
end
|
13
|
+
alias :record :__record
|
14
|
+
|
15
|
+
def __invocation(method_name, &blk)
|
16
|
+
invocations = __invocations(method_name, &blk)
|
17
|
+
|
18
|
+
if invocations.empty?
|
19
|
+
return nil
|
20
|
+
end
|
21
|
+
|
22
|
+
return invocations.first
|
23
|
+
end
|
24
|
+
alias :invocation :__invocation
|
25
|
+
|
26
|
+
def __invocations(method_name=nil, &blk)
|
27
|
+
if method_name.nil? && blk.nil?
|
28
|
+
return __records
|
29
|
+
end
|
30
|
+
|
31
|
+
invocations = __records.select { |invocation| invocation.method_name == method_name }
|
32
|
+
|
33
|
+
if blk.nil?
|
34
|
+
return invocations
|
35
|
+
end
|
36
|
+
|
37
|
+
if invocations.empty?
|
38
|
+
return []
|
39
|
+
end
|
40
|
+
|
41
|
+
invocations.select do |invocation|
|
42
|
+
invocation.parameters.find { |k, v| blk.(k, v)}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
alias :invocations :__invocations
|
46
|
+
|
47
|
+
def __invoked?(method_name, &blk)
|
48
|
+
invocation = __invocation(method_name, &blk)
|
49
|
+
!invocation.nil?
|
50
|
+
end
|
51
|
+
alias :invoked? :__invoked?
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Mimic
|
2
|
+
def self.subject_methods(cls)
|
3
|
+
instance_method_names = cls.instance_methods.sort
|
4
|
+
instance_method_names -= Mimic.preserved_methods
|
5
|
+
|
6
|
+
instance_methods = instance_method_names.map do |method_name|
|
7
|
+
cls.instance_method(method_name)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/mimic/void.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evt-mimic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- The Eventide Project
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: evt-invocation
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test_bench
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: " "
|
42
|
+
email: opensource@eventide-project.org
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/mimic.rb
|
48
|
+
- lib/mimic/build.rb
|
49
|
+
- lib/mimic/class.rb
|
50
|
+
- lib/mimic/controls.rb
|
51
|
+
- lib/mimic/controls/invocation.rb
|
52
|
+
- lib/mimic/controls/mimic.rb
|
53
|
+
- lib/mimic/controls/recorder.rb
|
54
|
+
- lib/mimic/controls/subject.rb
|
55
|
+
- lib/mimic/controls/subject/block_parameter.rb
|
56
|
+
- lib/mimic/controls/subject/mixed_parameters.rb
|
57
|
+
- lib/mimic/controls/subject/multiple_assignment_named_parameter.rb
|
58
|
+
- lib/mimic/controls/subject/multiple_assignment_parameter.rb
|
59
|
+
- lib/mimic/controls/subject/named_parameters.rb
|
60
|
+
- lib/mimic/controls/subject/no_parameters.rb
|
61
|
+
- lib/mimic/controls/subject/positional_parameters.rb
|
62
|
+
- lib/mimic/define_methods.rb
|
63
|
+
- lib/mimic/mimic.rb
|
64
|
+
- lib/mimic/preserved_methods.rb
|
65
|
+
- lib/mimic/proofs.rb
|
66
|
+
- lib/mimic/proofs/method/parameters.rb
|
67
|
+
- lib/mimic/recorder.rb
|
68
|
+
- lib/mimic/remove_methods.rb
|
69
|
+
- lib/mimic/subject_methods.rb
|
70
|
+
- lib/mimic/void.rb
|
71
|
+
homepage: https://github.com/eventide-project/mimic
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '2.4'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubygems_version: 3.0.1
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Copy a class's instance interface to an anonymous, new object that acts as
|
94
|
+
a substitutable mimic for the class
|
95
|
+
test_files: []
|