Module: Declare::Assertions

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

Instance Method Summary collapse

Instance Method Details

#_declared!Object (private)


258
259
260
# File 'lib/declare/assertions.rb', line 258

def _declared!
  ::Declare.declared!
end

#CATCH(exception_klass, &block) ⇒ Object

pass if occured the error is just a own instance


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

def CATCH(exception_klass, &block)
  block.call
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


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

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


56
57
58
59
# File 'lib/declare/assertions.rb', line 56

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

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


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

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

#EQUAL?(other) ⇒ Boolean

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


136
137
138
# File 'lib/declare/assertions.rb', line 136

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

#failure(ecpected, actual, level = 1) ⇒ Object (private)


266
267
268
# File 'lib/declare/assertions.rb', line 266

def failure(ecpected, actual, level=1)
  ::Declare.failure! "#{_declare_called_from level}\n  Expected: #{ecpected}\n  Actual  : #{actual}\n\n"
end

#FALTHY(object) ⇒ Object Also known as: falthy, NG


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

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

#FALTHY?(object) ⇒ Boolean Also known as: falthy?


200
201
202
# File 'lib/declare/assertions.rb', line 200

def FALTHY?(object)
  ! object
end

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


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

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?


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

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

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


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

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

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

true if under “==”


74
75
76
# File 'lib/declare/assertions.rb', line 74

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

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


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

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?


31
32
33
# File 'lib/declare/assertions.rb', line 31

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

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


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

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?


111
112
113
# File 'lib/declare/assertions.rb', line 111

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

#NOT(other) ⇒ Object


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

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?


93
94
95
# File 'lib/declare/assertions.rb', line 93

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

#passObject (private)


262
263
264
# File 'lib/declare/assertions.rb', line 262

def pass
  ::Declare.pass!
end

#RESCUE(exception_klass, &block) ⇒ Object

pass if occured the error is a own/subclassis instance


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

def RESCUE(exception_klass, &block)
  fmt_err = ->err_cls{err_cls.ancestors.take_while{|mod|mod != Object}.join(' < ')}
  block.call
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


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

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?”


156
157
158
# File 'lib/declare/assertions.rb', line 156

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

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


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

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

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


179
180
181
# File 'lib/declare/assertions.rb', line 179

def TRUTHY?(object)
  !! object
end