From 044de639b8c34285f24e244378c9116b0a86b7e6 Mon Sep 17 00:00:00 2001 From: Aleksey Pirogov Date: Thu, 4 Oct 2018 13:00:40 +0300 Subject: [PATCH] Add golden tests for pretty printer (#103) * Add golden tests for pretty printer * Rework example TOML in tests using EDSL, export defaultOptions --- .gitattributes | 1 + src/Toml/PrefixTree.hs | 1 + src/Toml/Printer.hs | 16 ++++----- test/Test/Toml/Printer/Golden.hs | 44 +++++++++++++++++++++++++ test/golden/pretty_default.golden | 14 ++++++++ test/golden/pretty_indented_only.golden | 14 ++++++++ test/golden/pretty_sorted_only.golden | 14 ++++++++ test/golden/pretty_unformatted.golden | 14 ++++++++ tomland.cabal | 2 ++ 9 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 .gitattributes create mode 100644 test/Test/Toml/Printer/Golden.hs create mode 100644 test/golden/pretty_default.golden create mode 100644 test/golden/pretty_indented_only.golden create mode 100644 test/golden/pretty_sorted_only.golden create mode 100644 test/golden/pretty_unformatted.golden diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..b474ab7d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.golden -text diff --git a/src/Toml/PrefixTree.hs b/src/Toml/PrefixTree.hs index ba947039..30c9bc3c 100644 --- a/src/Toml/PrefixTree.hs +++ b/src/Toml/PrefixTree.hs @@ -2,6 +2,7 @@ module Toml.PrefixTree ( PrefixTree (..) + , (<|) , singleT , insertT , lookupT diff --git a/src/Toml/Printer.hs b/src/Toml/Printer.hs index 30ca8102..fb84f55f 100644 --- a/src/Toml/Printer.hs +++ b/src/Toml/Printer.hs @@ -4,6 +4,7 @@ module Toml.Printer ( PrintOptions(..) + , defaultOptions , pretty , prettyOptions , prettyTomlInd @@ -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 } @ diff --git a/test/Test/Toml/Printer/Golden.hs b/test/Test/Toml/Printer/Golden.hs new file mode 100644 index 00000000..2114dec6 --- /dev/null +++ b/test/Test/Toml/Printer/Golden.hs @@ -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 diff --git a/test/golden/pretty_default.golden b/test/golden/pretty_default.golden new file mode 100644 index 00000000..3cbae1f9 --- /dev/null +++ b/test/golden/pretty_default.golden @@ -0,0 +1,14 @@ +a = "a" +b = "bb" +c = "cccc" +d = "ddd" + +[baz] + +[doo] + +[foo] + +[qux.doo] + egg = "?" + spam = "!" diff --git a/test/golden/pretty_indented_only.golden b/test/golden/pretty_indented_only.golden new file mode 100644 index 00000000..96ed251d --- /dev/null +++ b/test/golden/pretty_indented_only.golden @@ -0,0 +1,14 @@ +a = "a" +c = "cccc" +d = "ddd" +b = "bb" + +[doo] + +[foo] + +[baz] + +[qux.doo] + egg = "?" + spam = "!" diff --git a/test/golden/pretty_sorted_only.golden b/test/golden/pretty_sorted_only.golden new file mode 100644 index 00000000..2b9667f8 --- /dev/null +++ b/test/golden/pretty_sorted_only.golden @@ -0,0 +1,14 @@ +a = "a" +b = "bb" +c = "cccc" +d = "ddd" + +[baz] + +[doo] + +[foo] + +[qux.doo] +egg = "?" +spam = "!" diff --git a/test/golden/pretty_unformatted.golden b/test/golden/pretty_unformatted.golden new file mode 100644 index 00000000..05816d79 --- /dev/null +++ b/test/golden/pretty_unformatted.golden @@ -0,0 +1,14 @@ +a = "a" +c = "cccc" +d = "ddd" +b = "bb" + +[doo] + +[foo] + +[baz] + +[qux.doo] +egg = "?" +spam = "!" diff --git a/tomland.cabal b/tomland.cabal index ef098f06..185f27b9 100644 --- a/tomland.cabal +++ b/tomland.cabal @@ -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 @@ -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