quacks-like 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/LICENSE +21 -0
  2. data/lib/quacks_like.rb +72 -0
  3. metadata +65 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009, Michael H. Buselli
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ''AS IS'' AND ANY
13
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
16
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,72 @@
1
+ #
2
+ # Copyright (c) 2009, Michael H. Buselli
3
+ # See LICENSE for details on permitted use.
4
+ #
5
+
6
+ #
7
+ # QuacksLike is a module for RSpec to add matchers that test if an
8
+ # object is fully duck-typed to pretend to be another class. This kind
9
+ # of thing is really only necessary when passing such an object as the
10
+ # return value in an API where you don't know exactly how it will be
11
+ # consumed, but it needs to "quack like an Array" or something. It does
12
+ # its job by checking every instance method in the class that the target
13
+ # object needs to "quack like" and makes sure the target both responds
14
+ # to that method name and that the arity of the method is appropriate.
15
+ #
16
+ # Usage (in RSpec files):
17
+ # require 'quacks_like'
18
+ #
19
+ # it "should return an object that quacks like a Hash" do
20
+ # my_func.should quack_like_a(Hash)
21
+ # end
22
+ #
23
+ # it "should return an object that does not quack like an Array" do
24
+ # my_func.should_not quack_like_an(Array)
25
+ # end
26
+ #
27
+ class QuacksLike
28
+ def initialize (quack_class)
29
+ @quack_class = quack_class
30
+ end
31
+
32
+ def arity_compare (quack_arity, target_arity)
33
+ if quack_arity >= 0
34
+ target_arity == quack_arity or target_arity.between?(-1 - quack_arity, -1)
35
+ else
36
+ target_arity.between?(quack_arity, -1)
37
+ end
38
+ end
39
+ private :arity_compare
40
+
41
+ def matches? (target)
42
+ @target = target
43
+ @mismatches = []
44
+
45
+ @quack_class.instance_methods.each do |m|
46
+ if not @target.respond_to?(m)
47
+ @mismatches.push "does not respond to :#{m}"
48
+ elsif not arity_compare(q = @quack_class.instance_method(m).arity,
49
+ t = @target.method(m).arity)
50
+ @mismatches.
51
+ push "quacking method :#{m} has arity #{q} but found arity #{t}"
52
+ end
53
+ end
54
+
55
+ @mismatches.empty?
56
+ end
57
+
58
+ def failure_message_for_should
59
+ [@target.inspect, @mismatches.join(', ')].join(' ')
60
+ end
61
+
62
+ def failure_message_for_should_not
63
+ "expected #{@target.inspect} to not quack like a(n) #{@quack_class.inspect}"
64
+ end
65
+ end
66
+
67
+
68
+ def quack_like_a (quack_class)
69
+ QuacksLike.new(quack_class)
70
+ end
71
+ alias quack_like_an quack_like_a
72
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quacks-like
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael H Buselli
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-11 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " QuacksLike is a module for RSpec to add matchers that test if an\n object is fully duck-typed to pretend to be another class. This\n kind of thing is really only necessary when passing such an\n object as the return value in an API where you don't know\n exactly how it will be consumed, but it needs to \"quack like an\n Array\" or something. It does its job by checking every instance\n method in the class that the target object needs to \"quack like\"\n and makes sure the target both responds to that method name and\n that the arity of the method is appropriate.\n"
22
+ email:
23
+ - cosine@cosine.org
24
+ - michael@buselli.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - LICENSE
33
+ - lib/quacks_like.rb
34
+ has_rdoc: true
35
+ homepage: http://cosine.org/ruby/QuacksLike/
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project: quacks-like
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: "QuacksLike \xE2\x80\x94 RSpec matcher for duck-type testing"
64
+ test_files: []
65
+