mocoso 1.2.3 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4ce992af24ab54bbc81b79a79d7dbdf724100e4b
4
- data.tar.gz: b227a941453ebab128714128ce3890d75e866d99
2
+ SHA256:
3
+ metadata.gz: e1997578f91130d296fb1669445e696cb2b7adeacaa70b3fd7240deec4276c67
4
+ data.tar.gz: 78cd3a3d162d15912bb5abf5a514ec3147cec51f5dd8986e33ba756477f7e6e3
5
5
  SHA512:
6
- metadata.gz: 6534712d4d2499d1fcf5ea3a2edafac02c0349402b7e1570f9cdc8bfcce4fb25187d3f5176253de2a5fa8439c8ff13206dc40784f3160e9e36f871a804e65b6e
7
- data.tar.gz: 232c690c99d95153e499298299de93fe9adef13a6652e005820dff63316ffe04793483a93a60b89db47c04893ea48503c102c65fb7cc491a1e16de0e0ef9c990
6
+ metadata.gz: c0a9f6aa59985dc465c9b0bd1c6f553b2020e97d664af8c1fa6ae4f54adf97cbdab62d1107bd2a4f41f595404120d35d0694db3f807bbe9270063d0c14213a4f
7
+ data.tar.gz: dd67a90d7e4ce71ffdcef572aaa563bc709c76484bafe00c88494a51b71599141ae95f693d17547f5de9f172b38b60f199652033db968b4c98e162269c6649be
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## 2.0.0 (2026-11-30)
4
+
5
+ ### Breaking changes
6
+
7
+ - Requires Ruby 3.3 or later, the oldest Ruby that is not end of life.
8
+ - `expect` raises `Mocoso::ExpectationError` when the expected method is never called.
9
+ - `expect` raises `KeyError` before running the block when the `:return` option is missing.
10
+
11
+ ### Fixes
12
+
13
+ - Stubs forward keyword arguments and blocks to callable values.
14
+ - The stubbed object ends up as it started. It no longer keeps a copy of the original method, and private methods stay private.
15
+ - Stubbing a missing method raises the original `NameError`.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2016 Francesco Rodríguez
1
+ Copyright (c) 2014 Francesco Rodríguez
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,106 +1,43 @@
1
- Mocoso [![Build Status](https://travis-ci.org/frodsan/mocoso.svg)](https://travis-ci.org/frodsan/mocoso)
2
- ======
1
+ # Mocoso [![CI](https://github.com/frodsan/mocoso/actions/workflows/ci.yml/badge.svg)](https://github.com/frodsan/mocoso/actions/workflows/ci.yml)
3
2
 
4
- Yet Another Simple Stub & Mock library. This is inspired by
5
- [Minitest::Mock][minitest] and [Override][override].
3
+ A Ruby stub and mock library in [30 lines of code](lib/mocoso.rb).
6
4
 
7
- Description
8
- -----------
9
-
10
- Mocoso meets the following criteria:
11
-
12
- * Simplicity.
13
- * Always restore stubbed methods to their original implementations.
14
- * Doesn't allow to stub or mock undefined methods.
15
- * Doesn't monkey-patch any class or object.
16
- * Test-framework agnostic. No integration code.
17
-
18
- Installation
19
- ------------
20
-
21
- Add this line to your application's Gemfile:
22
-
23
- ```ruby
24
- gem "mocoso"
25
- ```
26
-
27
- And then execute:
28
-
29
- ```
30
- $ bundle
31
- ```
32
-
33
- Or install it yourself as:
34
-
35
- ```
36
- $ gem install mocoso
37
- ```
38
-
39
- Usage
40
- -----
41
-
42
- A quick example (uses [Cutest][cutest]):
5
+ ## Usage
43
6
 
44
7
  ```ruby
45
- require "cutest"
46
8
  require "mocoso"
47
9
 
48
10
  include Mocoso
49
11
 
50
- test "mocking a class method" do
51
- user = User.new
52
-
53
- expect(User, :find, with: [1], return: user) do
54
- assert_equal user, User.find(1)
55
- end
12
+ user = User.new
56
13
 
57
- assert_equal nil, User.find(1)
14
+ stub(user, :valid?, true) do
15
+ user.valid? # => true
58
16
  end
59
17
 
60
- test "stubbing an instance method" do
61
- user = User.new
18
+ user.valid? # => false
62
19
 
63
- stub(user, :valid?, true) do
64
- assert user.valid?
65
- end
66
-
67
- assert !user.valid?
20
+ expect(User, :find, with: [1], return: user) do
21
+ User.find(1) # => user
22
+ User.find(2) # => raises Mocoso::ExpectationError, "Expected [1], got [2]"
68
23
  end
69
24
  ```
70
25
 
71
- Check [Official Documentation][docs] for more details.
72
-
73
- Contributing
74
- ------------
75
-
76
- Fork the project with:
77
-
78
- ```
79
- $ git clone git@github.com:frodsan/mocoso.git
80
- ```
81
-
82
- To install dependencies, use:
83
-
84
- ```
85
- $ bundle install
86
- ```
87
-
88
- To run the test suite, do:
26
+ A stubbed value that responds to `call` runs with the method's arguments:
89
27
 
90
- ```
91
- $ rake test
28
+ ```ruby
29
+ stub(user, :name, ->(prefix) { "#{prefix} Jane" }) do
30
+ user.name("Dr.") # => "Dr. Jane"
31
+ end
92
32
  ```
93
33
 
94
- For bug reports and pull requests use [GitHub][issues].
34
+ ## What it does
95
35
 
96
- License
97
- -------
36
+ - **Stubs**: `stub` replaces a method on one object for the length of a block, then puts the original back, even when the block raises.
37
+ - **Mocks**: `expect` works like `stub`, but raises `ExpectationError` if the method is called with arguments other than `with:`, or never called. Leave out `with:` to expect no arguments.
38
+ - **No monkey patching**: the replacement lives on the object's singleton class. No class is reopened and nothing global is patched.
39
+ - **Any test framework**: it only needs a block.
98
40
 
99
- Mocoso is released under the [MIT License][mit].
41
+ ## Why
100
42
 
101
- [docs]: http://rubydoc.info/github/harmoni/mocoso/
102
- [cutest]: https://github.com/djanowski/cutest/
103
- [issues]: https://github.com/frodsan/mocoso/issues
104
- [minitest]: https://github.com/seattlerb/minitest/
105
- [mit]: http://www.opensource.org/licenses/MIT
106
- [override]: https://github.com/soveran/override/
43
+ I wrote Mocoso to see how little Ruby code it takes to write a stub and mock library.
data/lib/mocoso.rb CHANGED
@@ -1,136 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Yet Another Simple Stub & Mock library, that also:
4
- #
5
- # - Always restore stubbed methods to their original implementations.
6
- # - Doesn't allow to stub or mock undefined methods.
7
- # - Doesn't monkey-patch any class or object.
8
- # - Test-framework agnostic (Doesn't need integration code).
9
- #
10
- # == Setup
11
- #
12
- # Execute:
13
- #
14
- # gem install mocoso
15
- #
16
- # == Usage
17
- #
18
- # Quick start:
19
- #
20
- # require "cutest"
21
- # require "mocoso"
22
- #
23
- # include Mocoso
24
- #
25
- # test "mocking a class method" do
26
- # user = User.new
27
- #
28
- # expect(User, :find, with: [1], return: user) do
29
- # assert_equal user, User.find(1)
30
- # end
31
- #
32
- # assert_equal nil, User.find(1)
33
- # end
34
- #
35
- # test "stubbing an instance method" do
36
- # user = User.new
37
- #
38
- # stub(user, :valid?, true) do
39
- # assert user.valid?
40
- # end
41
- #
42
- # assert !user.valid?
43
- # end
44
- #
45
- # Note: this example uses the test framework Cutest[1]:
46
- #
47
- # Mocoso is inspired by Override[2], Minitest::Mock[3] and Mocha[4].
48
- #
49
- # * [1]: https://github.com/djanowski/cutest/
50
- # * [2]: https://github.com/soveran/override/
51
- # * [3]: https://github.com/seattlerb/minitest/
52
- # * [4]: https://github.com/freerange/mocha/
53
- #
54
3
  module Mocoso
55
- # Rewrites each method from +methods+ and defined in +object+. +methods+ is a
56
- # Hash that represents stubbed method name symbols as keys and corresponding
57
- # return values as values. The methods are restored after the given +block+
58
- # is executed.
59
- #
60
- # signup = SignupForm.new params[:user]
61
- #
62
- # signup.valid? # => false
63
- #
64
- # Mocoso.stub(signup, :valid?, true) do
65
- # signup.valid? # => true
66
- # end
67
- #
68
- # You can pass a callable object (responds to +call+) as a value:
69
- #
70
- # Mocoso.stub(subject, :foo, -> { "foo" }) do
71
- # subject.foo # => "foo"
72
- # end
73
- #
74
- # Mocoso.stub(subject, :bar, ->(value) { value }) do
75
- # subject.bar("foo") # => "foo"
76
- # end
77
- #
78
4
  def stub(object, method, result)
79
- metaclass = object.singleton_class
80
5
  original = object.method(method)
6
+ singleton = object.singleton_class
7
+ singleton.remove_method(method) if original.owner == singleton
81
8
 
82
- metaclass.send(:undef_method, method)
83
- metaclass.send(:define_method, method) do |*args|
84
- result.respond_to?(:call) ? result.call(*args) : result
9
+ singleton.define_method(method) do |*args, **kwargs, &block|
10
+ result.respond_to?(:call) ? result.call(*args, **kwargs, &block) : result
85
11
  end
86
12
 
87
- yield
88
- ensure
89
- metaclass.send(:undef_method, method)
90
- metaclass.send(:define_method, method, original)
13
+ begin
14
+ yield
15
+ ensure
16
+ singleton.remove_method(method)
17
+ singleton.define_method(method, original) if original.owner == singleton
18
+ end
91
19
  end
92
20
 
93
- # Expects that method +method+ is called with the arguments specified in the
94
- # +:with+ option (defaults to +[]+ if it's not given) and return the value
95
- # specified in the +:return+ option. If expectations are not met, it raises
96
- # Mocoso::ExpectationError error. It uses #stub internally, so it will restore
97
- # the methods to their original implementation after the +block+ is executed.
98
- #
99
- # class User < Model
100
- # end
101
- #
102
- # user = User[1]
103
- #
104
- # Mocoso.expect(user, :update, with: [:age, 18], return: true) do
105
- # user.update(:name, "new name")
106
- # # => Mocoso::ExpectationError:
107
- # # Expected [:age, 18], got [:name, "new name"]
108
- #
109
- # user.update(:age, 18)
110
- # # => true
111
- # end
112
- #
113
21
  def expect(object, method, options, &block)
114
- expectation = ->(*params) do
115
- with = options.fetch(:with, [])
22
+ with = options.fetch(:with, [])
23
+ value = options.fetch(:return)
24
+ called = false
116
25
 
117
- if params != with
118
- raise ExpectationError, "Expected #{ with }, got #{ params }"
119
- end
26
+ expectation = ->(*params) do
27
+ raise ExpectationError, "Expected #{with}, got #{params}" if params != with
120
28
 
121
- options.fetch(:return)
29
+ called = true
30
+ value
122
31
  end
123
32
 
124
- stub(object, method, expectation, &block)
33
+ stub(object, method, expectation, &block).tap do
34
+ raise ExpectationError, "Expected #{method} to be called with #{with}" unless called
35
+ end
125
36
  end
126
37
 
127
- # Raised by #expect when a expectation is not fulfilled.
128
- #
129
- # Mocoso.expect(object, :method, with: "argument", return: nil) do
130
- # object.method("unexpected argument")
131
- # # => Mocoso::ExpectationError:
132
- # # Expected ["argument"], got ["unexpected argument"]
133
- # end
134
- #
135
38
  ExpectationError = Class.new(StandardError)
136
39
  end
metadata CHANGED
@@ -1,72 +1,60 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocoso
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2016-04-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: cutest
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '1.2'
18
+ version: '0'
20
19
  type: :development
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - "~>"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '1.2'
25
+ version: '0'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: rake
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - "~>"
30
+ - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '11.0'
32
+ version: '0'
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - "~>"
37
+ - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '11.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rubocop
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.39'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.39'
55
- description: A simple stub & mock library.
56
- email: hello@frodsan.com
39
+ version: '0'
40
+ description: A Ruby stub and mock library in 30 lines of code
41
+ email: frodsan@me.com
57
42
  executables: []
58
43
  extensions: []
59
44
  extra_rdoc_files: []
60
45
  files:
46
+ - CHANGELOG.md
61
47
  - LICENSE
62
48
  - README.md
63
49
  - lib/mocoso.rb
64
- - test/scary_mocks_and_nice_stubs.rb
65
50
  homepage: https://github.com/frodsan/mocoso
66
51
  licenses:
67
52
  - MIT
68
- metadata: {}
69
- post_install_message:
53
+ metadata:
54
+ source_code_uri: https://github.com/frodsan/mocoso
55
+ changelog_uri: https://github.com/frodsan/mocoso/blob/master/CHANGELOG.md
56
+ bug_tracker_uri: https://github.com/frodsan/mocoso/issues
57
+ rubygems_mfa_required: 'true'
70
58
  rdoc_options: []
71
59
  require_paths:
72
60
  - lib
@@ -74,17 +62,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
62
  requirements:
75
63
  - - ">="
76
64
  - !ruby/object:Gem::Version
77
- version: '0'
65
+ version: '3.3'
78
66
  required_rubygems_version: !ruby/object:Gem::Requirement
79
67
  requirements:
80
68
  - - ">="
81
69
  - !ruby/object:Gem::Version
82
70
  version: '0'
83
71
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.5.1
86
- signing_key:
72
+ rubygems_version: 4.0.3
87
73
  specification_version: 4
88
- summary: A simple stub & mock library.
89
- test_files:
90
- - test/scary_mocks_and_nice_stubs.rb
74
+ summary: A Ruby stub and mock library in 30 lines of code
75
+ test_files: []
@@ -1,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/setup"
4
- require "cutest"
5
- require_relative "../lib/mocoso"
6
-
7
- class Subject
8
- def foo
9
- "foo"
10
- end
11
-
12
- def bar
13
- "bar"
14
- end
15
-
16
- def baz(value)
17
- value
18
- end
19
-
20
- def self.foo
21
- "foo"
22
- end
23
- end
24
-
25
- include Mocoso
26
-
27
- setup do
28
- Subject.new
29
- end
30
-
31
- test "raises error if block is not given" do |subject|
32
- assert_raise { stub(subject, :foo, "foo") }
33
- end
34
-
35
- test "raises error if object not respond to the given method" do |subject|
36
- assert_raise(NameError) { stub(subject, :nan, nil) }
37
- end
38
-
39
- test "stubbed method return new value" do |subject|
40
- before = subject.foo
41
-
42
- stub(subject, :foo, "new foo") do
43
- assert_equal "new foo", subject.foo
44
- end
45
-
46
- assert_equal before, subject.foo
47
- end
48
-
49
- test "stubs singleton method" do
50
- before = Subject.foo
51
-
52
- stub(Subject, :foo, "new foo") do
53
- assert_equal "new foo", Subject.foo
54
- end
55
-
56
- assert_equal before, Subject.foo
57
- end
58
-
59
- test "stubs method with a callable object" do |subject|
60
- before = subject.foo
61
-
62
- stub(subject, :foo, -> { "new foo" }) do
63
- assert_equal "new foo", subject.foo
64
- end
65
-
66
- assert_equal before, subject.foo
67
- end
68
-
69
- test "stubs method with a callable object that requires arguments" do |subject|
70
- before = subject.foo
71
-
72
- stub(subject, :foo, ->(a) { "new #{ a }" }) do
73
- assert_equal "new foo", subject.foo("foo")
74
- end
75
-
76
- assert_equal before, subject.foo
77
- end
78
-
79
- test "succeeds if expectations are met" do |subject|
80
- expect(subject, :baz, with: ["value"], return: "result") do
81
- assert_equal "result", subject.baz("value")
82
- end
83
-
84
- assert_equal "baz", subject.baz("baz")
85
- end
86
-
87
- test "raises an error if expectation are not met" do |subject|
88
- expect(subject, :baz, with: ["value"], return: "result") do
89
- assert_raise(Mocoso::ExpectationError) { subject.baz("another") }
90
- end
91
- end
92
-
93
- test "expectation without with option defaults to empty array" do |subject|
94
- expect(subject, :foo, return: "new foo") do
95
- assert_equal "new foo", subject.foo
96
- end
97
- end
98
-
99
- test "expectation with multiple arguments" do |subject|
100
- expect(
101
- subject,
102
- :foo,
103
- with: ["new foo", { optional: true }],
104
- return: "new foo"
105
- ) do
106
- assert_equal "new foo", subject.foo("new foo", optional: true)
107
- end
108
- end