motion-stump 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 582335357cf4b0b510e0ebb140abfd520f9c05bb
4
- data.tar.gz: e1571647e3da93c289ef8d443565ab13b00c9722
3
+ metadata.gz: dafe29c6f7a0e5c2425dbed30c336b6ab3a354db
4
+ data.tar.gz: 6174a4006293ab88a138d6f8d470fdeab9d2a3a9
5
5
  SHA512:
6
- metadata.gz: 37cd5afe544468ce18afe677f6eb0020d6111845f605bf1ebc26916877e5b30431978045a189b1d0d269de58d8c0850f558ed5ab9dc8dc69a5c09b00d6e835b5
7
- data.tar.gz: d32793edd2866cf12ea63245cf4ef278b0c372fbc1c195ce669a251b267aa7bb9d137db4f9b525f4c478238bb855d5876fb57b6bd31992ca47ab404631f4e81c
6
+ metadata.gz: ba97813ed8f07dc31bf1dd2a5a078bebc60f019ebb2a41c895bd1b7f8173a16dc54901ea6687d3802a7518722eef643d786b8811ba26bd6522a04030cbdb6bc1
7
+ data.tar.gz: efd82644bee3d4749b377db7bcbfe32cddec8e998b426966b49f07144ddcf82e06de79719140f1c275e6db36576d059629eb0a37bbf8b145c6943163692a04be
data/.gitignore CHANGED
@@ -1 +1,3 @@
1
- Gemfile.lock
1
+ Gemfile.lock
2
+ .repl_history
3
+ build
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in bubble-wrap.gemspec
4
4
  gemspec
data/Rakefile CHANGED
@@ -1,20 +1,19 @@
1
- # -*- coding: utf-8 -*-
2
- require "bundler/gem_tasks"
3
-
4
- $:.unshift("/Library/RubyMotion/lib")
5
-
6
- require 'motion/project'
7
- require 'bundler'
8
- Bundler.setup :default, :development
9
- require 'motion-stump'
10
-
11
- Motion::Project::App.setup do |app|
12
- app.name = 'motion-stump'
13
- end
14
-
15
- desc "Build the gem"
16
- task :gem do
17
- sh "bundle exec gem build motion-stump.gemspec"
18
- sh "mkdir -p pkg"
19
- sh "mv *.gem pkg/"
20
- end
1
+ # -*- coding: utf-8 -*-
2
+ require "bundler/gem_tasks"
3
+
4
+ $:.unshift("/Library/RubyMotion/lib")
5
+ require 'motion/project/template/ios'
6
+ require 'bundler'
7
+ Bundler.setup :default, :development
8
+ require 'motion-stump'
9
+
10
+ Motion::Project::App.setup do |app|
11
+ app.name = 'motion-stump'
12
+ end
13
+
14
+ desc "Build the gem"
15
+ task :gem do
16
+ sh "bundle exec gem build motion-stump.gemspec"
17
+ sh "mkdir -p pkg"
18
+ sh "mv *.gem pkg/"
19
+ end
File without changes
@@ -5,26 +5,18 @@ end
5
5
  ## Extend Motion config to include our spec helper
6
6
  module Motion; module Project;
7
7
  class Config
8
- alias_method :spec_files_before_stump, :spec_files
9
- def spec_files
10
- core = Dir.chdir(motiondir + '/lib/motion') { (['spec.rb'] + Dir.glob(File.join('spec', 'helpers', '*.rb'))).map { |x| File.expand_path(x) } }
11
- (core + [File.join(File.dirname(__FILE__), 'stump/stump_spec_helper.rb')] + spec_files_before_stump).uniq
8
+ alias_method :spec_core_files_without_stump, :spec_core_files
9
+ def spec_core_files
10
+ spec_core_files_without_stump + [File.join(File.dirname(__FILE__), 'motion/stump_spec_helper.rb')]
12
11
  end
13
12
  end
14
13
  end; end
15
14
 
16
-
17
15
  ## Include stump in dev mode
18
16
  Motion::Project::App.setup do |app|
19
17
  app.development do
20
- [
21
- File.join(File.dirname(__FILE__), 'stump/version.rb'),
22
- File.join(File.dirname(__FILE__), 'stump/metaid.rb'),
23
- File.join(File.dirname(__FILE__), 'stump/stub.rb'),
24
- File.join(File.dirname(__FILE__), 'stump/mocks.rb'),
25
- File.join(File.dirname(__FILE__), 'stump/mock.rb'),
26
- File.join(File.dirname(__FILE__), 'stump/proxy.rb'),
27
- File.join(File.dirname(__FILE__), 'stump/app_delegate.rb')
28
- ].reverse.each {|f| app.files.unshift(f) }
18
+ stump_lib_dir = File.join(File.dirname(__FILE__), 'stump')
19
+ stump_files = Dir.glob(stump_lib_dir + "/**/*.rb")
20
+ app.files.unshift(stump_files)
29
21
  end
30
22
  end
