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
The base test case class. We need to be careful about what local state is stored.
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
- #be(*arguments) ⇒ Object
- #be_a(klass) ⇒ Object
- #be_equal(other) ⇒ Object
- #be_falsey ⇒ Object
- #be_nil ⇒ Object
- #be_truthy ⇒ Object
- #be_within(value) ⇒ Object
-
#before ⇒ Object
A hook which is called before the test is executed.
- #expect(subject = nil, &block) ⇒ Object
- #have(*predicates) ⇒ Object
- #have_any(*predicates) ⇒ Object
- #have_attributes(**attributes) ⇒ Object
- #have_duration ⇒ Object
- #have_keys(*keys) ⇒ Object
- #have_value(predicate) ⇒ Object
- #inform ⇒ Object
-
#initialize(assertions) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #mock(target, &block) ⇒ Object
- #raise_exception ⇒ Object
- #receive(method, &block) ⇒ Object
- #respond_to(method) ⇒ Object
-
#skip(reason) ⇒ Object
Skip the current test with a reason.
- #skip_if_maximum_ruby_version(version) ⇒ Object
- #skip_if_ruby_platform(pattern) ⇒ Object
- #skip_unless_constant_defined(constant, target = Object) ⇒ Object
- #skip_unless_method_defined(method, target) ⇒ Object
- #skip_unless_minimum_ruby_version(version) ⇒ Object
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
#after(error = nil) ⇒ Object
A hook which is called after the test is executed.
If you override this method, you must call super.
28 29 |
# File 'lib/sus/base.rb', line 28 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.
36 37 38 39 40 41 42 43 44 |
# File 'lib/sus/base.rb', line 36 def around(&block) self.before return block.call rescue => error raise ensure self.after(error) end |
#assert ⇒ Object
46 47 48 |
# File 'lib/sus/base.rb', line 46 def assert(...) @__assertions__.assert(...) end |
#be(*arguments) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/sus/be.rb', line 152 def be(*arguments) if arguments.any? Be.new(*arguments) else Be end end |
#be_a(klass) ⇒ Object
160 161 162 |
# File 'lib/sus/be.rb', line 160 def be_a(klass) Be.new(:is_a?, klass) end |
#be_equal(other) ⇒ Object
168 169 170 |
# File 'lib/sus/be.rb', line 168 def be_equal(other) Be.new(:equal?, other) end |
#be_falsey ⇒ Object
36 37 38 |
# File 'lib/sus/be_truthy.rb', line 36 def be_falsey BeFalsey end |
#be_truthy ⇒ Object
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 |
#before ⇒ Object
A hook which is called before the test is executed.
If you override this method, you must call super.
22 23 |
# File 'lib/sus/base.rb', line 22 def before end |
#expect(subject = nil, &block) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/sus/expect.rb', line 55 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
76 77 78 |
# File 'lib/sus/have.rb', line 76 def have(*predicates) Have::All.new(predicates) end |
#have_any(*predicates) ⇒ Object
104 105 106 |
# File 'lib/sus/have.rb', line 104 def have_any(*predicates) Have::Any.new(predicates) end |
#have_attributes(**attributes) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/sus/have.rb', line 96 def have_attributes(**attributes) predicates = attributes.map do |key, value| Have::Attribute.new(key, value) end Have::All.new(predicates) end |
#have_duration ⇒ Object
39 40 41 |
# File 'lib/sus/have_duration.rb', line 39 def have_duration(...) HaveDuration.new(...) end |
#have_keys(*keys) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sus/have.rb', line 80 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
108 109 110 |
# File 'lib/sus/have.rb', line 108 def have_value(predicate) Have::Any.new([Have::Value.new(predicate)]) end |
#inform ⇒ Object
50 51 52 |
# File 'lib/sus/base.rb', line 50 def inform(...) @__assertions__.inform(...) end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/sus/base.rb', line 15 def inspect "\#<Sus::Base for #{self.class.description.inspect}>" end |
#mock(target, &block) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/sus/mock.rb', line 119 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
57 58 59 |
# File 'lib/sus/raise_exception.rb', line 57 def raise_exception(...) RaiseException.new(...) end |
#receive(method, &block) ⇒ Object
199 200 201 |
# File 'lib/sus/receive.rb', line 199 def receive(method, &block) Receive.new(self, method, &block) 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_if_ruby_platform(pattern) ⇒ Object
93 94 95 96 97 |
# File 'lib/sus/it.rb', line 93 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
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 |