2019-07-21 02:47:35 +03:00
|
|
|
module DeriveNounTests (tests) where
|
|
|
|
|
|
|
|
import Data.Acquire
|
2020-01-23 07:16:09 +03:00
|
|
|
import Data.Conduit
|
|
|
|
import Data.Conduit.List
|
2019-07-21 02:47:35 +03:00
|
|
|
import Test.QuickCheck hiding ((.&.))
|
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.QuickCheck
|
|
|
|
import Test.Tasty.TH
|
2020-06-09 00:27:58 +03:00
|
|
|
import Urbit.EventLog.LMDB
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Prelude
|
|
|
|
import Urbit.Vere.Pier.Types
|
2019-07-21 02:47:35 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
import Control.Concurrent (runInBoundThread, threadDelay)
|
2019-07-21 02:47:35 +03:00
|
|
|
import Data.LargeWord (LargeKey(..))
|
|
|
|
import GHC.Natural (Natural)
|
|
|
|
|
2020-06-09 00:27:58 +03:00
|
|
|
import qualified Urbit.EventLog.LMDB as Log
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Sum Types -------------------------------------------------------------------
|
|
|
|
|
|
|
|
data Nums = One | Two | TwentyTwo | NineHundredNintyNine
|
|
|
|
deriving (Eq, Show, Enum, Bounded)
|
|
|
|
|
|
|
|
data ThreeWords = ThreeWords Word Word Word
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data FooBar = FooBarQueenAlice Word Word
|
|
|
|
| FooBarBob Word
|
2019-07-23 05:35:15 +03:00
|
|
|
| FooBarCharlie
|
2019-07-21 02:47:35 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data BarZaz = BZQueenAlice Word Word
|
|
|
|
| BZBob Word
|
2019-07-23 05:35:15 +03:00
|
|
|
| BZCharlie
|
2019-07-21 02:47:35 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data ZazBaz = QueenAlice Word Word
|
|
|
|
| Bob Word
|
2019-07-23 05:35:15 +03:00
|
|
|
| Charlie
|
2019-07-21 02:47:35 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2019-07-21 03:50:56 +03:00
|
|
|
data Empty
|
|
|
|
|
|
|
|
data Poly a b = PLeft a
|
|
|
|
| PRite b
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2019-07-21 02:47:35 +03:00
|
|
|
deriveNoun ''Nums
|
|
|
|
deriveNoun ''ThreeWords
|
|
|
|
deriveNoun ''FooBar
|
|
|
|
deriveNoun ''BarZaz
|
|
|
|
deriveNoun ''ZazBaz
|
2019-07-21 03:50:56 +03:00
|
|
|
deriveNoun ''Empty
|
|
|
|
deriveNoun ''Poly
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
instance Arbitrary ThreeWords where
|
|
|
|
arbitrary = ThreeWords <$> arbitrary <*> arbitrary <*> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary Nums where
|
|
|
|
arbitrary = oneof (pure <$> [ minBound .. maxBound ])
|
|
|
|
|
|
|
|
instance Arbitrary FooBar where
|
|
|
|
arbitrary = oneof [ FooBarQueenAlice <$> arbitrary <*> arbitrary
|
2019-07-23 05:35:15 +03:00
|
|
|
, FooBarBob <$> arbitrary
|
|
|
|
, pure FooBarCharlie
|
|
|
|
]
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
instance Arbitrary BarZaz where
|
|
|
|
arbitrary = oneof [ BZQueenAlice <$> arbitrary <*> arbitrary
|
2019-07-23 05:35:15 +03:00
|
|
|
, BZBob <$> arbitrary
|
|
|
|
, pure BZCharlie
|
|
|
|
]
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
instance Arbitrary ZazBaz where
|
|
|
|
arbitrary = oneof [ QueenAlice <$> arbitrary <*> arbitrary
|
2019-07-23 05:35:15 +03:00
|
|
|
, Bob <$> arbitrary
|
|
|
|
, pure Charlie
|
|
|
|
]
|
2019-07-21 02:47:35 +03:00
|
|
|
|
2019-07-21 03:50:56 +03:00
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (Poly a b) where
|
|
|
|
arbitrary = oneof [ PLeft <$> arbitrary
|
|
|
|
, PRite <$> arbitrary
|
|
|
|
]
|
|
|
|
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
roundTrip :: forall a. (Eq a, ToNoun a, FromNoun a) => a -> Bool
|
|
|
|
roundTrip x = Just x == fromNoun (toNoun x)
|
|
|
|
|
|
|
|
throughNoun :: (ToNoun a, FromNoun b) => a -> Maybe b
|
|
|
|
throughNoun = fromNoun . toNoun
|
|
|
|
|
|
|
|
nounEquiv :: (Eq a, Eq b, ToNoun a, ToNoun b, FromNoun a, FromNoun b)
|
|
|
|
=> (a -> b) -> a -> Bool
|
|
|
|
nounEquiv cvt x =
|
|
|
|
and [ Just x == throughNoun y
|
|
|
|
, Just y == throughNoun x
|
|
|
|
]
|
|
|
|
where y = cvt x
|
|
|
|
|
|
|
|
|
|
|
|
-- Sanity Checks ---------------------------------------------------------------
|
|
|
|
|
|
|
|
enumSanity :: Nums -> Bool
|
|
|
|
enumSanity x = toNoun x == byHand x
|
|
|
|
where
|
|
|
|
byHand = \case
|
|
|
|
One -> toNoun (Cord "one")
|
|
|
|
Two -> toNoun (Cord "two")
|
|
|
|
TwentyTwo -> toNoun (Cord "twenty-two")
|
|
|
|
NineHundredNintyNine -> toNoun (Cord "nine-hundred-ninty-nine")
|
|
|
|
|
|
|
|
recSanity :: ThreeWords -> Bool
|
|
|
|
recSanity x = toNoun x == byHand x
|
|
|
|
where
|
|
|
|
byHand (ThreeWords x y z) = toNoun (x, y, z)
|
|
|
|
|
|
|
|
sumSanity :: ZazBaz -> Bool
|
|
|
|
sumSanity x = toNoun x == byHand x
|
|
|
|
where
|
|
|
|
byHand = \case
|
|
|
|
QueenAlice x y -> toNoun (Cord "queen-alice", x, y)
|
|
|
|
Bob x -> toNoun (Cord "bob", x)
|
2019-07-23 05:35:15 +03:00
|
|
|
Charlie -> toNoun (Cord "charlie")
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
abbrPrefixSanity :: BarZaz -> Bool
|
|
|
|
abbrPrefixSanity x = toNoun x == byHand x
|
|
|
|
where
|
|
|
|
byHand = \case
|
|
|
|
BZQueenAlice x y -> toNoun (Cord "queen-alice", x, y)
|
|
|
|
BZBob x -> toNoun (Cord "bob", x)
|
2019-07-23 05:35:15 +03:00
|
|
|
BZCharlie -> toNoun (Cord "charlie")
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
typePrefixSanity :: FooBar -> Bool
|
|
|
|
typePrefixSanity x = toNoun x == byHand x
|
|
|
|
where
|
|
|
|
byHand = \case
|
|
|
|
FooBarQueenAlice x y -> toNoun (Cord "queen-alice", x, y)
|
|
|
|
FooBarBob x -> toNoun (Cord "bob", x)
|
2019-07-23 05:35:15 +03:00
|
|
|
FooBarCharlie -> toNoun (Cord "charlie")
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Strip Sum Prefixes ----------------------------------------------------------
|
|
|
|
|
|
|
|
barZazBaz :: BarZaz -> Bool
|
|
|
|
barZazBaz = nounEquiv $ \case BZQueenAlice x y -> QueenAlice x y
|
|
|
|
BZBob x -> Bob x
|
2019-07-23 05:35:15 +03:00
|
|
|
BZCharlie -> Charlie
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
fooBarBaz :: FooBar -> Bool
|
|
|
|
fooBarBaz = nounEquiv $ \case FooBarQueenAlice x y -> QueenAlice x y
|
|
|
|
FooBarBob x -> Bob x
|
2019-07-23 05:35:15 +03:00
|
|
|
FooBarCharlie -> Charlie
|
2019-07-21 02:47:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
tests :: TestTree
|
|
|
|
tests =
|
|
|
|
testGroup "Log"
|
|
|
|
[ testProperty "Enum Sanity" $ enumSanity
|
|
|
|
, testProperty "Sum Sanity" $ sumSanity
|
|
|
|
, testProperty "Record Sanity" $ recSanity
|
|
|
|
, testProperty "Type-Prefix Sanity" $ abbrPrefixSanity
|
|
|
|
, testProperty "Abbrv-Prefix Sanity" $ typePrefixSanity
|
2019-07-21 03:50:56 +03:00
|
|
|
, testProperty "Round Trip Rec (Poly)" $ roundTrip @(Poly Bool Bool)
|
2019-07-21 02:47:35 +03:00
|
|
|
, testProperty "Round Trip Rec (ThreeWords)" $ roundTrip @ThreeWords
|
|
|
|
, testProperty "Round Trip Enum (Nums)" $ roundTrip @Nums
|
|
|
|
, testProperty "Round Trip Sum (FooBar)" $ roundTrip @FooBar
|
|
|
|
, testProperty "Round Trip Sum (BarZaz)" $ roundTrip @BarZaz
|
|
|
|
, testProperty "Round Trip Sum (ZazBaz)" $ roundTrip @ZazBaz
|
|
|
|
, testProperty "Prefix Test 1" $ barZazBaz
|
|
|
|
, testProperty "Prefix Test 2" $ fooBarBaz
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
-- Generate Arbitrary Values ---------------------------------------------------
|
|
|
|
|
|
|
|
arb :: Arbitrary a => Gen a
|
|
|
|
arb = arbitrary
|