fix getFileExtension so that it only returns file extensions. Add tests to Storage.t to validate that

This commit is contained in:
Colin Kuskie 2006-05-24 23:25:21 +00:00
parent c9915ad6b3
commit e1fee84dc5
2 changed files with 50 additions and 5 deletions

View file

@ -504,7 +504,8 @@ sub getFileContentsAsScalar {
=head2 getFileExtension ( filename )
Returns the extension or type of this file.
Returns the extension or type of this file. If there's no extension, will either return
undef or the empty string, dependent on the absence or presence of a dot.
=head3 filename
@ -515,8 +516,8 @@ The filename of the file you wish to find out the type for.
sub getFileExtension {
my $self = shift;
my $filename = shift;
my $extension = lc($filename);
$extension =~ s/.*\.(.*?)$/$1/;
$filename = lc $filename;
my ($extension) = $filename =~ /\.([^.]*)$/;
return $extension;
}

View file

@ -19,7 +19,35 @@ use WebGUI::Storage;
use Test::More;
plan tests => 26; # increment this value for each test you create
my $extensionTests = [
{
filename => 'filename',
extension => undef,
comment => 'no extension',
},
{
filename => 'filename.',
extension => '',
comment => 'dot, but no extension',
},
{
filename => 'filename.txt',
extension => 'txt',
comment => 'simple extension',
},
{
filename => 'filename.TXT',
extension => 'txt',
comment => 'extensions are all lowercase',
},
{
filename => 'filename.FOO.BAR',
extension => 'bar',
comment => 'multiple extensions return last extension',
},
];
plan tests => 27 + scalar @{ $extensionTests }; # increment this value for each test you create
my $session = WebGUI::Test->session;
@ -120,9 +148,25 @@ is (-s $filePath, length $content, 'file is the right size');
is ($storage1->getFileSize($filename), length $content, 'getFileSize returns correct size');
open my $fcon, "< ".$filePath or
die "Unable to open $filePath for reading: $!\n";
my $fileContents;
{
local undef $/;
$fileContents = <$fcon>;
}
close $fcon;
is ($fileContents, $content, 'file contents match');
is ($storage1->getFileContentsAsScalar($filename), $content, 'getFileContentsAsScalar matches');
foreach my $extTest (@{ $extensionTests }) {
is( $storage1->getFileExtension($extTest->{filename}), $extTest->{extension}, $extTest->{comment} );
}
TODO: {
local $TODO = "Tests to make later";
ok(0, 'Add a file to the storage location via addFileFromFilesystem');
ok(0, 'Add a file to the storage location via addFileFromHashref');
ok(0, 'Test renaming of files inside of a storage location');
}