mirror of
https://github.com/haskell-nix/hnix-store.git
synced 2024-09-11 18:05:56 +03:00
tests: custom Arbitrary UTCTime instance, drop quickcheck-instances
in favor of our own small set of required instances.
This commit is contained in:
parent
6c0edf2fcf
commit
cc931dde1e
@ -117,7 +117,6 @@ library
|
||||
, network
|
||||
, mtl
|
||||
, QuickCheck
|
||||
, quickcheck-instances
|
||||
, unordered-containers
|
||||
, vector
|
||||
hs-source-dirs: src
|
||||
@ -175,7 +174,6 @@ test-suite remote
|
||||
, time
|
||||
, hspec
|
||||
, QuickCheck
|
||||
, quickcheck-instances
|
||||
, unordered-containers
|
||||
|
||||
test-suite remote-io
|
||||
|
@ -9,7 +9,6 @@ import System.Nix.Store.Remote.Types
|
||||
|
||||
import Test.QuickCheck (Arbitrary(..), oneof)
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
deriving via GenericArbitrary CheckMode
|
||||
instance Arbitrary CheckMode
|
||||
|
@ -4,21 +4,16 @@ module NixSerializerSpec (spec) where
|
||||
|
||||
import Crypto.Hash (MD5, SHA1, SHA256, SHA512)
|
||||
import Data.Dependent.Sum (DSum((:=>)))
|
||||
import Data.Fixed (Uni)
|
||||
import Data.Time (NominalDiffTime)
|
||||
import Data.Time (UTCTime)
|
||||
import Data.Word (Word64)
|
||||
import Test.Hspec (Expectation, Spec, describe, it, parallel, shouldBe)
|
||||
import Test.Hspec.QuickCheck (prop)
|
||||
import Test.QuickCheck (Gen, arbitrary, forAll, suchThat)
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
import qualified Data.Time.Clock.POSIX
|
||||
import qualified Data.Serializer
|
||||
import qualified System.Nix.Build
|
||||
import qualified System.Nix.Hash
|
||||
|
||||
import System.Nix.Arbitrary ()
|
||||
import System.Nix.Build (BuildResult)
|
||||
import System.Nix.Derivation (Derivation(inputDrvs))
|
||||
import System.Nix.StorePath (StoreDir)
|
||||
import System.Nix.StorePath.Metadata (Metadata(..))
|
||||
@ -71,19 +66,7 @@ spec = parallel $ do
|
||||
prop "Maybe Text"
|
||||
$ forAll (arbitrary `suchThat` (/= Just ""))
|
||||
$ roundtripS maybeText
|
||||
prop "UTCTime" $ do
|
||||
let
|
||||
-- scale to seconds and back
|
||||
toSeconds :: Int -> NominalDiffTime
|
||||
toSeconds n = realToFrac (toEnum n :: Uni)
|
||||
fromSeconds :: NominalDiffTime -> Int
|
||||
fromSeconds = (fromEnum :: Uni -> Int) . realToFrac
|
||||
|
||||
roundtripS @Int @() $
|
||||
Data.Serializer.mapIsoSerializer
|
||||
(fromSeconds . Data.Time.Clock.POSIX.utcTimeToPOSIXSeconds)
|
||||
(Data.Time.Clock.POSIX.posixSecondsToUTCTime . toSeconds)
|
||||
time
|
||||
prop "UTCTime" $ roundtripS @UTCTime @() time
|
||||
|
||||
describe "Combinators" $ do
|
||||
prop "list" $ roundtripS @[Int] @() (list int)
|
||||
@ -94,12 +77,7 @@ spec = parallel $ do
|
||||
describe "Complex" $ do
|
||||
prop "BuildResult"
|
||||
$ forAll (arbitrary `suchThat` ((/= Just "") . System.Nix.Build.errorMessage))
|
||||
$ \br ->
|
||||
roundtripS @BuildResult buildResult
|
||||
-- fix time to 0 as we test UTCTime above
|
||||
$ br { System.Nix.Build.startTime = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
|
||||
, System.Nix.Build.stopTime = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
|
||||
}
|
||||
$ roundtripS buildResult
|
||||
|
||||
prop "StorePath" $
|
||||
roundtripSReader @StoreDir storePath
|
||||
@ -118,9 +96,6 @@ spec = parallel $ do
|
||||
prop "Metadata (StorePath)"
|
||||
$ \sd -> forAll (arbitrary `suchThat` (\m -> narHashIsSHA256 m && narBytes m /= Just 0))
|
||||
$ roundtripSReader @StoreDir pathMetadata sd
|
||||
. (\m -> m
|
||||
{ registrationTime = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
|
||||
})
|
||||
|
||||
prop "Some HashAlgo" $
|
||||
roundtripS someHashAlgo
|
||||
|
@ -2,25 +2,21 @@
|
||||
|
||||
module SerializeSpec (spec) where
|
||||
|
||||
import Data.Fixed (Uni)
|
||||
import Data.Serialize (Serialize(..))
|
||||
import Data.Serialize.Get (Get, runGet)
|
||||
import Data.Serialize.Put (Putter, runPut)
|
||||
import Data.Text (Text)
|
||||
import Data.Time (NominalDiffTime)
|
||||
import Test.Hspec (Expectation, Spec, describe, it, parallel, shouldBe)
|
||||
import Test.Hspec.QuickCheck (prop)
|
||||
import Test.Hspec.Nix (roundtrips)
|
||||
import Test.QuickCheck (arbitrary, forAll, suchThat)
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
import qualified Data.Either
|
||||
import qualified Data.HashSet
|
||||
import qualified Data.Time.Clock.POSIX
|
||||
import qualified System.Nix.Build
|
||||
|
||||
import System.Nix.Arbitrary ()
|
||||
import System.Nix.Build (BuildMode(..), BuildStatus(..))
|
||||
import System.Nix.Build (BuildMode(..), BuildResult, BuildStatus(..))
|
||||
import System.Nix.Derivation (Derivation(inputDrvs))
|
||||
import System.Nix.Store.Remote.Arbitrary ()
|
||||
import System.Nix.Store.Remote.Serialize (getDerivation, putDerivation)
|
||||
@ -61,18 +57,6 @@ spec = parallel $ do
|
||||
prop "Bool" $ roundtrips2 putBool getBool
|
||||
prop "ByteString" $ roundtrips2 putByteString getByteString
|
||||
|
||||
prop "UTCTime" $ do
|
||||
let
|
||||
-- scale to seconds and back
|
||||
toSeconds :: Int -> NominalDiffTime
|
||||
toSeconds n = realToFrac (toEnum n :: Uni)
|
||||
fromSeconds :: NominalDiffTime -> Int
|
||||
fromSeconds = (fromEnum :: Uni -> Int) . realToFrac
|
||||
|
||||
roundtrips2
|
||||
(putTime . Data.Time.Clock.POSIX.posixSecondsToUTCTime . toSeconds)
|
||||
(fromSeconds . Data.Time.Clock.POSIX.utcTimeToPOSIXSeconds <$> getTime)
|
||||
|
||||
describe "Combinators" $ do
|
||||
prop "Many" $ roundtrips2 (putMany putInt) (getMany (getInt @Int))
|
||||
prop "[ByteString]" $ roundtrips2 putByteStrings getByteStrings
|
||||
@ -95,12 +79,7 @@ spec = parallel $ do
|
||||
prop "BuildStatus" $ roundtripS @BuildStatus
|
||||
it "BuildResult" $
|
||||
forAll (arbitrary `suchThat` ((/= Just "") . System.Nix.Build.errorMessage))
|
||||
$ \br ->
|
||||
roundtripS
|
||||
-- fix time to 0 as we test UTCTime above
|
||||
$ br { System.Nix.Build.startTime = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
|
||||
, System.Nix.Build.stopTime = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
|
||||
}
|
||||
$ roundtripS @BuildResult
|
||||
|
||||
prop "ProtoVersion" $ roundtripS @ProtoVersion
|
||||
|
||||
|
@ -35,7 +35,11 @@ common commons
|
||||
library
|
||||
import: commons
|
||||
exposed-modules:
|
||||
System.Nix.Arbitrary
|
||||
Data.ByteString.Arbitrary
|
||||
, Data.HashSet.Arbitrary
|
||||
, Data.Text.Arbitrary
|
||||
, Data.Vector.Arbitrary
|
||||
, System.Nix.Arbitrary
|
||||
, System.Nix.Arbitrary.Base
|
||||
, System.Nix.Arbitrary.Build
|
||||
, System.Nix.Arbitrary.ContentAddress
|
||||
@ -46,6 +50,7 @@ library
|
||||
, System.Nix.Arbitrary.Store.Types
|
||||
, System.Nix.Arbitrary.StorePath
|
||||
, System.Nix.Arbitrary.StorePath.Metadata
|
||||
, System.Nix.Arbitrary.UTCTime
|
||||
, Test.Hspec.Nix
|
||||
build-depends:
|
||||
base >=4.12 && <5
|
||||
@ -55,10 +60,13 @@ library
|
||||
, crypton
|
||||
, dependent-sum > 0.7
|
||||
, generic-arbitrary < 1.1
|
||||
, hashable
|
||||
, hspec
|
||||
, QuickCheck
|
||||
, quickcheck-instances
|
||||
, text
|
||||
, time
|
||||
, unordered-containers
|
||||
, vector
|
||||
hs-source-dirs: src
|
||||
|
||||
test-suite props
|
||||
|
10
hnix-store-tests/src/Data/ByteString/Arbitrary.hs
Normal file
10
hnix-store-tests/src/Data/ByteString/Arbitrary.hs
Normal file
@ -0,0 +1,10 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
module Data.ByteString.Arbitrary () where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import qualified Data.ByteString.Char8
|
||||
|
||||
instance Arbitrary ByteString where
|
||||
arbitrary = Data.ByteString.Char8.pack <$> arbitrary
|
||||
shrink xs = Data.ByteString.Char8.pack <$> shrink (Data.ByteString.Char8.unpack xs)
|
11
hnix-store-tests/src/Data/HashSet/Arbitrary.hs
Normal file
11
hnix-store-tests/src/Data/HashSet/Arbitrary.hs
Normal file
@ -0,0 +1,11 @@
|
||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||
module Data.HashSet.Arbitrary where
|
||||
|
||||
import Data.Hashable (Hashable)
|
||||
import Data.HashSet (HashSet)
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import qualified Data.HashSet
|
||||
|
||||
instance (Hashable a, Eq a, Arbitrary a) => Arbitrary (HashSet a) where
|
||||
arbitrary = Data.HashSet.fromList <$> arbitrary
|
||||
shrink hashset = Data.HashSet.fromList <$> shrink (Data.HashSet.toList hashset)
|
10
hnix-store-tests/src/Data/Text/Arbitrary.hs
Normal file
10
hnix-store-tests/src/Data/Text/Arbitrary.hs
Normal file
@ -0,0 +1,10 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
module Data.Text.Arbitrary () where
|
||||
|
||||
import Data.Text (Text)
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import qualified Data.Text
|
||||
|
||||
instance Arbitrary Text where
|
||||
arbitrary = Data.Text.pack <$> arbitrary
|
||||
shrink xs = Data.Text.pack <$> shrink (Data.Text.unpack xs)
|
20
hnix-store-tests/src/Data/Vector/Arbitrary.hs
Normal file
20
hnix-store-tests/src/Data/Vector/Arbitrary.hs
Normal file
@ -0,0 +1,20 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
-- Stolen from quickcheck-instances (BSD-3)
|
||||
module Data.Vector.Arbitrary () where
|
||||
|
||||
import Data.Vector (Vector)
|
||||
import Test.QuickCheck (Arbitrary(..), Arbitrary1(..), arbitrary1, shrink1)
|
||||
import qualified Data.Vector
|
||||
|
||||
instance Arbitrary1 Vector where
|
||||
liftArbitrary =
|
||||
fmap Data.Vector.fromList
|
||||
. liftArbitrary
|
||||
liftShrink shr =
|
||||
fmap Data.Vector.fromList
|
||||
. liftShrink shr
|
||||
. Data.Vector.toList
|
||||
|
||||
instance Arbitrary a => Arbitrary (Vector a) where
|
||||
arbitrary = arbitrary1
|
||||
shrink = shrink1
|
@ -1,5 +1,10 @@
|
||||
module System.Nix.Arbitrary where
|
||||
|
||||
import Data.ByteString.Arbitrary ()
|
||||
import Data.HashSet.Arbitrary ()
|
||||
import Data.Text.Arbitrary ()
|
||||
import Data.Vector.Arbitrary ()
|
||||
|
||||
import System.Nix.Arbitrary.Base ()
|
||||
import System.Nix.Arbitrary.Build ()
|
||||
import System.Nix.Arbitrary.ContentAddress ()
|
||||
|
@ -7,7 +7,6 @@ import System.Nix.Base
|
||||
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
deriving via GenericArbitrary BaseEncoding
|
||||
instance Arbitrary BaseEncoding
|
||||
|
@ -3,11 +3,12 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
module System.Nix.Arbitrary.Build where
|
||||
|
||||
import System.Nix.Build
|
||||
|
||||
import Data.Text.Arbitrary ()
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
import System.Nix.Arbitrary.UTCTime ()
|
||||
|
||||
import System.Nix.Build
|
||||
|
||||
deriving via GenericArbitrary BuildMode
|
||||
instance Arbitrary BuildMode
|
||||
|
@ -4,12 +4,13 @@
|
||||
module System.Nix.Arbitrary.Derivation where
|
||||
|
||||
import Data.Text (Text)
|
||||
import Data.Text.Arbitrary ()
|
||||
import Data.Vector.Arbitrary ()
|
||||
import System.Nix.Derivation
|
||||
import System.Nix.StorePath (StorePath)
|
||||
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
import System.Nix.Arbitrary.StorePath ()
|
||||
|
||||
deriving via GenericArbitrary (Derivation StorePath Text)
|
||||
|
@ -4,13 +4,13 @@
|
||||
module System.Nix.Arbitrary.Hash where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.ByteString.Arbitrary ()
|
||||
import Crypto.Hash (Digest, MD5(..), SHA1(..), SHA256(..), SHA512(..))
|
||||
import Data.Dependent.Sum (DSum((:=>)))
|
||||
import Data.Some (Some(Some))
|
||||
import System.Nix.Hash (HashAlgo(..))
|
||||
|
||||
import Test.QuickCheck (Arbitrary(arbitrary), oneof)
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
import qualified Crypto.Hash
|
||||
|
||||
|
@ -9,7 +9,6 @@ import Crypto.Random (drgNewTest, withDRG)
|
||||
import qualified Data.ByteString as BS
|
||||
import qualified Data.Text as Text
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
import Test.QuickCheck
|
||||
|
||||
import System.Nix.Signature
|
||||
|
@ -4,16 +4,17 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
module System.Nix.Arbitrary.StorePath.Metadata where
|
||||
|
||||
import Data.HashSet.Arbitrary ()
|
||||
import System.Nix.Arbitrary.ContentAddress ()
|
||||
import System.Nix.Arbitrary.Hash ()
|
||||
import System.Nix.Arbitrary.Signature ()
|
||||
import System.Nix.Arbitrary.StorePath ()
|
||||
import System.Nix.Arbitrary.UTCTime ()
|
||||
import System.Nix.StorePath (StorePath)
|
||||
import System.Nix.StorePath.Metadata (Metadata, StorePathTrust)
|
||||
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))
|
||||
import Test.QuickCheck.Instances ()
|
||||
|
||||
deriving via GenericArbitrary StorePathTrust
|
||||
instance Arbitrary StorePathTrust
|
||||
|
26
hnix-store-tests/src/System/Nix/Arbitrary/UTCTime.hs
Normal file
26
hnix-store-tests/src/System/Nix/Arbitrary/UTCTime.hs
Normal file
@ -0,0 +1,26 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
-- Stolen from quickcheck-instances (BSD-3)
|
||||
-- UTCTime/DiffTime slightly modified to produce
|
||||
-- values rounded to whole seconds
|
||||
module System.Nix.Arbitrary.UTCTime where
|
||||
|
||||
import Data.Time (Day(..), DiffTime, UTCTime(..))
|
||||
import Test.QuickCheck (Arbitrary(..))
|
||||
|
||||
instance Arbitrary Day where
|
||||
arbitrary = ModifiedJulianDay <$> (2000 +) <$> arbitrary
|
||||
shrink = (ModifiedJulianDay <$>) . shrink . Data.Time.toModifiedJulianDay
|
||||
|
||||
instance Arbitrary DiffTime where
|
||||
-- without abs something weird happens, try it
|
||||
arbitrary = fromInteger . abs <$> arbitrary
|
||||
|
||||
instance Arbitrary UTCTime where
|
||||
arbitrary =
|
||||
UTCTime
|
||||
<$> arbitrary
|
||||
<*> arbitrary
|
||||
shrink ut@(UTCTime day dayTime) =
|
||||
[ ut { Data.Time.utctDay = d' } | d' <- shrink day ]
|
||||
++ [ ut { Data.Time.utctDayTime = t' } | t' <- shrink dayTime ]
|
||||
|
Loading…
Reference in New Issue
Block a user