assert-send 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 061b8556f3e944f2b10f10ca599d714771a1be02
4
+ data.tar.gz: fa716318ec5be32e45c00268cb78a89b51e0c0e6
5
+ SHA512:
6
+ metadata.gz: f4d6862f34865714adacc409690383097cd754b84f4083fc44471c30a0412653dd9ac9b2e76f4e6af7c56d65abb6fc67396dd6cea4d9351903c6f38aa44e6be8
7
+ data.tar.gz: 172d46ad30118d1d7aba472d80f694804220e996555a93128fcafa027f40b202351b285c3f1356b497a3376fb4a1ca0c093c2f7067816089d2265fc9b7f6c52c
data/.gems.test ADDED
@@ -0,0 +1,2 @@
1
+ cutest -v 1.2.1
2
+ mocoso -v 1.2.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Lucas Tolchinsky
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Wait for it
2
+
3
+ This little piece of work helps you know if an expected message was passed somewhere down your call stack.
4
+
5
+ ### Usage
6
+
7
+ It's quite simple. Imagine you want to make sure `user.save` gets called.
8
+
9
+ ```ruby
10
+ include AssertSend
11
+
12
+ user = User.find(...)
13
+
14
+ assert_send(user, :save) do
15
+ your_dangerous_code!
16
+ end
17
+ ```
18
+
19
+ If `your_dangerous_code!` doesn't call `user.save` in the call stack, it'll raise a `AssertSend::ExpectationError`.
20
+
21
+ It's important to know that this will **not** stub your method. The original implementation it's never tampered with.
22
+
23
+ If you're looking to stub things, I suggest you having a look at [frodsan/mocoso](https://github.com/frodsan/mocoso).
24
+
25
+ ### Installation
26
+
27
+ ```
28
+ gem install assert-send
29
+ ```
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "assert-send"
3
+ s.version = "0.0.2"
4
+ s.summary = "Easy message expectation for tests in Ruby"
5
+ s.description = s.summary
6
+ s.authors = ["Lucas Tolchinsky"]
7
+ s.email = ["lucas.tolchinsky@gmail.com"]
8
+ s.homepage = "https://github.com/tonchis/assert-send"
9
+ s.license = "MIT"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.add_development_dependency "cutest"
12
+ end
@@ -0,0 +1,22 @@
1
+ module AssertSend
2
+ ExpectationError = Class.new(StandardError)
3
+
4
+ def assert_send(object, message, &block)
5
+ metaclass = object.singleton_class
6
+ original = object.method(message)
7
+ executed = nil
8
+
9
+ metaclass.send(:define_method, message) do |*args|
10
+ executed = true
11
+ original.(*args)
12
+ end
13
+
14
+ res = yield
15
+
16
+ executed ? res : (raise ExpectationError, "Expected #{object} to receive message :#{message}")
17
+ ensure
18
+ metaclass.send(:undef_method, message)
19
+ metaclass.send(:define_method, message, original)
20
+ end
21
+ end
22
+
@@ -0,0 +1,70 @@
1
+ require "cutest"
2
+ require_relative "../lib/assert_send"
3
+
4
+ include AssertSend
5
+
6
+ scope do
7
+ class Foo
8
+ def bar
9
+ :bar
10
+ end
11
+ end
12
+
13
+ test "raise ExpectationError if the message was not passed" do
14
+ foo = Foo.new
15
+
16
+ assert_raise(AssertSend::ExpectationError) do
17
+ assert_send(foo, :bar) {}
18
+ end
19
+
20
+ assert assert_send(foo, :bar) { foo.bar }
21
+ end
22
+ end
23
+
24
+ scope do
25
+ class Foo
26
+ def self.bar
27
+ :bar
28
+ end
29
+ end
30
+
31
+ class Baz
32
+ def yes
33
+ Foo.bar
34
+ end
35
+
36
+ def no
37
+ :sorry
38
+ end
39
+ end
40
+
41
+ setup { Baz.new }
42
+
43
+ test "spots the message deep into the stack" do |baz|
44
+ assert assert_send(Foo, :bar) { baz.yes }
45
+
46
+ assert_raise(AssertSend::ExpectationError) do
47
+ assert_send(Foo, :bar) { baz.no }
48
+ end
49
+ end
50
+ end
51
+
52
+ scope do
53
+ class Numberz
54
+ def self.one
55
+ 1
56
+ end
57
+
58
+ def self.successor(n)
59
+ n + Numberz.one
60
+ end
61
+ end
62
+
63
+ test "dont mess with the execution chain" do
64
+ assert_send(Numberz, :one) do
65
+ assert_equal 5, Numberz.successor(4)
66
+ end
67
+
68
+ assert_equal 5, assert_send(Numberz, :one) { Numberz.successor(4) }
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert-send
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Tolchinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
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
+ description: Easy message expectation for tests in Ruby
28
+ email:
29
+ - lucas.tolchinsky@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gems.test"
35
+ - LICENSE
36
+ - README.md
37
+ - assert_send.gemspec
38
+ - lib/assert_send.rb
39
+ - test/assert_send_test.rb
40
+ homepage: https://github.com/tonchis/assert-send
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.0
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Easy message expectation for tests in Ruby
64
+ test_files: []