soveran-override 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.
- data/LICENSE +19 -0
- data/README.markdown +63 -0
- data/Rakefile +35 -0
- data/lib/override.rb +37 -0
- data/test/all_test.rb +87 -0
- metadata +66 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Michel Martens
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
Override
|
2
|
+
============
|
3
|
+
|
4
|
+
The as-simple-as-possible-but-not-simpler stubbing library.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
This is the pure esence of the stubbing concept: it takes an object,
|
10
|
+
a hash of methods/results, and proceeds to rewrite each method in the
|
11
|
+
object. It can be used as a stubbing strategy in most cases, and I'd
|
12
|
+
say that cases that don't fit this pattern have a very bad code smell,
|
13
|
+
because are either dealing with internals or with side effects.
|
14
|
+
|
15
|
+
Usage
|
16
|
+
-----
|
17
|
+
|
18
|
+
require 'override'
|
19
|
+
|
20
|
+
@user = User.spawn
|
21
|
+
override(@user, :name => "Foobar", :email => "foobar@example.org")
|
22
|
+
override(User, :find => @user)
|
23
|
+
|
24
|
+
In case you don't know what spawn means, check my other library for
|
25
|
+
testing at http://github.com/soveran/spawner.
|
26
|
+
|
27
|
+
Installation
|
28
|
+
------------
|
29
|
+
|
30
|
+
$ gem sources -a http://gems.github.com (you only have to do this once)
|
31
|
+
$ sudo gem install soveran-override
|
32
|
+
|
33
|
+
Thanks
|
34
|
+
------
|
35
|
+
|
36
|
+
Thanks to Tim Goh for his advice of using a hash for rewriting multiple
|
37
|
+
methods at once.
|
38
|
+
|
39
|
+
License
|
40
|
+
-------
|
41
|
+
|
42
|
+
Copyright (c) 2009 Michel Martens
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person
|
45
|
+
obtaining a copy of this software and associated documentation
|
46
|
+
files (the "Software"), to deal in the Software without
|
47
|
+
restriction, including without limitation the rights to use,
|
48
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
49
|
+
copies of the Software, and to permit persons to whom the
|
50
|
+
Software is furnished to do so, subject to the following
|
51
|
+
conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
58
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
60
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
61
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
62
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
63
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
|
6
|
+
gem_spec_file = 'override.gemspec'
|
7
|
+
|
8
|
+
gem_spec = eval(File.read(gem_spec_file)) rescue nil
|
9
|
+
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = false
|
15
|
+
end
|
16
|
+
|
17
|
+
Rake::GemPackageTask.new(gem_spec) do |pkg|
|
18
|
+
pkg.need_zip = false
|
19
|
+
pkg.need_tar = false
|
20
|
+
rm_f FileList['pkg/**/*.*']
|
21
|
+
end if gem_spec
|
22
|
+
|
23
|
+
desc "Generate the gemspec file."
|
24
|
+
task :gemspec do
|
25
|
+
require 'erb'
|
26
|
+
|
27
|
+
File.open(gem_spec_file, 'w') do |f|
|
28
|
+
f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Builds and installs the gem."
|
33
|
+
task :install => :repackage do
|
34
|
+
`sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
|
35
|
+
end
|
data/lib/override.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Override
|
2
|
+
#
|
3
|
+
# This is the pure esence of the stubbing concept: it takes an object, a
|
4
|
+
# method and the desired result, and proceeds to rewrite the method in
|
5
|
+
# the object. It can be used as a stubbing strategy in most cases, and
|
6
|
+
# I'd say that cases that don't fit this pattern have a very bad smell
|
7
|
+
# because are either dealing with internals or with side effects.
|
8
|
+
#
|
9
|
+
# Usage:
|
10
|
+
#
|
11
|
+
# @user = User.spawn
|
12
|
+
# override(@user, :name => "Foobar", :email => "foobar@example.org")
|
13
|
+
# override(User, :find => @user)
|
14
|
+
#
|
15
|
+
# In case you don't know what spawn means, check my other library for
|
16
|
+
# testing at http://github.com/soveran/spawner.
|
17
|
+
#
|
18
|
+
# Note: the arity strictness in Ruby 1.9 demands for that trick that
|
19
|
+
# drops the arguments passed to the redefined method. It's not necessary
|
20
|
+
# in Ruby 1.8.
|
21
|
+
#
|
22
|
+
require "rubygems"
|
23
|
+
require "metaid"
|
24
|
+
|
25
|
+
# def override object, method, result
|
26
|
+
# object.metaclass.send(:define_method, method) do |*_|
|
27
|
+
# result
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
|
31
|
+
def override object, methods
|
32
|
+
methods.each do |method, result|
|
33
|
+
object.metaclass.send(:define_method, method) do |*_|
|
34
|
+
result
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/test/all_test.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
require File.dirname(__FILE__) + "/../lib/override"
|
6
|
+
|
7
|
+
class Foo
|
8
|
+
def bar
|
9
|
+
"Bar"
|
10
|
+
end
|
11
|
+
|
12
|
+
def baz
|
13
|
+
yield "Baz"
|
14
|
+
end
|
15
|
+
|
16
|
+
def qux a, b, c
|
17
|
+
"Qux"
|
18
|
+
end
|
19
|
+
|
20
|
+
def == other
|
21
|
+
bar == other.bar
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class TestOverride < Test::Unit::TestCase
|
26
|
+
context "dealing with arguments" do
|
27
|
+
setup do
|
28
|
+
@foo = Foo.new
|
29
|
+
override(@foo, :bar => "Hello")
|
30
|
+
end
|
31
|
+
|
32
|
+
should "work without arguments" do
|
33
|
+
assert_equal "Hello", @foo.bar
|
34
|
+
end
|
35
|
+
|
36
|
+
should "discard arguments" do
|
37
|
+
assert_equal "Hello", @foo.bar(1)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "accepting different return values" do
|
42
|
+
setup do
|
43
|
+
@foo = Foo.new
|
44
|
+
@foo2 = Foo.new
|
45
|
+
end
|
46
|
+
|
47
|
+
should "work for string returns" do
|
48
|
+
override(@foo, :bar => "Hello")
|
49
|
+
assert_equal "Hello", @foo.bar
|
50
|
+
end
|
51
|
+
|
52
|
+
should "work for numeric returns" do
|
53
|
+
override(@foo, :bar => 23)
|
54
|
+
assert_equal 23, @foo.bar
|
55
|
+
end
|
56
|
+
|
57
|
+
should "work for object returns" do
|
58
|
+
override(@foo, :bar => @foo2)
|
59
|
+
assert_equal @foo2, @foo.bar
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "working with methods that acepted attributes or blocks" do
|
64
|
+
setup do
|
65
|
+
@foo = Foo.new
|
66
|
+
end
|
67
|
+
|
68
|
+
should "work for methods that used to receive blocks" do
|
69
|
+
override(@foo, :baz => "Hey!")
|
70
|
+
assert_equal "Hey!", @foo.baz { |x| x }
|
71
|
+
end
|
72
|
+
|
73
|
+
should "work for methods that used to receive arguments" do
|
74
|
+
override(@foo, :qux => "Yay!")
|
75
|
+
assert_equal "Yay!", @foo.qux(1, 2, 3)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "rewriting multiple methods at once" do
|
80
|
+
should "override all the passed methods" do
|
81
|
+
override(@foo, :bar => 1, :baz => 2, :qux => 3)
|
82
|
+
assert_equal 1, @foo.bar
|
83
|
+
assert_equal 2, @foo.baz
|
84
|
+
assert_equal 3, @foo.qux
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: soveran-override
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michel Martens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: metaid
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: michel@soveran.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/override.rb
|
35
|
+
- README.markdown
|
36
|
+
- LICENSE
|
37
|
+
- Rakefile
|
38
|
+
- test/all_test.rb
|
39
|
+
has_rdoc: false
|
40
|
+
homepage: http://github.com/soveran/override
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: The as-simple-as-possible-but-not-simpler stubbing library.
|
65
|
+
test_files: []
|
66
|
+
|