summaryrefslogtreecommitdiffhomepage
path: root/tests/Language
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Language')
-rw-r--r--tests/Language/Haskell/Stylish/Config/Tests.hs142
-rw-r--r--tests/Language/Haskell/Stylish/Step/Imports/Tests.hs114
-rw-r--r--tests/Language/Haskell/Stylish/Step/TrailingWhitespace/Tests.hs6
3 files changed, 261 insertions, 1 deletions
diff --git a/tests/Language/Haskell/Stylish/Config/Tests.hs b/tests/Language/Haskell/Stylish/Config/Tests.hs
new file mode 100644
index 0000000..f62b571
--- /dev/null
+++ b/tests/Language/Haskell/Stylish/Config/Tests.hs
@@ -0,0 +1,142 @@
+module Language.Haskell.Stylish.Config.Tests
+ ( tests
+ ) where
+
+
+--------------------------------------------------------------------------------
+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
+
+--------------------------------------------------------------------------------
+tests :: Test
+tests = testGroup "Language.Haskell.Stylish.Config"
+ [ testCase "Extensions extracted correctly from .cabal file"
+ testExtensionsFromDotCabal
+ , testCase "Extensions extracted correctly from .stylish-haskell.yaml file"
+ testExtensionsFromDotStylish
+ , testCase "Extensions extracted correctly from .stylish-haskell.yaml and .cabal files"
+ testExtensionsFromBoth
+ ]
+--------------------------------------------------------------------------------
+
+-- | 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
+ mapM_ (\(k, v) -> writeFile k v) files
+ -- create an empty directory and change into it
+ createDirectory "src"
+ setCurrentDirectory "src"
+ -- 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
+
+--------------------------------------------------------------------------------
+testExtensionsFromDotCabal :: Assertion
+testExtensionsFromDotCabal =
+ assert $ (expected ==) . Set.fromList <$>
+ createFilesAndGetExtensions [("test.cabal", dotCabal True)]
+ where
+ expected = Set.fromList ["ScopedTypeVariables", "DataKinds"]
+
+--------------------------------------------------------------------------------
+testExtensionsFromDotStylish :: Assertion
+testExtensionsFromDotStylish =
+ assert $ (expected ==) . Set.fromList <$>
+ createFilesAndGetExtensions [(".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)]
+ where
+ expected = Set.fromList
+ ["ScopedTypeVariables", "DataKinds", "TemplateHaskell", "QuasiQuotes"]
+
+-- | Example cabal file borrowed from
+-- https://www.haskell.org/cabal/users-guide/developing-packages.html
+-- with some default-extensions added
+dotCabal :: Bool -> String
+dotCabal includeExtensions = unlines $
+ [ "name: TestPackage"
+ , "version: 0.0"
+ , "synopsis: Package with library and two programs"
+ , "license: BSD3"
+ , "author: Angela Author"
+ , "build-type: Simple"
+ , "cabal-version: >= 1.2"
+ , ""
+ , "library"
+ , " build-depends: HUnit"
+ , " exposed-modules: A, B, C"
+ ] ++
+ [if includeExtensions then " default-extensions: ScopedTypeVariables"
+ else ""]
+ ++
+ [ ""
+ , "executable program1"
+ , " main-is: Main.hs"
+ , " hs-source-dirs: prog1"
+ , " other-modules: A, B"
+ ] ++
+ [if includeExtensions then " default-extensions: DataKinds"
+ else ""]
+
+-- | Example .stylish-haskell.yaml
+dotStylish :: String
+dotStylish = 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: 110"
+ , "language_extensions:"
+ , " - TemplateHaskell"
+ , " - QuasiQuotes"
+ ]
diff --git a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
index 67c7c5a..760018a 100644
--- a/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/Imports/Tests.hs
@@ -32,11 +32,15 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 06" case06
, testCase "case 07" case07
, testCase "case 08" case08
+ , testCase "case 08b" case08b
, testCase "case 09" case09
, testCase "case 10" case10
, testCase "case 11" case11
+ , testCase "case 11b" case11b
, testCase "case 12" case12
+ , testCase "case 12b" case12b
, testCase "case 13" case13
+ , testCase "case 13b" case13b
, testCase "case 14" case14
, testCase "case 15" case15
, testCase "case 16" case16
@@ -50,6 +54,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Imports.Tests"
, testCase "case 21" case21
, testCase "case 22" case22
, testCase "case 23" case23
+ , testCase "case 23b" case23b
, testCase "case 24" case24
, testCase "case 25" case25
, testCase "case 26 (issue 185)" case26
@@ -213,6 +218,28 @@ case08 = expected
--------------------------------------------------------------------------------
+case08b :: Assertion
+case08b = expected
+ @=? testStep (step 80 $ Options Global WithModuleName True Inline Inherit (LPConstant 4) True False) input
+ where
+ expected = unlines
+ ["module Herp where"
+ , ""
+ , "import Control.Monad"
+ , "import Data.List as List (concat, foldl, foldr, head, init,"
+ , " last, length, map, null, reverse, tail, (++))"
+ , "import Data.Map (Map, insert, lookup, (!))"
+ , "import qualified Data.Map as M"
+ , "import Only.Instances ()"
+ , ""
+ , "import Foo (Bar (..))"
+ , "import Herp.Derp.Internals hiding (foo)"
+ , ""
+ , "herp = putStrLn \"import Hello world\""
+ ]
+
+
+--------------------------------------------------------------------------------
case09 :: Assertion
case09 = expected
@=? testStep (step 80 $ Options Global WithAlias True Multiline Inherit (LPConstant 4) True False) input
@@ -313,6 +340,27 @@ case11 = expected
]
+case11b :: Assertion
+case11b = expected
+ @=? testStep (step 80 $ Options Group WithModuleName True Inline Inherit (LPConstant 4) True False) input
+ where
+ expected = unlines
+ [ "module Herp where"
+ , ""
+ , "import Control.Monad"
+ , "import Data.List as List (concat, foldl, foldr, head, init, last,"
+ , " length, map, null, reverse, tail, (++))"
+ , "import Data.Map (Map, insert, lookup, (!))"
+ , "import qualified Data.Map as M"
+ , "import Only.Instances ()"
+ , ""
+ , "import Foo (Bar (..))"
+ , "import Herp.Derp.Internals hiding (foo)"
+ , ""
+ , "herp = putStrLn \"import Hello world\""
+ ]
+
+
--------------------------------------------------------------------------------
case12 :: Assertion
case12 = expected
@@ -329,6 +377,18 @@ case12 = expected
--------------------------------------------------------------------------------
+case12b :: Assertion
+case12b = expected
+ @=? testStep (step 80 $ Options Group WithModuleName True Inline Inherit (LPConstant 2) True False) input'
+ where
+ input' = unlines
+ [ "import Data.List (map)"
+ ]
+
+ expected = input'
+
+
+--------------------------------------------------------------------------------
case13 :: Assertion
case13 = expected
@=? testStep (step 80 $ Options None WithAlias True InlineWithBreak Inherit (LPConstant 4) True False) input'
@@ -346,6 +406,23 @@ case13 = expected
--------------------------------------------------------------------------------
+case13b :: Assertion
+case13b = expected
+ @=? testStep (step 80 $ Options None WithModuleName True InlineWithBreak Inherit (LPConstant 4) True False) input'
+ where
+ input' = unlines
+ [ "import qualified Data.List as List (concat, foldl, foldr, head, init,"
+ , " last, length, map, null, reverse, tail, (++))"
+ ]
+
+ expected = unlines
+ [ "import qualified Data.List as List"
+ , " (concat, foldl, foldr, head, init, last, length, map, null, reverse, tail,"
+ , " (++))"
+ ]
+
+
+--------------------------------------------------------------------------------
case14 :: Assertion
case14 = expected
@=? testStep
@@ -451,6 +528,7 @@ case18 = expected @=? testStep
, "import Data.Acid as Acid (closeAcidState, createCheckpoint, openLocalStateFrom)"
]
+
--------------------------------------------------------------------------------
case19 :: Assertion
case19 = expected @=? testStep
@@ -467,6 +545,7 @@ case19 = expected @=? testStep
, " intersperse)"
]
+
case19b :: Assertion
case19b = expected @=? testStep
(step 40 $ Options File NewLine True InlineWithBreak RightAfter (LPConstant 17) True False) case19input
@@ -482,6 +561,7 @@ case19b = expected @=? testStep
, " intersperse)"
]
+
case19c :: Assertion
case19c = expected @=? testStep
(step 40 $ Options File NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input
@@ -497,6 +577,7 @@ case19c = expected @=? testStep
, " intersperse)"
]
+
case19d :: Assertion
case19d = expected @=? testStep
(step 40 $ Options Global NewLine True InlineWithBreak RightAfter LPModuleName True False) case19input
@@ -512,6 +593,7 @@ case19d = expected @=? testStep
, " intersperse)"
]
+
case19input :: String
case19input = unlines
[ "import Prelude.Compat hiding (foldMap)"
@@ -520,6 +602,7 @@ case19input = unlines
, "import Data.List (foldl', intercalate, intersperse)"
]
+
--------------------------------------------------------------------------------
case20 :: Assertion
case20 = expected
@@ -538,6 +621,7 @@ case20 = expected
, "import Data.Set (empty)"
]
+
--------------------------------------------------------------------------------
case21 :: Assertion
case21 = expected
@@ -568,6 +652,7 @@ case21 = expected
, "import X9 hiding (x, y, z, x)"
]
+
--------------------------------------------------------------------------------
case22 :: Assertion
case22 = expected
@@ -594,6 +679,7 @@ case22 = expected
"theLongestNameYet, shortName)"
]
+
--------------------------------------------------------------------------------
case23 :: Assertion
case23 = expected
@@ -618,6 +704,33 @@ case23 = expected
, "import Data.ALongName.Foo (Foo, Goo, Boo)"
]
+
+--------------------------------------------------------------------------------
+case23b :: Assertion
+case23b = expected
+ @=? testStep (step 40 $ Options None WithModuleName False Inline Inherit (LPConstant 4) True True) input'
+ where
+ expected = unlines
+ [ "import Data.Acid ( AcidState )"
+ , "import Data.Default.Class"
+ , " ( Default (def) )"
+ , ""
+ , "import Data.Monoid ( (<>) )"
+ , ""
+ , "import Data.ALongName.Foo ( Boo, Foo,"
+ , " Goo )"
+ ]
+
+ input' = unlines
+ [ "import Data.Acid (AcidState)"
+ , "import Data.Default.Class (Default(def))"
+ , ""
+ , "import Data.Monoid ((<>) )"
+ , ""
+ , "import Data.ALongName.Foo (Foo, Goo, Boo)"
+ ]
+
+
--------------------------------------------------------------------------------
case24 :: Assertion
case24 = expected
@@ -641,6 +754,7 @@ case24 = expected
"GooReallyLong, BooReallyLong)"
]
+
--------------------------------------------------------------------------------
case25 :: Assertion
case25 = expected
diff --git a/tests/Language/Haskell/Stylish/Step/TrailingWhitespace/Tests.hs b/tests/Language/Haskell/Stylish/Step/TrailingWhitespace/Tests.hs
index 1394edb..0593c0a 100644
--- a/tests/Language/Haskell/Stylish/Step/TrailingWhitespace/Tests.hs
+++ b/tests/Language/Haskell/Stylish/Step/TrailingWhitespace/Tests.hs
@@ -28,12 +28,16 @@ case01 = expected @=? testStep step input
where
input = unlines
[ "module Main where"
- , " "
+ , " \t"
, "data Foo = Bar | Qux\t "
+ , "\12" -- page break
+ , " \12" -- malformed page break
]
expected = unlines
[ "module Main where"
, ""
, "data Foo = Bar | Qux"
+ , "\12" -- page break
+ , ""
]