jeremymcanally-stump 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/License.txt +20 -0
- data/Manifest.txt +23 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +118 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/lib/stump/core_ext/test_case.rb +21 -0
- data/lib/stump/metaid.rb +17 -0
- data/lib/stump/mock.rb +51 -0
- data/lib/stump/mocks.rb +23 -0
- data/lib/stump/stub.rb +34 -0
- data/lib/stump/version.rb +9 -0
- data/lib/stump.rb +11 -0
- data/setup.rb +1585 -0
- data/stump.gemspec +43 -0
- data/test/test_helper.rb +12 -0
- data/test/test_mock.rb +111 -0
- data/test/test_proxy.rb +109 -0
- data/test/test_stub.rb +85 -0
- metadata +75 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Jeremy McAnally
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.rdoc
|
6
|
+
Rakefile
|
7
|
+
config/hoe.rb
|
8
|
+
config/requirements.rb
|
9
|
+
lib/stump.rb
|
10
|
+
lib/stump/metaid.rb
|
11
|
+
lib/stump/mock.rb
|
12
|
+
lib/stump/mocks.rb
|
13
|
+
lib/stump/proxy.rb
|
14
|
+
lib/stump/stub.rb
|
15
|
+
lib/stump/core_ext/test_case.rb
|
16
|
+
lib/stump/version.rb
|
17
|
+
setup.rb
|
18
|
+
tasks/deployment.rake
|
19
|
+
tasks/environment.rake
|
20
|
+
test/test_helper.rb
|
21
|
+
test/test_proxy.rb
|
22
|
+
test/test_mock.rb
|
23
|
+
test/test_stub.rb
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
= stump
|
2
|
+
|
3
|
+
* http://www.github.com/jeremymcanally/stump
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Stubbing and mocking that isn't painful, fanciful, and doesn't inspire any sort of contempt or bitterness.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Stubbing right on the object.
|
12
|
+
|
13
|
+
# Returns 'hey!'
|
14
|
+
MyClass.stub!(:thing, :return => "hey!")
|
15
|
+
# Returns nil
|
16
|
+
your_object.stub!(:hello)
|
17
|
+
|
18
|
+
* Pure stub objects.
|
19
|
+
|
20
|
+
my_stub = stub(:thing, :return => "dude, a thing!")
|
21
|
+
my_stub.thing # => "dude, a thing!"
|
22
|
+
|
23
|
+
* Mocking right on the object.
|
24
|
+
|
25
|
+
my_object = "things are fun"
|
26
|
+
my_object.mock!(:fancy, "ooo fancy!")
|
27
|
+
my_object.fancy # => "ooo fancy!"
|
28
|
+
|
29
|
+
* Pure mock objects.
|
30
|
+
|
31
|
+
my_mock = mock(:hello, :return => "what fun is this?")
|
32
|
+
my_mock.hello # => "what fun is this?"
|
33
|
+
|
34
|
+
* Proxying objects.
|
35
|
+
|
36
|
+
class Greeting
|
37
|
+
def bonjour
|
38
|
+
"Bonjour!"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
greet_me = Greeting.new
|
43
|
+
greet_me.proxy!(:bonjour)
|
44
|
+
greet_me.bonjour # => "Bonjour!"
|
45
|
+
|
46
|
+
greet_you = Greeting.new
|
47
|
+
greet_you.proxy!(:bonjour)
|
48
|
+
|
49
|
+
# finish test...
|
50
|
+
# ! Unmet expectations: #<Greeting:0x371d24> expected hello
|
51
|
+
|
52
|
+
== SYNOPSIS:
|
53
|
+
|
54
|
+
# Stub class methods
|
55
|
+
MyClass.stub!(:thing, :return => "hey!")
|
56
|
+
# Stub method on object
|
57
|
+
your_object.stub!(:my_method)
|
58
|
+
# Stub method on object, requiring parameters
|
59
|
+
my_object.stub!(:other_method) do |arg1, arg2|
|
60
|
+
puts arg1
|
61
|
+
puts arg2
|
62
|
+
end
|
63
|
+
|
64
|
+
# Mock method on object
|
65
|
+
MyClass.mock!(:my_method, :return => "hehe")
|
66
|
+
# Mock method on object, requiring parameters
|
67
|
+
my_obj.mock!(:hehe, :return => nil) do |arg1, arg2, arg3|
|
68
|
+
# Fun party!
|
69
|
+
end
|
70
|
+
|
71
|
+
# Sets expectation it will receive a method, calling the real method
|
72
|
+
MyClass.proxy!(:the_method)
|
73
|
+
# Returns static value but still has side effects of real method call
|
74
|
+
MyClass.proxy!(:that_method, :return => "poop")
|
75
|
+
|
76
|
+
== NOTES:
|
77
|
+
|
78
|
+
* Be sure you call +super+ in +teardown+ if you create your own +teardown+ methods. Otherwise your mocks won't get verified!
|
79
|
+
|
80
|
+
== REQUIREMENTS:
|
81
|
+
|
82
|
+
* Test::Unit (batteries included with Ruby)
|
83
|
+
|
84
|
+
== INSTALL:
|
85
|
+
|
86
|
+
$ gem sources -a http://gems.github.com
|
87
|
+
$ sudo gem install jeremymcanally-stump
|
88
|
+
|
89
|
+
|
90
|
+
== ACKNOWLEDGEMENTS:
|
91
|
+
|
92
|
+
* Thanks to Jim Weirich for Flexmock, David Chelmisky et. al. for RSpec's mocking stuff, and Brian Takita for rr. All of those things fed into stump.
|
93
|
+
* BIG thanks to Nathan Sutton (fowlduck) for helping me work through how to get +proxy!+ to work with ActiveRecord/method_missing.
|
94
|
+
|
95
|
+
== LICENSE:
|
96
|
+
|
97
|
+
(The MIT License)
|
98
|
+
|
99
|
+
Copyright (c) 2008 Jeremy McAnally
|
100
|
+
|
101
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
102
|
+
a copy of this software and associated documentation files (the
|
103
|
+
'Software'), to deal in the Software without restriction, including
|
104
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
105
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
106
|
+
permit persons to whom the Software is furnished to do so, subject to
|
107
|
+
the following conditions:
|
108
|
+
|
109
|
+
The above copyright notice and this permission notice shall be
|
110
|
+
included in all copies or substantial portions of the Software.
|
111
|
+
|
112
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
113
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
114
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
115
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
116
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
117
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
118
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'stump/version'
|
2
|
+
|
3
|
+
AUTHOR = 'Jeremy McAnally' # can also be an array of Authors
|
4
|
+
EMAIL = "jeremy@entp.com"
|
5
|
+
DESCRIPTION = "Stubbing and mocking that isn't painful."
|
6
|
+
GEM_NAME = 'stump' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'stump' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
# ['activesupport', '>= 1.3.1']
|
12
|
+
] # An array of rubygem dependencies [name, version]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "unknown"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = Stump::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'stump documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
#p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
67
|
+
end
|
68
|
+
|
69
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
70
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
71
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
72
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
73
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Test
|
2
|
+
module Unit
|
3
|
+
class TestCase
|
4
|
+
def stumpdown!
|
5
|
+
begin
|
6
|
+
if !Stump::Mocks.failures.nil? && !Stump::Mocks.failures.empty?
|
7
|
+
fails = Stump::Mocks.failures.map {|object, method| "#{object.inspect} expected #{method}"}.join(", ")
|
8
|
+
|
9
|
+
flunk "Unmet expectations: #{fails}"
|
10
|
+
end
|
11
|
+
ensure
|
12
|
+
Stump::Mocks.clear!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
stumpdown!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/stump/metaid.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# thanks _why
|
2
|
+
# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
|
3
|
+
class Object
|
4
|
+
# The hidden singleton lurks behind everyone
|
5
|
+
def metaclass; class << self; self; end; end
|
6
|
+
def meta_eval &blk; metaclass.instance_eval &blk; end
|
7
|
+
|
8
|
+
# Adds methods to a metaclass
|
9
|
+
def meta_def name, &blk
|
10
|
+
meta_eval { define_method name, &blk }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Defines an instance method within a class
|
14
|
+
def class_def name, &blk
|
15
|
+
class_eval { define_method name, &blk }
|
16
|
+
end
|
17
|
+
end
|
data/lib/stump/mock.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
class Object
|
2
|
+
# Create a mock method on an object. A mock object will place an expectation
|
3
|
+
# on behavior and cause a test failure if it's not fulfilled.
|
4
|
+
#
|
5
|
+
# ==== Examples
|
6
|
+
#
|
7
|
+
# my_string = "a wooden rabbit"
|
8
|
+
# my_string.mock!(:retreat!, :return => "run away! run away!")
|
9
|
+
# my_string.mock!(:question, :return => "what is the airspeed velocity of an unladen sparrow?")
|
10
|
+
#
|
11
|
+
# # test/your_test.rb
|
12
|
+
# my_string.retreat! # => "run away! run away!"
|
13
|
+
# # If we let the test case end at this point, it fails with:
|
14
|
+
# # Unmet expectation: #<Sparrow:1ee7> expected question
|
15
|
+
#
|
16
|
+
def mock!(method, options = {}, &block)
|
17
|
+
Stump::Mocks.add([self, method])
|
18
|
+
|
19
|
+
behavior = if block_given?
|
20
|
+
lambda do |*args|
|
21
|
+
raise ArgumentError if block.arity >= 0 && args.length != block.arity
|
22
|
+
|
23
|
+
Stump::Mocks.verify([self, method])
|
24
|
+
block.call(args)
|
25
|
+
end
|
26
|
+
else
|
27
|
+
lambda do |*args|
|
28
|
+
Stump::Mocks.verify([self, method])
|
29
|
+
return options[:return]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
meta_def method, &behavior
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Kernel
|
38
|
+
# Create a pure mock object rather than mocking specific methods on an object.
|
39
|
+
#
|
40
|
+
# ==== Examples
|
41
|
+
#
|
42
|
+
# my_mock = mock(:thing, :return => "whee!")
|
43
|
+
# my_mock.thing # => "whee"
|
44
|
+
#
|
45
|
+
def mock(method, options = {}, &block)
|
46
|
+
mock_object = Object.new
|
47
|
+
mock_object.mock!(method, options, &block)
|
48
|
+
mock_object
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/stump/mocks.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Stump
|
2
|
+
# A class to track the mocks and proxies that have been satisfied
|
3
|
+
class Mocks
|
4
|
+
class <<self
|
5
|
+
def add(mock)
|
6
|
+
@mocks ||= []
|
7
|
+
@mocks << mock
|
8
|
+
end
|
9
|
+
|
10
|
+
def verify(mock)
|
11
|
+
@mocks.delete(mock)
|
12
|
+
end
|
13
|
+
|
14
|
+
def failures
|
15
|
+
@mocks
|
16
|
+
end
|
17
|
+
|
18
|
+
def clear!
|
19
|
+
@mocks = []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/stump/stub.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Object
|
2
|
+
# Create a stub method on an object. Simply returns a value for a method call on
|
3
|
+
# an object.
|
4
|
+
#
|
5
|
+
# ==== Examples
|
6
|
+
#
|
7
|
+
# my_string = "a wooden rabbit"
|
8
|
+
# my_string.stub!(:retreat!, :return => "run away! run away!")
|
9
|
+
#
|
10
|
+
# # test/your_test.rb
|
11
|
+
# my_string.retreat! # => "run away! run away!"
|
12
|
+
#
|
13
|
+
def stub!(method, options = {}, &block)
|
14
|
+
behavior = (block_given? ? block : proc { return options[:return] })
|
15
|
+
|
16
|
+
meta_def method, &behavior
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Kernel
|
21
|
+
# Create a pure stub object.
|
22
|
+
#
|
23
|
+
# ==== Examples
|
24
|
+
#
|
25
|
+
# stubbalicious = stub(:failure, "wat u say?")
|
26
|
+
# stubbalicious.failure # => "wat u say?"
|
27
|
+
#
|
28
|
+
def stub(method, options = {}, &block)
|
29
|
+
stub_object = Object.new
|
30
|
+
stub_object.stub!(method, options, &block)
|
31
|
+
|
32
|
+
stub_object
|
33
|
+
end
|
34
|
+
end
|
data/lib/stump.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'stump/version'
|
5
|
+
require 'stump/metaid'
|
6
|
+
require 'stump/core_ext/test_case'
|
7
|
+
|
8
|
+
require 'stump/stub'
|
9
|
+
require 'stump/mocks'
|
10
|
+
require 'stump/mock'
|
11
|
+
require 'stump/proxy'
|