@@ -9,17 +9,19 @@ module Stump
9
9
  end
10
10
 
11
11
  def verify_mocks
12
- if !Stump::Mocks.failures.nil? && !Stump::Mocks.failures.empty?
13
- fails = Stump::Mocks.failures.map {|object, method| "#{object.inspect} expected #{method}"}.join(", ")
14
- should.flunk "Unmet expectations: #{fails}"
12
+ begin
13
+ if !Stump::Mocks.failures.nil? && !Stump::Mocks.failures.empty?
14
+ fails = Stump::Mocks.failures.map {|object, method| "#{object.inspect} expected #{method}"}.join(", ")
15
+ should.flunk "Unmet expectations: #{fails}"
16
+ end
17
+ ensure
18
+ Stump::Mocks.clear!
15
19
  end
16
20
  end
17
21
 
18
22
  def it_with_mock_verification(description, &block)
19
23
  @after << proc { verify_mocks }
20
24
  it_without_mock_verification(description, &block)
21
- ensure
22
- Stump::Mocks.clear!
23
25
  end
24
26
  end
25
27
  end
@@ -29,4 +31,4 @@ begin
29
31
  Bacon::Context.class_eval { include Stump::Baconize::ContextExtensions }
30
32
  rescue LoadError
31
33
  puts 'Bacon is not available.'
32
- end
34
+ end
@@ -7,7 +7,11 @@ class Object
7
7
 
8
8
  # Adds methods to a metaclass
9
9
  def meta_def name, &blk
10
- meta_eval { define_method name, &blk }
10
+ meta_eval {
11
+ define_method(name) {|*args, &block|
12
+ blk.call(*args, &block)
13
+ }
14
+ }
11
15
  end
12
16
 
13
17
  # Defines an instance method within a class
@@ -0,0 +1,30 @@
1
+ class Object
2
+ def safe_meta_def method_name, &method_body
3
+ metaclass.remember_original_method(method_name)
4
+ meta_eval {
5
+ define_method(method_name) {|*args, &block|
6
+ method_body.call(*args, &block)
7
+ }
8
+ }
9
+ end
10
+
11
+ def reset(method_name)
12
+ metaclass.restore_original_method(method_name)
13
+ end
14
+
15
+ protected
16
+
17
+ def remember_original_method(method_name)
18
+ alias_method "__original_#{method_name}".to_sym, method_name if method_defined?(method_name)
19
+ self
20
+ end
21
+
22
+ def restore_original_method(method_name)
23
+ original_method_name = "__original_#{method_name}".to_sym
24
+ if method_defined?(original_method_name)
25
+ alias_method method_name, original_method_name
26
+ remove_method original_method_name
27
+ end
28
+ self
29
+ end
30
+ end
@@ -1,25 +1,25 @@
1
1
  class Object
2
2
  # Create a mock method on an object. A mock object will place an expectation
3
3
  # on behavior and cause a test failure if it's not fulfilled.
4
- #
4
+ #
5
5
  # ==== Examples
6
6
  #
7
7
  # my_string = "a wooden rabbit"
8
8
  # my_string.mock!(:retreat!, :return => "run away! run away!")
9
9
  # my_string.mock!(:question, :return => "what is the airspeed velocity of an unladen sparrow?")
10
- #
10
+ #
11
11
  # # test/your_test.rb
12
12
  # my_string.retreat! # => "run away! run away!"
13
13
  # # If we let the test case end at this point, it fails with:
14
14
  # # Unmet expectation: #<Sparrow:1ee7> expected question
15
15
  #
16
- def mock!(method, options = {}, &block)
16
+ def mock!(method, options = {}, &block)
17
17
  Stump::Mocks.add([self, method])
18
-
18
+
19
19
  behavior = if block_given?
20
- lambda do |*args|
20
+ lambda do |*args|
21
21
  raise ArgumentError if block.arity >= 0 && args.length != block.arity
22
-
22
+
23
23
  Stump::Mocks.verify([self, method])
24
24
  block.call(*args)
25
25
  end
@@ -29,20 +29,20 @@ class Object
29
29
  yield(options[:yield])
30
30
  end
31
31
  else
32
- lambda do |*args|
32
+ lambda do |*args|
33
33
  Stump::Mocks.verify([self, method])
34
34
  return options[:return]
35
35
  end
36
36
  end
37
37
 
38
- meta_def method, &behavior
38
+ safe_meta_def method, &behavior
39
39
  end
40
40
 
41
41
  def should_not_call(method)
42
42
  behavior = lambda do |*args|
43
43
  should.flunk "Umet expectations: #{method} expected to not be called"
44
44
  end
45
- meta_def method, &behavior
45
+ safe_meta_def method, &behavior
46
46
  end
47
47
  end
48
48
 
@@ -50,7 +50,7 @@ module Kernel
50
50
  # Create a pure mock object rather than mocking specific methods on an object.
51
51
  #
52
52
  # ==== Examples
53
- #
53
+ #
54
54
  # my_mock = mock(:thing, :return => "whee!")
