1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
{-|
Module : FromValueSpec
Description : Exercise various components of FromValue
Copyright : (c) Eric Mertens, 2023
License : ISC
Maintainer : emertens@gmail.com
-}
module FromValueSpec (spec) where
import Control.Applicative ((<|>), empty)
import Control.Monad (when)
import Test.Hspec (it, shouldBe, Spec)
import Toml (Result(..), Value(..))
import Toml.FromValue (FromValue(fromValue), optKey, reqKey, warnTable, pickKey, runParseTable)
import Toml.FromValue.Matcher (Matcher, runMatcher)
import Toml.FromValue.ParseTable (KeyAlt(..))
import Toml.Pretty (prettyMatchMessage)
import Toml.ToValue (table, (.=))
humanMatcher :: Matcher a -> Result String a
humanMatcher m =
case runMatcher m of
Failure e -> Failure (prettyMatchMessage <$> e)
Success w x -> Success (prettyMatchMessage <$> w) x
spec :: Spec
spec =
do it "handles one reqKey" $
humanMatcher (runParseTable (reqKey "test") (table ["test" .= "val"]))
`shouldBe`
Success [] "val"
it "handles one optKey" $
humanMatcher (runParseTable (optKey "test") (table ["test" .= "val"]))
`shouldBe`
Success [] (Just "val")
it "handles one missing optKey" $
humanMatcher (runParseTable (optKey "test") (table ["nottest" .= "val"]))
`shouldBe`
Success ["unexpected key: nottest in top"] (Nothing :: Maybe String)
it "handles one missing reqKey" $
humanMatcher (runParseTable (reqKey "test") (table ["nottest" .= "val"]))
`shouldBe`
(Failure ["missing key: test in top"] :: Result String String)
it "handles one mismatched reqKey" $
humanMatcher (runParseTable (reqKey "test") (table ["test" .= "val"]))
`shouldBe`
(Failure ["type error. wanted: integer got: string in top.test"] :: Result String Integer)
it "handles one mismatched optKey" $
humanMatcher (runParseTable (optKey "test") (table ["test" .= "val"]))
`shouldBe`
(Failure ["type error. wanted: integer got: string in top.test"] :: Result String (Maybe Integer))
it "handles concurrent errors" $
humanMatcher (runParseTable (reqKey "a" <|> empty <|> reqKey "b") (table []))
`shouldBe`
(Failure ["missing key: a in top", "missing key: b in top"] :: Result String Integer)
it "handles concurrent value mismatch" $
let v = String "" in
humanMatcher (Left <$> fromValue v <|> empty <|> Right <$> fromValue v)
`shouldBe`
(Failure [
"type error. wanted: boolean got: string in top",
"type error. wanted: integer got: string in top"]
:: Result String (Either Bool Int))
it "doesn't emit an error for empty" $
humanMatcher (runParseTable empty (table []))
`shouldBe`
(Failure [] :: Result String Integer)
it "matches single characters" $
runMatcher (fromValue (String "x"))
`shouldBe`
Success [] 'x'
it "rejections non-single characters" $
humanMatcher (fromValue (String "xy"))
`shouldBe`
(Failure ["type error. wanted: character got: string in top"] :: Result String Char)
it "collects warnings in table matching" $
let pt =
do i1 <- reqKey "k1"
i2 <- reqKey "k2"
let n = i1 + i2
when (odd n) (warnTable "k1 and k2 sum to an odd value")
pure n
in
humanMatcher (runParseTable pt (table ["k1" .= (1 :: Integer), "k2" .= (2 :: Integer)]))
`shouldBe`
Success ["k1 and k2 sum to an odd value in top"] (3 :: Integer)
it "offers helpful messages when no keys match" $
let pt = pickKey [Key "this" \_ -> pure 'a', Key "." \_ -> pure 'b']
in
humanMatcher (runParseTable pt (table []))
`shouldBe`
(Failure ["possible keys: this, \".\" in top"] :: Result String Char)
it "generates an error message on an empty pickKey" $
let pt = pickKey []
in
humanMatcher (runParseTable pt (table []))
`shouldBe`
(Failure [] :: Result String Char)
|