2019-07-24 07:03:04 +03:00
|
|
|
module ArvoTests (tests) where
|
|
|
|
|
|
|
|
import Data.Acquire
|
2019-08-08 01:24:02 +03:00
|
|
|
import Data.Conduit
|
|
|
|
import Data.Conduit.List
|
|
|
|
import Data.Ord.Unicode
|
|
|
|
import Network.HTTP.Types.Method
|
|
|
|
import Test.QuickCheck hiding ((.&.))
|
2019-07-24 07:03:04 +03:00
|
|
|
import Test.Tasty
|
|
|
|
import Test.Tasty.QuickCheck
|
|
|
|
import Test.Tasty.TH
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo
|
|
|
|
import Urbit.Prelude
|
|
|
|
import Urbit.Time
|
|
|
|
import Urbit.Vere.Log
|
|
|
|
import Urbit.Vere.Pier.Types
|
2019-07-24 07:03:04 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
import Control.Concurrent (runInBoundThread, threadDelay)
|
2019-07-24 07:03:04 +03:00
|
|
|
import Data.LargeWord (LargeKey(..))
|
|
|
|
import GHC.Natural (Natural)
|
2020-01-23 07:16:09 +03:00
|
|
|
import Network.Socket (tupleToHostAddress)
|
2019-07-24 07:03:04 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import qualified Urbit.Vere.Log as Log
|
2019-07-24 07:03:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Utils -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
roundTrip :: forall a. (Eq a, ToNoun a, FromNoun a) => a -> Bool
|
|
|
|
roundTrip x = Just x == fromNoun (toNoun x)
|
|
|
|
|
|
|
|
nounEq :: (ToNoun a, ToNoun b) => a -> b -> Bool
|
|
|
|
nounEq x y = toNoun x == toNoun y
|
|
|
|
|
|
|
|
data EvExample = EvEx Ev Noun
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
eventSanity :: [EvExample] -> Bool
|
|
|
|
eventSanity = all $ \(EvEx e n) -> toNoun e == n
|
|
|
|
|
|
|
|
instance Arbitrary EvExample where
|
|
|
|
arbitrary = oneof $ fmap pure $
|
2019-12-17 13:14:34 +03:00
|
|
|
[ EvEx (EvVane $ VaneVane $ VEVeer (Jael, ()) "" (Path []) "")
|
2019-07-24 07:03:04 +03:00
|
|
|
(toNoun (Path ["vane", "vane", "jael"], Cord "veer", (), (), ()))
|
|
|
|
]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
tests :: TestTree
|
|
|
|
tests =
|
|
|
|
testGroup "Arvo Events and Effects"
|
2019-08-01 01:44:14 +03:00
|
|
|
[ testProperty "Round Trip Effect" (roundTrip @Ef)
|
|
|
|
, testProperty "Round Trip Event" (roundTrip @Ev)
|
|
|
|
, testProperty "Round Trip AmesDest" (roundTrip @AmesDest)
|
2019-07-24 07:03:04 +03:00
|
|
|
, testProperty "Basic Event Sanity" eventSanity
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
-- Arbitrary Instances ---------------------------------------------------------
|
|
|
|
|
|
|
|
instance Arbitrary Natural where
|
|
|
|
arbitrary = (fromIntegral . abs @Integer) <$> arb
|
|
|
|
|
|
|
|
newtype DumbChar = Dumb { unDumb :: Char }
|
|
|
|
|
|
|
|
instance Arbitrary DumbChar where
|
|
|
|
arbitrary = Dumb <$> choose ('a', 'z')
|
|
|
|
|
|
|
|
instance Arbitrary Text where
|
|
|
|
arbitrary = pack . fmap unDumb <$> arbitrary
|
|
|
|
|
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (LargeKey a b) where
|
|
|
|
arbitrary = LargeKey <$> arb <*> arb
|
|
|
|
|
|
|
|
instance Arbitrary ByteString where
|
|
|
|
arbitrary = encodeUtf8 <$> arbitrary
|
|
|
|
|
|
|
|
instance Arbitrary EvilPath where arbitrary = EvilPath <$> arb
|
|
|
|
instance Arbitrary Path where arbitrary = Path <$> arb
|
|
|
|
instance Arbitrary Knot where arbitrary = MkKnot <$> arb
|
|
|
|
instance Arbitrary Tape where arbitrary = Tape <$> arb
|
|
|
|
instance Arbitrary BigTape where arbitrary = BigTape <$> arb
|
|
|
|
instance Arbitrary Bytes where arbitrary = MkBytes <$> arb
|
|
|
|
instance Arbitrary Octs where arbitrary = Octs <$> arb
|
|
|
|
instance Arbitrary File where arbitrary = File <$> arb
|
|
|
|
instance Arbitrary Cord where arbitrary = Cord <$> arb
|
|
|
|
instance Arbitrary Wen where arbitrary = Wen <$> arb
|
|
|
|
instance Arbitrary Gap where arbitrary = Gap . abs <$> arb
|
|
|
|
instance Arbitrary Port where arbitrary = Port <$> arb
|
|
|
|
instance Arbitrary Ship where arbitrary = Ship <$> arb
|
|
|
|
instance Arbitrary Address where arbitrary = AAmes <$> arb
|
2019-08-01 01:44:14 +03:00
|
|
|
|
2019-12-19 17:13:20 +03:00
|
|
|
instance Arbitrary a => Arbitrary (Patp a) where
|
|
|
|
arbitrary = Patp <$> arb
|
|
|
|
|
2019-08-01 01:44:14 +03:00
|
|
|
genIpv4 :: Gen Ipv4
|
|
|
|
genIpv4 = do
|
|
|
|
x <- arbitrary
|
|
|
|
if (x == 0 || (x≥256 && x≤512))
|
|
|
|
then genIpv4
|
|
|
|
else pure (Ipv4 x)
|
2019-08-01 00:52:49 +03:00
|
|
|
|
2019-12-17 13:14:34 +03:00
|
|
|
instance (Arbitrary a, Arbitrary b) => Arbitrary (Each a b) where
|
|
|
|
arbitrary = oneof [ EachNo <$> arb, EachYes <$> arb ]
|
|
|
|
|
|
|
|
instance (Arbitrary a) => Arbitrary (Jammed a) where
|
|
|
|
arbitrary = Jammed <$> arbitrary
|
2019-07-24 07:03:04 +03:00
|
|
|
|
|
|
|
instance Arbitrary Ef where
|
|
|
|
arbitrary = oneof [ EfVega <$> arb <*> arb
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary AmesEv where
|
2019-12-17 13:14:34 +03:00
|
|
|
arbitrary = oneof [ AmesEvHear () <$> arb <*> arb
|
|
|
|
, AmesEvHole () <$> arb <*> arb
|
2019-07-24 07:03:04 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary HttpRequest where
|
|
|
|
arbitrary = HttpRequest <$> arb <*> arb <*> arb <*> arb
|
|
|
|
|
|
|
|
instance Arbitrary HttpServerReq where
|
|
|
|
arbitrary = HttpServerReq <$> arb <*> arb <*> arb
|
|
|
|
|
|
|
|
instance Arbitrary HttpServerEv where
|
|
|
|
arbitrary = oneof [ HttpServerEvRequest <$> arb <*> arb
|
|
|
|
, HttpServerEvLive <$> arb <*> arb <*> arb
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary BlipEv where
|
|
|
|
arbitrary = oneof [ BlipEvAmes <$> arb
|
|
|
|
, BlipEvHttpServer <$> arb
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary Ev where
|
|
|
|
arbitrary = oneof [ EvVane <$> arb
|
|
|
|
, EvBlip <$> arb
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary Vane where
|
|
|
|
arbitrary = oneof [ VaneVane <$> arb
|
|
|
|
, VaneZuse <$> arb
|
|
|
|
]
|
|
|
|
|
|
|
|
instance Arbitrary VaneName where
|
|
|
|
arbitrary = oneof $ pure <$> [minBound .. maxBound]
|
|
|
|
|
|
|
|
instance Arbitrary VaneEv where
|
|
|
|
arbitrary = VEVeer <$> arb <*> arb <*> arb <*> arb
|
|
|
|
|
|
|
|
instance Arbitrary ZuseEv where
|
|
|
|
arbitrary = ZEVeer () <$> arb <*> arb <*> arb
|
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
instance Arbitrary StdMethod where
|
|
|
|
arbitrary = oneof $ pure <$> [ minBound .. maxBound ]
|
|
|
|
|
|
|
|
instance Arbitrary Header where
|
|
|
|
arbitrary = Header <$> arb <*> arb
|
|
|
|
|
2019-09-18 06:33:38 +03:00
|
|
|
instance Arbitrary BigCord where
|
|
|
|
arbitrary = BigCord <$> arb
|
|
|
|
|
|
|
|
|
2019-08-08 01:24:02 +03:00
|
|
|
instance Arbitrary ServId where arbitrary = ServId <$> arb
|
|
|
|
|
|
|
|
instance Arbitrary UD where arbitrary = UD <$> arb
|
|
|
|
instance Arbitrary UV where arbitrary = UV <$> arb
|
|
|
|
|
2019-12-17 13:14:34 +03:00
|
|
|
instance Arbitrary AmesAddress where
|
|
|
|
arbitrary = AAIpv4 <$> arb <*> arb
|
|
|
|
|
|
|
|
instance Arbitrary Ipv4 where arbitrary = Ipv4 <$> arb
|
2019-07-25 04:59:45 +03:00
|
|
|
|
2019-07-24 07:03:04 +03:00
|
|
|
-- Generate Arbitrary Values ---------------------------------------------------
|
|
|
|
|
|
|
|
arb :: Arbitrary a => Gen a
|
|
|
|
arb = arbitrary
|