This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* coalgebras for arbitrary and parser * removed tagged literals
- Loading branch information
1 parent
a7e98a7
commit 10be5a8
Showing
8 changed files
with
74 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,30 @@ | ||
module Data.Json.Extended.Signature.Gen | ||
( arbitraryBaseEJsonF | ||
, arbitraryEJsonF | ||
, arbitraryEJsonFWithKeyGen | ||
( arbitraryEJsonF | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Array as A | ||
import Data.DateTime as DT | ||
import Data.Enum (toEnum) | ||
import Data.HugeNum as HN | ||
import Data.Json.Extended.Signature.Core (EJsonF(..), EJsonMap(..)) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Tuple as T | ||
|
||
import Matryoshka (CoalgebraM) | ||
|
||
import Test.StrongCheck.Arbitrary as SC | ||
import Test.StrongCheck.Gen as Gen | ||
|
||
arbitraryBaseEJsonF ∷ ∀ a. Gen.Gen (EJsonF a) | ||
arbitraryBaseEJsonF = | ||
arbitraryEJsonF ∷ CoalgebraM Gen.Gen EJsonF Int | ||
arbitraryEJsonF 0 = | ||
Gen.oneOf (pure Null) | ||
[ Boolean <$> SC.arbitrary | ||
, Integer <$> SC.arbitrary | ||
, Decimal <$> arbitraryDecimal | ||
, String <$> SC.arbitrary | ||
, Timestamp <$> arbitraryDateTime | ||
, Date <$> arbitraryDate | ||
, Time <$> arbitraryTime | ||
, Interval <$> SC.arbitrary | ||
, ObjectId <$> SC.arbitrary | ||
, pure Null | ||
[ map Boolean SC.arbitrary | ||
, map Integer SC.arbitrary | ||
, map Decimal $ map HN.fromNumber SC.arbitrary | ||
, map String SC.arbitrary | ||
] | ||
|
||
arbitraryEJsonFWithKeyGen | ||
∷ ∀ a | ||
. (Eq a) | ||
⇒ Gen.Gen a | ||
→ Gen.Gen a | ||
→ Gen.Gen (EJsonF a) | ||
arbitraryEJsonFWithKeyGen keyGen rec = | ||
Gen.oneOf (pure Null) | ||
[ arbitraryBaseEJsonF | ||
, Array <$> Gen.arrayOf rec | ||
, Map <<< EJsonMap <$> do | ||
keys ← distinctArrayOf keyGen | ||
vals ← Gen.vectorOf (A.length keys) rec | ||
pure $ A.zip keys vals | ||
arbitraryEJsonF n = do | ||
len ← Gen.chooseInt 0 $ n - 1 | ||
Gen.oneOf (arbitraryEJsonF 0) | ||
[ pure $ Array $ A.replicate len $ n - 1 | ||
, pure $ Map $ EJsonMap $ A.replicate len $ T.Tuple (n - 1) (n - 1) | ||
] | ||
|
||
where | ||
arbitraryTuple ∷ Gen.Gen (T.Tuple a a) | ||
arbitraryTuple = | ||
T.Tuple | ||
<$> keyGen | ||
<*> rec | ||
|
||
arbitraryEJsonF | ||
∷ ∀ a | ||
. (Eq a) | ||
⇒ Gen.Gen a | ||
→ Gen.Gen (EJsonF a) | ||
arbitraryEJsonF rec = | ||
arbitraryEJsonFWithKeyGen rec rec | ||
|
||
distinctArrayOf | ||
∷ ∀ a | ||
. (Eq a) | ||
⇒ Gen.Gen a | ||
→ Gen.Gen (Array a) | ||
distinctArrayOf = | ||
map A.nub | ||
<<< Gen.arrayOf | ||
|
||
arbitraryDecimal ∷ Gen.Gen HN.HugeNum | ||
arbitraryDecimal = | ||
HN.fromNumber | ||
<$> SC.arbitrary | ||
|
||
arbitraryDateTime ∷ Gen.Gen DT.DateTime | ||
arbitraryDateTime = DT.DateTime <$> arbitraryDate <*> arbitraryTime | ||
|
||
arbitraryDate ∷ Gen.Gen DT.Date | ||
arbitraryDate = do | ||
year ← Gen.chooseInt 1950 2050 | ||
month ← Gen.chooseInt 1 12 | ||
day ← Gen.chooseInt 1 31 | ||
pure $ DT.canonicalDate | ||
(fromMaybe bottom (toEnum year)) | ||
(fromMaybe bottom (toEnum month)) | ||
(fromMaybe bottom (toEnum day)) | ||
|
||
arbitraryTime ∷ Gen.Gen DT.Time | ||
arbitraryTime = do | ||
hour ← Gen.chooseInt 0 23 | ||
minute ← Gen.chooseInt 0 59 | ||
second ← Gen.chooseInt 0 59 | ||
pure $ DT.Time | ||
(fromMaybe bottom (toEnum hour)) | ||
(fromMaybe bottom (toEnum minute)) | ||
(fromMaybe bottom (toEnum second)) | ||
bottom |
Oops, something went wrong.