Class: Section9::UnitTest::VerifyObject

Inherits:
VerifyBaseObject show all
Defined in:
lib/section9/unittest.rb

Constant Summary

Constants inherited from VerifyBaseObject

Section9::UnitTest::VerifyBaseObject::DEPTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testcase, actual = nil, location = nil, &block) ⇒ VerifyObject

Returns a new instance of VerifyObject.



173
174
175
176
177
178
# File 'lib/section9/unittest.rb', line 173

def initialize(testcase, actual=nil, location=nil, &block)
  @testcase = testcase
  @actual = actual
  @block = block
  @location = location
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



318
319
320
321
322
# File 'lib/section9/unittest.rb', line 318

def method_missing(method_name, *args)
  _wrap {
    _method_missing(true, method_name, *args)
  }
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



179
180
181
# File 'lib/section9/unittest.rb', line 179

def location
  @location
end

Instance Method Details

#<(expected) ⇒ Object



220
# File 'lib/section9/unittest.rb', line 220

def < (expected); _wrap { @testcase.assert_operator(@actual, :<,  expected) } end

#<=(expected) ⇒ Object



221
# File 'lib/section9/unittest.rb', line 221

def <=(expected); _wrap { @testcase.assert_operator(@actual, :<=, expected) } end

#==(expected) ⇒ Object

def ==(expected); _wrap { @testcase.assert_equal(expected, @actual) } end



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/section9/unittest.rb', line 205

def ==(expected)
  _wrap {
    if @actual != expected && @actual.is_a?(String) && expected.is_a?(String) && expected =~ /\n/
      diff = _diff_context(expected, @actual, "--- expected\n+++ actual\n")
      begin
        @testcase.assert_equal(expected, @actual)
      rescue Exception => ex
        ex.message << "\n" << diff.chomp
        raise ex
      end
    else
      @testcase.assert_equal(expected, @actual)
    end
  }
end

#=~(expected) ⇒ Object



224
# File 'lib/section9/unittest.rb', line 224

def =~(expected); _wrap { @testcase.assert_match(expected, @actual) } end

#>(expected) ⇒ Object



222
# File 'lib/section9/unittest.rb', line 222

def > (expected); _wrap { @testcase.assert_operator(@actual, :>,  expected) } end

#>=(expected) ⇒ Object



223
# File 'lib/section9/unittest.rb', line 223

def >=(expected); _wrap { @testcase.assert_operator(@actual, :>=, expected) } end

#called?(expected) ⇒ Boolean

Returns:

  • (Boolean)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/section9/unittest.rb', line 287

def called?(expected)
  _wrap {
    defined?(Section9::Recorder)  or
      raise ArgumentError.new("Recorder class is not defined: please require 'section9/recorder'.")
    @actual.is_a?(Section9::Recorder::Called)  or
      raise ArgumentError.new("called?(): actual should be Recorder::Called object, but #{expected.inspect}.")
    c = @actual
    case expected
    when Array
      arr = expected
      arr[0] == c.obj   or @testcase.assert_equal arr[0], c.obj,  "receiver object: "
      arr[1] == c.name  or @testcase.assert_equal arr[1], c.name, "method name: "
      arr[2] == c.args  or @testcase.assert_equal arr[2], c.args, "arguments: "
      arr[3] == c.ret   or @testcase.assert_equal arr[3], c.ret,  "return value: "
      @testcase.assert_equal arr, c.to_a
    when Hash
      d = expected.dup()
      [:obj, :name, :args, :ret].each {|key| d.delete(key) }
      d.each {|k, v| raise ArgumentError.new("#{k.inspect}: unexpected key for verify.called?().") }
      d = expected
      ! d.key?(:obj)  || d[:obj]  == c.obj   or @testcase.assert_equal d[:obj],  c.obj,  "receiver object: "
      ! d.key?(:name) || d[:name] == c.name  or @testcase.assert_equal d[:name], c.name, "method name: "
      ! d.key?(:args) || d[:args] == c.args  or @testcase.assert_equal d[:args], c.args, "arguments: "
      ! d.key?(:ret)  || d[:ret]  == c.ret   or @testcase.assert_equal d[:ret],  c.ret,  "return value: "
    else
      msg = "verify.called?(): argument should be Array or Hash, but got #{d.inspect}."
      raise ArgumentError.new(msg)
    end
  }
