tilia-0.2.0.0: tests/Tilia/SettingsSpec.hs
{-# LANGUAGE OverloadedStrings #-}
-- | The settings a project writes in its @tilia.toml@.
module Tilia.SettingsSpec (spec) where
import Data.Text (Text)
import System.Directory (emptyPermissions, setPermissions)
import System.FilePath ((</>))
import Test.Hspec
import Tilia.Settings
import Tilia.TestUtils (withFiles)
spec :: Spec
spec = do
describe "a settings file" $ do
it "that is empty sets nothing" $
parseSettings "" `shouldBe` Right defaultSettings
it "that holds only comments and empty lines sets nothing" $
parseSettings "# Nothing here.\n\n # Nor here.\n"
`shouldBe` Right defaultSettings
it "sets the column limit" $
"column-limit = 100\n" `limitsAt` 100
it "reads a key and a value however they are spaced" $ do
"column-limit=100" `limitsAt` 100
" column-limit = 90 " `limitsAt` 90
it "reads a comment after a value" $
"column-limit = 100 # a hundred columns" `limitsAt` 100
it "reads a value with a sign" $
"column-limit = +100" `limitsAt` 100
it "sets the indentation" $ do
parseSettings "indentation = 4\n"
`shouldBe` Right defaultSettings{settingsIndentation = FourColumns}
parseSettings "indentation = 2\n"
`shouldBe` Right defaultSettings{settingsIndentation = TwoColumns}
it "sets leading commas" $ do
parseSettings "leading-commas = true\n"
`shouldBe` Right defaultSettings{settingsCommaStyle = LeadingCommas}
parseSettings "leading-commas = false\n"
`shouldBe` Right defaultSettings{settingsCommaStyle = TrailingCommas}
it "sets leading arrows" $ do
parseSettings "leading-arrows = true\n"
`shouldBe` Right defaultSettings{settingsArrowStyle = LeadingArrows}
parseSettings "leading-arrows = false\n"
`shouldBe` Right defaultSettings{settingsArrowStyle = TrailingArrows}
it "sets several settings" $
parseSettings
"column-limit = 100\nindentation = 4\nleading-commas = true\n\
\leading-arrows = true\n"
`shouldBe` Right
Settings
{ settingsColumnLimit = 100,
settingsIndentation = FourColumns,
settingsCommaStyle = LeadingCommas,
settingsArrowStyle = LeadingArrows
}
describe "a settings file is refused where" $ do
it "the column limit is not a whole number greater than 0" $ do
let why = "column-limit is a whole number greater than 0"
"column-limit = 0" `fails` (1, why)
"column-limit = -80" `fails` (1, why)
"column-limit = true" `fails` (1, why)
"column-limit = 99999999999999999999" `fails` (1, why)
it "the indentation is neither 2 nor 4" $ do
let why = "indentation is 2 or 4"
"indentation = 3" `fails` (1, why)
"indentation = 8" `fails` (1, why)
"indentation = true" `fails` (1, why)
it "leading commas are neither true nor false" $
"leading-commas = 1" `fails` (1, "leading-commas is true or false")
it "leading arrows are neither true nor false" $
"leading-arrows = 1" `fails` (1, "leading-arrows is true or false")
it "a value is a string" $
"column-limit = \"100\""
`fails` (1, "a value is true, false or a whole number")
it "a number begins with a zero" $
"column-limit = 0100"
`fails` (1, "a value is true, false or a whole number")
it "a setting is given twice" $
"column-limit = 100\n\ncolumn-limit = 90\n"
`fails` (3, "column-limit is already set on line 1")
it "a setting is unknown" $
"# A comment.\nwidth = 80\n" `fails` (2, "there is no setting width")
it "a line is not key = value" $ do
"[tilia]" `fails` (1, "expected a line of key = value")
"column-limit" `fails` (1, "expected a line of key = value")
"\"column-limit\" = 100" `fails` (1, "expected a line of key = value")
describe "reading a project's settings" $ do
it "gives the defaults where the project has no settings file" $
withFiles [] $ \root ->
readSettings root `shouldReturn` Right defaultSettings
it "reads the project's settings file" $
withFiles [(settingsFile, "column-limit = 100\n")] $ \root ->
readSettings root
`shouldReturn` Right defaultSettings{settingsColumnLimit = 100}
it "reads a settings file past a byte order mark" $
withFiles [(settingsFile, "\xFEFF\&column-limit = 100\n")] $ \root ->
readSettings root
`shouldReturn` Right defaultSettings{settingsColumnLimit = 100}
it "says why it cannot read a settings file without naming it" $
withFiles [(settingsFile, "column-limit = 100\n")] $ \root -> do
setPermissions (root </> settingsFile) emptyPermissions
readSettings root
`shouldReturn` Left (SettingsUnreadable "permission denied")
-- | Check that a settings file sets the column limit.
limitsAt ::
-- | The file's text.
Text ->
-- | The column limit.
Int ->
Expectation
limitsAt written limit =
parseSettings written
`shouldBe` Right defaultSettings{settingsColumnLimit = limit}
-- | Check that a settings file is refused at a line, for a reason.
fails ::
-- | The file's text.
Text ->
-- | The number of the line, and why.
(Int, Text) ->
Expectation
fails written (n, why) =
parseSettings written `shouldBe` Left (SettingsMalformed n why)