Module: Declare::Assertions

Included in:
AssertionsScope
Defined in:
lib/declare/assertions.rb

Instance Method Summary collapse

Instance Method Details

#CATCH(exception_klass, &block) ⇒ Object

pass if occurred the error is just a own instance

Parameters:

  • exception_klass (Class)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/declare/assertions.rb', line 238

def CATCH(exception_klass, &block)
  yield
rescue ::Exception
  if $!.instance_of?(exception_klass)
    pass
  else
    failure("Faced a exception, that instance of #{exception_klass}.",
            "Faced a exception, that instance of #{$!.class}.", 2)
  end
else
  failure("Faced a exception, that instance of #{exception_klass}.",
          'The block was not faced any exceptions.', 2)
ensure
  _declared!
end

#EQL(sample) ⇒ Object Also known as: eql



59
60
61
62
63
64
65
66
67
# File 'lib/declare/assertions.rb', line 59

def EQL(sample)
  if EQL?(sample)
    pass
  else
    failure('It\'s able to use key in any Hash object.')
  end
ensure
  _declared!
end

#EQL?(sample) ⇒ Boolean

true if can use for hash-key

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/declare/assertions.rb', line 54

def EQL?(sample)
  @it.eql?(sample) && sample.eql?(@it) && (@it.hash == sample.hash) &&
    { @it => true }.key?(sample)
end

#EQUAL(other) ⇒ Object Also known as: equal, SAME



138
139
140
141
142
143
144
145
146
147
# File 'lib/declare/assertions.rb', line 138

def EQUAL(other)
  if EQUAL?(other)
    pass
  else
    failure('@it.equal?(other) && other.equal?(@it) && (@it.__id__.equal? other.__id__) #=> truthy',
            "falsy, it(#{@it.__id__}), other(#{other.__id__})")
  end
ensure
  _declared!
end

#EQUAL?(other) ⇒ Boolean

true if bidirectional passed #equal, and __id__ is same value

Returns:

  • (Boolean)


134
135
136
# File 'lib/declare/assertions.rb', line 134

def EQUAL?(other)
  @it.equal?(other) && other.equal?(@it) && @it.__id__.equal?(other.__id__)
end

#FALSY(object) ⇒ Object Also known as: falsy, NG



204
205
206
207
208
209
210
211
212
213
# File 'lib/declare/assertions.rb', line 204

def FALSY(object)
  if FALSY?(object)
    pass
  else
    failure('It is a falsy(nil/false) object.',
            "\"#{object.inspect}\" is a truthy(not nil/false) object.")
  end
ensure
  _declared!
end

#FALSY?(object) ⇒ Boolean Also known as: falsy?

Returns:

  • (Boolean)


198
199
200
# File 'lib/declare/assertions.rb', line 198

def FALSY?(object)
  !object
end

#INSTANCE_OF(klass) ⇒ Object Also known as: A

Parameters:

  • klass (Class)


15
16
17
18
19
20
21
22
23
24
# File 'lib/declare/assertions.rb', line 15

def INSTANCE_OF(klass)
  if INSTANCE_OF?(klass)
    pass
  else
    failure("It is #{klass}'s instance.",
            "It is #{@it.class}'s instance.")
  end
ensure
  _declared!
end

#INSTANCE_OF?(klass) ⇒ Boolean Also known as: A?

Parameters:

  • klass (Class)

Returns:

  • (Boolean)


7
8
9
# File 'lib/declare/assertions.rb', line 7

def INSTANCE_OF?(klass)
  @it.instance_of?(klass)
end

#IS(other) ⇒ Object Also known as: is



78
79
80
81
82
83
84
85
86
87
# File 'lib/declare/assertions.rb', line 78

def IS(other)
  if IS?(other)
    pass
  else
    failure('it == other',
            "#{@it.inspect} == #{other.inspect}")
  end
ensure
  _declared!
end

#IS?(other, bidirectional: true) ⇒ Boolean Also known as: is?

true if under “==”

Returns:

  • (Boolean)


72
73
74
# File 'lib/declare/assertions.rb', line 72

def IS?(other, bidirectional: true)
  (@it == other) && (bidirectional ? (other == @it) : true)
