This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
covers 'facets/file/append'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :append do
|
||||
|
||||
setup do
|
||||
require 'fileutils'
|
||||
@test_file = 'tmp/append.txt'
|
||||
FileUtils.rm(@test_file) if File.exist?(@test_file)
|
||||
end
|
||||
|
||||
test do
|
||||
File.append(@test_file, "line 1\n")
|
||||
File.read(@test_file).assert == "line 1\n"
|
||||
File.append(@test_file, "line 2\n")
|
||||
File.read(@test_file).assert == "line 1\nline 2\n"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
covers 'facets/file/create'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :create do
|
||||
|
||||
test_file = 'tmp/rewrite.txt'
|
||||
test_data = 'This is a test!'
|
||||
|
||||
test do
|
||||
File.create(test_file, test_data)
|
||||
File.read(test_file).assert == test_data
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
covers 'facets/file/ext'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :ext do
|
||||
|
||||
test "typical filename" do
|
||||
s = 'file.txt'
|
||||
File.ext(s).assert == 'txt'
|
||||
File.ext(s, 'old').assert == 'file.old'
|
||||
File.ext(s, '').assert == 'file'
|
||||
end
|
||||
|
||||
test "no extension" do
|
||||
s = 'file'
|
||||
File.ext(s).assert == ''
|
||||
File.ext(s, 'txt').assert == 'file.txt'
|
||||
File.ext(s, '').assert == 'file'
|
||||
end
|
||||
|
||||
test "non-standard extensions" do
|
||||
s = '.profile'
|
||||
File.ext(s).assert == ''
|
||||
File.ext(s, 'new').assert == '.profile.new'
|
||||
File.ext(s, '').assert == '.profile'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
covers 'facets/file/null'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :null do
|
||||
|
||||
test do
|
||||
assert File.null
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
covers 'facets/file/read_binary'
|
||||
|
||||
test_case File do
|
||||
|
||||
# TODO: Is this even really testable?
|
||||
class_method :read_binary do
|
||||
|
||||
test_file = 'tmp/read_binary.txt'
|
||||
test_data = "line 1\nline 2\nline 3"
|
||||
|
||||
test do
|
||||
File.open(test_file, 'w'){ |f| f << test_data }
|
||||
File.read_binary(test_file).assert == test_data
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
covers 'facets/file/read_list'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :read_list do
|
||||
|
||||
test_file = 'tmp/read_list.txt'
|
||||
|
||||
test do
|
||||
File.open(test_file, 'w'){ |f| f << "line 1\nline 2\n#line 3" }
|
||||
File.read_list(test_file).assert == ['line 1', 'line 2']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
covers 'facets/file/rewrite'
|
||||
|
||||
test_case File do
|
||||
|
||||
test_data = 'This is a test!'
|
||||
|
||||
class_method :rewrite do
|
||||
|
||||
setup do
|
||||
@test_file = 'tmp/rewrite.txt'
|
||||
File.open(@test_file, 'w'){ |w| w << test_data }
|
||||
end
|
||||
|
||||
test do
|
||||
File.rewrite(@test_file){ |s| s.reverse }
|
||||
File.read(@test_file).assert == test_data.reverse
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class_method :rewrite! do
|
||||
|
||||
setup do
|
||||
@test_file = 'tmp/rewrite.txt'
|
||||
File.open(@test_file, 'w'){ |w| w << test_data }
|
||||
end
|
||||
|
||||
test do
|
||||
File.rewrite!(@test_file){ |s| s.reverse! }
|
||||
File.read(@test_file).assert == test_data.reverse
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
covers 'facets/file/rootname'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :rootname do
|
||||
|
||||
test do
|
||||
File.rootname('test/core/file').assert == 'test'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,32 @@
|
||||
covers 'facets/file/sanitize'
|
||||
|
||||
test_case File do
|
||||
|
||||
# TODO: Write file identity tests.
|
||||
class_method :sanitize do
|
||||
|
||||
test "spaces become underscores" do
|
||||
File.sanitize('This is a test').assert == "This_is_a_test"
|
||||
end
|
||||
|
||||
test "Windows-style path dividers" do
|
||||
File.sanitize('This\is\test').assert == "test"
|
||||
end
|
||||
|
||||
test "Unix-style path dividers" do
|
||||
File.sanitize('This/is/test').assert == "test"
|
||||
end
|
||||
|
||||
test "non-word characters" do
|
||||
File.sanitize('This/te#$#@!st').assert == "te_____st"
|
||||
end
|
||||
|
||||
test "initial dot" do
|
||||
File.sanitize('.').assert == "_."
|
||||
File.sanitize('....').assert == "_...."
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
covers 'facets/file/split_all'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :split_all do
|
||||
|
||||
test do
|
||||
fp = "test/core/file"
|
||||
File.split_all(fp).assert == ['test', 'core', 'file']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
covers 'facets/file/write'
|
||||
|
||||
test_case File do
|
||||
|
||||
class_method :write do
|
||||
|
||||
test_file = 'tmp/write.txt'
|
||||
|
||||
test do
|
||||
data = "Test data\n"
|
||||
|
||||
nbytes = File.write(test_file, data)
|
||||
|
||||
out = File.read(test_file)
|
||||
out.assert == data
|
||||
out.size.assert == nbytes
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
covers 'facets/file/writelines'
|
||||
|
||||
#require File.dirname(__FILE__) + '/helpers/mockfile'
|
||||
test_case File do
|
||||
|
||||
class_method :writelines do
|
||||
|
||||
test_file = 'tmp/writelines.txt'
|
||||
test_data = %w[one two three four five]
|
||||
|
||||
test do
|
||||
File.writelines(test_file, test_data)
|
||||
|
||||
out = File.readlines(test_file)
|
||||
out.map{ |l| l.chomp }.assert == test_data
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user