mockdzi 1.0.0rc2
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 +7 -0
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/lib/mockdzi.rb +69 -0
- data/lib/mockdzi/any.rb +28 -0
- data/lib/mockdzi/instance_call.rb +39 -0
- data/lib/mockdzi/mock_method.rb +50 -0
- data/lib/mockdzi/version.rb +3 -0
- data/mockdzi.gemspec +37 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 67d76002fcede8d8ab93cd07aa5afbda7c66ac8080e6087782ca88e6bd65b22c
|
|
4
|
+
data.tar.gz: 85262196ff14451387baca1b7411e602cdf470043f3338e847a52800a4ca0209
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '04948938603c3feb011f3653d7a8d72eef81074ce94a5ec485326d8bc32617999f3a8adf8acb98c2f0f65c8bc7ad51f93bf85fb41215cf3cf60ca1f276d1af76'
|
|
7
|
+
data.tar.gz: dcfb714947c0ac576c0558ee01eae851fb8ef7c41298e8b6e1da177f99998c192af36a82f96e3af1e167a99659e6088bb73091154ffec91d373083b5de141ef8
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Hdzi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Mockdzi
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mockdzi`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'mockdzi'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install mockdzi
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mockdzi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the Mockdzi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mockdzi/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/lib/mockdzi.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'mockdzi/version'
|
|
2
|
+
require 'mockdzi/instance_call'
|
|
3
|
+
require 'mockdzi/any'
|
|
4
|
+
require 'mockdzi/mock_method'
|
|
5
|
+
|
|
6
|
+
# When the functionality you need is not yet developed,
|
|
7
|
+
# you can use mock to temporarily simulate the relevant functionality.
|
|
8
|
+
# When the functionality is complete, you just need to delete the mock configuration,
|
|
9
|
+
# and you don't need to change the specific code implementation.
|
|
10
|
+
# Mock Api:
|
|
11
|
+
# mock_class = Mockdzi.mock(SomeClass)
|
|
12
|
+
# mock_class.instance_call(:mock_method, arg1).then_return 0
|
|
13
|
+
#
|
|
14
|
+
# instance = SomeClass.new
|
|
15
|
+
# instance.mock_method arg1 # => 0
|
|
16
|
+
class Mockdzi
|
|
17
|
+
class << self
|
|
18
|
+
alias mock new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(cls, mock_class: nil, show_details: false) # :nodoc:
|
|
22
|
+
@cls = cls
|
|
23
|
+
@show_details = show_details
|
|
24
|
+
prepare(mock_class)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Mock instance method
|
|
28
|
+
def instance_call(m, *args)
|
|
29
|
+
InstanceCall.new @cls, m, *args, details: @show_details ? caller.first : nil
|
|
30
|
+
rescue => e
|
|
31
|
+
raise "Maybe #{@cls} doesn't mock class!\n#{e}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Mock class method
|
|
35
|
+
def class_call(m, *args)
|
|
36
|
+
InstanceCall.new @cls.singleton_class, m, *args, details: @show_details ? caller.first : nil
|
|
37
|
+
rescue => e
|
|
38
|
+
raise "Maybe #{@cls} doesn't mock singleton_class!\n#{e}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def prepare(mock_class)
|
|
44
|
+
case mock_class
|
|
45
|
+
when nil
|
|
46
|
+
prepare_class @cls
|
|
47
|
+
when :both
|
|
48
|
+
prepare_class @cls
|
|
49
|
+
prepare_class @cls.singleton_class
|
|
50
|
+
when :only
|
|
51
|
+
prepare_class @cls.singleton_class
|
|
52
|
+
else
|
|
53
|
+
raise "Dont support 'mock_class' without :both and :only"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def prepare_class(cls)
|
|
58
|
+
cls.class_eval do
|
|
59
|
+
extend MockMethod
|
|
60
|
+
|
|
61
|
+
@__origin_methods = Hash[]
|
|
62
|
+
@__mock_methods = Hash[]
|
|
63
|
+
instance_methods(false).each do |m|
|
|
64
|
+
@__origin_methods[m] = instance_method m
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
data/lib/mockdzi/any.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class Mockdzi
|
|
2
|
+
# Support for any of the parameters in the mock:
|
|
3
|
+
# Mock Api:
|
|
4
|
+
# mock_class = Mockdzi.mock(SomeClass)
|
|
5
|
+
# mock_class.instance_call(:mock_method, Mockdzi.any(Integer)).then_return 0
|
|
6
|
+
#
|
|
7
|
+
# instance = SomeClass.new
|
|
8
|
+
# instance.mock_method 1 # => 0
|
|
9
|
+
# instance.mock_method 2 # => 0
|
|
10
|
+
class Any
|
|
11
|
+
def initialize(cls)
|
|
12
|
+
@cls = cls
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Mockdzi.any API
|
|
18
|
+
def any(cls)
|
|
19
|
+
mock_object = cls.instance_variable_get :@__any_mock_object
|
|
20
|
+
unless mock_object
|
|
21
|
+
mock_object = Any.new cls
|
|
22
|
+
cls.instance_variable_set :@__any_mock_object, mock_object
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
mock_object
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class Mockdzi
|
|
2
|
+
class InstanceCall # :nodoc:
|
|
3
|
+
MOCK_METHOD_PROC = proc do |name, args, block|
|
|
4
|
+
base = is_a?(Module) ? singleton_class : self.class
|
|
5
|
+
m = base.mock_method_get(name, args)
|
|
6
|
+
m ? instance_exec(args, block, &m) : raise("method '#{self.class}##{name}' can't be called with args:#{args}")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def initialize(cls, m, *args, details: nil) # :nodoc:
|
|
10
|
+
puts "Mock #{cls}##{m} with #{args} in #{details}" if details
|
|
11
|
+
unless cls.mock_method? m
|
|
12
|
+
cls.send(:define_method, m) {|*a, &b| instance_exec m, a, b, &MOCK_METHOD_PROC}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
@cls, @m, @args = cls, m, args
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def then_return(var) # :nodoc:
|
|
19
|
+
__then {var}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def then_raise(*args) # :nodoc:
|
|
23
|
+
__then {raise *args}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def then_real_call # :nodoc:
|
|
27
|
+
m = @m
|
|
28
|
+
__then do |args, block|
|
|
29
|
+
self.class.instance_variable_get(:@__origin_methods)[m].bind(self).call(*args, &block)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def __then(&block) # :nodoc:
|
|
36
|
+
@cls.mock_method_set @m, @args, &block
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
class Mockdzi
|
|
2
|
+
# Set the relevant mock methods.
|
|
3
|
+
module MockMethod
|
|
4
|
+
BLOCK_INDENT = :__mock_block__
|
|
5
|
+
|
|
6
|
+
# Check if it is a mock method.
|
|
7
|
+
def mock_method?(name)
|
|
8
|
+
@__mock_methods.include? name
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Seach the mock
|
|
12
|
+
def mock_method_get(name, args)
|
|
13
|
+
deep_search_mock(args, @__mock_methods[name])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Set the mock
|
|
17
|
+
def mock_method_set(name, args, &block)
|
|
18
|
+
method_info = [name] + args
|
|
19
|
+
temp = @__mock_methods
|
|
20
|
+
method_info.each do |info|
|
|
21
|
+
temp[info] = Hash[] unless temp[info]
|
|
22
|
+
temp = temp[info]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
temp[BLOCK_INDENT] = block
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def deep_search_mock(args, matcher)
|
|
31
|
+
if args.empty? or matcher.nil?
|
|
32
|
+
matcher ? matcher[BLOCK_INDENT] : nil
|
|
33
|
+
else
|
|
34
|
+
param, rest = args[0], args[1..-1]
|
|
35
|
+
deep_result = deep_search_mock(rest, matcher[param])
|
|
36
|
+
return deep_result if deep_result
|
|
37
|
+
# search any
|
|
38
|
+
param.class.ancestors.each do |ancestor|
|
|
39
|
+
any_object = ancestor.instance_variable_get(:@__any_mock_object)
|
|
40
|
+
if any_object
|
|
41
|
+
deep_result = deep_search_mock(rest, matcher[any_object])
|
|
42
|
+
return deep_result if deep_result
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/mockdzi.gemspec
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "mockdzi/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "mockdzi"
|
|
7
|
+
spec.version = Mockdzi::VERSION
|
|
8
|
+
spec.authors = ["Hdzi"]
|
|
9
|
+
spec.email = ["taojinhou@qq.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = %q{Ruby Mock Framework}
|
|
12
|
+
spec.description = %q{A mocking framework for unit tests written in Ruby}
|
|
13
|
+
spec.homepage = "https://github.com/Hdzi/mockdzi"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
18
|
+
# if spec.respond_to?(:metadata)
|
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
|
20
|
+
# else
|
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
|
22
|
+
# "public gem pushes."
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
27
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
28
|
+
`git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/})}
|
|
29
|
+
end
|
|
30
|
+
spec.bindir = "exe"
|
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
|
|
32
|
+
spec.require_paths = ["lib"]
|
|
33
|
+
|
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
37
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mockdzi
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0rc2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hdzi
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-06-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
55
|
+
description: A mocking framework for unit tests written in Ruby
|
|
56
|
+
email:
|
|
57
|
+
- taojinhou@qq.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- Gemfile
|
|
64
|
+
- LICENSE.txt
|
|
65
|
+
- README.md
|
|
66
|
+
- Rakefile
|
|
67
|
+
- lib/mockdzi.rb
|
|
68
|
+
- lib/mockdzi/any.rb
|
|
69
|
+
- lib/mockdzi/instance_call.rb
|
|
70
|
+
- lib/mockdzi/mock_method.rb
|
|
71
|
+
- lib/mockdzi/version.rb
|
|
72
|
+
- mockdzi.gemspec
|
|
73
|
+
homepage: https://github.com/Hdzi/mockdzi
|
|
74
|
+
licenses:
|
|
75
|
+
- MIT
|
|
76
|
+
metadata: {}
|
|
77
|
+
post_install_message:
|
|
78
|
+
rdoc_options: []
|
|
79
|
+
require_paths:
|
|
80
|
+
- lib
|
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">"
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: 1.3.1
|
|
91
|
+
requirements: []
|
|
92
|
+
rubyforge_project:
|
|
93
|
+
rubygems_version: 2.7.6
|
|
94
|
+
signing_key:
|
|
95
|
+
specification_version: 4
|
|
96
|
+
summary: Ruby Mock Framework
|
|
97
|
+
test_files: []
|