Skip to content

Commit

Permalink
[#318] Fix roundtrip test for Key (#321)
Browse files Browse the repository at this point in the history
Resolves #318
  • Loading branch information
chshersh committed May 19, 2020
1 parent 156f2db commit c1b8881
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ The changelog is available [on GitHub][2].
Add tests on all kinds of `TomlDecodeError` with `decode` function.
* [#313](https://github.com/kowainik/tomland/issues/313):
Store `Key` in the `BiMapError` constructor of `TomlDecodeError`.
* [#318](https://github.com/kowainik/tomland/issues/318):
Export a function for parsing TOML keys.
* [#311](https://github.com/kowainik/tomland/issues/311):
Reimplement custom `TomlState` instead of using `MaybeT` and `State`.

Expand Down
15 changes: 4 additions & 11 deletions src/Toml/Codec/BiMap/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import Text.Read (readEither)

import Toml.Codec.BiMap (BiMap (..), TomlBiMap, TomlBiMapError (..), iso, mkAnyValueBiMap, prism,
tShow, wrongConstructor)
import Toml.Parser.Key (keyP)
import Toml.Parser (TomlParseError (..), parseKey)
import Toml.Type.AnyValue (AnyValue (..), applyAsToAny, matchBool, matchDay, matchDouble,
matchHours, matchInteger, matchLocal, matchText, matchZoned,
mkMatchError, toMArray)
Expand All @@ -118,8 +118,6 @@ import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL

import qualified Toml.Parser.Core as P (errorBundlePretty, parse)

----------------------------------------------------------------------------
-- Primitive
----------------------------------------------------------------------------
Expand Down Expand Up @@ -564,7 +562,7 @@ _EnumBounded = _EnumBoundedText >>> _Text
_KeyText :: TomlBiMap Key Text
_KeyText = BiMap
{ forward = Right . prettyKey
, backward = textToKey
, backward = first (ArbitraryError . unTomlParseError) . parseKey
}

{- | Bidirectional converter between 'Key' and 'String'. Usually used
Expand All @@ -575,7 +573,7 @@ as an argument for 'Toml.Codec.Combinator.Map.tableMap'.
_KeyString :: TomlBiMap Key String
_KeyString = BiMap
{ forward = Right . T.unpack . prettyKey
, backward = textToKey . T.pack
, backward = first (ArbitraryError . unTomlParseError) . parseKey . T.pack
}

{- | Bidirectional converter between 'Key' and 'Int'. Usually used
Expand All @@ -586,14 +584,9 @@ as an argument for 'Toml.Codec.Combinator.Map.tableIntMap'.
_KeyInt :: TomlBiMap Key Int
_KeyInt = BiMap
{ forward = first (ArbitraryError . T.pack) . readEither . T.unpack . prettyKey
, backward = textToKey . tShow
, backward = first (ArbitraryError . unTomlParseError) . parseKey . tShow
}

textToKey :: Text -> Either TomlBiMapError Key
textToKey t = case P.parse keyP "" t of
Left err -> Left $ ArbitraryError $ T.pack $ P.errorBundlePretty err
Right key -> Right key

----------------------------------------------------------------------------
-- General purpose bimaps
----------------------------------------------------------------------------
Expand Down
14 changes: 13 additions & 1 deletion src/Toml/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ Parser for text to TOML AST.
module Toml.Parser
( TomlParseError (..)
, parse
, parseKey
) where

import Control.DeepSeq (NFData)
import Data.Text (Text)
import GHC.Generics (Generic)

import Toml.Parser.Item (tomlP)
import Toml.Parser.Key (keyP)
import Toml.Parser.Validate (validateItems)
import Toml.Type (TOML)
import Toml.Type.Key (Key)
import Toml.Type.TOML (TOML)

import qualified Data.Text as T
import qualified Toml.Parser.Core as P (errorBundlePretty, parse)
Expand All @@ -46,3 +49,12 @@ parse t = case P.parse tomlP "" t of
Right items -> case validateItems items of
Left err -> Left $ TomlParseError $ T.pack $ show err
Right toml -> Right toml

{- | Parse TOML 'Key' from 'Text'.
@since 1.3.0.0
-}
parseKey :: Text -> Either TomlParseError Key
parseKey t = case P.parse keyP "" t of
Left err -> Left $ TomlParseError $ T.pack $ P.errorBundlePretty err
Right key -> Right key
2 changes: 2 additions & 0 deletions test/Test/Toml/Parser/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ keySpecs = describe "keyP" $ do
parseKey "_" "_"
parseKey "__" "__"
parseKey "___" "___"
it "parses quotes" $
parseKey "\".\"" $ "\".\"" :|| []
5 changes: 2 additions & 3 deletions test/Test/Toml/Type/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ module Test.Toml.Type.Key
( keySpec
) where

import Data.String (IsString (..))
import Hedgehog (forAll, tripping)
import Test.Hspec (Arg, Expectation, Spec, SpecWith, describe, it, shouldBe)
import Test.Hspec.Hedgehog (hedgehog)

import Test.Toml.Gen (genKey)
import Toml.Type.Key (KeysDiff (..), keysDiff)

import qualified Data.Text as Text
import qualified Toml.Parser as Parser
import qualified Toml.Type.Printer as Printer


Expand All @@ -22,7 +21,7 @@ keySpec = describe "TOML Key" $ do
keyRoundtripSpec :: SpecWith (Arg Expectation)
keyRoundtripSpec = it "Key printing: fromString . prettyKey ≡ id" $ hedgehog $ do
key <- forAll genKey
tripping key Printer.prettyKey (Just . fromString . Text.unpack)
tripping key Printer.prettyKey Parser.parseKey

keysDiffSpec :: Spec
keysDiffSpec = describe "Key difference" $ do
Expand Down

0 comments on commit c1b8881

Please sign in to comment.