Class: SleepingKingStudios::Tools::Assertions

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

instance

Instance Method Details

#assert(error_class: AssertionError, message: nil) { ... } ⇒ Object

Asserts that the block returns a truthy value.

Parameters:

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Yields:

  • The block to evaluate.

Yield Returns:

  • (Object)

    The returned value of the block.

Raises:

  • AssertionError if the block does not return 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,
    message || '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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Raises:

  • AssertionError if the value is not a Class.



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,
    message || "#{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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Raises:

  • AssertionError if the value is not 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,
    message || "#{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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • expected (Class)

    The expected class.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • ArgumentError if the expected class is not a Class.

  • AssertionError if the value is not an instance of the expected 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,
    message || "#{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 #===.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • expected (#===)

    The expected object.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • AssertionError if the value does not match the expected object.



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

  message ||=
    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, message, 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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • error_class (Class) (defaults to: AssertionError)

    The exception class to raise on a failure.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • AssertionError if the value is not a String or a Symbol, or if the value is empty.



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, message || "#{as} can't be blank", caller(1..-1)
  end

  unless value.is_a?(String) || value.is_a?(Symbol)
    raise error_class,
      message || "#{as} is not a String or a Symbol",
      caller(1..-1)
  end

  return unless value.empty?

  raise error_class, message || "#{as} can't be blank", caller(1..-1)
end

#validate(message: nil) { ... } ⇒ Object

Asserts that the block returns a truthy value.

Parameters:

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Yields:

  • The block to evaluate.

Yield Returns:

  • (Object)

    The returned value of the block.

Raises:

  • ArgumentError if the block does not return 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,
    message || '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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Raises:

  • AssertionError if the value is not a Class.



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,
    message || "#{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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

Raises:

  • ArgumentError if the value is not 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,
    message || "#{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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • expected (Class)

    The expected class.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • ArgumentError if the expected class is not a Class.

  • AssertionError if the value is not an instance of the expected 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,
    message || "#{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 #===.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • expected (#===)

    The expected object.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • ArgumentError if the value does not match the expected object.



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

  message ||=
    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, message, 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.

Parameters:

  • value (Object)

    The value to assert on.

  • as (String) (defaults to: 'value')

    The name of the asserted value.

  • message (String) (defaults to: nil)

    The exception message to raise on a failure.

  • optional (true, false) (defaults to: false)

    If true, allows nil values.

Raises:

  • ArgumentError if the value is not a String or a Symbol, or if the value is empty.



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, message || "#{as} can't be blank", caller(1..-1)
  end

  unless value.is_a?(String) || value.is_a?(Symbol)
    raise ArgumentError,
      message || "#{as} is not a String or a Symbol",
      caller(1..-1)
  end

  return unless value.empty?

  raise ArgumentError, message || "#{as} can't be blank", caller(1..-1)
end