tilia-0.2.0.0: tests/Tilia/ColumnLimitSpec.hs
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
-- | Formatting at column limits other than the default one.
module Tilia.ColumnLimitSpec (spec) where
import Control.Monad (guard, when)
import Data.Choice (pattern Do)
import Data.Foldable (for_)
import Data.Functor ((<&>))
import Data.Map.Strict qualified as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import System.FilePath (dropExtension, takeFileName, (</>))
import Test.Hspec
import Test.Hspec.QuickCheck (modifyMaxSuccess)
import Test.QuickCheck
import Text.Read (readMaybe)
import Tilia.Corpus (Example (..), answerFile, obtain, vendoredExamples)
import Tilia.Format (describeFormatError, formattingFailed)
import Tilia.Palette (Palette (Plain))
import Tilia.Settings (Settings (..), alternativeSettings, defaultSettings)
import Tilia.TestConfig (formatExample)
import Tilia.TestUtils (haskellFilesIn, switchedOn, withEachSettings)
import Tilia.Utils (readUtf8File, writeUtf8File)
spec :: Spec
spec = do
files <- runIO (haskellFilesIn ("corpora" </> "column-limit"))
accepting <- runIO (switchedOn "TILIA_VENDORED_ACCEPT")
let limits = Set.toList (Set.fromList (mapMaybe limitOf files))
inputs = [f | f <- files, Nothing <- [limitOf f]]
describe "a module formatted at a column limit" $
for_ inputs $ \input ->
for_ limits $ \limit ->
for_ answers $ \(mark, named, settings) ->
it (takeFileName input <> " at " <> show limit <> named) $ do
let reference = answerFile (mark <> show limit) input
Just source <- readUtf8File input
formatExample
(Do #check)
settings{settingsColumnLimit = limit}
Nothing
Map.empty
(takeFileName input)
source
>>= \case
Left e ->
expectationFailure (T.unpack (describeFormatError Plain e))
Right formatted -> do
when accepting (writeUtf8File reference formatted)
readUtf8File reference >>= \case
Nothing ->
expectationFailure $
reference
<> " is missing; TILIA_VENDORED_ACCEPT=1 cabal test"
<> " writes it"
Just expected -> formatted `shouldBe` expected
vendored <- runIO (either (fail . T.unpack) pure =<< obtain vendoredExamples)
modules <-
runIO $
traverse
(\f -> (f,) . fromMaybe "" <$> readUtf8File f)
(fmap exampleInput vendored <> inputs)
parallel . modifyMaxSuccess (const 5) . withEachSettings $ \settings ->
describe "formats at any column limit in one pass, as the same program" $
for_ modules $ \(path, source) ->
it path . forAll (choose (1, widest source)) $ \limit ->
ioProperty $
formatExample
(Do #check)
settings{settingsColumnLimit = limit}
Nothing
Map.empty
(takeFileName path)
source
<&> \case
Left e
| formattingFailed e ->
counterexample
(T.unpack (describeFormatError Plain e))
False
_ -> property True
-- | The width of the widest line of a module.
widest :: Text -> Int
widest = maximum . (1 :) . fmap T.length . T.lines
-- | Each mark of the files that hold what a module formats to, as @-out-@
-- in @name-out-N.hs@, with what its tests are named with and the settings
-- it is formatted with.
answers :: [(String, String, Settings)]
answers =
[ ("-out-", "", defaultSettings),
("-out'-", " with the alternative settings", alternativeSettings)
]
-- | The column limit a file of the collection says what a module formats
-- to at, as @name-out-N.hs@ and @name-out'-N.hs@ do, if it is one of those.
limitOf :: FilePath -> Maybe Int
limitOf f = do
let (front, n) = T.breakOnEnd "-" (T.pack (dropExtension f))
guard (any (\(mark, _, _) -> T.pack mark `T.isSuffixOf` front) answers)
readMaybe (T.unpack n)