Skip to content

Commit

Permalink
official extname tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpclark committed Mar 4, 2018
1 parent 0bcf6a3 commit b90d45b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
33 changes: 0 additions & 33 deletions test/basename_test.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
require 'test_helper'

class BasenameTest < Minitest::Test
DRIVE = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
POSIX = /cygwin|mswin|bccwin|mingw|emx/ !~ RUBY_PLATFORM
NTFS = !(/mingw|mswin|bccwin/ !~ RUBY_PLATFORM)

def regular_file
return @file if defined? @file
@file = make_tmp_filename("file")
make_file("foo", @file)
@file
end

def make_tmp_filename(prefix)
"#{@dir}/#{prefix}.test"
end

def make_file(content, file)
open(file, "w") {|fh| fh << content }
end

def assert_incompatible_encoding
d = "\u{3042}\u{3044}".encode("utf-16le")
assert_raises(Encoding::CompatibilityError) {yield d}
m = Class.new {define_method(:to_path) {d}}
assert_raises(Encoding::CompatibilityError) {yield m.new}
end

def utf8_file
return @utf8file if defined? @utf8file
@utf8file = make_tmp_filename("\u3066\u3059\u3068")
make_file("foo", @utf8file)
@utf8file
end

def setup
@dir = Dir.mktmpdir("rubytest-file")
File.chown(-1, Process.gid, @dir)
Expand Down
41 changes: 39 additions & 2 deletions test/extname_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
require 'test_helper'

class ExtnameTest < Minitest::Test
def setup
@dir = Dir.mktmpdir("rubytest-file")
File.chown(-1, Process.gid, @dir)
end

def teardown
GC.start
FileUtils.remove_entry_secure @dir
end

def test_extname_official
assert_equal(".test", FasterPath.extname(regular_file))
assert_equal(".test", FasterPath.extname(utf8_file))
prefixes = ["", "/", ".", "/.", "bar/.", "/bar/."]
infixes = ["", " ", "."]
infixes2 = infixes + [".ext "]
appendixes = [""]
if NTFS
appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
end
prefixes.each do |prefix|
appendixes.each do |appendix|
infixes.each do |infix|
path = "#{prefix}foo#{infix}#{appendix}"
assert_equal("", FasterPath.extname(path), "FasterPath.extname(#{path.inspect})")
end
infixes2.each do |infix|
path = "#{prefix}foo#{infix}.ext#{appendix}"
assert_equal(".ext", FasterPath.extname(path), "FasterPath.extname(#{path.inspect})")
end
end
end
# bug3175 = '[ruby-core:29627]'
# assert_equal(".rb", FasterPath.extname("/tmp//bla.rb"), bug3175)
assert_incompatible_encoding {|d| FasterPath.extname(d)} if ENV['ENCODING'] = true
end

def test_extname
assert_equal ".rb", FasterPath.extname("foo.rb")
assert_equal ".rb", FasterPath.extname("/foo/bar.rb")
Expand All @@ -27,8 +64,8 @@ def test_extname_edge_cases
def test_substitutability_of_rust_and_ruby_impls
result_pair = lambda do |str|
[
File.send(:extname, str),
FasterPath.extname(str)
File.send(:extname, str),
FasterPath.extname(str)
]
end
assert_equal( *result_pair.("foo.rb") )
Expand Down
37 changes: 37 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,40 @@
require 'color_pound_spec_reporter'

Minitest::Reporters.use! [ColorPoundSpecReporter.new]

::Minitest::Assertions.module_eval do
def assert_incompatible_encoding
d = "\u{3042}\u{3044}".encode("utf-16le")
assert_raises(Encoding::CompatibilityError) {yield d}
m = Class.new {define_method(:to_path) {d}}
assert_raises(Encoding::CompatibilityError) {yield m.new}
end
end

::Minitest::Test.class_eval do
DRIVE = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
POSIX = /cygwin|mswin|bccwin|mingw|emx/ !~ RUBY_PLATFORM
NTFS = !(/mingw|mswin|bccwin/ !~ RUBY_PLATFORM)

def regular_file
return @file if defined? @file
@file = make_tmp_filename("file")
make_file("foo", @file)
@file
end

def make_tmp_filename(prefix)
"#{@dir}/#{prefix}.test"
end

def make_file(content, file)
open(file, "w") {|fh| fh << content }
end

def utf8_file
return @utf8file if defined? @utf8file
@utf8file = make_tmp_filename("\u3066\u3059\u3068")
make_file("foo", @utf8file)
@utf8file
end
end

0 comments on commit b90d45b

Please sign in to comment.