much-stub 0.1.2 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MuchStub
4
+ class Call
5
+ attr_reader :pargs, :kargs, :block
6
+
7
+ def initialize(*pargs, **kargs, &block)
8
+ @pargs = pargs.empty? ? nil : pargs
9
+ @kargs = kargs.empty? ? nil : kargs
10
+ @block = block
11
+ end
12
+
13
+ def args
14
+ @args ||= [*pargs, kargs].compact
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "much-stub/call"
4
+
5
+ module MuchStub
6
+ class CallSpy < ::BasicObject
7
+ METHOD_NAME_REPLACEMENTS = {
8
+ "!" => "_bang",
9
+ "?" => "_predicate",
10
+ }.freeze
11
+
12
+ def initialize(**return_values)
13
+ @call_spy_return_values = return_values.transform_keys(&:to_s)
14
+ @call_spy_method_calls = ::Hash.new{ |hash, key| hash[key] = [] }
15
+ @call_spy_method_return_values =
16
+ ::Hash.new{ |hash, key| hash[key] = call_spy_return_value_proc(key) }
17
+ end
18
+
19
+ def call_spy_tap
20
+ yield self
21
+ self
22
+ end
23
+
24
+ def ==(other)
25
+ equal?(other)
26
+ end
27
+
28
+ def ===(other)
29
+ equal?(other)
30
+ end
31
+
32
+ def eql?(other)
33
+ equal?(other)
34
+ end
35
+
36
+ def equal?(other)
37
+ __id__ == other.__id__
38
+ end
39
+
40
+ def hash
41
+ __id__
42
+ end
43
+
44
+ def respond_to?(*)
45
+ true
46
+ end
47
+
48
+ def inspect
49
+ "#<MuchStub::CallSpy:#{"0x0%x" % (__id__ << 1)}>"
50
+ end
51
+
52
+ private
53
+
54
+ def call_spy_method_return_value(method_name, much_stub_call)
55
+ @call_spy_method_return_values[method_name.to_s].call(much_stub_call)
56
+ end
57
+
58
+ def call_spy_return_value_proc(method_name)
59
+ value = @call_spy_return_values.fetch(method_name, ::Proc.new{ self })
60
+ value.respond_to?(:call) ? value : ::Proc.new{ value }
61
+ end
62
+
63
+ def call_spy_normalize_method_name(name)
64
+ METHOD_NAME_REPLACEMENTS.reduce(name.to_s) do |acc, (source, replacement)|
65
+ acc.gsub(source, replacement)
66
+ end
67
+ end
68
+
69
+ def call_spy_define_spied_method(name)
70
+ method_name = call_spy_normalize_method_name(name)
71
+ call_spy_define_metaclass_method(name) do |*args, &block|
72
+ call = ::MuchStub::Call.new(*args, &block)
73
+ @call_spy_method_calls[method_name] << call
74
+ call_spy_method_return_value(name, call)
75
+ end
76
+ end
77
+
78
+ def call_spy_define_query_method(query_method_match)
79
+ spied_method_name = query_method_match[1]
80
+ query_method_suffix = query_method_match[2]
81
+ method_name = call_spy_normalize_method_name(spied_method_name)
82
+ call_spy_define_metaclass_method(
83
+ "#{method_name}#{query_method_suffix}",
84
+ ) do
85
+ yield(method_name) if ::Kernel.block_given?
86
+ end
87
+ end
88
+
89
+ def call_spy_define_metaclass_method(name, &block)
90
+ metaclass = class << self; self; end
91
+ metaclass.define_method(name, &block)
92
+ end
93
+
94
+ def respond_to_missing?(_name, *_args)
95
+ false
96
+ end
97
+
98
+ def method_missing(name, *args, &block)
99
+ if (match = name.match(/(\w+)(_calls)\z/))
100
+ call_spy_define_query_method(match) do |method_name|
101
+ @call_spy_method_calls[method_name]
102
+ end
103
+ elsif (match = name.match(/(\w+)(_last_called_with)\z/))
104
+ call_spy_define_query_method(match) do |method_name|
105
+ __send__("#{method_name}_calls").last
106
+ end
107
+ elsif (match = name.match(/(\w+)(_called_with)\z/))
108
+ call_spy_define_query_method(match) do |method_name|
109
+ __send__("#{method_name}_last_called_with")
110
+ end
111
+ elsif (match = name.match(/(\w+)(_call_count)\z/))
112
+ call_spy_define_query_method(match) do |method_name|
113
+ __send__("#{method_name}_calls").size
114
+ end
115
+ elsif (match = name.match(/(\w+)(_called\?)\z/))
116
+ call_spy_define_query_method(match) do |method_name|
117
+ __send__("#{method_name}_call_count") > 0
118
+ end
119
+ else
120
+ call_spy_define_spied_method(name)
121
+ end
122
+ __send__(name, *args, &block)
123
+ end
124
+ end
125
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MuchStub
2
- VERSION = "0.1.2"
4
+ VERSION = "0.1.7"
3
5
  end
