Class: Sus::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/base.rb,
lib/sus/be.rb,
lib/sus/it.rb,
lib/sus/have.rb,
lib/sus/mock.rb,
lib/sus/expect.rb,
lib/sus/receive.rb,
lib/sus/be_truthy.rb,
lib/sus/be_within.rb,
lib/sus/respond_to.rb,
lib/sus/have_duration.rb,
lib/sus/raise_exception.rb

Overview

The base test case class. We need to be careful about what local state is stored.

Instance Method Summary collapse

Constructor Details

#initialize(assertions) ⇒ Base

Returns a new instance of Base.



11
12
13
# File 'lib/sus/base.rb', line 11

def initialize(assertions)
	@__assertions__ = assertions
end

Instance Method Details

#afterObject



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

def after
end

#aroundObject



25
26
27
28
29
30
31
# File 'lib/sus/base.rb', line 25

def around
	self.before
	
	return yield
ensure
	self.after
end

#assertObject



33
34
35
# File 'lib/sus/base.rb', line 33

def assert(...)
	@__assertions__.assert(...)
end

#be(*arguments) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/sus/be.rb', line 62

def be(*arguments)
	if arguments.any?
		Be.new(*arguments)
	else
		Be
	end
end

#be_a(klass) ⇒ Object



70
71
72
# File 'lib/sus/be.rb', line 70

def be_a(klass)
	Be.new(:is_a?, klass)
end

#be_equal(other) ⇒ Object



78
79
80
# File 'lib/sus/be.rb', line 78

def be_equal(other)
	Be.new(:equal?, other)
end

#be_falseyObject



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

def be_falsey
	BeFalsey
end

#be_nilObject



74
75
76
# File 'lib/sus/be.rb', line 74

def be_nil
	Be::NIL
end

#be_truthyObject



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

def be_truthy
	BeTruthy
end

#be_within(value) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/sus/be_within.rb', line 52

def be_within(value)
	case value
	when Range
		BeWithin::Bounded.new(value)
	else
		BeWithin.new(value)
	end
end

#beforeObject



19
20
# File 'lib/sus/base.rb', line 19

def before
end

#expect(subject = nil, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sus/expect.rb', line 51

def expect(subject = nil, &block)
	if block_given?
		Expect.new(@__assertions__, block, distinct: true)
	else
		Expect.new(@__assertions__, subject, distinct: true)
	end
end

#have(*predicates) ⇒ Object



71
72
73
# File 'lib/sus/have.rb', line 71

def have(*predicates)
	Have::All.new(predicates)
end

#have_any(*predicates) ⇒ Object



99
100
101
# File 'lib/sus/have.rb', line 99

def have_any(*predicates)
	Have::Any.new(predicates)
end

#have_attributes(**attributes) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/sus/have.rb', line 91

def have_attributes(**attributes)
	predicates = attributes.map do |key, value|
		Have::Attribute.new(key, value)
	end
	
	Have::All.new(predicates)
end

#have_durationObject



39
40
41
# File 'lib/sus/have_duration.rb', line 39

def have_duration(...)
	HaveDuration.new(...)
end

#have_keys(*keys) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sus/have.rb', line 75

def have_keys(*keys)
	predicates = []
	
	keys.each do |key|
		if key.is_a?(Hash)
			key.each do |key, predicate|
				predicates << Have::Key.new(key, predicate)
			end
		else
			predicates << Have::Key.new(key)
		end
	end
	
	Have::All.new(predicates)
end

#have_value(predicate) ⇒ Object



103
104
105
# File 'lib/sus/have.rb', line 103

def have_value(predicate)
	Have::Any.new([Have::Value.new(predicate)])
end

#informObject



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

def inform(...)
	@__assertions__.inform(...)
end

#inspectObject



15
16
17
# File 'lib/sus/base.rb', line 15

def inspect
	"\#<Sus::Base for #{self.class.description.inspect}>"
end

#mock(target, &block) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/sus/mock.rb', line 102

def mock(target, &block)
	# Pull in the extra functionality:
	self.singleton_class.prepend(Mocks)

	# Redirect the method to the new functionality:
	self.mock(target, &block)
end

#raise_exceptionObject



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

def raise_exception(...)
	RaiseException.new(...)
end

#receive(method) ⇒ Object



181
182
183
# File 'lib/sus/receive.rb', line 181

def receive(method)
	Receive.new(self, method)
end

#respond_to(method) ⇒ Object



90
91
92
# File 'lib/sus/respond_to.rb', line 90

def respond_to(method)
	RespondTo.new(method)
end

#skip(reason) ⇒ Object

Skip the current test with a reason.



64
65
66
67
# File 'lib/sus/it.rb', line 64

def skip(reason)
	@__assertions__.skip(reason)
	throw :skip, reason
end

#skip_if_maximum_ruby_version(version) ⇒ Object



87
88
89
90
91
# File 'lib/sus/it.rb', line 87

def skip_if_maximum_ruby_version(version)
	if RUBY_VERSION >= version
		skip "Ruby #{version} is not supported, but running #{RUBY_VERSION}!"
	end
end

#skip_unless_constant_defined(constant, target = Object) ⇒ Object



75
76
77
78
79
# File 'lib/sus/it.rb', line 75

def skip_unless_constant_defined(constant, target = Object)
	unless target.const_defined?(constant)
		skip "Constant #{constant} is not defined in #{target}!"
	end
end

#skip_unless_method_defined(method, target) ⇒ Object



69
70
71
72
73
# File 'lib/sus/it.rb', line 69

def skip_unless_method_defined(method, target)
	unless target.method_defined?(method)
		skip "Method #{method} is not defined in #{target}!"
	end
end

#skip_unless_minimum_ruby_version(version) ⇒ Object



81
82
83
84
85
# File 'lib/sus/it.rb', line 81

def skip_unless_minimum_ruby_version(version)
	unless RUBY_VERSION >= version
		skip "Ruby #{version} is required, but running #{RUBY_VERSION}!"
	end
end