1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Ormolu.PrinterSpec (spec) where
import Control.Exception
import Control.Monad
import Data.List (isSuffixOf)
import Data.Map qualified as Map
import Data.Maybe (isJust)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.IO qualified as T
import Ormolu
import Ormolu.Fixity
import Ormolu.Utils.IO
import Path
import Path.IO
import System.Environment (lookupEnv)
import System.FilePath qualified as F
import Test.Hspec
spec :: Spec
spec = do
es <- runIO locateExamples
forM_ es checkExample
-- | Fixity overrides that are to be used with the test examples.
testsuiteOverrides :: FixityOverrides
testsuiteOverrides =
FixityOverrides
( Map.fromList
[ (".=", FixityInfo InfixR 8),
("#", FixityInfo InfixR 5)
]
)
-- | Check a single given example.
checkExample :: Path Rel File -> Spec
checkExample srcPath' = it (fromRelFile srcPath' ++ " works") . withNiceExceptions $ do
let srcPath = examplesDir </> srcPath'
inputPath = fromRelFile srcPath
config =
defaultConfig
{ cfgSourceType = detectSourceType inputPath,
cfgFixityOverrides = testsuiteOverrides,
cfgDependencies =
Set.fromList
[ "base",
"esqueleto",
"hspec",
"lens",
"servant"
]
}
expectedOutputPath <- deriveOutput srcPath
-- 1. Given input snippet of source code parse it and pretty print it.
-- 2. Parse the result of pretty-printing again and make sure that AST
-- is the same as AST of the original snippet. (This happens in
-- 'ormoluFile' automatically.)
formatted0 <- ormoluFile config inputPath
-- 3. Check the output against expected output. Thus all tests should
-- include two files: input and expected output.
whenShouldRegenerateOutput $
T.writeFile (fromRelFile expectedOutputPath) formatted0
expected <- readFileUtf8 $ fromRelFile expectedOutputPath
shouldMatch False formatted0 expected
-- 4. Check that running the formatter on the output produces the same
-- output again (the transformation is idempotent).
formatted1 <- ormolu config "<formatted>" formatted0
shouldMatch True formatted1 formatted0
-- | Build list of examples for testing.
locateExamples :: IO [Path Rel File]
locateExamples =
filter isInput . snd <$> listDirRecurRel examplesDir
-- | Does given path look like input path (as opposed to expected output
-- path)?
isInput :: Path Rel File -> Bool
isInput path =
let s = fromRelFile path
(s', exts) = F.splitExtensions s
in exts `elem` [".hs", ".hsig"] && not ("-out" `isSuffixOf` s')
-- | For given path of input file return expected name of output.
deriveOutput :: Path Rel File -> IO (Path Rel File)
deriveOutput path =
parseRelFile $
F.addExtension (radical ++ "-out") exts
where
(radical, exts) = F.splitExtensions (fromRelFile path)
-- | A version of 'shouldBe' that is specialized to comparing 'Text' values.
-- It also prints multi-line snippets in a more readable form.
shouldMatch :: Bool -> Text -> Text -> Expectation
shouldMatch idempotenceTest actual expected =
when (actual /= expected) . expectationFailure $
unlines
[ ">>>>>>>>>>>>>>>>>>>>>> expected (" ++ pass ++ "):",
T.unpack expected,
">>>>>>>>>>>>>>>>>>>>>> but got:",
T.unpack actual
]
where
pass =
if idempotenceTest
then "idempotence pass"
else "first pass"
examplesDir :: Path Rel Dir
examplesDir = $(mkRelDir "data/examples")
-- | Inside this wrapper 'OrmoluException' will be caught and displayed
-- nicely using 'displayException'.
withNiceExceptions ::
-- | Action that may throw the exception
Expectation ->
Expectation
withNiceExceptions m = m `catch` h
where
h :: OrmoluException -> IO ()
h = expectationFailure . displayException
whenShouldRegenerateOutput :: IO () -> IO ()
whenShouldRegenerateOutput action = do
shouldRegenerateOutput <- isJust <$> lookupEnv "ORMOLU_REGENERATE_EXAMPLES"
when shouldRegenerateOutput action
|