mocha-shot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,5 @@
1
+ 0.1.0
2
+
3
+ Terse Expectations:
4
+ the_mock.expects.foo(1,2) {|a, b| :bar}
5
+ the_mock.expects.foo(1,2).returns(:bar)
data/COPYING ADDED
@@ -0,0 +1,3 @@
1
+ Copyright Mocha Shot Team 2007
2
+
3
+ You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt) or under the MIT license (see MIT-LICENSE file).
data/MIT-LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2007 Mocha Shot Team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,14 @@
1
+ == Mocha Shot
2
+
3
+ Mocha-Shot[http://mocha-shot.rubyforge.org] is a terser Mocha[http://mocha.rubyforge.org] mock framework. It features syntax like:
4
+
5
+ require 'rubygems'
6
+ require 'mocha/shot'
7
+
8
+ the_mock.expects.foo(1,2) {|a, b| :bar}
9
+ # equivalent to
10
+ the_mock.expects(:foo).with(1,2) {|a, b| :bar}
11
+
12
+ the_mock.expects.foo(1,2).returns(:bar)
13
+ # equivalent to
14
+ the_mock.expects.(:foo).with(1,2).returns(:bar)
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/contrib/rubyforgepublisher'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ desc "Runs all of the specs"
9
+ task :spec do
10
+ dir = File.dirname(__FILE__)
11
+ require "#{dir}/spec/spec_suite"
12
+ SpecSuite.new.run
13
+ end
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = "mocha-shot"
17
+ s.version = "0.1.0"
18
+ s.summary = "A terser Mocha mock framework. This library is based off some patches to Mocha with make the syntax " <<
19
+ "shorter and easier to read. These changes may or may not be folded into Mocha's trunk."
20
+ s.test_files = Dir.glob('spec/*_spec.rb')
21
+ s.description = s.summary
22
+
23
+ s.files = FileList[
24
+ '[A-Z]*',
25
+ '*.rb',
26
+ 'lib/**/*.rb',
27
+ 'spec/**/*.rb'
28
+ ].to_a
29
+ s.require_path = 'lib'
30
+
31
+ s.has_rdoc = true
32
+ s.extra_rdoc_files = ['README', 'COPYING', 'MIT-LICENSE']
33
+ s.rdoc_options << '--title' << 'Mocha Shot' << '--main' << 'README' << '--line-numbers'
34
+
35
+ s.test_files = Dir.glob('spec/*_spec.rb')
36
+ s.require_path = 'lib'
37
+ s.autorequire = 'mocha/shot'
38
+ s.add_dependency('mocha', '>= 0.4.0')
39
+
40
+ s.author = "Brian Takita"
41
+ s.email = "brian.takita@gmail.com"
42
+ s.homepage = "http://mocha-shot.rubyforge.org"
43
+ s.rubyforge_project = "mocha-shot"
44
+ s.test_file = "spec/spec_suite.rb"
45
+ end
46
+
47
+ Rake::GemPackageTask.new(spec) do |pkg|
48
+ pkg.need_zip = true
49
+ pkg.need_tar = true
50
+ end
data/lib/mocha/shot.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "rubygems"
2
+ gem "mocha"
3
+ require "mocha"
4
+ require "mocha/shot/terse_mock"
5
+ require "mocha/shot/terse_stub"
6
+ require "mocha/shot/terse_object_stub"
7
+ require "mocha/shot/single_return_value"
8
+ require "mocha/shot/mock"
9
+ require "mocha/shot/object"
@@ -0,0 +1,19 @@
1
+ module Mocha
2
+ class Mock
3
+ def expects(*args)
4
+ if args.empty? || args.first.is_a?(Array)
5
+ return TerseMock.new(self, args.first)
6
+ else
7
+ return __expects__(*args)
8
+ end
9
+ end
10
+
11
+ def stubs(*args)
12
+ if args.empty? || args.first.is_a?(Array)
13
+ return TerseStub.new(self, args.first)
14
+ else
15
+ return __stubs__(*args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ class Object
2
+ alias_method :__stubs__, :stubs
3
+
4
+ def stubs(symbol=nil)
5
+ return __stubs__(symbol) if symbol
6
+ Mocha::TerseObjectStub.new(self)
7
+ end
8
+
9
+ alias_method :expects, :stubs
10
+ end
@@ -0,0 +1,4 @@
1
+ module Mocha # :nodoc:
2
+ class SingleReturnValue # :nodoc:
3
+ end
4
+ end
@@ -0,0 +1,16 @@
1
+ module Mocha # :nodoc:
2
+ class TerseMock
3
+ def initialize(mock, backtrace)
4
+ @mock = mock
5
+ @backtrace = backtrace
6
+ end
7
+
8
+ def method_missing(method_name, *args, &block)
9
+ if block
10
+ @mock.__expects__(method_name, @backtrace).with(*args).returns(block)
11
+ else
12
+ @mock.__expects__(method_name, @backtrace).with(*args)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Mocha # :nodoc:
2
+ class TerseObjectStub
3
+ def initialize(object)
4
+ @object = object
5
+ end
6
+
7
+ def method_missing(method_name, *args, &block)
8
+ if block
9
+ @object.__stubs__(method_name).with(*args).returns(block)
10
+ else
11
+ @object.__stubs__(method_name).with(*args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module Mocha # :nodoc:
2
+ class TerseStub
3
+ def initialize(mock, backtrace)
4
+ @mock = mock
5
+ @backtrace = backtrace
6
+ end
7
+
8
+ def method_missing(method_name, *args, &block)
9
+ if block
10
+ @mock.__stubs__(method_name, @backtrace).with(*args).returns(block)
11
+ else
12
+ @mock.__stubs__(method_name, @backtrace).with(*args)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../spec_helper"
3
+
4
+ module Mocha
5
+ describe Mock, "#expects with terse syntax" do
6
+ it "sets single expectation" do
7
+ mock = Mock.new
8
+ mock.expects.method1.returns(1)
9
+ mock.method1.should == 1
10
+ end
11
+
12
+ it "passes backtrace into expectation" do
13
+ mock = Mock.new
14
+ backtrace = []
15
+ expectation = mock.expects(backtrace).method1
16
+ expectation.backtrace.should == backtrace
17
+ end
18
+
19
+ it "finds matching expectation" do
20
+ mock = Mock.new
21
+ expectation1 = mock.expects.my_method(:argument1, :argument2)
22
+ expectation2 = mock.expects.my_method(:argument3, :argument4)
23
+ mock.matching_expectation(:my_method, :argument1, :argument2).should == expectation1
24
+ mock.matching_expectation(:my_method, :argument3, :argument4).should == expectation2
25
+ end
26
+
27
+ it "finds most recent matching expectation" do
28
+ mock = Mock.new
29
+ expectation1 = mock.expects.my_method(:argument1, :argument2)
30
+ expectation2 = mock.expects.my_method(:argument1, :argument2)
31
+ mock.matching_expectation(:my_method, :argument1, :argument2).should == expectation2
32
+ end
33
+
34
+ it "invokes expectation and returns result" do
35
+ mock = Mock.new
36
+ mock.expects.my_method {:result}
37
+ result = mock.my_method
38
+ result.should == :result
39
+ end
40
+
41
+ it "yields supplied parameters to block" do
42
+ mock = Mock.new
43
+ parameters_for_yield = [1, 2, 3]
44
+ mock.expects.method1.yields(*parameters_for_yield)
45
+ yielded_parameters = nil
46
+ mock.method1() { |*parameters| yielded_parameters = parameters }
47
+ yielded_parameters.should == parameters_for_yield
48
+ end
49
+ end
50
+
51
+ describe Mock, "#stub with terse syntax" do
52
+ it "passes backtrace into stub" do
53
+ mock = Mock.new
54
+ backtrace = []
55
+ stub = mock.stubs(backtrace).method1
56
+ stub.backtrace.should == backtrace
57
+ end
58
+
59
+ it "creates and adds stubs" do
60
+ mock = Mock.new
61
+ stub1 = mock.stubs.method1
62
+ stub2 = mock.stubs.method2
63
+ mock.expectations.to_set.should == [stub1, stub2].to_set
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,34 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/../spec_helper"
3
+
4
+ describe Object, "#expects with original syntax" do
5
+ it "still works" do
6
+ o = Object.new
7
+ o.expects(:foo).returns("bar")
8
+ o.foo.should == "bar"
9
+ end
10
+ end
11
+
12
+ describe Object, "#expects with terse syntax" do
13
+ it "sets single expectation" do
14
+ o = Object.new
15
+ o.expects.foo {"bar"}
16
+ o.foo.should == "bar"
17
+ end
18
+ end
19
+
20
+ describe Object, "#stubs with original syntax" do
21
+ it "still works" do
22
+ o = Object.new
23
+ o.stubs(:foo).returns("bar")
24
+ o.foo.should == "bar"
25
+ end
26
+ end
27
+
28
+ describe Object, "#stubs with terse syntax" do
29
+ it "sets single expectation" do
30
+ o = Object.new
31
+ o.stubs.foo {"bar"}
32
+ o.foo.should == "bar"
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ gem "rspec", "0.9.3"
3
+ require "spec"
4
+ dir = File.dirname(__FILE__)
5
+ $LOAD_PATH.unshift File.expand_path("#{dir}/../lib")
6
+ require "mocha/shot"
7
+ require "set"
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :mocha
11
+ end
@@ -0,0 +1,12 @@
1
+ class SpecSuite
2
+ def run
3
+ dir = File.dirname(__FILE__)
4
+ Dir["#{dir}/**/*_spec.rb"].each do |file|
5
+ require file
6
+ end
7
+ end
8
+ end
9
+
10
+ if __FILE__ == $0
11
+ SpecSuite.new.run
12
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: mocha-shot
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-05-10 00:00:00 -07:00
8
+ summary: A terser Mocha mock framework. This library is based off some patches to Mocha with make the syntax shorter and easier to read. These changes may or may not be folded into Mocha's trunk.
9
+ require_paths:
10
+ - lib
11
+ email: brian.takita@gmail.com
12
+ homepage: http://mocha-shot.rubyforge.org
13
+ rubyforge_project: mocha-shot
14
+ description: A terser Mocha mock framework. This library is based off some patches to Mocha with make the syntax shorter and easier to read. These changes may or may not be folded into Mocha's trunk.
15
+ autorequire: mocha/shot
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Brian Takita
31
+ files:
32
+ - Rakefile
33
+ - README
34
+ - MIT-LICENSE
35
+ - COPYING
36
+ - CHANGES
37
+ - lib/mocha/shot.rb
38
+ - lib/mocha/shot/terse_mock.rb
39
+ - lib/mocha/shot/terse_stub.rb
40
+ - lib/mocha/shot/mock.rb
41
+ - lib/mocha/shot/object.rb
42
+ - lib/mocha/shot/terse_object_stub.rb
43
+ - lib/mocha/shot/single_return_value.rb
44
+ - spec/spec_helper.rb
45
+ - spec/spec_suite.rb
46
+ - spec/shot/mock_spec.rb
47
+ - spec/shot/object_spec.rb
48
+ test_files:
49
+ - spec/spec_suite.rb
50
+ rdoc_options:
51
+ - --title
52
+ - Mocha Shot
53
+ - --main
54
+ - README
55
+ - --line-numbers
56
+ extra_rdoc_files:
57
+ - README
58
+ - COPYING
59
+ - MIT-LICENSE
60
+ executables: []
61
+
62
+ extensions: []
63
+
64
+ requirements: []
65
+
66
+ dependencies:
67
+ - !ruby/object:Gem::Dependency
68
+ name: mocha
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Version::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.4.0
75
+ version: