1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

envutil.rb: file_assertion

* test/ruby/envutil.rb (Test::Unit::Assertions#file_assertion):
  rewrite file assertions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-10-15 13:14:48 +00:00
parent a17cb167ea
commit 32569104e1
9 changed files with 42 additions and 23 deletions

View file

@ -1,3 +1,8 @@
Mon Oct 15 22:14:44 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* test/ruby/envutil.rb (Test::Unit::Assertions#file_assertion):
rewrite file assertions.
Mon Oct 15 09:41:17 2012 Koichi Sasada <ko1@atdot.net>
* vm_insnhelper.c (VM_CALLEE_SETUP_ARG): skip CI_SET_FASTPATH() if

View file

@ -225,12 +225,25 @@ module Test
assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0")
end
def assert_file(predicate, path)
assert(File.__send__(predicate, path), "Expected file #{path.inspect} to be #{predicate}")
def file_assertion
FileAssertion
end
def assert_file_not(predicate, path)
assert(!File.__send__(predicate, path), "Expected file #{path.inspect} not to be #{predicate}")
class << (FileAssertion = Object.new)
include Assertions
def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate
predicate = $'
neg = " not"
end
result = File.__send__(predicate, *args)
result = !result if neg
mesg = "Expected file " << args.shift.inspect
mesg << mu_pp(args) unless args.empty?
mesg << "#{neg} to be #{predicate}"
assert(result, mesg)
end
alias method_missing assert_file_predicate
end
end
end

View file

@ -44,7 +44,7 @@ class TestFile < Test::Unit::TestCase
bug6487 = '[ruby-core:45203]'
f = Tempfile.new(__method__.to_s)
f.close
assert_file(:exist?, f.path)
file_assertion.exist?(f.path)
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:utf-8')}
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:bom|utf-8')}
f.close(true)
@ -356,7 +356,7 @@ class TestFile < Test::Unit::TestCase
assert_raise(ArgumentError) do
open(path + "\0bar", "w") {}
end
assert_file_not(:exist?, path)
file_assertion.not_exist?(path)
end
end
end

View file

@ -120,7 +120,7 @@ class TestFileExhaustive < Test::Unit::TestCase
Dir.mktmpdir do |dir|
prefix = File.join(dir, "...a")
Dir.mkdir(prefix)
assert_file(:exist?, prefix)
file_assertion.exist?(prefix)
assert_nothing_raised { File.stat(prefix) }
@ -170,9 +170,9 @@ class TestFileExhaustive < Test::Unit::TestCase
end
def test_exist_p
assert_file(:exist?, @dir)
assert_file(:exist?, @file)
assert_file_not(:exist?, @nofile)
file_assertion.exist?(@dir)
file_assertion.exist?(@file)
file_assertion.not_exist?(@nofile)
end
def test_readable_p
@ -399,8 +399,8 @@ class TestFileExhaustive < Test::Unit::TestCase
def test_rename
assert_equal(0, File.rename(@file, @nofile))
assert_file_not(:exist?, @file)
assert_file(:exist?, @nofile)
file_assertion.not_exist?(@file)
file_assertion.exist?(@nofile)
assert_equal(0, File.rename(@nofile, @file))
assert_raise(Errno::ENOENT) { File.rename(@nofile, @file) }
end
@ -787,21 +787,21 @@ class TestFileExhaustive < Test::Unit::TestCase
def test_truncate
assert_equal(0, File.truncate(@file, 1))
assert_file(:exist?, @file)
file_assertion.exist?(@file)
assert_equal(1, File.size(@file))
assert_equal(0, File.truncate(@file, 0))
assert_file(:exist?, @file)
assert_file(:zero?, @file)
file_assertion.exist?(@file)
file_assertion.zero?(@file)
make_file("foo", @file)
assert_raise(Errno::ENOENT) { File.truncate(@nofile, 0) }
f = File.new(@file, "w")
assert_equal(0, f.truncate(2))
assert_file(:exist?, @file)
file_assertion.exist?(@file)
assert_equal(2, File.size(@file))
assert_equal(0, f.truncate(0))
assert_file(:exist?, @file)
assert_file(:zero?, @file)
file_assertion.exist?(@file)
file_assertion.zero?(@file)
f.close
make_file("foo", @file)

View file

@ -1460,6 +1460,7 @@ class TestM17N < Test::Unit::TestCase
def test_combchar_codepoint
assert_equal([0x30BB, 0x309A], "\u30BB\u309A".codepoints.to_a)
assert_equal([0x30BB, 0x309A], "\u30BB\u309A".codepoints.to_a)
end
def each_encoding(*strings)

View file

@ -403,8 +403,8 @@ class TestProcess < Test::Unit::TestCase
with_tmpchdir {|d|
Dir.mkdir "foo"
system(*PWD, :chdir => "foo", :out => "open_chdir_test")
assert_file(:exist?, "open_chdir_test")
assert_file_not(:exist?, "foo/open_chdir_test")
file_assertion.exist?("open_chdir_test")
file_assertion.not_exist?("foo/open_chdir_test")
assert_equal("#{d}/foo", File.read("open_chdir_test").chomp)
}
end

View file

@ -109,7 +109,7 @@ class TestRequire < Test::Unit::TestCase
Dir.mktmpdir do |tmp|
req = File.join(tmp, "very_long_file_name.rb")
File.write(req, "p :ok\n")
assert_file(:exist?, req)
file_assertion.exist?(req)
req[/.rb$/i] = ""
assert_in_out_err(['--disable-gems'], <<-INPUT, %w(:ok), [])
require "#{req}"

View file

@ -388,7 +388,7 @@ class TestRubyOptions < Test::Unit::TestCase
rubybin = Regexp.quote(EnvUtil.rubybin)
pat = Regexp.quote(notexist)
bug1573 = '[ruby-core:23717]'
assert_file_not(:exist?, notexist)
file_assertion.not_exist?(notexist)
assert_in_out_err(["-r", notexist, "-ep"], "", [], /.* -- #{pat} \(LoadError\)/, bug1573)
assert_in_out_err([notexist], "", [], /#{rubybin}:.* -- #{pat} \(LoadError\)/, bug1573)
end

View file

@ -69,7 +69,7 @@ class TestWhileuntil < Test::Unit::TestCase
tmp.close
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
assert_file_not(:exist?, tmpfilename)
file_assertion.not_exist?(tmpfilename)
}
end