55
55
  # my_mock.thing # => "whee"
56
56
  #
@@ -13,13 +13,7 @@ class Object
13
13
  def stub!(method_name, options = {}, &stubbed)
14
14
  behavior = (block_given? ? stubbed : lambda { return options[:return] })
15
15
 
16
- class << self
17
- self
18
- end.instance_eval {
19
- define_method(method_name) { |*args, &block|
20
- behavior.call(*args, &block)
21
- }
22
- }
16
+ safe_meta_def method_name, &behavior
23
17
  end
24
18
  end
25
19
 
@@ -1,3 +1,3 @@
1
1
  module Stump
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,6 +6,10 @@ describe "Application 'stump-test'" do
6
6
  return Dog.new
7
7
  end
8
8
 
9
+ def self.kind
10
+ 'Mammal'
11
+ end
12
+
9
13
  def bark
10
14
  "Woof!"
11
15
  end
@@ -31,7 +35,7 @@ describe "Application 'stump-test'" do
31
35
  end
32
36
 
33
37
  describe "#stub!" do
34
- it "should stub a class method" do
38
+ it "should stub a class method" do
35
39
  Dog.stub!(:thing, return: :thing)
36
40
  Dog.should.not.be.nil
37
41
  Dog.thing.should == :thing
@@ -53,7 +57,7 @@ describe "Application 'stump-test'" do
53
57
  my_obj.hello("foo", "bar").should.be == "foo,bar"
54
58
  end
55
59
  end
56
-
60
+
57
61
  describe "#stub" do
58
62
  it "should create a pure stub" do
59
63
  my_stub = stub(:thing, return: "dude, a thing!")
@@ -108,7 +112,7 @@ describe "Application 'stump-test'" do
108
112
  end
109
113
 
110
114
  describe "#mock" do
111
- it "should create pure mock" do
115
+ it "should create pure mock" do
112
116
  my_mock = mock(:hello, return: "hi")
113
117
  my_mock.hello.should == "hi"
114
118
  end
@@ -141,4 +145,61 @@ describe "Application 'stump-test'" do
141
145
  end
142
146
  end
143
147
  end
148
+
149
+ describe "#reset" do
150
+ describe "stubbing" do
151
+ it "should restore original class method" do
152
+ Dog.stub!(:kind, return: 'Reptile')
153
+ Dog.kind.should == 'Reptile'
154
+ Dog.reset(:kind)
155
+ Dog.kind.should == 'Mammal'
156
+ end
157
+
158
+ it "should restore original instance method" do
159
+ @dog.stub!(:bark, return: 'Meow!')
160
+ @dog.bark.should == 'Meow!'
161
+ @dog.reset(:bark)
162
+ @dog.bark.should == 'Woof!'
163
+ end
164
+ end
165
+
166
+ describe "mocking" do
167
+ it "should restore original class method" do
168
+ Dog.mock!(:kind, return: 'Reptile')
169
+ Dog.kind.should == 'Reptile'
170
+ Dog.reset(:kind)
171
+ Dog.kind.should == 'Mammal'
172
+ end
173
+
174
+ it "should restore original instance method" do
175
+ @dog.mock!(:bark, return: 'Meow!')
176
+ @dog.bark.should == 'Meow!'
177
+ @dog.reset(:bark)
178
+ @dog.bark.should == 'Woof!'
179
+ end
180
+ end
181
+ end
182
+
183
+ describe "after each scenario" do
184
+ it "should verify mocks" do
185
+ Should.class_eval do
186
+ def ignored_flunk(message)
187
+ end
188
+
189
+ alias_method :original_flunk, :flunk
190
+ alias_method :flunk, :ignored_flunk
191
+ end
192
+
193
+ Dog.mock!(:kind, return: 'Reptile')
194
+ 1.should == 1
195
+ end
196
+
197
+ it "should have cleared mocks from the previous scenario" do
198
+ 1.should == 1
199
+
200
+ Should.class_eval do
201
+ alias_method :flunk, :original_flunk
202
+ end
203
+ end
204
+ end
144
205
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-stump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Chong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-17 00:00:00.000000000 Z
11
+ date: 2013-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: motion-redgreen
@@ -51,14 +51,15 @@ files:
51
51
  - License.txt
52
52
  - README.md
53
53
  - Rakefile
54
+ - app/app_delegate.rb
54
55
  - lib/motion-stump.rb
55
- - lib/stump/app_delegate.rb
56
+ - lib/motion/stump_spec_helper.rb
56
57
  - lib/stump/metaid.rb
58
+ - lib/stump/metareset.rb
57
59
  - lib/stump/mock.rb
58
60
  - lib/stump/mocks.rb
59
61
  - lib/stump/proxy.rb
60
62
  - lib/stump/stub.rb
61
- - lib/stump/stump_spec_helper.rb
62
63
  - lib/stump/version.rb
63
64
  - motion-stump.gemspec
64
65
  - spec/main_spec.rb