From e1fee84dc521058d4a1e11c004e66e7a3472ae10 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 24 May 2006 23:25:21 +0000 Subject: [PATCH] fix getFileExtension so that it only returns file extensions. Add tests to Storage.t to validate that --- lib/WebGUI/Storage.pm | 7 ++++--- t/Storage.t | 48 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/WebGUI/Storage.pm b/lib/WebGUI/Storage.pm index ce74a2690..b7c7693b1 100644 --- a/lib/WebGUI/Storage.pm +++ b/lib/WebGUI/Storage.pm @@ -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; } diff --git a/t/Storage.t b/t/Storage.t index 98274917f..d1058e77e 100644 --- a/t/Storage.t +++ b/t/Storage.t @@ -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'); }