Skip to content

Commit

Permalink
Add golden tests for pretty printer (#103)
Browse files Browse the repository at this point in the history
* Add golden tests for pretty printer

* Rework example TOML in tests using EDSL, export defaultOptions
  • Loading branch information
astynax authored and vrom911 committed Oct 4, 2018
1 parent 384d840 commit 044de63
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.golden -text
1 change: 1 addition & 0 deletions src/Toml/PrefixTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Toml.PrefixTree
( PrefixTree (..)
, (<|)
, singleT
, insertT
, lookupT
Expand Down
16 changes: 8 additions & 8 deletions src/Toml/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Toml.Printer
( PrintOptions(..)
, defaultOptions
, pretty
, prettyOptions
, prettyTomlInd
Expand Down Expand Up @@ -49,16 +50,15 @@ For example, this
@
TOML
{ tomlPairs = HashMap.fromList [(Key "title", String "TOML example")]
, tomlTables = HashMap.fromList
[( TableId (NonEmpty.fromList ["example", "owner"])
, TOML
{ tomlPairs = HashMap.fromList [(Key "name", String "Kowainik")]
, tomlTables = mempty
, tomlTableArrays = mempty
{ tomlPairs = HashMap.fromList
[("title", AnyValue $ Text "TOML example")]
, tomlTables = PrefixTree.fromList
[( "example" <| "owner"
, mempty
{ tomlPairs = HashMap.fromList
[("name", AnyValue $ Text "Kowainik")]
}
)]
, tomlTableArrays = mempty
}
@
Expand Down
44 changes: 44 additions & 0 deletions test/Test/Toml/Printer/Golden.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- | This module contains golden tests for @Toml.Printer@.

module Test.Toml.Printer.Golden
( test_prettyGolden
) where

import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Silver (goldenVsAction)
import Toml (TOML)
import Toml.Edsl ((=:), mkToml, table)
import Toml.PrefixTree ((<|))
import Toml.Printer (PrintOptions (..), defaultOptions, prettyOptions)

example :: TOML
example = mkToml $ do
"b" =: "bb"
"a" =: "a"
"d" =: "ddd"
"c" =: "cccc"
table ("qux" <| "doo") $ do
"spam" =: "!"
"egg" =: "?"
table "foo" $ pure ()
table "doo" $ pure ()
table "baz" $ pure ()

noFormatting :: PrintOptions
noFormatting = PrintOptions
{ shouldSort = False
, indent = 0
}

test_prettyGolden :: TestTree
test_prettyGolden =
testGroup "Toml.Printer"
[ test "pretty_default" defaultOptions
, test "pretty_sorted_only" noFormatting{ shouldSort = True}
, test "pretty_indented_only" noFormatting{ indent = 4 }
, test "pretty_unformatted" noFormatting
]
where
test name options =
goldenVsAction name ("test/golden/" ++ name ++ ".golden")
(pure $ prettyOptions options example) id
14 changes: 14 additions & 0 deletions test/golden/pretty_default.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a = "a"
b = "bb"
c = "cccc"
d = "ddd"

[baz]

[doo]

[foo]

[qux.doo]
egg = "?"
spam = "!"
14 changes: 14 additions & 0 deletions test/golden/pretty_indented_only.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a = "a"
c = "cccc"
d = "ddd"
b = "bb"

[doo]

[foo]

[baz]

[qux.doo]
egg = "?"
spam = "!"
14 changes: 14 additions & 0 deletions test/golden/pretty_sorted_only.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a = "a"
b = "bb"
c = "cccc"
d = "ddd"

[baz]

[doo]

[foo]

[qux.doo]
egg = "?"
spam = "!"
14 changes: 14 additions & 0 deletions test/golden/pretty_unformatted.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
a = "a"
c = "cccc"
d = "ddd"
b = "bb"

[doo]

[foo]

[baz]

[qux.doo]
egg = "?"
spam = "!"
2 changes: 2 additions & 0 deletions tomland.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ test-suite tomland-test
Test.Toml.Parsing.Unit
Test.Toml.PrefixTree.Property
Test.Toml.PrefixTree.Unit
Test.Toml.Printer.Golden
Test.Toml.TOML.Property

build-tool-depends: tasty-discover:tasty-discover ^>= 4.2.1
Expand All @@ -97,6 +98,7 @@ test-suite tomland-test
, tasty ^>= 1.1.0.3
, tasty-hedgehog ^>= 0.2.0.0
, tasty-hspec ^>= 1.1
, tasty-silver ^>= 3.1.11
, text
, time
, tomland
Expand Down

0 comments on commit 044de63

Please sign in to comment.