end

#dir_exist?Boolean

Returns:

  • (Boolean)


274
275
276
277
278
279
# File 'lib/section9/unittest.rb', line 274

def dir_exist?
  _wrap {
    File.directory?(@actual) or
      @testcase.flunk "Directory '#{@actual}' not exist."
  }
end

#exist?Boolean

Returns:

  • (Boolean)


281
282
283
284
285
# File 'lib/section9/unittest.rb', line 281

def exist?
  _wrap {
    raise NoMethodError.new("'exist?()' is available only with '.NOT'. please use 'file_exist?' or 'dir_exist?' instead.")
  }
end

#file_exist?Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
# File 'lib/section9/unittest.rb', line 267

def file_exist?
  _wrap {
    File.file?(@actual) or
      @testcase.flunk "File '#{@actual}' not exist."
  }
end

#in_delta?(expected, delta) ⇒ Boolean

Returns:

  • (Boolean)


236
# File 'lib/section9/unittest.rb', line 236

def in_delta?   (expected, delta); _wrap { @testcase.assert_in_delta(expected, @actual, delta) } end

#instance_of?(expected) ⇒ Boolean

Returns:

  • (Boolean)


235
# File 'lib/section9/unittest.rb', line 235

def instance_of?(expected); _wrap { @testcase.assert_instance_of(expected, @actual) } end

#kind_of?(expected) ⇒ Boolean

def is_a? (expected); _wrap { @testcase.assert_kind_of(expected, @actual) } end

Returns:

  • (Boolean)


234
# File 'lib/section9/unittest.rb', line 234

def kind_of?    (expected); _wrap { @testcase.assert_kind_of(expected, @actual) } end

#nil?Boolean

Returns:

  • (Boolean)


231
# File 'lib/section9/unittest.rb', line 231

def nil?        ();         _wrap { @testcase.assert_nil(@actual) } end

#NOTObject



181
182
183
184
185
186
187
# File 'lib/section9/unittest.rb', line 181

def NOT
  @testcase.__unregister(self)
  location = caller(1).first
  vo = VerifyNotObject.new(@testcase, @actual, location, &@block)
  @testcase.__register(vo)
  return vo
end

#raise?(errcls, errmsg = nil) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/section9/unittest.rb', line 239

def raise?(errcls, errmsg=nil)
  #if block_given?
  #  ex = @testcase.assert_raise(errcls) { yield }
  #  @testcase.assert_equal(errmsg, ex.message) if errmsg
  #else
  _wrap {
    proc_obj = @block || @actual
    ex = @testcase.assert_raise(errcls) { proc_obj.call }
    if errmsg
      errmsg.is_a?(Regexp) ? @testcase.assert_match(errmsg, ex.message) \
                           : @testcase.assert_equal(errmsg, ex.message)
    end
    class << proc_obj
      def exception; @_exception; end
    end
    proc_obj.instance_variable_set(:@_exception, ex)
    ex
  }
  #end
end

#respond_to?(expected) ⇒ Boolean

Returns:

  • (Boolean)


237
# File 'lib/section9/unittest.rb', line 237

def respond_to? (expected); _wrap { @testcase.assert_respond_to(@actual, expected) } end

#same?(expected) ⇒ Boolean

Returns:

  • (Boolean)


232
# File 'lib/section9/unittest.rb', line 232

def same?       (expected); _wrap { @testcase.assert_same(expected, @actual) } end

#throw?(expected) ⇒ Boolean

Returns:

  • (Boolean)


260
261
262
263
264
265
# File 'lib/section9/unittest.rb', line 260

def throw?(expected)
  _wrap {
    proc_obj = @block || @actual
    @testcase.assert_throws(expected) { proc_obj.call }
  }
end