mock_zen 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/README ADDED
@@ -0,0 +1 @@
1
+ Mock zen is a lightweight mocking system for Ruby.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ desc "Erases the doc folder and recreates the RDoc"
2
+ task :doc do
3
+ doc_dir = File.join(File.dirname(__FILE__), "doc")
4
+ FileUtils.rm_r(doc_dir) if File.exists?(doc_dir)
5
+ `rdoc`
6
+ end
7
+
8
+ desc "Runs all tests"
9
+ task :test do
10
+ test_dir = File.join(File.dirname(__FILE__), "test")
11
+ run_tests_in_directory(test_dir)
12
+ end
13
+
14
+ def run_tests_in_directory(path)
15
+ Dir.open(path) do |dir|
16
+ dir.each do |filename|
17
+ file_path = File.join(path, filename)
18
+ next if /^\.(\.)?$/ =~ filename
19
+
20
+ if /.*_test.rb$/ =~ file_path
21
+ puts "Running #{filename}"
22
+ puts `ruby #{file_path}`
23
+ elsif File.directory? file_path
24
+ run_tests_in_directory(file_path)
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/mock/spy.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Mock
2
+
3
+ class Mock::Spy
4
+
5
+ def initialize
6
+ @methods_called = Hash.new(0)
7
+ end
8
+
9
+ def called?(method_name)
10
+ @methods_called[method_name.to_sym] if method_name.respond_to? :to_sym
11
+ end
12
+
13
+ private
14
+ def method_missing(name, *args, &block)
15
+ @methods_called[name] += 1
16
+ end
17
+
18
+ end
19
+
20
+ end
data/lib/mock/stub.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Mock
2
+
3
+ class Mock::Stub
4
+ # Creates the stub with the actions passed. The stub will respond to the keys in
5
+ # the hash and return the corresponding values.
6
+ # stub = Mock::Stub(method_call: false)
7
+ # stub.method_call # false
8
+ def initialize(actions={})
9
+ @actions = actions
10
+ end
11
+
12
+ def add_behaviour(options)
13
+ options = Mock::parse_actions(options)
14
+ @actions.merge! options
15
+ self
16
+ end
17
+
18
+ private
19
+ def method_missing(name, *args, &block)
20
+ raise Mock::UndefinedActionError, "Undefined action #{name}" unless @actions[name]
21
+ @actions[name]
22
+ end
23
+
24
+ alias :<< :add_behaviour
25
+ end
26
+
27
+ end
@@ -0,0 +1,6 @@
1
+ module Mock
2
+
3
+ class Mock::UndefinedActionError < RuntimeError
4
+ end
5
+
6
+ end
data/lib/mock.rb ADDED
@@ -0,0 +1,30 @@
1
+ require_relative 'mock/spy'
2
+ require_relative 'mock/stub'
3
+ require_relative 'mock/undefined_action_error'
4
+
5
+ module Mock
6
+
7
+ # Creates a stub with the given actions
8
+ def Mock::Stub(actions={})
9
+ Stub.new(parse_actions(actions))
10
+ end
11
+
12
+ def Mock::Spy()
13
+ Spy.new
14
+ end
15
+
16
+ protected
17
+ # Ensures that the actions have Symbol or String keys
18
+ def Mock::parse_actions(actions)
19
+ actions.reject! do |key, value|
20
+ !key.instance_of?(String) and !key.instance_of?(Symbol)
21
+ end
22
+
23
+ actions.each do |key, value|
24
+ if key.instance_of? String
25
+ actions.delete key
26
+ actions[key.to_sym] = value
27
+ end
28
+ end
29
+ end
30
+ end
data/test/spy_test.rb ADDED
@@ -0,0 +1,22 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SpyTest < Test::Unit::TestCase
4
+ context "A spy" do
5
+
6
+ should "record how many times a given method is called" do
7
+ spy = Mock::Spy()
8
+ 5.times do
9
+ spy.method_call
10
+ end
11
+
12
+ assert_equal 5, spy.called?(:method_call)
13
+ end
14
+
15
+ should "return 0 if the method wasn't called at all" do
16
+ spy = Mock::Spy()
17
+
18
+ assert_equal 0, spy.called?(:method_call)
19
+ end
20
+ end
21
+
22
+ end
data/test/stub_test.rb ADDED
@@ -0,0 +1,34 @@
1
+ require_relative 'test_helper'
2
+
3
+ class StubTest < Test::Unit::TestCase
4
+
5
+ context "An stub" do
6
+ should "fail if nothing is registered" do
7
+ stub = Mock::Stub()
8
+ assert_raise Mock::UndefinedActionError do
9
+ stub.method_call
10
+ end
11
+ end
12
+
13
+ should "not fali if the method called is registered" do
14
+ stub = Mock::Stub(method_call: true)
15
+ assert_nothing_raised do
16
+ stub.method_call
17
+ end
18
+ end
19
+
20
+ should "return what is defined on the hash" do
21
+ stub = Mock::Stub(method_call: true)
22
+ assert stub.method_call
23
+ end
24
+
25
+ should "respond to an added behaviour" do
26
+ stub = Mock::Stub()
27
+ assert_raise Mock::UndefinedActionError do
28
+ stub.method_call
29
+ end
30
+ stub.add_behaviour(method_call: true)
31
+ assert stub.method_call
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,2 @@
1
+ require 'shoulda'
2
+ require_relative '../lib/mock'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mock_zen
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Guilherme Carvalho
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-08 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " Mock zen is an extremely simple mocking library.\n Makes TATFT an easy and sane thing to do. \n"
22
+ email: guilherme@guava.com.br
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/mock.rb
31
+ - lib/mock/spy.rb
32
+ - lib/mock/stub.rb
33
+ - lib/mock/undefined_action_error.rb
34
+ - Rakefile
35
+ - test/spy_test.rb
36
+ - test/stub_test.rb
37
+ - test/test_helper.rb
38
+ - README
39
+ has_rdoc: true
40
+ homepage: http://zen.guilhermecarvalho.com.br
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
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.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A radically simple mocking library
69
+ test_files: []
70
+