mocoso 1.2.2 → 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: 84fde22389db6d310917952800c3a3464ced1fb5
4
- data.tar.gz: 408db6f0dafa0234cd1e627816dc85e804e39ba3
2
+ SHA256:
3
+ metadata.gz: e1997578f91130d296fb1669445e696cb2b7adeacaa70b3fd7240deec4276c67
4
+ data.tar.gz: 78cd3a3d162d15912bb5abf5a514ec3147cec51f5dd8986e33ba756477f7e6e3
5
5
  SHA512:
6
- metadata.gz: 822cc257ce2fee101df1202af9a172a499d646fd71585fa210dae2daf44904f42ab2bafdbba0a9faf23db01186da3b638a44b2e98fe19bac5708b866de92a34b
7
- data.tar.gz: 312f6a5d353fd08fe169551ec6586a0e8b12fe231108b710d1a432e75bf335e3f36bdae52f9ad4d997983a4f9a6f3de727bee3681a73b1e7dc2be7df9fa0284d
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-2015 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,62 +1,43 @@
1
- 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
- Usage
19
- -----
20
-
21
- A quick example (uses [Cutest][cutest]):
5
+ ## Usage
22
6
 
23
7
  ```ruby
24
- require "cutest"
25
8
  require "mocoso"
26
9
 
27
10
  include Mocoso
28
11
 
29
- test "mocking a class method" do
30
- user = User.new
31
-
32
- expect(User, :find, with: [1], return: user) do
33
- assert_equal user, User.find(1)
34
- end
12
+ user = User.new
35
13
 
36
- assert_equal nil, User.find(1)
14
+ stub(user, :valid?, true) do
15
+ user.valid? # => true
37
16
  end
38
17
 
39
- test "stubbing an instance method" do
40
- user = User.new
18
+ user.valid? # => false
41
19
 
42
- stub(user, :valid?, true) do
43
- assert user.valid?
44
- end
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]"
23
+ end
24
+ ```
25
+
26
+ A stubbed value that responds to `call` runs with the method's arguments:
45
27
 
46
- assert !user.valid?
28
+ ```ruby
29
+ stub(user, :name, ->(prefix) { "#{prefix} Jane" }) do
30
+ user.name("Dr.") # => "Dr. Jane"
47
31
  end
48
32
  ```
49
33
 
50
- Check [Official Documentation][docs] for more details.
34
+ ## What it does
51
35
 
52
- Installation
53
- ------------
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.
54
40
 
55
- ```bash
56
- $ gem install mocoso
57
- ```
41
+ ## Why
58
42
 
59
- [docs]: http://rubydoc.info/github/harmoni/mocoso/
60
- [cutest]: https://github.com/djanowski/cutest/
61
- [override]: https://github.com/soveran/override/
62
- [minitest]: https://github.com/seattlerb/minitest/
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,127 +1,39 @@
1
- # Yet Another Simple Stub & Mock library, that also:
2
- #
3
- # - Always restore stubbed methods to their original implementations.
4
- # - Doesn't allow to stub or mock undefined methods.
5
- # - Doesn't monkey-patch any class or object.
6
- # - Test-framework agnostic (Doesn't need integration code).
7
- #
8
- # == Setup
9
- #
10
- # Execute:
11
- #
12
- # gem install mocoso
13
- #
14
- # == Usage
15
- #
16
- # Quick start:
17
- #
18
- # require "cutest"
19
- # require "mocoso"
20
- #
21
- # include Mocoso
22
- #
23
- # test "mocking a class method" do
24
- # user = User.new
25
- #
26
- # expect(User, :find, with: [1], return: user) do
27
- # assert_equal user, User.find(1)
28
- # end
29
- #
30
- # assert_equal nil, User.find(1)
31
- # end
32
- #
33
- # test "stubbing an instance method" do
34
- # user = User.new
35
- #
36
- # stub(user, :valid?, true) do
37
- # assert user.valid?
38
- # end
39
- #
40
- # assert !user.valid?
41
- # end
42
- #
43
- # Note: this example uses the test framework Cutest[1]:
44
- #
45
- # Mocoso is inspired by (stolen from) Override[2], Minitest::Mock[3] and Mocha[4].
46
- #
47
- # * [1]: https://github.com/djanowski/cutest/
48
- # * [2]: https://github.com/soveran/override/
49
- # * [3]: https://github.com/seattlerb/minitest/
50
- # * [4]: https://github.com/freerange/mocha/
51
- #
1
+ # frozen_string_literal: true
2
+
52
3
  module Mocoso
53
- # Rewrites each method from +methods+ and defined in +object+. +methods+ is a
54
- # Hash that represents stubbed method name symbols as keys and corresponding
55
- # return values as values. The methods are restored after the given +block+
56
- # is executed.
57
- #
58
- # signup = SignupForm.new params[:user]
59
- #
60
- # signup.valid? # => false
61
- #
62
- # Mocoso.stub(signup, :valid?, true) do
63
- # signup.valid? # => true
64
- # end
65
- #
66
- # You can pass a callable object (responds to +call+) as a value:
67
- #
68
- # Mocoso.stub(subject, :foo, -> { "foo" }) do
69
- # subject.foo # => "foo"
70
- # end
71
- #
72
- # Mocoso.stub(subject, :bar, ->(value) { value }) do
73
- # subject.bar("foo") # => "foo"
74
- # end
75
- #
76
4
  def stub(object, method, result)
77
- metaclass = object.singleton_class
78
5
  original = object.method(method)
6
+ singleton = object.singleton_class
7
+ singleton.remove_method(method) if original.owner == singleton
79
8
 
80
- metaclass.send(:define_method, method) do |*args|
81
- result.respond_to?(:call) ? result.(*args) : result
9
+ singleton.define_method(method) do |*args, **kwargs, &block|
10
+ result.respond_to?(:call) ? result.call(*args, **kwargs, &block) : result
82
11
  end
83
12
 
84
- yield
85
- ensure
86
- metaclass.send(:undef_method, method)
87
- 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
88
19
  end
89
20
 
90
- # Expects that method +method+ is called with the arguments specified in the
91
- # +:with+ option (defaults to +[]+ if it's not given) and return the value
92
- # specified in the +:return+ option. If expectations are not met, it raises
93
- # Mocoso::ExpectationError error. It uses #stub internally, so it will restore
94
- # the methods to their original implementation after the +block+ is executed.
95
- #
96
- # class User < Model
97
- # end
98
- #
99
- # user = User[1]
100
- #
101
- # Mocoso.expect(user, :update, with: [{ name: "new name" }], return: true) do
102
- # subject.update(unexpected: nil)
103
- # # => Mocoso::ExpectationError: Expected [{:name=>"new name"}], got [{:unexpected=>nil}]
104
- #
105
- # user.update(name: "new name")
106
- # # => true
107
- # end
108
- #
109
21
  def expect(object, method, options, &block)
110
- expectation = ->(*params) {
111
- with = options.fetch(:with, [])
22
+ with = options.fetch(:with, [])
23
+ value = options.fetch(:return)
24
+ called = false
25
+
26
+ expectation = ->(*params) do
112
27
  raise ExpectationError, "Expected #{with}, got #{params}" if params != with
113
- options.fetch(:return)
114
- }
115
28
 
116
- stub(object, method, expectation, &block)
29
+ called = true
30
+ value
31
+ end
32
+
33
+ stub(object, method, expectation, &block).tap do
34
+ raise ExpectationError, "Expected #{method} to be called with #{with}" unless called
35
+ end
117
36
  end
118
37
 
119
- # Raised by #expect when a expectation is not fulfilled.
120
- #
121
- # Mocoso.expect(object, :method, with: "argument", return: nil) do
122
- # object.method("unexpected argument")
123
- # # => Mocoso::ExpectationError: Expected ["argument"], got ["unexpected argument"]
124
- # end
125
- #
126
38
  ExpectationError = Class.new(StandardError)
127
39
  end
metadata CHANGED
@@ -1,48 +1,60 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocoso
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
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: 2015-09-26 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.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.2
27
- description: A simple stub & mock library.
28
- email:
29
- - frodsan@protonmail.ch
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: A Ruby stub and mock library in 30 lines of code
41
+ email: frodsan@me.com
30
42
  executables: []
31
43
  extensions: []
32
44
  extra_rdoc_files: []
33
45
  files:
34
- - ".gems"
46
+ - CHANGELOG.md
35
47
  - LICENSE
36
48
  - README.md
37
49
  - lib/mocoso.rb
38
- - makefile
39
- - mocoso.gemspec
40
- - test/scary_mocks_and_nice_stubs.rb
41
- homepage: https://github.com/harmoni/mocoso
50
+ homepage: https://github.com/frodsan/mocoso
42
51
  licenses:
43
52
  - MIT
