Class: SleepingKingStudios::Tools::Assertions
- Defined in:
- lib/sleeping_king_studios/tools/assertions.rb
Overview
Methods for asserting on the state of a function or application.
Defined Under Namespace
Classes: AssertionError
Instance Method Summary collapse
-
#assert(error_class: AssertionError, message: nil) { ... } ⇒ Object
Asserts that the block returns a truthy value.
-
#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil) ⇒ Object
Asserts that the value is either true or false.
-
#assert_class(value, as: 'value', error_class: AssertionError, message: nil) ⇒ Object
Asserts that the value is a Class.
-
#assert_instance_of(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value is an example of the given Class.
-
#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value matches the expected object using #===.
-
#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value is a non-empty String or Symbol.
-
#validate(message: nil) { ... } ⇒ Object
Asserts that the block returns a truthy value.
-
#validate_boolean(value, as: 'value', message: nil) ⇒ Object
Asserts that the value is either true or false.
-
#validate_class(value, as: 'value', message: nil) ⇒ Object
Asserts that the value is a Class.
-
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value is an example of the given Class.
-
#validate_matches(value, expected:, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value matches the expected object using #===.
-
#validate_name(value, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value is a non-empty String or Symbol.
Methods inherited from Base
Instance Method Details
#assert(error_class: AssertionError, message: nil) { ... } ⇒ Object
Asserts that the block returns a truthy value.
20 21 22 23 24 25 26 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 20 def assert(error_class: AssertionError, message: nil, &block) return if block.call raise error_class, || 'block returned a falsy value', caller(1..-1) end |
#assert_boolean(value, as: 'value', error_class: AssertionError, message: nil) ⇒ Object
Asserts that the value is either true or false.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 36 def assert_boolean( value, as: 'value', error_class: AssertionError, message: nil ) return if value.equal?(true) || value.equal?(false) raise error_class, || "#{as} must be true or false", caller(1..-1) end |
#assert_class(value, as: 'value', error_class: AssertionError, message: nil) ⇒ Object
Asserts that the value is a Class.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 57 def assert_class( value, as: 'value', error_class: AssertionError, message: nil ) return if value.is_a?(Class) raise error_class, || "#{as} is not a Class", caller(1..-1) end |
#assert_instance_of(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value is an example of the given Class.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 82 def assert_instance_of( # rubocop:disable Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) unless expected.is_a?(Class) raise ArgumentError, 'expected must be a Class' end return if optional && value.nil? return if value.is_a?(expected) raise error_class, || "#{as} is not an instance of #{class_name(expected)}", caller(1..-1) end |
#assert_matches(value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value matches the expected object using #===.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 112 def assert_matches( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/ParameterLists value, expected:, as: 'value', error_class: AssertionError, message: nil, optional: false ) return if optional && value.nil? return if expected === value # rubocop:disable Style/CaseEquality ||= case expected when Module "#{as} is not an instance of #{class_name(expected)}" when Proc "#{as} does not match the Proc" when Regexp "#{as} does not match the pattern #{expected.inspect}" else "#{as} does not match the expected value" end raise error_class, , caller(1..-1) end |
#assert_name(value, as: 'value', error_class: AssertionError, message: nil, optional: false) ⇒ Object
Asserts that the value is a non-empty String or Symbol.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 148 def assert_name( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity value, as: 'value', error_class: AssertionError, message: nil, optional: false ) if value.nil? return if optional raise error_class, || "#{as} can't be blank", caller(1..-1) end unless value.is_a?(String) || value.is_a?(Symbol) raise error_class, || "#{as} is not a String or a Symbol", caller(1..-1) end return unless value.empty? raise error_class, || "#{as} can't be blank", caller(1..-1) end |
#validate(message: nil) { ... } ⇒ Object
Asserts that the block returns a truthy value.
180 181 182 183 184 185 186 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 180 def validate(message: nil, &block) return if block.call raise ArgumentError, || 'block returned a falsy value', caller(1..-1) end |
#validate_boolean(value, as: 'value', message: nil) ⇒ Object
Asserts that the value is either true or false.
195 196 197 198 199 200 201 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 195 def validate_boolean(value, as: 'value', message: nil) return if value.equal?(true) || value.equal?(false) raise ArgumentError, || "#{as} must be true or false", caller(1..-1) end |
#validate_class(value, as: 'value', message: nil) ⇒ Object
Asserts that the value is a Class.
210 211 212 213 214 215 216 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 210 def validate_class(value, as: 'value', message: nil) return if value.is_a?(Class) raise ArgumentError, || "#{as} is not a Class", caller(1..-1) end |
#validate_instance_of(value, expected:, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value is an example of the given Class.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 229 def validate_instance_of( value, expected:, as: 'value', message: nil, optional: false ) unless expected.is_a?(Class) raise ArgumentError, 'expected must be a Class' end return if optional && value.nil? return if value.is_a?(expected) raise ArgumentError, || "#{as} is not an instance of #{class_name(expected)}", caller(1..-1) end |
#validate_matches(value, expected:, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value matches the expected object using #===.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 257 def validate_matches( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength value, expected:, as: 'value', message: nil, optional: false ) return if optional && value.nil? return if expected === value # rubocop:disable Style/CaseEquality ||= case expected when Module "#{as} is not an instance of #{class_name(expected)}" when Proc "#{as} does not match the Proc" when Regexp "#{as} does not match the pattern #{expected.inspect}" else "#{as} does not match the expected value" end raise ArgumentError, , caller(1..-1) end |
#validate_name(value, as: 'value', message: nil, optional: false) ⇒ Object
Asserts that the value is a non-empty String or Symbol.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/sleeping_king_studios/tools/assertions.rb', line 291 def validate_name( # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity value, as: 'value', message: nil, optional: false ) if value.nil? return if optional raise ArgumentError, || "#{as} can't be blank", caller(1..-1) end unless value.is_a?(String) || value.is_a?(Symbol) raise ArgumentError, || "#{as} is not a String or a Symbol", caller(1..-1) end return unless value.empty? raise ArgumentError, || "#{as} can't be blank", caller(1..-1) end |