Class: AssertFile

Inherits:
Object show all
Defined in:
lib/el4r/el4r-sub.rb

Overview

assert-file Example1:

es = AssertFile.new("testdata/shorten-test.e")
el = AssertFile.new("testdata/shorten-test.el")
system "shorten.rb -l #{el} -e #{es}  testdata/shorten-test-input.e"
assert_file(es)
assert_file(el)

Example2:

assert_file(:expected=>expected, :actual=>actual, :no_remove=>true)

Example3:

AssertFile.transaction(expected) {|asf|
  system "foo input > #{asf}"
}

Constant Summary collapse

@@basedir =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ AssertFile

new(“expected_filename”) new(:expeced=>“expected_filename”, :actual=>“actual_filename”) new(:expeced=>“expected_filename”, :actual=>“actual_filename”, :diff=>“diff”)



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/el4r/el4r-sub.rb', line 335

def initialize(arg)
  require 'test/unit'

  case arg
  when String               # expected
    @expected = arg
    @actual = arg+".actual"
    @diff = arg+".diff"
  when Hash
    @basedir = arg[:basedir]
    @expected = arg[:expected]
    @no_remove = arg[:no_remove]

    @actual = arg[:actual] || (@expected+".actual")
    @diff = arg[:diff] || (@expected+".diff")
  else
    raise TypeError, "AssertFile.new: must be String or Hash."
  end
  @basedir ||= @@basedir
  FileUtils.mkdir_p @basedir if @basedir
  @expected = pathconv(@expected)
  @actual = pathconv(@actual)
  @diff = pathconv(@diff)
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual.



359
360
361
# File 'lib/el4r/el4r-sub.rb', line 359

def actual
  @actual
end

#diffObject

Returns the value of attribute diff.



359
360
361
# File 'lib/el4r/el4r-sub.rb', line 359

def diff
  @diff
end

#expectedObject

Returns the value of attribute expected.



359
360
361
# File 'lib/el4r/el4r-sub.rb', line 359

def expected
  @expected
end

#no_removeObject

Returns the value of attribute no_remove.



359
360
361
# File 'lib/el4r/el4r-sub.rb', line 359

def no_remove
  @no_remove
end

Class Method Details

.basedir=(basedir) ⇒ Object



308
309
310
# File 'lib/el4r/el4r-sub.rb', line 308

def self.basedir=(basedir)
  @@basedir = basedir
end

.transaction(*args, &block) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/el4r/el4r-sub.rb', line 312

def self.transaction(*args, &block)
  if block_given?
    testcase = eval("self", block)
    assert_files = args.map{|x| new(x) }

    if @@basedir
      Dir.chdir(@@basedir) { yield assert_files }
    else
      yield(assert_files)
    end
    assert_files.each{|asf| testcase.assert_file(asf)}
  else
    raise ArgumentError, "must have block"
  end
end

Instance Method Details

#make_diffObject



374
375
376
# File 'lib/el4r/el4r-sub.rb', line 374

def make_diff
  system "diff -u #{expected} #{actual} | tee #{diff}"
end

#pathconv(path) ⇒ Object



361
362
363
364
365
366
367
# File 'lib/el4r/el4r-sub.rb', line 361

def pathconv(path)
  if @basedir
    File.expand_path(path, @basedir)
  else
    path
  end
end

#to_sObject Also known as: to_str



378
379
380
# File 'lib/el4r/el4r-sub.rb', line 378

def to_s
  actual
end


370
371
372
# File 'lib/el4r/el4r-sub.rb', line 370

def unlink_diff
  File.unlink(diff) if File.exist?(diff)
end