Class: Sus::Base
- Inherits:
-
Object
- Object
- Sus::Base
- 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
Represents the base test case class. Provides core functionality for test execution including hooks for setup and teardown.
Instance Method Summary collapse
-
#after(error = nil) ⇒ Object
A hook which is called after the test is executed.
-
#around(&block) ⇒ Object
Wrap logic around the test being executed.
-
#assert ⇒ Object
Make an assertion about a condition.
-
#be(*arguments) ⇒ Object
Create a Be predicate matcher.
-
#be_a(klass) ⇒ Object
Create a predicate that checks if the subject is an instance of a class.
-
#be_equal(other) ⇒ Object
Create a predicate that checks object identity equality.
-
#be_falsey ⇒ Object
Create a predicate that checks if the subject is falsey.
-
#be_nil ⇒ Object
Create a predicate that checks if the subject is nil.
-
#be_truthy ⇒ Object
Create a predicate that checks if the subject is truthy.
-
#be_within(value) ⇒ Object
Create a predicate that checks if the subject is within a tolerance or range.
-
#before ⇒ Object
A hook which is called before the test is executed.
-
#expect(subject = nil, &block) ⇒ Object
Create an expectation about a subject or block.
-
#have(*predicates) ⇒ Object
Create a predicate that checks if the subject has all of the given predicates.
-
#have_any(*predicates) ⇒ Object
Create a predicate that checks if the subject matches any of the given predicates.
-
#have_attributes(**attributes) ⇒ Object
Create a predicate that checks if the subject has the specified attributes with matching values.
-
#have_duration ⇒ Object
Create a predicate that measures the duration of a block execution.
-
#have_keys(*keys) ⇒ Object
Create a predicate that checks if the subject (hash) has the specified keys.
-
#have_value(predicate) ⇒ Object
Create a predicate that checks if the subject (collection) has any value matching the predicate.
-
#inform ⇒ Object
Print an informational message during test execution.
-
#initialize(assertions) ⇒ Base
constructor
Initialize a new test case instance.
- #inspect ⇒ Object
-
#mock(target, &block) ⇒ Object
Create or access a mock for the given target.
-
#raise_exception ⇒ Object
Create a predicate that checks if a block raises an exception.
-
#receive(method, &block) ⇒ Object
Create an expectation that a method will be called.
-
#respond_to(method) ⇒ Object
Create a predicate that checks if the subject responds to a method.
-
#skip(reason) ⇒ Object
Skip the current test with a reason.
-
#skip_if_maximum_ruby_version(version) ⇒ Object
Skip the test if the Ruby version exceeds the maximum supported version.
-
#skip_if_ruby_platform(pattern) ⇒ Object
Skip the test if the Ruby platform matches the pattern.
-
#skip_unless_constant_defined(constant, target = Object) ⇒ Object
Skip the test unless a constant is defined.
-
#skip_unless_method_defined(method, target) ⇒ Object
Skip the test unless a method is defined on the target.
-
#skip_unless_minimum_ruby_version(version) ⇒ Object
Skip the test unless the Ruby version meets the minimum requirement.
Constructor Details
#initialize(assertions) ⇒ Base
Initialize a new test case instance.
13 14 15 |
# File 'lib/sus/base.rb', line 13 def initialize(assertions) @__assertions__ = assertions end |
Instance Method Details
#after(error = nil) ⇒ Object
A hook which is called after the test is executed.
If you override this method, you must call super.
31 32 |
# File 'lib/sus/base.rb', line 31 def after(error = nil) end |
#around(&block) ⇒ Object
Wrap logic around the test being executed.
Invokes the before hook, then the block, then the after hook.
39 40 41 42 43 44 45 46 47 |
# File 'lib/sus/base.rb', line 39 def around(&block) self.before return block.call rescue => error raise ensure self.after(error) end |
#assert ⇒ Object
Make an assertion about a condition.
52 53 54 |
# File 'lib/sus/base.rb', line 52 def assert(...) @__assertions__.assert(...) end |
#be(*arguments) ⇒ Object
Create a Be predicate matcher.
228 229 230 231 232 233 234 |
# File 'lib/sus/be.rb', line 228 def be(*arguments) if arguments.any? Be.new(*arguments) else Be end end |
#be_a(klass) ⇒ Object
Create a predicate that checks if the subject is an instance of a class.
239 240 241 |
# File 'lib/sus/be.rb', line 239 def be_a(klass) Be.new(:is_a?, klass) end |
#be_equal(other) ⇒ Object
Create a predicate that checks object identity equality.
252 253 254 |
# File 'lib/sus/be.rb', line 252 def be_equal(other) Be.new(:equal?, other) end |
#be_falsey ⇒ Object
Create a predicate that checks if the subject is falsey.
52 53 54 |
# File 'lib/sus/be_truthy.rb', line 52 def be_falsey BeFalsey end |
#be_nil ⇒ Object
Create a predicate that checks if the subject is nil.
245 246 247 |
# File 'lib/sus/be.rb', line 245 def be_nil Be::NIL end |
#be_truthy ⇒ Object
Create a predicate that checks if the subject is truthy.
46 47 48 |
# File 'lib/sus/be_truthy.rb', line 46 def be_truthy BeTruthy end |
#be_within(value) ⇒ Object
Create a predicate that checks if the subject is within a tolerance or range.
77 78 79 80 81 82 83 84 |
# File 'lib/sus/be_within.rb', line 77 def be_within(value) case value when Range BeWithin::Bounded.new(value) else BeWithin.new(value) end end |
#before ⇒ Object
A hook which is called before the test is executed.
If you override this method, you must call super.
25 26 |
# File 'lib/sus/base.rb', line 25 def before end |
#expect(subject = nil, &block) ⇒ Object
Create an expectation about a subject or block.
78 79 80 81 82 83 84 |
# File 'lib/sus/expect.rb', line 78 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
Create a predicate that checks if the subject has all of the given predicates.
106 107 108 |
# File 'lib/sus/have.rb', line 106 def have(*predicates) Have::All.new(predicates) end |
#have_any(*predicates) ⇒ Object
Create a predicate that checks if the subject matches any of the given predicates.
143 144 145 |
# File 'lib/sus/have.rb', line 143 def have_any(*predicates) Have::Any.new(predicates) end |
#have_attributes(**attributes) ⇒ Object
Create a predicate that checks if the subject has the specified attributes with matching values.
132 133 134 135 136 137 138 |
# File 'lib/sus/have.rb', line 132 def have_attributes(**attributes) predicates = attributes.map do |key, value| Have::Attribute.new(key, value) end Have::All.new(predicates) end |
#have_duration ⇒ Object
Create a predicate that measures the duration of a block execution.
55 56 57 |
# File 'lib/sus/have_duration.rb', line 55 def have_duration(...) HaveDuration.new(...) end |
#have_keys(*keys) ⇒ Object
Create a predicate that checks if the subject (hash) has the specified keys.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/sus/have.rb', line 113 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
Create a predicate that checks if the subject (collection) has any value matching the predicate.
150 151 152 |
# File 'lib/sus/have.rb', line 150 def have_value(predicate) Have::Any.new([Have::Value.new(predicate)]) end |
#inform ⇒ Object
Print an informational message during test execution.
58 59 60 |
# File 'lib/sus/base.rb', line 58 def inform(...) @__assertions__.inform(...) end |
#inspect ⇒ Object
18 19 20 |
# File 'lib/sus/base.rb', line 18 def inspect "\#<Sus::Base for #{self.class.description.inspect}>" end |
#mock(target, &block) ⇒ Object
Create or access a mock for the given target.
157 158 159 160 161 162 163 |
# File 'lib/sus/mock.rb', line 157 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_exception ⇒ Object
Create a predicate that checks if a block raises an exception.
73 74 75 |
# File 'lib/sus/raise_exception.rb', line 73 def raise_exception(...) RaiseException.new(...) end |
#receive(method, &block) ⇒ Object
Create an expectation that a method will be called.
281 282 283 |
# File 'lib/sus/receive.rb', line 281 def receive(method, &block) Receive.new(self, method, &block) end |
#respond_to(method) ⇒ Object
Create a predicate that checks if the subject responds to a method.
117 118 119 |
# File 'lib/sus/respond_to.rb', line 117 def respond_to(method) RespondTo.new(method) end |
#skip(reason) ⇒ Object
Skip the current test with a reason.
85 86 87 88 |
# File 'lib/sus/it.rb', line 85 def skip(reason) @__assertions__.skip(reason) throw :skip, reason end |
#skip_if_maximum_ruby_version(version) ⇒ Object
Skip the test if the Ruby version exceeds the maximum supported version.
118 119 120 121 122 |
# File 'lib/sus/it.rb', line 118 def skip_if_maximum_ruby_version(version) if RUBY_VERSION >= version skip "Ruby #{version} is not supported, but running #{RUBY_VERSION}!" end end |
#skip_if_ruby_platform(pattern) ⇒ Object
Skip the test if the Ruby platform matches the pattern.
126 127 128 129 130 |
# File 'lib/sus/it.rb', line 126 def skip_if_ruby_platform(pattern) if match = RUBY_PLATFORM.match(pattern) skip "Ruby platform #{match} is not supported!" end end |
#skip_unless_constant_defined(constant, target = Object) ⇒ Object
Skip the test unless a constant is defined.
102 103 104 105 106 |
# File 'lib/sus/it.rb', line 102 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
Skip the test unless a method is defined on the target.
93 94 95 96 97 |
# File 'lib/sus/it.rb', line 93 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
Skip the test unless the Ruby version meets the minimum requirement.
110 111 112 113 114 |
# File 'lib/sus/it.rb', line 110 def skip_unless_minimum_ruby_version(version) unless RUBY_VERSION >= version skip "Ruby #{version} is required, but running #{RUBY_VERSION}!" end end |