minitest-moar 0.0.3 → 0.0.4

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: 9da15c41e06552c11a6f9e7a9a2efd7cbdfa078c
4
- data.tar.gz: b2f5d70d6afc748b234238c7fb3e199d8aef2755
3
+ metadata.gz: 7bd36ad22b5cbc2fe05af43cbda9cd06e93b390f
4
+ data.tar.gz: e9a7a1629f9a259d9dca78543087057ea61eba9c
5
5
  SHA512:
6
- metadata.gz: e26d9dfbe2269e33817a4a26ab05f981d2c0d4a9668f96715459e7dbde4ae0842afbeb875f8f0481d4f7880a65f877a678923ef903546c2235f6cb08396bd5cc
7
- data.tar.gz: 2f9e4688ccb30a2e0dca263a3788c991ec7ed80609da39d9cb1480a2161241a75c2b48868fff9b18cdd5b15489b6327091b01f3a854aeae6ab480fccbb5496a6
6
+ metadata.gz: 617a0b96ae8bc72f5ca2ba858a72c8cd53e16b247aba124a60083df40b5b1d3441d740cc41d00fb5637bcbaa4527f5cae3761445d5f787bda15ace6429e41050
7
+ data.tar.gz: e31b2670df6dc3356bcdb57a758538d528418211b834b852a76c07618901f7a14be4d3412fd68a6fd14b721140df2d0908b17d553ba6ac80d964ab3b696253c0
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in minitest-moar.gemspec
4
4
  gemspec
5
5
  gem 'byebug'
6
+ gem 'm', github: 'zamith/m'
@@ -2,32 +2,42 @@ module Minitest::Moar::Stubbing
2
2
  def stub klass, name, val_or_callable = nil, *block_args, &block
3
3
  @invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
4
4
 
5
- new_name = "__minitest_stub__#{name}"
6
-
7
5
  metaclass = class << klass; self; end
8
- instance_stub metaclass, name, val_or_callable, block_args, &block
6
+
7
+ if metaclass.respond_to? name and not metaclass.instance_methods.map(&:to_s).include? name.to_s then
8
+ metaclass.send :define_method, name do |*args|
9
+ super(*args)
10
+ end
11
+ end
12
+ __stub__ metaclass, name, val_or_callable, block_args, &block
9
13
  end
10
14
 
11
- def instance_stub klass, name, val_or_callable = nil, *block_args, &block
12
- @invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
15
+ def instance_stub obj, name, val_or_callable = nil, *block_args, &block
16
+ if obj.respond_to? name and not obj.methods.map(&:to_s).include? name.to_s then
17
+ obj.send :define_method, name do |*args|
18
+ super(*args)
19
+ end
20
+ end
13
21
 
14
- invoker = Proc.new { |klass, name| @invocations[klass][name] += 1 }
22
+ __stub__ obj, name, val_or_callable, block_args, &block
23
+ end
15
24
 
25
+ def __stub__ obj, name, val_or_callable = nil, *block_args, &block
16
26
  new_name = "__minitest_stub__#{name}"
17
27
 
18
- if respond_to? name and not methods.map(&:to_s).include? name.to_s then
19
- klass.send :define_method, name do |*args|
20
- super(*args)
21
- end
22
- end
28
+ @invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
29
+
30
+ invoker = Proc.new { |klass, name| @invocations[klass][name] += 1 }
23
31
 
24
- klass.send :alias_method, new_name, name
32
+ obj.send :alias_method, new_name, name
25
33
 
26
- klass.send :define_method, name do |*args, &blk|
27
- if self.is_a?(Class)
34
+ # Module.method(:define_method).bind(klass).call(name) do |*args, &blk|
35
+ obj.send :define_method, name do |*args, &blk|
36
+ if ::Class === self
28
37
  invoker.call(self, name)
29
38
  else
30
- invoker.call("Instance of #{self.class}", name)
39
+ klass = Kernel.instance_method(:class).bind(self).call
40
+ invoker.call("Instance of #{klass}", name)
31
41
  end
32
42
 
33
43
  ret = if val_or_callable.respond_to? :call then
@@ -41,11 +51,11 @@ module Minitest::Moar::Stubbing
41
51
  ret
42
52
  end
43
53
 
44
- yield klass
54
+ yield obj
45
55
  ensure
46
- klass.send :undef_method, name
47
- klass.send :alias_method, name, new_name
48
- klass.send :undef_method, new_name
56
+ obj.send :undef_method, name
57
+ obj.send :alias_method, name, new_name
58
+ obj.send :undef_method, new_name
49
59
  end
50
60
  end
51
61
 
@@ -1,5 +1,5 @@
1
1
  module Minitest
2
2
  module Moar
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.6"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.1"
23
24
  end
@@ -39,4 +39,26 @@ class StubbingTest < Minitest::Test
39
39
 
40
40
  assert_equal 1, @called
41
41
  end
42
+
43
+ def test_basic_object_stubbing
44
+ dog = Dog.new
45
+
46
+ @called = 0
47
+ caller = Proc.new { @called += 1 }
48
+ stub dog, :name, caller do
49
+ dog.name
50
+ end
51
+
52
+ assert_equal 1, @called
53
+ end
54
+
55
+ def test_module_stubbing
56
+ @called = 0
57
+ caller = Proc.new { @called += 1 }
58
+ stub Project, :name, caller do
59
+ Project.name
60
+ end
61
+
62
+ assert_equal 1, @called
63
+ end
42
64
  end
@@ -15,3 +15,15 @@ class Person
15
15
  book.read
16
16
  end
17
17
  end
18
+
19
+ class Dog < BasicObject
20
+ def name
21
+ 'Boomer'
22
+ end
23
+ end
24
+
25
+ module Project
26
+ def self.name
27
+ 'Best Project Evar'
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-moar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Cardarella
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-20 00:00:00.000000000 Z
11
+ date: 2014-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
41
55
  description: Moar Minitest Please!
42
56
  email:
43
57
  - bcardarella@gmail.com