diff options
Diffstat (limited to 'tests/Language/Haskell/Stylish/Config/Tests.hs')
-rw-r--r-- | tests/Language/Haskell/Stylish/Config/Tests.hs | 135 |
1 files changed, 96 insertions, 39 deletions
diff --git a/tests/Language/Haskell/Stylish/Config/Tests.hs b/tests/Language/Haskell/Stylish/Config/Tests.hs index f62b571..a8b2ee2 100644 --- a/tests/Language/Haskell/Stylish/Config/Tests.hs +++ b/tests/Language/Haskell/Stylish/Config/Tests.hs @@ -4,17 +4,17 @@ module Language.Haskell.Stylish.Config.Tests -------------------------------------------------------------------------------- -import Control.Exception hiding (assert) import qualified Data.Set as Set import System.Directory -import System.FilePath ((</>)) -import System.IO.Error -import System.Random import Test.Framework (Test, testGroup) import Test.Framework.Providers.HUnit (testCase) import Test.HUnit (Assertion, assert) + + -------------------------------------------------------------------------------- import Language.Haskell.Stylish.Config +import Language.Haskell.Stylish.Tests.Util + -------------------------------------------------------------------------------- tests :: Test @@ -25,38 +25,20 @@ tests = testGroup "Language.Haskell.Stylish.Config" testExtensionsFromDotStylish , testCase "Extensions extracted correctly from .stylish-haskell.yaml and .cabal files" testExtensionsFromBoth + , testCase "Correctly read .stylish-haskell.yaml file with default max column number" + testDefaultColumns + , testCase "Correctly read .stylish-haskell.yaml file with specified max column number" + testSpecifiedColumns + , testCase "Correctly read .stylish-haskell.yaml file with no max column number" + testNoColumns ] --------------------------------------------------------------------------------- --- | Create a temporary directory with a randomised name built from the template provided -createTempDirectory :: String -> IO FilePath -createTempDirectory template = do - tmpRootDir <- getTemporaryDirectory - dirId <- randomIO :: IO Word - findTempName tmpRootDir dirId - where - findTempName :: FilePath -> Word -> IO FilePath - findTempName tmpRootDir x = do - let dirpath = tmpRootDir </> template ++ show x - r <- try $ createDirectory dirpath - case r of - Right _ -> return dirpath - Left e | isAlreadyExistsError e -> findTempName tmpRootDir (x+1) - | otherwise -> ioError e - --- | Perform an action inside a temporary directory tree and purge the tree afterwords -withTestDirTree :: IO a -> IO a -withTestDirTree action = bracket - ((,) <$> getCurrentDirectory <*> createTempDirectory "stylish_haskell") - (\(current, temp) -> - setCurrentDirectory current *> - removeDirectoryRecursive temp) - (\(_, temp) -> setCurrentDirectory temp *> action) +-------------------------------------------------------------------------------- -- | Put an example config files (.cabal/.stylish-haskell.yaml/both) -- into the current directory and extract extensions from it. -createFilesAndGetExtensions :: [(FilePath, String)] -> IO Extensions -createFilesAndGetExtensions files = withTestDirTree $ do +createFilesAndGetConfig :: [(FilePath, String)] -> IO Config +createFilesAndGetConfig files = withTestDirTree $ do mapM_ (\(k, v) -> writeFile k v) files -- create an empty directory and change into it createDirectory "src" @@ -64,34 +46,65 @@ createFilesAndGetExtensions files = withTestDirTree $ do -- from that directory read the config file and extract extensions -- to make sure the search for .cabal file works config <- loadConfig (const (pure ())) Nothing - pure $ configLanguageExtensions config + pure config + -------------------------------------------------------------------------------- testExtensionsFromDotCabal :: Assertion testExtensionsFromDotCabal = - assert $ (expected ==) . Set.fromList <$> - createFilesAndGetExtensions [("test.cabal", dotCabal True)] + assert $ (expected ==) . Set.fromList . configLanguageExtensions <$> + createFilesAndGetConfig [("test.cabal", dotCabal True)] where expected = Set.fromList ["ScopedTypeVariables", "DataKinds"] + -------------------------------------------------------------------------------- testExtensionsFromDotStylish :: Assertion testExtensionsFromDotStylish = - assert $ (expected ==) . Set.fromList <$> - createFilesAndGetExtensions [(".stylish-haskell.yaml", dotStylish)] + assert $ (expected ==) . Set.fromList . configLanguageExtensions <$> + createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish)] where expected = Set.fromList ["TemplateHaskell", "QuasiQuotes"] + -------------------------------------------------------------------------------- testExtensionsFromBoth :: Assertion testExtensionsFromBoth = - assert $ (expected ==) . Set.fromList <$> - createFilesAndGetExtensions [ ("test.cabal", dotCabal True) - , (".stylish-haskell.yaml", dotStylish)] + assert $ (expected ==) . Set.fromList . configLanguageExtensions <$> + createFilesAndGetConfig [ ("test.cabal", dotCabal True) + , (".stylish-haskell.yaml", dotStylish)] where expected = Set.fromList ["ScopedTypeVariables", "DataKinds", "TemplateHaskell", "QuasiQuotes"] + +-------------------------------------------------------------------------------- +testSpecifiedColumns :: Assertion +testSpecifiedColumns = + assert $ (expected ==) . configColumns <$> + createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish)] + where + expected = Just 110 + + +-------------------------------------------------------------------------------- +testDefaultColumns :: Assertion +testDefaultColumns = + assert $ (expected ==) . configColumns <$> + createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish2)] + where + expected = Just 80 + + +-------------------------------------------------------------------------------- +testNoColumns :: Assertion +testNoColumns = + assert $ (expected ==) . configColumns <$> + createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish3)] + where + expected = Nothing + + -- | Example cabal file borrowed from -- https://www.haskell.org/cabal/users-guide/developing-packages.html -- with some default-extensions added @@ -135,8 +148,52 @@ dotStylish = unlines $ , " align: false" , " remove_redundant: true" , " - trailing_whitespace: {}" + , " - records:" + , " equals: \"same_line\"" + , " first_field: \"indent 2\"" + , " field_comment: 2" + , " deriving: 4" , "columns: 110" , "language_extensions:" , " - TemplateHaskell" , " - QuasiQuotes" ] + +-- | Example .stylish-haskell.yaml +dotStylish2 :: String +dotStylish2 = unlines $ + [ "steps:" + , " - imports:" + , " align: none" + , " list_align: after_alias" + , " long_list_align: inline" + , " separate_lists: true" + , " - language_pragmas:" + , " style: vertical" + , " align: false" + , " remove_redundant: true" + , " - trailing_whitespace: {}" + , "language_extensions:" + , " - TemplateHaskell" + , " - QuasiQuotes" + ] + +-- | Example .stylish-haskell.yaml +dotStylish3 :: String +dotStylish3 = unlines $ + [ "steps:" + , " - imports:" + , " align: none" + , " list_align: after_alias" + , " long_list_align: inline" + , " separate_lists: true" + , " - language_pragmas:" + , " style: vertical" + , " align: false" + , " remove_redundant: true" + , " - trailing_whitespace: {}" + , "columns: null" + , "language_extensions:" + , " - TemplateHaskell" + , " - QuasiQuotes" + ] |