rsubstitute 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 David Tchepak (dave@davesquared.net)
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ class Call
2
+ def initialize(method, *args, &block)
3
+ @method, @args, @block = method, args, block
4
+ end
5
+
6
+ def matches(method, *args, &block)
7
+ @method == method and args_match(args)
8
+ end
9
+
10
+ private
11
+
12
+ def args_match(args)
13
+ return true if @args == args
14
+ return false if @args.nil? or args.nil?
15
+ return @args <=> args
16
+ end
17
+ end
18
+
19
+ class Object
20
+ def returns(value)
21
+ sub = self.instance_variable_get(:@substitute)
22
+ raise "Not from a substitute" if sub.nil?
23
+ sub.returns(value)
24
+ end
25
+ end
26
+
27
+ class Substitute
28
+
29
+ def initialize()
30
+ @calls = []
31
+ @results = []
32
+ @assert_next = false
33
+ end
34
+
35
+ def received
36
+ @assert_next = true
37
+ self
38
+ end
39
+
40
+ def method_missing(method, *args, &block)
41
+ assert_was_received = @assert_next
42
+ @assert_next = false
43
+ if assert_was_received and not @calls.any? {|x| x.matches(method, args, block)}
44
+ raise "Call #{method} not received with specified args"
45
+ end
46
+
47
+ @calls << Call.new(method, args, block)
48
+ matching_result_pair = @results.reverse.find do |x|
49
+ x[0].matches(method, args, block)
50
+ end
51
+ if (matching_result_pair.nil?)
52
+ value_to_return = Object.new
53
+ else
54
+ value_to_return = matching_result_pair[1]
55
+ end
56
+
57
+ value_to_return.instance_variable_set(:@substitute, self)
58
+ return value_to_return
59
+ end
60
+
61
+ def returns(value)
62
+ @results << [@calls.pop(), value]
63
+ end
64
+ end
65
+
data/rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems'
4
+ require 'spec/rake/spectask'
5
+
6
+ def get_build_version
7
+ version_tag = /v(\d+)\.(\d+)\.(\d+)\-(\d+)/.match(`git describe --tags --long --match v*`.chomp)
8
+ "#{version_tag[1]}.#{version_tag[2]}.#{version_tag[3]}"
9
+ end
10
+
11
+ gem_spec = Gem::Specification.new do |s|
12
+ s.name = 'rsubstitute'
13
+ s.version = get_build_version()
14
+ s.has_rdoc = false
15
+ s.summary = 'An experimental, friendly substitute for Ruby mocking frameworks.'
16
+ s.description = 'An attempt to bring simple Arrange Act Assert mocking to Ruby. No strict mocks. No hassles. This is currently only an experiment from someone that knows virtually nothing about Ruby; use at your own risk!'
17
+ s.homepage = "http://github.com/dtchepak/rsubstitute"
18
+ s.author = 'David Tchepak'
19
+ s.email = 'dave@davesquared.net'
20
+ s.files = FileList['lib/**/*.rb', 'bin/*', '[A-Z]*', 'test/**/*'].to_a
21
+ s.require_path = "lib"
22
+ s.required_ruby_version = '>= 1.9.1'
23
+ end
24
+
25
+ Rake::GemPackageTask.new(gem_spec) do |p|
26
+ p.gem_spec = gem_spec
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:run_specs) do |t|
30
+ t.spec_files = FileList['test/*_spec.rb']
31
+ t.verbose = true
32
+ t.warning = true
33
+ t.spec_opts << "--format nested"
34
+ end
35
+
36
+ task :default => :run_specs
37
+
data/readme.markdown ADDED
@@ -0,0 +1 @@
1
+ An experiment in Ruby mocking.
@@ -0,0 +1,52 @@
1
+ require_relative '../lib/rsubstitute.rb'
2
+ require 'spec'
3
+ require 'shoulda'
4
+
5
+ describe "when method is called on substitute" do
6
+ before do
7
+ @sub = Substitute.new
8
+ @sub.some_method(1)
9
+ end
10
+
11
+ it "can assert method was received" do
12
+ @sub.received.some_method(1)
13
+ end
14
+
15
+ it "throws when a different method was received" do
16
+ lambda {
17
+ @sub.received.different_method(123, 345)
18
+ }.should raise_exception
19
+ end
20
+
21
+ it "throws when the same method with different args was received" do
22
+ lambda {
23
+ @sub.received.some_method("a")
24
+ }.should raise_exception
25
+ end
26
+ end
27
+
28
+ describe "when setting a return value for a method" do
29
+ before do
30
+ @value_to_return = Object.new
31
+ @sub = Substitute.new
32
+ @sub.some_method(1).returns(@value_to_return)
33
+ end
34
+
35
+ it "returns that value when called" do
36
+ result = @sub.some_method(1)
37
+ result.should == @value_to_return
38
+ end
39
+
40
+ it "returns something else when called with different args" do
41
+ result = @sub.some_method(2)
42
+ result.should != @value_to_return
43
+ end
44
+
45
+ it "returns a new value when a new value is set for the method" do
46
+ @sub.some_method(1).returns(5)
47
+ result = @sub.some_method(1)
48
+ result.should_not equal @value_to_return
49
+ result.should equal 5
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsubstitute
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - David Tchepak
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-09 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: An attempt to bring simple Arrange Act Assert mocking to Ruby. No strict mocks. No hassles. This is currently only an experiment from someone that knows virtually nothing about Ruby; use at your own risk!
22
+ email: dave@davesquared.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/rsubstitute.rb
31
+ - LICENSE
32
+ - rakefile
33
+ - readme.markdown
34
+ - test/rsubstitute_spec.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/dtchepak/rsubstitute
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 1
51
+ - 9
52
+ - 1
53
+ version: 1.9.1
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: An experimental, friendly substitute for Ruby mocking frameworks.
69
+ test_files: []
70
+