Class: Sus::Receive
- Inherits:
-
Object
- Object
- Sus::Receive
- Defined in:
- lib/sus/receive.rb
Defined Under Namespace
Classes: Times, WithArguments, WithBlock, WithOptions
Constant Summary collapse
- CALL_ORIGINAL =
Object.new
Instance Method Summary collapse
- #and_return(*returning) ⇒ Object
- #call(assertions, subject) ⇒ Object
- #call_original? ⇒ Boolean
-
#initialize(base, method) ⇒ Receive
constructor
A new instance of Receive.
- #once ⇒ Object
- #print(output) ⇒ Object
- #twice ⇒ Object
- #validate(mock, assertions, arguments, options, block) ⇒ Object
- #with(*arguments, **options) ⇒ Object
- #with_arguments(predicate) ⇒ Object
- #with_block(predicate = Be.new(:!=, nil)) ⇒ Object
- #with_call_count(predicate) ⇒ Object
- #with_options(predicate) ⇒ Object
Constructor Details
#initialize(base, method) ⇒ Receive
Returns a new instance of Receive.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sus/receive.rb', line 12 def initialize(base, method) @base = base @method = method @times = Times.new @arguments = nil @options = nil @block = nil @returning = CALL_ORIGINAL end |
Instance Method Details
#and_return(*returning) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/sus/receive.rb', line 63 def and_return(*returning) if returning.size == 1 @returning = returning.first else @returning = returning end return self end |
#call(assertions, subject) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sus/receive.rb', line 78 def call(assertions, subject) assertions.nested(self) do |assertions| mock = @base.mock(subject) called = 0 if call_original? mock.before(@method) do |*arguments, **, &block| called += 1 validate(mock, assertions, arguments, , block) end else mock.replace(@method) do |*arguments, **, &block| called += 1 validate(mock, assertions, arguments, , block) next @returning end end if @times assertions.defer do @times.call(assertions, called) end end end end |
#call_original? ⇒ Boolean
108 109 110 |
# File 'lib/sus/receive.rb', line 108 def call_original? @returning == CALL_ORIGINAL end |
#once ⇒ Object
48 49 50 51 |
# File 'lib/sus/receive.rb', line 48 def once @times = Times.new(Be.new(:==, 1)) return self end |
#print(output) ⇒ Object
23 24 25 |
# File 'lib/sus/receive.rb', line 23 def print(output) output.write("receive ", :variable, @method.to_s, :reset, " ") end |
#twice ⇒ Object
53 54 55 56 |
# File 'lib/sus/receive.rb', line 53 def twice @times = Times.new(Be.new(:==, 2)) return self end |
#validate(mock, assertions, arguments, options, block) ⇒ Object
72 73 74 75 76 |
# File 'lib/sus/receive.rb', line 72 def validate(mock, assertions, arguments, , block) @arguments.call(assertions, arguments) if @arguments @options.call(assertions, ) if @options @block.call(assertions, block) if @block end |
#with(*arguments, **options) ⇒ Object
42 43 44 45 46 |
# File 'lib/sus/receive.rb', line 42 def with(*arguments, **) with_arguments(Be.new(:==, arguments)) if arguments.any? (Be.new(:==, )) if .any? return self end |
#with_arguments(predicate) ⇒ Object
27 28 29 30 |
# File 'lib/sus/receive.rb', line 27 def with_arguments(predicate) @arguments = WithArguments.new(predicate) return self end |
#with_block(predicate = Be.new(:!=, nil)) ⇒ Object
37 38 39 40 |
# File 'lib/sus/receive.rb', line 37 def with_block(predicate = Be.new(:!=, nil)) @block = WithBlock.new(predicate) return self end |
#with_call_count(predicate) ⇒ Object
58 59 60 61 |
# File 'lib/sus/receive.rb', line 58 def with_call_count(predicate) @times = Times.new(predicate) return self end |
#with_options(predicate) ⇒ Object
32 33 34 35 |
# File 'lib/sus/receive.rb', line 32 def (predicate) @options = WithOptions.new(predicate) return self end |