simple_stub 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f41c5fa17fb28bc656a40b36da9dfc56bb481d9f08105b61da41ba82ea5be698
4
+ data.tar.gz: c1e85f87feb9e7433ce90cc1abcc8c2a87fe6e75f098e531bfaebeed94c5f078
5
+ SHA512:
6
+ metadata.gz: c5e3d3c3f4d181124a982c986b9055c3c2c23973f10ff45995b9c769e58521e10357766fecc983015e51f13ed333cb8a89dfeb28ce63618958c73ca70e9144db
7
+ data.tar.gz: 027d8dc539cbf1e1f301e653eb6e4ff32cab626a87bd0fe298df93740669e8e6696672e765f978cf436f9d68f0aa03bea12889bcf47c39e893ab72b66bcc65d5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at iwaki@i3-systems.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in simple_stub.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 YusukeIwaki
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,78 @@
1
+ # SimpleStub
2
+
3
+ Defining simple scoped stub with apply/reset interface
4
+
5
+ ```ruby
6
+ fixed_time = Time.now
7
+ stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time }
8
+
9
+ stub_time_now.apply!
10
+ # Time.now is fixed here
11
+ stub_time_now.reset!
12
+ ```
13
+
14
+ ## Installation
15
+
16
+ ```ruby
17
+ gem 'simple_stub'
18
+ ```
19
+
20
+ then `bundle install`.
21
+
22
+ ## Usage
23
+
24
+ For stubbing a singleton method (or class method), use `SimpleStub.for_singleton_method`.
25
+
26
+ ```ruby
27
+ fixed_time = Time.now
28
+ stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time }
29
+
30
+ stub_time_now.apply!
31
+ # Time.now is fixed here
32
+ stub_time_now.reset!
33
+ ```
34
+
35
+ For stubbing an instance method (which affects to all of the instances), use `SimpleStub.for_instance_method`.
36
+
37
+ ```ruby
38
+ class Dog
39
+ def hello
40
+ 'Hello!'
41
+ end
42
+ end
43
+
44
+ stub_dog_hello = SimpleStub.for_instance_method(Dog, :hello) { 'Bow' }
45
+ dog1 = Dog.new
46
+ stub_dog1_hello = SimpleStub.for_singleton_method(dog1, :hello) { ">> #{super()} <<" }
47
+
48
+ stub_dog_hello.apply!
49
+ Dog.new.hello # => 'Bow'
50
+ dog1.hello # => 'Bow'
51
+ stub_dog1_hello.apply!
52
+ dog1.hello # => '>> Bow <<'
53
+ ```
54
+
55
+ Since SimpleStub doesn't store any state in the instance, we can use it separatedly.
56
+
57
+ ```ruby
58
+ class SomethingSetup
59
+ def call
60
+ # All username becomes John Doe
61
+ SimpleStub.for_instance_method(User, :name) { 'John Doe' }.apply
62
+ end
63
+ end
64
+
65
+ class SomethingTeardown
66
+ def call
67
+ SimpleStub.for_instance_method(User, :name).reset
68
+ end
69
+ end
70
+ ```
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
75
+
76
+ ## Code of Conduct
77
+
78
+ Everyone interacting in the SimpleStub project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/simple_stub/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.test_files = FileList['test/**/test_*.rb']
9
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'simple_stub'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleStub
4
+ class AlreadyAppliedError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module SimpleStub
6
+ # Stub for instance methods, without any alias_method.
7
+ # This class internally prepends a module into the target class.
8
+ # {#apply} adds a method into the prepended module.
9
+ # {#reset} removes a method from the prepended module.
10
+ #
11
+ # This class must be stateless for the usecase that
12
+ # the caller of {#apply} and the caller of {#reset} can be different.
13
+ # Both
14
+ #
15
+ # @stub_user_name = SimpleStub::ForInstanceMethod(User, :name) { 'YusukeIwaki' }
16
+ # @stub_user_name.apply!
17
+ # # Any username is YusukeIwaki here
18
+ # @stub_user_name.reset!
19
+ #
20
+ # and
21
+ #
22
+ # stub_user_name = SimpleStub::ForInstanceMethod(User, :name) { 'YusukeIwaki' }
23
+ # stub_user_name.apply!
24
+ # # Any username is YusukeIwaki here
25
+ # stub_user_name = SimpleStub::ForInstanceMethod(User, :name) { 'YusukeIwaki' }
26
+ # stub_user_name.reset!
27
+ #
28
+ # should work.
29
+ #
30
+ class ForInstanceMethod
31
+ # @param klass [Class]
32
+ # @param method_name [Symbol]
33
+ def initialize(klass, method_name, &impl)
34
+ raise ArgumentError, "klass must be a Class. #{klass.class} specified." unless klass.is_a?(Class)
35
+ raise ArgumentError, 'method name must be a Symbol.' unless method_name.is_a?(Symbol)
36
+
37
+ @klass = klass
38
+ @method_name = method_name
39
+ @impl = impl
40
+ end
41
+
42
+ # Safer version of {#apply!}
43
+ # Nothing happens when the stub is already applied.
44
+ def apply
45
+ return if stub_defined?
46
+
47
+ apply_stub
48
+ end
49
+
50
+ # Apply the stub. If the stub is already applied, raises error.
51
+ def apply!
52
+ raise AlreadyAppliedError, "The stub for #{@klass}##{@method_name} is already applied" if stub_defined?
53
+
54
+ apply_stub
55
+ end
56
+
57
+ # Safer version of {#reset!}.
58
+ # Nothing happens when the stub is already applied.
59
+ def reset
60
+ return unless stub_defined?
61
+
62
+ reset_stub
63
+ end
64
+
65
+ # Remove the stub and revert to the original implementation. If the stub is not applied or already reset, raise error.
66
+ def reset!
67
+ raise NotAppliedError, "The stub for #{@klass}##{@method_name} is already applied" unless stub_defined?
68
+
69
+ reset_stub
70
+ end
71
+
72
+ private
73
+
74
+ def apply_stub
75
+ raise ArgumentError, 'Block must be given for applying stub' unless @impl
76
+
77
+ # define_method is private on Ruby <= 2.4
78
+ impl_module.send(:define_method, @method_name, &@impl)
79
+ end
80
+
81
+ def reset_stub
82
+ # remove_method is private on Ruby <= 2.4
83
+ impl_module.send(:remove_method, @method_name)
84
+ end
85
+
86
+ def impl_module
87
+ impl_module_or_nil || create_and_apply_impl_module
88
+ end
89
+
90
+ def impl_module_or_nil
91
+ ForInstanceMethod.const_get(impl_module_name) if ForInstanceMethod.const_defined?(impl_module_name)
92
+ end
93
+
94
+ def create_and_apply_impl_module
95
+ Module.new.tap do |mod|
96
+ # Name the module here
97
+ ForInstanceMethod.const_set(impl_module_name, mod)
98
+
99
+ @klass.prepend(mod)
100
+ end
101
+ end
102
+
103
+ def klass_digest
104
+ @klass_digest ||= Digest::SHA256.hexdigest(@klass.to_s)
105
+ end
106
+
107
+ def impl_module_name
108
+ @impl_module_name ||= "StubImpl#{klass_digest}"
109
+ end
110
+
111
+ def stub_defined?
112
+ # Avoid creating module just in asking if stub exists.
113
+ impl_module_or_nil&.method_defined?(@method_name)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleStub
4
+ class NotAppliedError < StandardError
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleStub
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simple_stub/version'
4
+ require 'simple_stub/already_applied_error'
5
+ require 'simple_stub/for_instance_method'
6
+ require 'simple_stub/not_applied_error'
7
+
8
+ module SimpleStub
9
+ # Stubs instance method.
10
+ # This affect to any instance of the target class.
11
+ #
12
+ # class Dog
13
+ # def hello
14
+ # 'Hello!'
15
+ # end
16
+ # end
17
+ #
18
+ # dog1 = Dog.new
19
+ # stub_dog_hello = SimpleStub.for_singleton_method(Dog, :hello) { 'Bow' }
20
+ # stub_dog_hello.apply!
21
+ # dog1.hello # => 'Bow'
22
+ # Dog.new.hello # => 'Bow'
23
+ # stub_dog_hello.reset!
24
+ # dog1.hello # => 'Hello!'
25
+ # Dog.new.hello # => 'Hello!'
26
+ #
27
+ # @param klass [Class]
28
+ # @param method_name [Symbol]
29
+ def for_instance_method(klass, method_name, &impl)
30
+ ForInstanceMethod.new(klass, method_name, &impl)
31
+ end
32
+
33
+ # Create a definition for stubbing singleton method.
34
+ # This can be used to stub class method like:
35
+ #
36
+ # fixed_time = Time.now
37
+ # stub_time_now = SimpleStub.for_singleton_method(Time, :now) { fixed_time }
38
+ # stub_time_now.apply!
39
+ # # Time.now returns fixed_time here.
40
+ # stub_time_now.reset!
41
+ #
42
+ # And also can be used for stubbing method of an object instance.
43
+ #
44
+ # class Dog
45
+ # def hello
46
+ # 'Hello!'
47
+ # end
48
+ # end
49
+ #
50
+ # dog1 = Dog.new
51
+ # stub_dog1 = SimpleStub.for_singleton_method(dog1, :hello) { 'Bow' }
52
+ # stub_dog1.apply!
53
+ # dog1.hello # => 'Bow'
54
+ # Dog.new.hello # => 'Hello!'
55
+ #
56
+ # @param receiver [Class|Object]
57
+ # @param method_name [Symbol]
58
+ def for_singleton_method(receiver, method_name, &impl)
59
+ ForInstanceMethod.new(receiver.singleton_class, method_name, &impl)
60
+ end
61
+
62
+ module_function :for_instance_method, :for_singleton_method
63
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'simple_stub/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'simple_stub'
9
+ spec.version = SimpleStub::VERSION
10
+ spec.authors = ['YusukeIwaki']
11
+ spec.email = ['q7w8e9w8q7w8e9@yahoo.co.jp']
12
+
13
+ spec.summary = 'Defining simple scoped stub with apply/reset interface'
14
+ spec.homepage = 'https://github.com/YusukeIwaki/simple_stub'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/}) || f.include?('.git')
20
+ end
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.required_ruby_version = '>= 2.3'
27
+ spec.add_development_dependency 'minitest'
28
+ spec.add_development_dependency 'rake'
29
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_stub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - YusukeIwaki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
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:
43
+ - q7w8e9w8q7w8e9@yahoo.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CODE_OF_CONDUCT.md
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/simple_stub.rb
56
+ - lib/simple_stub/already_applied_error.rb
57
+ - lib/simple_stub/for_instance_method.rb
58
+ - lib/simple_stub/not_applied_error.rb
59
+ - lib/simple_stub/version.rb
60
+ - simple_stub.gemspec
61
+ homepage: https://github.com/YusukeIwaki/simple_stub
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '2.3'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.2.22
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Defining simple scoped stub with apply/reset interface
84
+ test_files: []