stubble 0.1.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.
- data/MIT-LICENSE +20 -0
- data/README +27 -0
- data/Rakefile +97 -0
- data/lib/stubble.rb +93 -0
- data/test/stubble_test.rb +139 -0
- data/test/test_helper.rb +8 -0
- metadata +73 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [Matthew Rudy Jacobs]
|
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/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Stubble
|
2
|
+
========
|
3
|
+
|
4
|
+
Work towards a Ruby stubber that retains the original functionality under the hood.
|
5
|
+
|
6
|
+
Example
|
7
|
+
=======
|
8
|
+
|
9
|
+
Add some stubble
|
10
|
+
Stubble.add_stubble!(Time)
|
11
|
+
|
12
|
+
Tell it just to track a method
|
13
|
+
Time.track!(:now)
|
14
|
+
|
15
|
+
We can then later assert against calls
|
16
|
+
|
17
|
+
Or we can stub it
|
18
|
+
|
19
|
+
Time.stub!(:now, [Time.mktime(2010,12,17,10,40)])
|
20
|
+
|
21
|
+
After Time.now has been called once, the functionality will return to normal
|
22
|
+
|
23
|
+
Or we can call
|
24
|
+
|
25
|
+
Time.unstub!(:now)
|
26
|
+
|
27
|
+
Copyright (c) 2010 [Matthew Rudy Jacobs], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test Stubble'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for Stubble.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Stubble'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
require "rubygems"
|
26
|
+
require "rake/gempackagetask"
|
27
|
+
require "rake/rdoctask"
|
28
|
+
|
29
|
+
require "rake/testtask"
|
30
|
+
Rake::TestTask.new do |t|
|
31
|
+
t.libs << "test"
|
32
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
33
|
+
t.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task :default => ["test"]
|
38
|
+
|
39
|
+
# This builds the actual gem. For details of what all these options
|
40
|
+
# mean, and other ones you can add, check the documentation here:
|
41
|
+
#
|
42
|
+
# http://rubygems.org/read/chapter/20
|
43
|
+
#
|
44
|
+
spec = Gem::Specification.new do |s|
|
45
|
+
|
46
|
+
# Change these as appropriate
|
47
|
+
s.name = "stubble"
|
48
|
+
s.version = "0.1.0"
|
49
|
+
s.summary = "(experimental) simple stubbing"
|
50
|
+
s.author = "Matthew Rudy Jacobs"
|
51
|
+
s.email = "MatthewRudyJacobs@gmail.com"
|
52
|
+
s.homepage = "https://github.com/matthewrudy/stubble"
|
53
|
+
|
54
|
+
s.has_rdoc = true
|
55
|
+
s.extra_rdoc_files = %w(README)
|
56
|
+
s.rdoc_options = %w(--main README)
|
57
|
+
|
58
|
+
# Add any extra files to include in the gem
|
59
|
+
s.files = %w(MIT-LICENSE Rakefile README) + Dir.glob("{test,lib}/**/*")
|
60
|
+
s.require_paths = ["lib"]
|
61
|
+
|
62
|
+
# If you want to depend on other gems, add them here, along with any
|
63
|
+
# relevant versions
|
64
|
+
# s.add_dependency("some_other_gem", "~> 0.1.0")
|
65
|
+
|
66
|
+
# If your tests use any gems, include them here
|
67
|
+
# s.add_development_dependency("mocha") # for example
|
68
|
+
end
|
69
|
+
|
70
|
+
# This task actually builds the gem. We also regenerate a static
|
71
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
72
|
+
# be automatically building a gem for this project. If you're not
|
73
|
+
# using GitHub, edit as appropriate.
|
74
|
+
#
|
75
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
76
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
77
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
78
|
+
pkg.gem_spec = spec
|
79
|
+
end
|
80
|
+
|
81
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
82
|
+
task :gemspec do
|
83
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
84
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
85
|
+
end
|
86
|
+
|
87
|
+
# If you don't want to generate the .gemspec file, just remove this line. Reasons
|
88
|
+
# why you might want to generate a gemspec:
|
89
|
+
# - using bundler with a git source
|
90
|
+
# - building the gem without rake (i.e. gem build blah.gemspec)
|
91
|
+
# - maybe others?
|
92
|
+
task :package => :gemspec
|
93
|
+
|
94
|
+
desc 'Clear out RDoc and generated packages'
|
95
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
96
|
+
rm "#{spec.name}.gemspec"
|
97
|
+
end
|
data/lib/stubble.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
3
|
+
require 'active_support/core_ext/module/aliasing'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
|
6
|
+
module Stubble
|
7
|
+
|
8
|
+
class Catcher
|
9
|
+
|
10
|
+
def initialize()
|
11
|
+
@calls = {}
|
12
|
+
@stubs = {}
|
13
|
+
@tracking = Set.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def called!(method_name, args, block)
|
17
|
+
if @tracking.include?(method_name)
|
18
|
+
@calls[method_name]
|
19
|
+
@calls[method_name] << [args, !block.nil?]
|
20
|
+
if @stubs[method_name].present?
|
21
|
+
@stubs[method_name].shift
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def track!(method_name)
|
27
|
+
@calls[method_name] = []
|
28
|
+
@tracking << method_name
|
29
|
+
end
|
30
|
+
|
31
|
+
def untrack!(method_name)
|
32
|
+
@tracking.delete(method_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
def stub!(method_name, values)
|
36
|
+
@stubs[method_name] = values
|
37
|
+
end
|
38
|
+
|
39
|
+
def unstub!(method_name)
|
40
|
+
@stubs[method_name] = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def [](method_name)
|
44
|
+
@calls[method_name]
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_stubble!(object)
|
50
|
+
object.instance_eval do
|
51
|
+
@stubble = Stubble::Catcher.new
|
52
|
+
end
|
53
|
+
object.extend StubMethods
|
54
|
+
def object._stubble
|
55
|
+
@stubble
|
56
|
+
end
|
57
|
+
end
|
58
|
+
module_function :add_stubble!
|
59
|
+
|
60
|
+
module StubMethods
|
61
|
+
|
62
|
+
def track!(method_name)
|
63
|
+
self._stubble.track!(method_name)
|
64
|
+
unless methods.include?("#{method_name}_with_stubble")
|
65
|
+
class_eval <<-RUBY
|
66
|
+
def #{method_name}_with_stubble(*args, &block)
|
67
|
+
if stubbed_return = self._stubble.called!(#{method_name.inspect}, args, block)
|
68
|
+
stubbed_return
|
69
|
+
else
|
70
|
+
#{method_name}_without_stubble(*args, &block)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias_method_chain #{method_name.inspect}, :stubble
|
74
|
+
RUBY
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def untrack!(method_name)
|
79
|
+
self._stubble.untrack!(method_name)
|
80
|
+
end
|
81
|
+
|
82
|
+
def stub!(method_name, values)
|
83
|
+
track!(method_name)
|
84
|
+
self._stubble.stub!(method_name, values)
|
85
|
+
end
|
86
|
+
|
87
|
+
def unstub!(method_name)
|
88
|
+
self._stubble.unstub!(method_name)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'stubble'
|
3
|
+
|
4
|
+
class StubbleTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
extend ActiveSupport::Testing::Declarative
|
7
|
+
|
8
|
+
class StubThis
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@hits = 0
|
12
|
+
end
|
13
|
+
attr_reader :hits
|
14
|
+
|
15
|
+
def some_method
|
16
|
+
@hits +=1
|
17
|
+
end
|
18
|
+
|
19
|
+
def some_method_with_arguments(times, shout="boom")
|
20
|
+
shout * times
|
21
|
+
end
|
22
|
+
|
23
|
+
def some_method_with_block
|
24
|
+
yield @hits+=1
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
test "stubble is added on an individual basis" do
|
30
|
+
one = StubThis.new
|
31
|
+
two = StubThis.new
|
32
|
+
|
33
|
+
Stubble.add_stubble!(one)
|
34
|
+
assert one.respond_to?(:_stubble)
|
35
|
+
assert !two.respond_to?(:_stubble)
|
36
|
+
end
|
37
|
+
|
38
|
+
test "track! - tracks, but doesnt affect the function" do
|
39
|
+
this = StubThis.new
|
40
|
+
assert_equal 1, this.some_method
|
41
|
+
|
42
|
+
Stubble.add_stubble!(this)
|
43
|
+
this.track!(:some_method)
|
44
|
+
|
45
|
+
assert_equal 2, this.some_method
|
46
|
+
assert_equal 3, this.some_method
|
47
|
+
|
48
|
+
assert_equal [[[],false]]*2, this._stubble[:some_method]
|
49
|
+
end
|
50
|
+
|
51
|
+
test "track! - tracks arguments" do
|
52
|
+
this = StubThis.new
|
53
|
+
assert_equal "boom", this.some_method_with_arguments(1)
|
54
|
+
|
55
|
+
Stubble.add_stubble!(this)
|
56
|
+
this.track!(:some_method_with_arguments)
|
57
|
+
|
58
|
+
assert_equal "boomboom", this.some_method_with_arguments(2)
|
59
|
+
assert_equal "purple", this.some_method_with_arguments(1, "purple")
|
60
|
+
|
61
|
+
assert_equal [[[2],false], [[1,"purple"],false]], this._stubble[:some_method_with_arguments]
|
62
|
+
end
|
63
|
+
|
64
|
+
test "track! - tracks blocks" do
|
65
|
+
this = StubThis.new
|
66
|
+
|
67
|
+
this.some_method_with_block do |val|
|
68
|
+
assert_equal 1, val
|
69
|
+
end
|
70
|
+
|
71
|
+
Stubble.add_stubble!(this)
|
72
|
+
this.track!(:some_method_with_block)
|
73
|
+
|
74
|
+
this.some_method_with_block do |val|
|
75
|
+
assert_equal 2, val
|
76
|
+
end
|
77
|
+
this.some_method_with_block do |val|
|
78
|
+
assert_equal 3, val
|
79
|
+
end
|
80
|
+
|
81
|
+
assert_equal [[[],true], [[],true]], this._stubble[:some_method_with_block]
|
82
|
+
end
|
83
|
+
|
84
|
+
test "track! - can be called multiple times" do
|
85
|
+
this = StubThis.new
|
86
|
+
assert_equal 1, this.some_method
|
87
|
+
|
88
|
+
Stubble.add_stubble!(this)
|
89
|
+
this.track!(:some_method)
|
90
|
+
this.track!(:some_method)
|
91
|
+
|
92
|
+
assert_equal 2, this.some_method
|
93
|
+
end
|
94
|
+
|
95
|
+
test "untrack! - undoes a track" do
|
96
|
+
this = StubThis.new
|
97
|
+
assert_equal 1, this.some_method
|
98
|
+
|
99
|
+
Stubble.add_stubble!(this)
|
100
|
+
this.track!(:some_method)
|
101
|
+
|
102
|
+
assert_equal 2, this.some_method
|
103
|
+
assert_equal 1, this._stubble[:some_method].length
|
104
|
+
|
105
|
+
this.untrack!(:some_method)
|
106
|
+
|
107
|
+
assert_equal 3, this.some_method
|
108
|
+
assert_equal 1, this._stubble[:some_method].length # no new
|
109
|
+
end
|
110
|
+
|
111
|
+
test "stub! - defines the next values to be returned" do
|
112
|
+
this = StubThis.new
|
113
|
+
assert_equal 1, this.some_method
|
114
|
+
|
115
|
+
Stubble.add_stubble!(this)
|
116
|
+
this.stub!(:some_method, [:a, :b, :c])
|
117
|
+
|
118
|
+
assert_equal :a, this.some_method
|
119
|
+
assert_equal :b, this.some_method
|
120
|
+
assert_equal :c, this.some_method
|
121
|
+
|
122
|
+
assert_equal 2, this.some_method
|
123
|
+
end
|
124
|
+
|
125
|
+
test "unstub! - cancels a stub" do
|
126
|
+
this = StubThis.new
|
127
|
+
assert_equal 1, this.some_method
|
128
|
+
|
129
|
+
Stubble.add_stubble!(this)
|
130
|
+
this.stub!(:some_method, [:a, :b, :c])
|
131
|
+
|
132
|
+
assert_equal :a, this.some_method
|
133
|
+
|
134
|
+
this.unstub!(:some_method)
|
135
|
+
|
136
|
+
assert_equal 2, this.some_method
|
137
|
+
assert_equal 3, this.some_method
|
138
|
+
end
|
139
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stubble
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Rudy Jacobs
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-17 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: MatthewRudyJacobs@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- README
|
34
|
+
- test/stubble_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- lib/stubble.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: https://github.com/matthewrudy/stubble
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: (experimental) simple stubbing
|
72
|
+
test_files: []
|
73
|
+
|