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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
{-# Language OverloadedStrings #-}
{-|
Module : Main
Description : Unit tests for config-schema
Copyright : (c) Eric Mertens, 2017
License : ISC
Maintainer : emertens@gmail.com
-}
module Main (main) where
import Config
import Config.Schema
import Control.Applicative
import Data.Foldable
import Data.Text (Text)
import qualified Data.Text as Text
-- tests that are expected to pass.
--
-- The input sources are a list of lists of lines. Each outer list
-- element contains a list of lines representing a complete input
-- source. Each of these variations must pass the test.
test ::
Show a =>
Eq a =>
ValueSpec a {- ^ specification to match -} ->
a {- ^ expected output -} ->
[[Text]] {- ^ inputs sources -} ->
IO ()
test spec expected txtss =
for_ txtss $ \txts ->
case parse (Text.unlines txts) of
Left e -> fail (show e)
Right v ->
case loadValue spec v of
Left e -> fail (show e)
Right x | x == expected -> return ()
| otherwise -> fail ("Got " ++ show x ++ " but expected " ++
show expected)
main :: IO ()
main = sequenceA_
[ test anySpec ("Hello world"::Text)
[["\"Hello world\""]
,["\"H\\101l\\&l\\o157 \\"
," \\w\\x6frld\""]
]
, test anySpec (1234::Integer)
[["1234"]
,["1234.0"]
]
, test anySpec (0.65::Rational)
[["0.65e0"]
,["65e-2"]
,["6.5e-1"]
,["0.65"]
]
, test anyAtomSpec "default"
[["default"]]
, test (atomSpec "testing-1-2-3") ()
[["testing-1-2-3"]]
, test (listSpec anySpec) ([]::[Integer])
[["[]"]
,["[ ]"]]
, test (listSpec anyAtomSpec) ["ḿyatoḿ"]
[["[ḿyatoḿ]"]
,[" [ ḿyatoḿ ] "]
,["* ḿyatoḿ"]
]
, test anySpec [1,2,3::Int]
[["[1,2,3]"]
,["[1,2,3,]"]
,["* 1"
,"* 2"
,"* 3"]
]
, test (listSpec anySpec) [[1,2],[3,4::Int]]
[["[[1,2,],[3,4]]"]
,["*[1,2]"
,"*[3,4]"]
,["**1"
," *2"
,"* *3"
," *4"
]
]
, test (assocSpec anySpec) ([]::[(Text,Int)])
[["{}"]
,["{ }"]
]
, test (assocSpec anySpec) [("k1",10::Int), ("k2",20)]
[["{k1: 10, k2: 20}"]
,["{k1: 10, k2: 20,}"]
,["k1 : 10"
,"k2: 20"]
]
, test anySpec [ Left (1::Int), Right ("two"::Text) ]
[["[1, \"two\"]"]
,["* 1"
,"* \"two\""]
]
, test (sectionsSpec "test"
(liftA2 (,) (reqSection "k1" "") (reqSection "k2" "")))
(10 :: Int, 20 :: Int)
[["k1: 10"
,"k2: 20"]
,["k2: 20"
,"k1: 10"]
]
, test (sectionsSpec "test"
(liftA2 (,) (optSection "k1" "") (reqSection "k2" "")))
(Just 10 :: Maybe Int, 20 :: Int)
[["k1: 10"
,"k2: 20"]
,["k2: 20"
,"k1: 10"]
]
, test (sectionsSpec "test"
(liftA2 (,) (optSection "k1" "") (reqSection "k2" "")))
(Nothing :: Maybe Int, 20 :: Int)
[["k2: 20"]
,["{k2: 20}"]
]
-- This isn't a good idea, but it currently works
, test (sectionsSpec "test"
(liftA2 (,) (reqSection "k1" "") (reqSection "k1" "")))
("first"::Text, 50::Int)
[["k1: \"first\""
,"k1: 50"]
]
, test (sectionsSpec "test" (pure ())) ()
[["{}"]
]
]
|