2019-09-27 00:28:11 +03:00
|
|
|
module HoonMapSetTests (tests) where
|
|
|
|
|
2019-09-27 02:03:25 +03:00
|
|
|
import RIO.Directory
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude hiding (encodeUtf8)
|
2019-09-27 00:28:11 +03:00
|
|
|
|
2019-09-27 21:09:10 +03:00
|
|
|
import Data.Text.Lazy.Encoding (encodeUtf8)
|
|
|
|
import Numeric.Natural (Natural)
|
|
|
|
import Test.QuickCheck hiding ((.&.))
|
2019-09-27 00:28:11 +03:00
|
|
|
import Test.Tasty
|
2019-09-27 21:09:10 +03:00
|
|
|
import Test.Tasty.Golden as G
|
2019-09-27 00:28:11 +03:00
|
|
|
import Test.Tasty.QuickCheck
|
|
|
|
import Test.Tasty.TH
|
|
|
|
|
2019-09-27 21:09:10 +03:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2019-09-27 00:28:11 +03:00
|
|
|
|
2019-09-27 02:03:25 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
2019-09-27 00:28:11 +03:00
|
|
|
|
|
|
|
newtype SmallNoun = SN Noun
|
|
|
|
deriving newtype (Eq, Ord, Show, ToNoun)
|
|
|
|
|
|
|
|
instance Arbitrary SmallNoun where
|
|
|
|
arbitrary = SN <$> oneof [a, c, ac, ca, cc]
|
|
|
|
where
|
2019-09-27 02:03:25 +03:00
|
|
|
a = A . fromIntegral <$> arbitrary @Word8
|
2019-09-27 00:28:11 +03:00
|
|
|
c = C <$> a <*> a
|
|
|
|
ac = C <$> a <*> c
|
|
|
|
ca = C <$> c <*> a
|
|
|
|
cc = C <$> c <*> c
|
|
|
|
|
2019-09-27 02:03:25 +03:00
|
|
|
data TreeTest
|
|
|
|
= TTMap (HoonMap Noun Noun)
|
|
|
|
| TTSet (HoonSet Noun)
|
|
|
|
|
|
|
|
deriveNoun ''TreeTest
|
|
|
|
|
|
|
|
type TreeTests = [TreeTest]
|
|
|
|
|
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
roundTrip :: ∀a. Eq a => (a -> a) -> a -> Bool
|
|
|
|
roundTrip f x = f x == x
|
|
|
|
|
|
|
|
|
2019-09-27 00:28:11 +03:00
|
|
|
-- Props -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
mapRoundtrip :: Map SmallNoun SmallNoun -> Bool
|
|
|
|
mapRoundtrip = roundTrip (mapFromHoonMap . mapToHoonMap)
|
|
|
|
|
|
|
|
setRoundtrip :: Set SmallNoun -> Bool
|
|
|
|
setRoundtrip = roundTrip (setFromHoonSet . setToHoonSet)
|
|
|
|
|
|
|
|
|
2019-09-27 02:03:25 +03:00
|
|
|
-- Golden Tests ----------------------------------------------------------------
|
|
|
|
|
|
|
|
treeTestsIdentity :: TreeTests -> TreeTests
|
|
|
|
treeTestsIdentity = fmap go
|
|
|
|
where
|
|
|
|
go = \case
|
|
|
|
TTSet s -> (TTSet . setToHoonSet . setFromHoonSet) s
|
|
|
|
TTMap m -> (TTMap . mapToHoonMap . mapFromHoonMap) m
|
|
|
|
|
2019-09-27 21:09:10 +03:00
|
|
|
treeRTMug :: FilePath -> IO L.ByteString
|
|
|
|
treeRTMug inp = do
|
2019-09-27 02:03:25 +03:00
|
|
|
byt <- readFile inp
|
|
|
|
non <- cueBSExn byt
|
|
|
|
tee <- fromNounExn non
|
|
|
|
mug <- evaluate $ mug $ toNoun $ treeTestsIdentity tee
|
2019-09-27 21:09:10 +03:00
|
|
|
pure $ encodeUtf8 $ tlshow (mug :: Natural)
|
2019-09-27 02:03:25 +03:00
|
|
|
|
|
|
|
|
2019-09-27 21:09:10 +03:00
|
|
|
goldenFile :: String -> String -> (FilePath -> IO L.ByteString) -> TestTree
|
2019-09-27 02:03:25 +03:00
|
|
|
goldenFile testName testFileName action =
|
2019-09-27 21:09:10 +03:00
|
|
|
goldenVsString testName gold (action pill)
|
2019-09-27 02:03:25 +03:00
|
|
|
where
|
2020-01-24 08:20:43 +03:00
|
|
|
root = "pkg/hs/urbit-king/test/gold" </> testFileName
|
2019-09-27 02:03:25 +03:00
|
|
|
gold = root <.> "gold"
|
|
|
|
pill = root <.> "pill"
|
|
|
|
|
|
|
|
|
|
|
|
-- Test Tree -------------------------------------------------------------------
|
2019-09-27 00:28:11 +03:00
|
|
|
|
|
|
|
tests :: TestTree
|
|
|
|
tests =
|
|
|
|
testGroup "Map/Set Conversions"
|
2019-09-27 02:03:25 +03:00
|
|
|
[ goldenFile "Golden Map Roundtrip" "hoontree" treeRTMug
|
|
|
|
, testProperty "Map Rountrip" mapRoundtrip
|
2019-09-27 00:28:11 +03:00
|
|
|
, testProperty "Set Rountrip" setRoundtrip
|
|
|
|
]
|