44
- metadata: {}
45
- 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'
46
58
  rdoc_options: []
47
59
  require_paths:
48
60
  - lib
@@ -50,16 +62,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
50
62
  requirements:
51
63
  - - ">="
52
64
  - !ruby/object:Gem::Version
53
- version: '0'
65
+ version: '3.3'
54
66
  required_rubygems_version: !ruby/object:Gem::Requirement
55
67
  requirements:
56
68
  - - ">="
57
69
  - !ruby/object:Gem::Version
58
70
  version: '0'
59
71
  requirements: []
60
- rubyforge_project:
61
- rubygems_version: 2.4.8
62
- signing_key:
72
+ rubygems_version: 4.0.3
63
73
  specification_version: 4
64
- summary: A simple stub & mock library.
74
+ summary: A Ruby stub and mock library in 30 lines of code
65
75
  test_files: []
data/.gems DELETED
@@ -1 +0,0 @@
1
- cutest -v 1.2.2
data/makefile DELETED
@@ -1,2 +0,0 @@
1
- default:
2
- cutest test/*.rb
data/mocoso.gemspec DELETED
@@ -1,14 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = "mocoso"
3
- s.version = "1.2.2"
4
- s.summary = "A simple stub & mock library."
5
- s.description = s.summary
6
- s.authors = ["Francesco Rodríguez"]
7
- s.email = ["frodsan@protonmail.ch"]
8
- s.homepage = "https://github.com/harmoni/mocoso"
9
- s.license = "MIT"
10
-
11
- s.files = `git ls-files`.split("\n")
12
-
13
- s.add_development_dependency "cutest", "1.2.2"
14
- end
@@ -1,92 +0,0 @@
1
- require "cutest"
2
- require_relative "../lib/mocoso"
3
-
4
- class Subject
5
- def foo; "foo"; end
6
- def bar; "bar"; end
7
- def baz(value); value; end
8
-
9
- def self.foo
10
- "foo"
11
- end
12
- end
13
-
14
- include Mocoso
15
-
16
- setup do
17
- Subject.new
18
- end
19
-
20
- test "raises error if block is not given" do |subject|
21
- assert_raise { stub(subject, :foo, "foo") }
22
- end
23
-
24
- test "raises error if object not respond to the given method" do |subject|
25
- assert_raise(NameError) { stub(subject, :nan, nil) }
26
- end
27
-
28
- test "stubbed method return new value" do |subject|
29
- before = subject.foo
30
-
31
- stub(subject, :foo, "new foo") do
32
- assert_equal "new foo", subject.foo
33
- end
34
-
35
- assert_equal before, subject.foo
36
- end
37
-
38
- test "stubs singleton method" do
39
- before = Subject.foo
40
-
41
- stub(Subject, :foo, "new foo") do
42
- assert_equal "new foo", Subject.foo
43
- end
44
-
45
- assert_equal before, Subject.foo
46
- end
47
-
48
- test "stubs method with a callable object" do |subject|
49
- before = subject.foo
50
-
51
- stub(subject, :foo, -> { "new foo" }) do
52
- assert_equal "new foo", subject.foo
53
- end
54
-
55
- assert_equal before, subject.foo
56
- end
57
-
58
- test "stubs method with a callable object that requires arguments" do |subject|
59
- before = subject.foo
60
-
61
- stub(subject, :foo, ->(a) { "new #{a}" }) do
62
- assert_equal "new foo", subject.foo("foo")
63
- end
64
-
65
- assert_equal before, subject.foo
66
- end
67
-
68
- test "succeeds if expectations are met" do |subject|
69
- expect(subject, :baz, with: ["value"], return: "result") do
70
- assert_equal "result", subject.baz("value")
71
- end
72
-
73
- assert_equal "baz", subject.baz("baz")
74
- end
75
-
76
- test "raises an error if expectation are not met" do |subject|
77
- expect(subject, :baz, with: ["value"], return: "result") do
78
- assert_raise(Mocoso::ExpectationError) { subject.baz("another") }
79
- end
80
- end
81
-
82
- test "expectation without with option defaults to empty array" do |subject|
83
- expect(subject, :foo, return: "new foo") do
84
- assert_equal "new foo", subject.foo
85
- end
86
- end
87
-
88
- test "expectation with multiple arguments" do |subject|
89
- expect(subject, :foo, with: ["new foo", { optional: true }], return: "new foo") do
90
- assert_equal "new foo", subject.foo("new foo", optional: true)
91
- end
92
- end