Skip to content

Commit

Permalink
Merge pull request #19 from gvolpe/test-suite
Browse files Browse the repository at this point in the history
Adding tests for dconf parser
  • Loading branch information
gvolpe authored Aug 7, 2020
2 parents dd89bc3 + 08efa3a commit b2e650d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
15 changes: 15 additions & 0 deletions dconf2nix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ library
hs-source-dirs: src
default-language: Haskell2010

test-Suite dconf2nix-tests
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
other-modules: DConfTest
build-depends: base
, containers
, dconf2nix
, hedgehog
, parsec
, template-haskell
, text
default-language: Haskell2010
ghc-options: -Wall -threaded -with-rtsopts -N

executable dconf2nix
main-is: Main.hs
build-depends: base
Expand Down
4 changes: 2 additions & 2 deletions src/DConf/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ data Value = S Text
| T Value Value
| TL Value Value -- a tuple within a list
| L [Value]
deriving Show
deriving (Eq, Show)

type Header = Text
type Content = Map Key Value

data Entry = Entry
{ header :: Header
, content :: Content
} deriving Show
} deriving (Eq, Show)
49 changes: 49 additions & 0 deletions test/DConfTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}

module DConfTest
( dconfParserTests
)
where

import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Text ( Text )
import DConf ( dconfParser )
import DConf.Data
import Hedgehog
import Text.Parsec ( runParser )

prop_simple_parser :: Property
prop_simple_parser = withTests (100 :: TestLimit) simpleParser

prop_file_parser :: Property
prop_file_parser = withTests (10 :: TestLimit) fileParser

simpleParser :: Property
simpleParser =
let entries = runParser (dconfParser Normal) () "<test>" testInput
in property $ entries === Right [testOutput]

fileParser :: Property
fileParser = property $ do
input <- evalIO $ T.readFile "data/dconf.settings"
entries <- evalEither $ runParser (dconfParser Normal) () "<test>" input
length entries === 64

testInput :: Text
testInput = T.unlines
[ "[ org/gnome/desktop/peripherals/mouse ]"
, "natural-scroll=false"
, "speed=-0.5"
]

testOutput :: Entry
testOutput = Entry
{ header = "org/gnome/desktop/peripherals/mouse"
, content = M.fromList
[(Key "natural-scroll", B False), (Key "speed", D (-0.5))]
}

dconfParserTests :: Group
dconfParserTests = $$(discover)
11 changes: 11 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Main where

import Control.Monad ( unless )
import DConfTest
import Hedgehog
import System.Exit

main :: IO ()
main = do
results <- sequence [checkParallel dconfParserTests]
unless (and results) exitFailure

0 comments on commit b2e650d

Please sign in to comment.