@@ -1,23 +1,32 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
2
4
  lib = File.expand_path("../lib", __FILE__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require "much-stub/version"
5
7
 
6
8
  Gem::Specification.new do |gem|
7
- gem.name = "much-stub"
8
- gem.version = MuchStub::VERSION
9
- gem.authors = ["Kelly Redding", "Collin Redding"]
10
- gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
- gem.summary = "Stubbing API for replacing method calls on objects in test runs."
12
- gem.description = "Stubbing API for replacing method calls on objects in test runs."
13
- gem.homepage = "https://github.com/redding/much-stub"
14
- gem.license = "MIT"
15
-
16
- gem.files = `git ls-files`.split($/)
9
+ gem.name = "much-stub"
10
+ gem.version = MuchStub::VERSION
11
+ gem.authors = ["Kelly Redding", "Collin Redding"]
12
+ gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
13
+
14
+ gem.summary =
15
+ "Stubbing API for replacing method calls on objects in test runs."
16
+ gem.description =
17
+ "Stubbing API for replacing method calls on objects in test runs."
18
+
19
+ gem.homepage = "https://github.com/redding/much-stub"
20
+ gem.license = "MIT"
21
+
22
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
23
+
17
24
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
25
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
26
  gem.require_paths = ["lib"]
20
27
 
21
- gem.add_development_dependency("assert", ["~> 2.18.0"])
28
+ gem.required_ruby_version = "~> 2.5"
22
29
 
30
+ gem.add_development_dependency("assert", ["~> 2.19.2"])
31
+ gem.add_development_dependency("much-style-guide", ["~> 0.6.0"])
23
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # this file is automatically required when you run `assert`
2
4
  # put any test helpers here
3
5
 
@@ -12,7 +14,7 @@ require "test/support/factory"
12
14
  # 1.8.7 backfills
13
15
 
14
16
  # Array#sample
15
- if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
17
+ if !(a = []).respond_to?(:sample) && a.respond_to?(:choice)
16
18
  class Array
17
19
  alias_method :sample, :choice
18
20
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "assert/factory"
2
4
 
3
5
  module Factory
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "assert"
2
4
  require "much-stub"
3
5
 
@@ -55,15 +57,15 @@ module MuchStub
55
57
  end
56
58
 
57
59
  should "not allow stubbing methods with invalid arity" do
58
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
60
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
59
61
 
60
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
61
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
62
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
63
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
62
64
 
63
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
64
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
65
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
66
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
65
67
 
66
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
68
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
67
69
  end
68
70
 
69
71
  should "not allow calling methods with invalid arity" do
@@ -128,15 +130,15 @@ module MuchStub
128
130
  end
129
131
 
130
132
  should "not allow stubbing methods with invalid arity" do
131
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
133
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
132
134
 
133
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
134
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
135
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
136
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
135
137
 
136
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
137
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
138
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
139
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
138
140
 
139
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
141
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
140
142
  end
141
143
 
142
144
  should "not allow calling methods with invalid arity" do
@@ -201,15 +203,15 @@ module MuchStub
201
203
  end
202
204
 
203
205
  should "not allow stubbing methods with invalid arity" do
204
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
206
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
205
207
 
206
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
207
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
208
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
209
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
208
210
 
209
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
210
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
211
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
212
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
211
213
 
212
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
214
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
213
215
  end
214
216
 
215
217
  should "not allow calling methods with invalid arity" do
@@ -274,15 +276,15 @@ module MuchStub
274
276
  end
275
277
 
276
278
  should "not allow stubbing methods with invalid arity" do
277
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
279
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
278
280
 
279
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
280
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
281
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
282
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
281
283
 
282
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
283
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
284
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
285
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
284
286
 
285
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
287
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
286
288
  end
287
289
 
288
290
  should "not allow calling methods with invalid arity" do
@@ -348,15 +350,15 @@ module MuchStub
348
350
  end
349
351
 
350
352
  should "not allow stubbing methods with invalid arity" do
351
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
353
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
352
354
 
353
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
354
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
355
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
356
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
355
357
 
356
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
357
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
358
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
359
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
358
360
 
359
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
361
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
360
362
  end
361
363
 
362
364
  should "not allow calling methods with invalid arity" do
@@ -421,15 +423,15 @@ module MuchStub
421
423
  end
422
424
 
423
425
  should "not allow stubbing methods with invalid arity" do
424
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
426
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
425
427
 
426
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
427
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
428
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
429
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
428
430
 
429
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
430
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
431
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
432
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
431
433
 
432
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
434
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
433
435
  end
434
436
 
435
437
  should "not allow calling methods with invalid arity" do
@@ -495,15 +497,15 @@ module MuchStub
495
497
  end
496
498
 
497
499
  should "not allow stubbing methods with invalid arity" do
498
- assert_raises{ MuchStub.stub(subject, :noargs).with(1){ } }
500
+ assert_raises{ MuchStub.stub(subject, :noargs).with(1){} }
499
501
 
500
- assert_raises{ MuchStub.stub(subject, :withargs).with{ } }
501
- assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
502
+ assert_raises{ MuchStub.stub(subject, :withargs).with{} }
503
+ assert_raises{ MuchStub.stub(subject, :withargs).with(1, 2){} }
502
504
 
503
- assert_raises{ MuchStub.stub(subject, :minargs).with{ } }
504
- assert_raises{ MuchStub.stub(subject, :minargs).with(1){ } }
505
+ assert_raises{ MuchStub.stub(subject, :minargs).with{} }
506
+ assert_raises{ MuchStub.stub(subject, :minargs).with(1){} }
505
507
 
506
- assert_raises{ MuchStub.stub(subject, :withblock).with(1){ } }
508
+ assert_raises{ MuchStub.stub(subject, :withblock).with(1){} }
507
509
  end
508
510
 
509
511
  should "not allow calling methods with invalid arity" do
@@ -568,15 +570,15 @@ module MuchStub
568
570
  end
569
571
 
570
572
  should "allow stubbing methods with invalid arity" do
571
- assert_nothing_raised{ MuchStub.stub(subject, :noargs).with(1){ } }
573
+ assert_nothing_raised{ MuchStub.stub(subject, :noargs).with(1){} }
572
574
 
573
- assert_nothing_raised{ MuchStub.stub(subject, :withargs).with{ } }
574
- assert_nothing_raised{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
575
+ assert_nothing_raised{ MuchStub.stub(subject, :withargs).with{} }
576
+ assert_nothing_raised{ MuchStub.stub(subject, :withargs).with(1, 2){} }
575
577
 
576
- assert_nothing_raised{ MuchStub.stub(subject, :minargs).with{ } }
577
- assert_nothing_raised{ MuchStub.stub(subject, :minargs).with(1){ } }
578
+ assert_nothing_raised{ MuchStub.stub(subject, :minargs).with{} }
579
+ assert_nothing_raised{ MuchStub.stub(subject, :minargs).with(1){} }
578
580
 
579
- assert_nothing_raised{ MuchStub.stub(subject, :withblock).with(1){ } }
581
+ assert_nothing_raised{ MuchStub.stub(subject, :withblock).with(1){} }
580
582
  end
581
583
 
582
584
  should "allow calling methods with invalid arity" do
@@ -641,15 +643,15 @@ module MuchStub
641
643
  end
642
644
 
643
645
  should "allow stubbing methods with invalid arity" do
644
- assert_nothing_raised{ MuchStub.stub(subject, :noargs).with(1){ } }
646
+ assert_nothing_raised{ MuchStub.stub(subject, :noargs).with(1){} }
645
647
 
646
- assert_nothing_raised{ MuchStub.stub(subject, :withargs).with{ } }
647
- assert_nothing_raised{ MuchStub.stub(subject, :withargs).with(1, 2){ } }
648
+ assert_nothing_raised{ MuchStub.stub(subject, :withargs).with{} }
649
+ assert_nothing_raised{ MuchStub.stub(subject, :withargs).with(1, 2){} }
648
650
 
649
- assert_nothing_raised{ MuchStub.stub(subject, :minargs).with{ } }
650
- assert_nothing_raised{ MuchStub.stub(subject, :minargs).with(1){ } }
651
+ assert_nothing_raised{ MuchStub.stub(subject, :minargs).with{} }
652
+ assert_nothing_raised{ MuchStub.stub(subject, :minargs).with(1){} }
651
653
 
652
- assert_nothing_raised{ MuchStub.stub(subject, :withblock).with(1){ } }
654
+ assert_nothing_raised{ MuchStub.stub(subject, :withblock).with(1){} }
653
655
  end
654
656
 
655
657
  should "allow calling methods with invalid arity" do
@@ -682,37 +684,73 @@ module MuchStub
682
684
  end
683
685
 
684
686
  class TestClass
685
- def self.noargs; end
686
- def self.withargs(a); end
687
- def self.anyargs(*args); end
688
- def self.minargs(a, b, *args); end
689
- def self.withblock(&block); end
690
-
691
- def noargs; end
692
- def withargs(a); end
693
- def anyargs(*args); end
694
- def minargs(a, b, *args); end
695
- def withblock(&block); end
687
+ def self.noargs
688
+ end
689
+
690
+ def self.withargs(a)
691
+ end
692
+
693
+ def self.anyargs(*args)
694
+ end
695
+
696
+ def self.minargs(a, b, *args)
697
+ end
698
+
699
+ def self.withblock(&block)
700
+ end
701
+
702
+ def noargs
703
+ end
704
+
705
+ def withargs(a)
706
+ end
707
+
708
+ def anyargs(*args)
709
+ end
710
+
711
+ def minargs(a, b, *args)
712
+ end
713
+
714
+ def withblock(&block)
715
+ end
696
716
  end
697
717
 
698
718
  module TestModule
699
- def self.noargs; end
700
- def self.withargs(a); end
701
- def self.anyargs(*args); end
702
- def self.minargs(a, b, *args); end
703
- def self.withblock(&block); end
719
+ def self.noargs
720
+ end
721
+
722
+ def self.withargs(a)
723
+ end
724
+
725
+ def self.anyargs(*args)
726
+ end
727
+
728
+ def self.minargs(a, b, *args)
729
+ end
730
+
731
+ def self.withblock(&block)
732
+ end
704
733
  end
705
734
 
706
735
  module TestMixin
707
- def noargs; end
708
- def withargs(a); end
709
- def anyargs(*args); end
710
- def minargs(a, b, *args); end
711
- def withblock(&block); end
736
+ def noargs
737
+ end
738
+
739
+ def withargs(a)
740
+ end
741
+
742
+ def anyargs(*args)
743
+ end
744
+
745
+ def minargs(a, b, *args)
746
+ end
747
+
748
+ def withblock(&block)
749
+ end
712
750
  end
713
751
 
714
752
  class DelegateClass
715
- def self.respond_to?(*args)
753
+ def self.respond_to_missing?(*args)
716
754
  TestClass.respond_to?(*args) || super
717
755
  end
718
756
 
@@ -724,7 +762,7 @@ module MuchStub
724
762
  @delegate = TestClass.new
725
763
  end
726
764
 
727
- def respond_to?(*args)
765
+ def respond_to_missing?(*args)
728
766
  @delegate.respond_to?(*args) || super
729
767
  end
730
768