Class: Sus::Receive

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/receive.rb

Defined Under Namespace

Classes: Times, WithArguments, WithBlock, WithOptions

Instance Method Summary collapse

Constructor Details

#initialize(base, method, &block) ⇒ Receive

Returns a new instance of Receive.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sus/receive.rb', line 10

def initialize(base, method, &block)
	@base = base
	@method = method
	
	@times = Times.new
	@arguments = nil
	@options = nil
	@block = nil
	
	@returning = block
end

Instance Method Details

#and_raiseObject



78
79
80
81
82
83
84
# File 'lib/sus/receive.rb', line 78

def and_raise(...)
	@returning = proc do
		raise(...)
	end
	
	return self
end

#and_return(*returning, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sus/receive.rb', line 62

def and_return(*returning, &block)
	if block_given?
		if returning.any?
			raise ArgumentError, "Cannot specify both a block and returning values."
		end
		
		@returning = block
	elsif returning.size == 1
		@returning = proc{returning.first}
	else
		@returning = proc{returning}
	end
	
	return self
end

#call(assertions, subject) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sus/receive.rb', line 96

def call(assertions, subject)
	assertions.nested(self) do |assertions|
		mock = @base.mock(subject)
		
		called = 0
		
		if call_original?
			mock.before(@method) do |*arguments, **options, &block|
				called += 1
				
				validate(mock, assertions, arguments, options, block)
			end
		else
			mock.replace(@method) do |*arguments, **options, &block|
				called += 1
				
				validate(mock, assertions, arguments, options, block)
				
				next @returning.call(*arguments, **options, &block)
			end
		end
		
		if @times
			assertions.defer do
				@times.call(assertions, called)
			end
		end
	end
end

#call_original?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/sus/receive.rb', line 126

def call_original?
	@returning.nil?
end

#onceObject



47
48
49
50
# File 'lib/sus/receive.rb', line 47

def once
	@times = Times.new(Be.new(:==, 1))
	return self
end


22
23
24
# File 'lib/sus/receive.rb', line 22

def print(output)
	output.write("receive ", :variable, @method.to_s, :reset)
end

#twiceObject



52
53
54
55
# File 'lib/sus/receive.rb', line 52

def twice
	@times = Times.new(Be.new(:==, 2))
	return self
end

#validate(mock, assertions, arguments, options, block) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/sus/receive.rb', line 86

def validate(mock, assertions, arguments, options, block)
	return unless @arguments or @options or @block
	
	assertions.nested(self) do |assertions|
		@arguments.call(assertions, arguments) if @arguments
		@options.call(assertions, options) if @options
		@block.call(assertions, block) if @block
	end
end

#with(*arguments, **options) ⇒ Object



41
42
43
44
45
# File 'lib/sus/receive.rb', line 41

def with(*arguments, **options)
	with_arguments(Be.new(:==, arguments)) if arguments.any?
	with_options(Be.new(:==, options)) if options.any?
	return self
end

#with_arguments(predicate) ⇒ Object



26
27
28
29
# File 'lib/sus/receive.rb', line 26

def with_arguments(predicate)
	@arguments = WithArguments.new(predicate)
	return self
end

#with_block(predicate = Be.new(:!=, nil)) ⇒ Object



36
37
38
39
# File 'lib/sus/receive.rb', line 36

def with_block(predicate = Be.new(:!=, nil))
	@block = WithBlock.new(predicate)
	return self
end

#with_call_count(predicate) ⇒ Object



57
58
59
60
# File 'lib/sus/receive.rb', line 57

def with_call_count(predicate)
	@times = Times.new(predicate)
	return self
end

#with_options(predicate) ⇒ Object



31
32
33
34
# File 'lib/sus/receive.rb', line 31

def with_options(predicate)
	@options = WithOptions.new(predicate)
	return self
end