Skip to content

Commit

Permalink
[#334] parse and unparse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dariodsa committed Nov 14, 2020
1 parent a0a188f commit f922d9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion test/Test/Toml/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Test.Toml.Parser.Array (arraySpecs)
import Test.Toml.Parser.Bool (boolSpecs)
import Test.Toml.Parser.Date (dateSpecs)
import Test.Toml.Parser.Double (doubleSpecs)
import Test.Toml.Parser.Examples (examplesSpec)
import Test.Toml.Parser.Examples (examplesSpec, parseUnparseSpec)
import Test.Toml.Parser.Integer (integerSpecs)
import Test.Toml.Parser.Key (keySpecs)
import Test.Toml.Parser.Property (propertySpec)
Expand All @@ -20,6 +20,7 @@ import Test.Toml.Parser.Validate (validateSpec)
parserSpec :: Spec
parserSpec = describe "Parser for TOML" $ do
examplesSpec
parseUnparseSpec
propertySpec
validateSpec

Expand Down
17 changes: 16 additions & 1 deletion test/Test/Toml/Parser/Examples.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Test.Toml.Parser.Examples
( examplesSpec
, parseUnparseSpec
, parseUnparse
) where

import Data.Either (isRight)
import System.Directory (listDirectory)
import Test.Hspec (Spec, describe, it, runIO, shouldBe)

import Toml.Parser (parse)
import Toml.Type.Printer (pretty)

import qualified Data.Text.IO as TIO


examplesSpec :: Spec
examplesSpec = describe "Can parse official TOML examples" $ do
files <- runIO $ listDirectory exampleDir
Expand All @@ -21,5 +23,18 @@ example file = it ("can parse file " ++ file) $ do
toml <- TIO.readFile (exampleDir ++ file)
isRight (parse toml) `shouldBe` True

parseUnparseSpec :: Spec
parseUnparseSpec = describe "Can get same object after parse and unparse" $ do
files <- runIO $ listDirectory exampleDir
mapM_ parseUnparse files

parseUnparse :: FilePath -> Spec
parseUnparse file = it ("it will produce same text after parsing and unparsing " ++ file) $ do
toml <- TIO.readFile (exampleDir ++ file)
case parse toml of
Left _ -> error "File can't be parsed"
Right toml_obj -> (parse toml == ( parse $ pretty $ toml_obj)) `shouldBe` True

exampleDir :: FilePath
exampleDir = "test/examples/"

0 comments on commit f922d9a

Please sign in to comment.