Compare commits

..

3 Commits

Author SHA1 Message Date
98f732dbd2 Test the encoding and decoding 2020-12-31 08:51:37 +02:00
213e94c896 Get a proper working directory 2020-12-31 08:31:31 +02:00
a05a41efe8 Add hlint to the shell 2020-12-31 08:24:31 +02:00
7 changed files with 61 additions and 2 deletions

View File

@ -7,6 +7,9 @@ import Control.Monad.Buuka
import Data.Environment import Data.Environment
import UnliftIO.Directory
(XdgDirectory(XdgData), getXdgDirectory)
import qualified Operations import qualified Operations
commands :: Parser (BuukaM ()) commands :: Parser (BuukaM ())
@ -19,5 +22,5 @@ commands = subparser
main :: IO () main :: IO ()
main = do main = do
let env = Environment "." env <- Environment <$> getXdgDirectory XdgData "buuka"
execParser (info (commands <**> helper) fullDesc) >>= runBuukaM env execParser (info (commands <**> helper) fullDesc) >>= runBuukaM env

View File

@ -61,11 +61,13 @@ executable buuka
-- other-extensions: -- other-extensions:
build-depends: buuka build-depends: buuka
, optparse-applicative , optparse-applicative
, unliftio
hs-source-dirs: app hs-source-dirs: app
test-suite buuka-test test-suite buuka-test
import: common-stanza import: common-stanza
other-modules: Test.Database.Migrations other-modules: Test.Database.Migrations
Test.Data.Buuka
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
hs-source-dirs: test hs-source-dirs: test
main-is: MyLibTest.hs main-is: MyLibTest.hs

View File

@ -13,7 +13,7 @@ mkDerivation {
aeson base bytestring conduit conduit-extra containers exceptions aeson base bytestring conduit conduit-extra containers exceptions
filepath mtl transformers unliftio yaml filepath mtl transformers unliftio yaml
]; ];
executableHaskellDepends = [ base optparse-applicative ]; executableHaskellDepends = [ base optparse-applicative unliftio ];
testHaskellDepends = [ testHaskellDepends = [
aeson base hedgehog hedgehog-corpus tasty tasty-hedgehog text aeson base hedgehog hedgehog-corpus tasty tasty-hedgehog text
]; ];

View File

@ -8,6 +8,7 @@ in
mkShell { mkShell {
name = "shell-buuka"; name = "shell-buuka";
buildInputs = [ buildInputs = [
hlint
ghcid ghcid
stylish-haskell stylish-haskell
cabal2nix cabal2nix

View File

@ -5,6 +5,7 @@ module Data.Buuka
, BuukaU(..) , BuukaU(..)
, BuukaEntry(..) , BuukaEntry(..)
, URL(..) , URL(..)
, Buuka
, insert , insert
) )
@ -38,6 +39,7 @@ instance SafeJSON BuukaEntry where
type Version BuukaEntry = 0 type Version BuukaEntry = 0
newtype Buuka = Buuka ( Map URL BuukaEntry ) newtype Buuka = Buuka ( Map URL BuukaEntry )
deriving stock (Show, Eq)
deriving newtype (Semigroup, Monoid, FromJSON, ToJSON) deriving newtype (Semigroup, Monoid, FromJSON, ToJSON)
insert :: BuukaEntry -> Buuka -> Buuka insert :: BuukaEntry -> Buuka -> Buuka

View File

@ -2,11 +2,13 @@ module Main (main) where
import Test.Tasty import Test.Tasty
import qualified Test.Data.Buuka as Data.Buuka
import qualified Test.Database.Migrations as Database.Migrations import qualified Test.Database.Migrations as Database.Migrations
tests :: TestTree tests :: TestTree
tests = testGroup "buuka" tests = testGroup "buuka"
[ Database.Migrations.tests [ Database.Migrations.tests
, Data.Buuka.tests
] ]
main :: IO () main :: IO ()

49
test/Test/Data/Buuka.hs Normal file
View File

@ -0,0 +1,49 @@
module Test.Data.Buuka where
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Tasty
import Test.Tasty.Hedgehog
import qualified Data.Aeson as A
import Data.List
(intercalate)
import qualified Data.Foldable as F
import Data.Buuka
genUrl :: Gen URL
genUrl = URL . concat <$> sequence go
where
go = [ Gen.element protocols, Gen.element domains, Gen.element tlds, pure "/", genPath ]
protocols = ["http://", "https://"]
domains = ["example", "foo", "bar"]
tlds = ["com", "fi", "org", "net", "info"]
genPath = intercalate "/" <$> Gen.list (Range.linear 0 10) (Gen.element paths)
paths = ["foo", "bar", "asd", "xyzzy"]
genBuukaEntry :: Gen BuukaEntry
genBuukaEntry = BuukaEntry <$> genUrl <*> genTitle
where
genTitle = Gen.maybe (Gen.string (Range.linear 0 10) Gen.unicode)
genBuuka :: Gen Buuka
genBuuka = F.foldl' (flip insert) mempty <$> Gen.list (Range.linear 0 10) genBuukaEntry
prop_buukaentry_encode_decode :: Property
prop_buukaentry_encode_decode = property $ do
x <- forAll genBuukaEntry
tripping x A.encode A.eitherDecode
prop_buuka_encode_decode :: Property
prop_buuka_encode_decode = property $ do
x <- forAll genBuuka
tripping x A.encode A.eitherDecode
tests :: TestTree
tests = testGroup "Data.Buuka"
[ testProperty "Entries can be encoded and decoded" prop_buukaentry_encode_decode
, testProperty "Entire set can be encoded and decoded" prop_buuka_encode_decode
]