end

#KIND_OF(family) ⇒ Object Also known as: kind_of, KIND, IS_A



36
37
38
39
40
41
42
43
44
45
# File 'lib/declare/assertions.rb', line 36

def KIND_OF(family)
  if KIND_OF?(family)
    pass
  else
    failure("It is kind of #{family.inspect}.",
            (family.kind_of?(Module) ? "It.class(#{@it.class}) <-> other.ancestors(#{family.ancestors})" : "It is not kind of #{family.inspect}"))
  end
ensure
  _declared!
end

#KIND_OF?(family) ⇒ Boolean Also known as: KIND?

Returns:

  • (Boolean)


29
30
31
# File 'lib/declare/assertions.rb', line 29

def KIND_OF?(family)
  @it.kind_of?(family)
end

#MATCH(condition) ⇒ Object Also known as: match, SATISFY

Parameters:

  • condition (#===)


118
119
120
121
122
123
124
125
126
127
# File 'lib/declare/assertions.rb', line 118

def MATCH(condition)
  if ret = MATCH?(condition)
    pass
  else
    failure("return(#{condition} === It) is not nil/false.",
            "return(#{ret}).")
  end
ensure
  _declared!
end

#MATCH?(condition) ⇒ Boolean Also known as: match?, SATISFY?

Parameters:

  • condition (#===)

Returns:

  • (Boolean)


109
110
111
# File 'lib/declare/assertions.rb', line 109

def MATCH?(condition)
  condition === @it
end

#NOT(other) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/declare/assertions.rb', line 97

def NOT(other)
  if NOT?(other)
    pass
  else
    failure("It is not other(#{other.inspect}).",
            "It is other(#{other.inspect}).")
  end
ensure
  _declared!
end

#NOT?(other) ⇒ Boolean Also known as: not?

Returns:

  • (Boolean)


91
92
93
# File 'lib/declare/assertions.rb', line 91

def NOT?(other)
  (@it != other) && (other != @it) && !IS?(other)
end

#RESCUE(exception_klass, &block) ⇒ Object

pass if occurred the error is a own/subclasses instance

Parameters:

  • exception_klass (Class)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/declare/assertions.rb', line 221

def RESCUE(exception_klass, &block)
  fmt_err = ->err_cls { err_cls.ancestors.take_while { |mod| mod != Object }.join(' < ') }
  yield
rescue exception_klass
  pass
rescue ::Exception
  failure("Faced a exception, that kind of #{exception_klass}(#{fmt_err.call(exception_klass)}).",
          "Faced a exception, that instance of #{$!.class}(#{fmt_err.call($!.class)}).", 2)
else
  failure("Faced a exception, that kind of #{exception_klass}(#{fmt_err.call(exception_klass)}).",
          'The block was not faced any exceptions.', 2)
ensure
  _declared!
end

#RESPOND(message) ⇒ Object Also known as: respond, CAN



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/declare/assertions.rb', line 160

def RESPOND(message)
  message = message.to_sym

  if ret = RESPOND?(message)
    pass
  else
    failure("It.respond_to?(#{message.inspect}) #=> truthy(not nil/false)",
            "It.respond_to?(#{message.inspect}) #=> #{ret.inspect}")
  end
ensure
  _declared!
end

#RESPOND?(message) ⇒ Boolean Also known as: respond?

true if under “respond_to?”

Returns:

  • (Boolean)


154
155
156
# File 'lib/declare/assertions.rb', line 154

def RESPOND?(message)
  @it.respond_to?(message)
end

#TRUTHY(object) ⇒ Object Also known as: truthy, OK



183
184
185
186
187
188
189
190
191
192
# File 'lib/declare/assertions.rb', line 183

def TRUTHY(object)
  if TRUTHY?(object)
    pass
  else
    failure('It is a truthy(not nil/false) object.',
            "\"#{object.inspect}\" is a falsy(nil/false) object.")
  end
ensure
  _declared!
end

#TRUTHY?(object) ⇒ Boolean Also known as: truthy?

Returns:

  • (Boolean)


177
178
179
# File 'lib/declare/assertions.rb', line 177

def TRUTHY?(object)
  !!object
end