[commit: packages/filepath] RyanGlScott-patch-1, master: Amended isExtensionOf and its haddock, updated tests (f7b03a6)

git at git.haskell.org git at git.haskell.org
Thu Sep 13 14:51:09 UTC 2018


Repository : ssh://git@git.haskell.org/filepath

On branches: RyanGlScott-patch-1,master
Link       : http://git.haskell.org/packages/filepath.git/commitdiff/f7b03a6fe168da2d57f9282898f3de375431e854

>---------------------------------------------------------------

commit f7b03a6fe168da2d57f9282898f3de375431e854
Author: Jonatan H Sundqvist <jonatanhsundqvist at gmail.com>
Date:   Fri Aug 18 19:08:07 2017 +0200

    Amended isExtensionOf and its haddock, updated tests


>---------------------------------------------------------------

f7b03a6fe168da2d57f9282898f3de375431e854
 System/FilePath/Internal.hs | 26 ++++++++++++++++++++------
 tests/TestGen.hs            | 20 ++++++++++++++------
 2 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 1ad7e31..8840555 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -105,7 +105,7 @@ module System.FilePath.MODULE_NAME
 
 import Data.Char(toLower, toUpper, isAsciiLower, isAsciiUpper)
 import Data.Maybe(isJust)
-import Data.List(stripPrefix)
+import Data.List(stripPrefix, isSuffixOf)
 
 import System.Environment(getEnv)
 
@@ -313,14 +313,28 @@ hasExtension :: FilePath -> Bool
 hasExtension = any isExtSeparator . takeFileName
 
 
--- | Is the given string the final extension of the filename?
---   The extension should not include the separator.
+-- | Does the given filename have the specified extension?
 --
--- > "png" `isExtensionOf` "/directory/file.png"     == True
+--   The extension may exclude the separator
+-- > "png" `isExtensionOf` "/directory/file.png" == True
+--
+--   And it may include it
+-- > ".png" `isExtensionOf` "/directory/file.png" == True
+--
+--   Multiple extensions are allowed
+-- > ".tar.gz" `isExtensionOf` "bar/foo.tar.gz" == True
+--
+--   But partial matches are not
+-- > "ar.gz" `isExtensionOf` "bar/foo.tar.gz" == False
+--
+--   Extensions are matched from the end, so the following yields @False@
 -- > "png" `isExtensionOf` "/directory/file.png.jpg" == False
--- > "csv" `isExtensionOf` "/directory/data.csv"     == True
+
+--   The argument cannot simply be a suffix, it has to be to a valid extension
+-- > "csv/table.csv" `isExtensionOf` "/data/csv/table.csv" == False
 isExtensionOf :: String -> FilePath -> Bool
-isExtensionOf ext = (== ext) . drop 1 . takeExtension
+isExtensionOf ext@('.':_) = isSuffixOf ext . takeExtensions
+isExtensionOf ext         = isSuffixOf ('.':ext) . takeExtensions
 
 -- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
 --   Returns 'Nothing' if the FilePath does not have the given extension, or
diff --git a/tests/TestGen.hs b/tests/TestGen.hs
index cdff89c..7342871 100755
--- a/tests/TestGen.hs
+++ b/tests/TestGen.hs
@@ -106,12 +106,20 @@ tests =
     ,("W.hasExtension \"/directory/path.ext\" == True", property $ W.hasExtension "/directory/path.ext" == True)
     ,("P.hasExtension \"/directory/path\" == False", property $ P.hasExtension "/directory/path" == False)
     ,("W.hasExtension \"/directory/path\" == False", property $ W.hasExtension "/directory/path" == False)
-    ,("W.isExtensionOf \"png\" \"/directory/file.png\"     == True",  property $ W.isExtensionOf "png" "/directory/file.png"     == True)
-    ,("P.isExtensionOf \"png\" \"/directory/file.png\"     == True",  property $ P.isExtensionOf "png" "/directory/file.png"     == True)
-    ,("W.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
-    ,("P.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ P.isExtensionOf "png" "/directory/file.png.jpg" == False)
-    ,("W.isExtensionOf \"csv\" \"/directory/data.csv\"     == True",  property $ W.isExtensionOf "csv" "/directory/data.csv"     == True)
-    ,("P.isExtensionOf \"csv\" \"/directory/data.csv\"     == True",  property $ P.isExtensionOf "csv" "/directory/data.csv"     == True)
+
+    ,("W.isExtensionOf \"png\"           \"/directory/file.png\"     == True",  property $ W.isExtensionOf "png"           "/directory/file.png" == True)
+    ,("P.isExtensionOf \"png\"           \"/directory/file.png\"     == True",  property $ W.isExtensionOf "png"           "/directory/file.png" == True)
+    ,("W.isExtensionOf \".png\"          \"/directory/file.png\"     == True",  property $ W.isExtensionOf ".png"          "/directory/file.png" == True)
+    ,("P.isExtensionOf \".png\"          \"/directory/file.png\"     == True",  property $ W.isExtensionOf ".png"          "/directory/file.png" == True)
+    ,("W.isExtensionOf \".tar.gz\"       \"bar/foo.tar.gz\"          == True",  property $ W.isExtensionOf ".tar.gz"       "bar/foo.tar.gz" == True)
+    ,("P.isExtensionOf \".tar.gz\"       \"bar/foo.tar.gz\"          == True",  property $ W.isExtensionOf ".tar.gz"       "bar/foo.tar.gz" == True)
+    ,("W.isExtensionOf \"ar.gz\"         \"bar/foo.tar.gz\"          == False", property $ W.isExtensionOf "ar.gz"         "bar/foo.tar.gz" == False)
+    ,("P.isExtensionOf \"ar.gz\"         \"bar/foo.tar.gz\"          == False", property $ W.isExtensionOf "ar.gz"         "bar/foo.tar.gz" == False)
+    ,("W.isExtensionOf \"png\"           \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png"           "/directory/file.png.jpg" == False)
+    ,("P.isExtensionOf \"png\"           \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png"           "/directory/file.png.jpg" == False)
+    ,("W.isExtensionOf \"csv/table.csv\" \"/data/csv/table.csv\"     == False", property $ W.isExtensionOf "csv/table.csv" "/data/csv/table.csv" == False)
+    ,("P.isExtensionOf \"csv/table.csv\" \"/data/csv/table.csv\"     == False", property $ W.isExtensionOf "csv/table.csv" "/data/csv/table.csv" == False)
+
     ,("null (P.takeExtension x) == not (P.hasExtension x)", property $ \(QFilePath x) -> null (P.takeExtension x) == not (P.hasExtension x))
     ,("null (W.takeExtension x) == not (W.hasExtension x)", property $ \(QFilePath x) -> null (W.takeExtension x) == not (W.hasExtension x))
     ,("P.stripExtension \"hs.o\" \"foo.x.hs.o\" == Just \"foo.x\"", property $ P.stripExtension "hs.o" "foo.x.hs.o" == Just "foo.x")



More information about the ghc-commits mailing list