Skip to content

Commit

Permalink
feat: activate ormolu formatting of haskell file and pretty md/yaml f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
jbgi authored and dermetfan committed Nov 22, 2022
1 parent 3a42673 commit 5bb23a8
Show file tree
Hide file tree
Showing 33 changed files with 942 additions and 972 deletions.
2 changes: 1 addition & 1 deletion bors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ status = [
]
timeout_sec = 7200
required_approvals = 1
block_labels = [ "WIP", "DO NOT MERGE" ]
block_labels = ["WIP", "DO NOT MERGE"]
delete_merged_branches = true
2 changes: 1 addition & 1 deletion cardano-prelude-test/src/Test/Cardano/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Test.Cardano.Prelude
( module X
( module X,
)
where

Expand Down
60 changes: 29 additions & 31 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Base16.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
-- golden values are stored on disk as indexed, line-wrapped hex dumps. These
-- can then be nicely diffed to provide some indication of where serialisation
-- errors may have occurred.

module Test.Cardano.Prelude.Base16
( decodeBase16
, encodeBase16
, encodeWithIndex
( decodeBase16,
encodeBase16,
encodeWithIndex,
)
where

import Cardano.Prelude

import qualified Data.Attoparsec.ByteString.Char8 as PBC
import qualified Data.Attoparsec.ByteString.Lazy as PLB
import qualified Data.ByteString.Base16.Lazy as B16
Expand All @@ -35,32 +33,31 @@ encodeBase16 = lineWrapBS lineWrapLength . B16.encode
-- offset (line wrapped every 16 bytes).
encodeWithIndex :: LB.ByteString -> LB.ByteString
encodeWithIndex bs
|
-- If the length of the ByteString <= 16 (it hasn't been encoded to base-16
| -- If the length of the ByteString <= 16 (it hasn't been encoded to base-16
-- yet so we're not checking for <= 32), then just 'encode' rather than
-- prepending the byte offsets.
LB.length bs <= (lineWrapLength `div` 2) = encodeBase16 bs
LB.length bs <= (lineWrapLength `div` 2) =
encodeBase16 bs
| otherwise = LB.concat $ go 0 (chunkBS lineWrapLength $ B16.encode bs)
where
go :: Int64 -> [LB.ByteString] -> [LB.ByteString]
go _ [] = []
go acc (x : xs) =
let numDigits = numByteOffsetDigits $ LB.length bs
in
LB.concat [LB.pack $ printf "%0*x: " numDigits acc, x, "\n"]
: go (acc + 16) xs
where
go :: Int64 -> [LB.ByteString] -> [LB.ByteString]
go _ [] = []
go acc (x : xs) =
let numDigits = numByteOffsetDigits $ LB.length bs
in LB.concat [LB.pack $ printf "%0*x: " numDigits acc, x, "\n"]
: go (acc + 16) xs

-- | Given the number of bytes of data, determine the number of digits required
-- to represent the base-16 byte offset for a hexdump.
numByteOffsetDigits :: Int64 -> Int64
numByteOffsetDigits len
| len <= 0xff = 2
| len <= 0xfff = 3
| len <= 0xffff = 4
| len <= 0xfffff = 5
| len <= 0xffffff = 6
| len <= 0xff = 2
| len <= 0xfff = 3
| len <= 0xffff = 4
| len <= 0xfffff = 5
| len <= 0xffffff = 6
| len <= 0xfffffff = 7
| otherwise = 8
| otherwise = 8

-- | The length at which our encoding functions will line wrap. We've chosen a
-- length of 32 because we want only want to display 16 bytes of base-16
Expand All @@ -79,7 +76,6 @@ chunkBS n xs = case LB.uncons xs of
Just _ ->
let (taken, dropped) = LB.splitAt n xs in taken : chunkBS n dropped


--------------------------------------------------------------------------------
-- Decoding
--------------------------------------------------------------------------------
Expand All @@ -89,10 +85,12 @@ chunkBS n xs = case LB.uncons xs of
decodeBase16 :: LB.ByteString -> Maybe LB.ByteString
decodeBase16 bs
| -- No complex parsing is required for data whose length is <= 32.
LB.length bs <= lineWrapLength = either (const Nothing) Just (B16.decode bs)
LB.length bs <= lineWrapLength =
either (const Nothing) Just (B16.decode bs)
| otherwise = case PLB.maybeResult $ PLB.parse decodeParser bs of
Nothing -> Nothing
Just r -> either (const Nothing) Just (B16.decode (LB.fromStrict (BC.concat r)))
Nothing -> Nothing
Just r ->
either (const Nothing) Just (B16.decode (LB.fromStrict (BC.concat r)))

-- | Parser for several lines of data encoded using 'encode' or
-- 'encodeWithIndex'.
Expand All @@ -103,8 +101,8 @@ decodeParser = many $ encodedEntryParser <* PBC.endOfLine
-- 'encodeWithIndex'.
encodedEntryParser :: PBC.Parser ByteString
encodedEntryParser = do
_ <- PBC.hexadecimal :: PBC.Parser Int -- Read the byte offset
PBC.skipWhile (not . PBC.isSpace) -- Skip until whitespace
PBC.skipSpace -- Skip the whitespace
PBC.takeWhile (not . (`BC.elem` "\n\r")) -- Consume the data up until LF
-- or CR.
_ <- PBC.hexadecimal :: PBC.Parser Int -- Read the byte offset
PBC.skipWhile (not . PBC.isSpace) -- Skip until whitespace
PBC.skipSpace -- Skip the whitespace
PBC.takeWhile (not . (`BC.elem` "\n\r")) -- Consume the data up until LF
-- or CR.
18 changes: 7 additions & 11 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Gen.hs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
-- | Hedgehog generators for commonly used types

module Test.Cardano.Prelude.Gen
( genBytes
, genUTF8Byte
, gen32Bytes
, genWord32
, genWord16
, genNatural
, genNominalDiffTime
( genBytes,
genUTF8Byte,
gen32Bytes,
genWord32,
genWord16,
genNatural,
genNominalDiffTime,
)
where

import Cardano.Prelude

import Data.Time (NominalDiffTime)

import Hedgehog (Gen)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range


genBytes :: Int -> Gen ByteString
genBytes n = Gen.bytes (Range.singleton n)

Expand Down
161 changes: 79 additions & 82 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,138 +3,135 @@
{-# LANGUAGE ScopedTypeVariables #-}

-- | Helper functions for use in golden testing of datatypes

module Test.Cardano.Prelude.Golden
( discoverGolden
, eachOf
, goldenTestCanonicalJSONDec
, goldenTestJSONDec
, goldenTestJSON
, goldenTestJSONPretty
, getText
( discoverGolden,
eachOf,
goldenTestCanonicalJSONDec,
goldenTestJSONDec,
goldenTestJSON,
goldenTestJSONPretty,
getText,
)
where

import Cardano.Prelude

import Data.Aeson (FromJSON, ToJSON, eitherDecode, encode)
import Data.Aeson.Encode.Pretty
(Config(..), Indent(..), NumberFormat(..), encodePretty', keyOrder)
( Config (..),
Indent (..),
NumberFormat (..),
encodePretty',
keyOrder,
)
import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Builder (toLazyText)
import Formatting.Buildable (build)
import qualified Text.JSON.Canonical as Canonical

import Hedgehog
( Gen
, Group
, Property
, PropertyT
, TestLimit
, discoverPrefix
, forAll
, property
, withTests
, (===)
( Gen,
Group,
Property,
PropertyT,
TestLimit,
discoverPrefix,
forAll,
property,
withTests,
(===),
)
import Hedgehog.Internal.Property (failWith)
import Hedgehog.Internal.TH (TExpQ)

import qualified Text.JSON.Canonical as Canonical

discoverGolden :: TExpQ Group
discoverGolden = discoverPrefix "golden"

-- | Check that @eachOf@ @testLimit@ generated @things@ @hasProperty@
eachOf
:: (Show a, HasCallStack)
=> TestLimit
-> Gen a
-> (a -> PropertyT IO ())
-> Property
eachOf ::
(Show a, HasCallStack) =>
TestLimit ->
Gen a ->
(a -> PropertyT IO ()) ->
Property
eachOf testLimit things hasProperty =
withFrozenCallStack
$ withTests testLimit
. property
$ forAll things
>>= hasProperty
$ withTests testLimit
. property
$ forAll things
>>= hasProperty

-- | Check that Canonical JSON decodes to the datatype
goldenTestCanonicalJSONDec
:: (Eq a, Canonical.FromJSON (Either SchemaError) a, HasCallStack, Show a)
=> a
-> FilePath
-> Property
goldenTestCanonicalJSONDec ::
(Eq a, Canonical.FromJSON (Either SchemaError) a, HasCallStack, Show a) =>
a ->
FilePath ->
Property
goldenTestCanonicalJSONDec x path = withFrozenCallStack $ do
withTests 1 . property $ do
bs <- liftIO (LB.readFile path)
case Canonical.parseCanonicalJSON bs of
Left err -> failWith Nothing $ "could not parse: " <> show err
Left err -> failWith Nothing $ "could not parse: " <> show err
Right jsv -> case Canonical.fromJSON jsv of
Left (schErr :: SchemaError) ->
failWith Nothing $ LT.unpack $ toLazyText $ build schErr
Right x' -> x === x'

-- | Only check that the datatype equals the decoding of the file
goldenTestJSONDec
:: (Eq a, FromJSON a, HasCallStack, Show a) => a -> FilePath -> Property
goldenTestJSONDec ::
(Eq a, FromJSON a, HasCallStack, Show a) => a -> FilePath -> Property
goldenTestJSONDec x path = withFrozenCallStack $ withTests 1 . property $ do
bs <- liftIO (LB.readFile path)
case eitherDecode bs of
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'

goldenTestJSON
:: (Eq a, FromJSON a, HasCallStack, Show a, ToJSON a)
=> a
-> FilePath
-> Property
goldenTestJSON ::
(Eq a, FromJSON a, HasCallStack, Show a, ToJSON a) =>
a ->
FilePath ->
Property
goldenTestJSON x path = withFrozenCallStack $ withTests 1 . property $ do
bs <- liftIO (LB.readFile path)
encode x === bs
case eitherDecode bs of
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'

goldenTestJSONPretty
:: (Eq a, FromJSON a, HasCallStack, Show a, ToJSON a)
=> a
-> FilePath
-> Property
goldenTestJSONPretty x path =
withFrozenCallStack
$ withTests 1
. property
$ do
bs <- liftIO (LB.readFile path)
-- Sort keys by their order of appearance in the argument list
-- of `keyOrder`. Keys not in the argument list are moved to the
-- end, while their order is preserved.
let
defConfig' = Config
{ confIndent = Spaces 4
, confCompare = keyOrder ["file", "hash"]
, confNumFormat = Generic
, confTrailingNewline = False
}
encodePretty' defConfig' x === bs
case eitherDecode bs of
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'
goldenTestJSONPretty ::
(Eq a, FromJSON a, HasCallStack, Show a, ToJSON a) =>
a ->
FilePath ->
Property
goldenTestJSONPretty x path = withFrozenCallStack $ withTests 1 . property $ do
bs <- liftIO (LB.readFile path)
-- Sort keys by their order of appearance in the argument list
-- of `keyOrder`. Keys not in the argument list are moved to the
-- end, while their order is preserved.
let defConfig' =
Config
{ confIndent = Spaces 4,
confCompare = keyOrder ["file", "hash"],
confNumFormat = Generic,
confTrailingNewline = False
}
encodePretty' defConfig' x === bs
case eitherDecode bs of
Left err -> failWith Nothing $ "could not decode: " <> show err
Right x' -> x === x'

-- | Text used for example values in a number of golden tests
--
-- Changing existing values in this string will break existing golden
-- tests, but it us OK to append more data to the end.
staticText :: Text
staticText
= "Kmyw4lDSE5S4fSH6etNouiXezCyEjKc3tG4ja0kFjO8qzai26ZMPUEJfEy15ox5kJ0uKD\
\bi7i6dLXkuesVZ9JfHgjrctsLFt2NvovXnchsOvX05Y6LohlTNt5mkPFhUoXu1EZSJTIy\
\3fTU53b412r4AEusD7tcdRgH47yTr5hMO63bJnYBbmNperLHfiT1lP0MLQLh1J1DfoYBs\
\auoJOzvtAgvjHo6UFttnK6vZ3Cknpuob6uMS2MkJKmuoQsqsAYcRDWbJ2Rgw4bm2ndTM4\
\zFfuRDKvdrL6sDkuPNPYqxMWlqnXjSbU0eLtceZuKgXLHR8cdvsEvywt4JaZUQhnbq3Vl\
\7nZqcXdoi4XGTCgSGcGp8N0SDVhvkVh0QF1RVpWPnOMyYISJvuaHfo1zXMdq9tEdtJfID"
staticText =
"Kmyw4lDSE5S4fSH6etNouiXezCyEjKc3tG4ja0kFjO8qzai26ZMPUEJfEy15ox5kJ0uKD\
\bi7i6dLXkuesVZ9JfHgjrctsLFt2NvovXnchsOvX05Y6LohlTNt5mkPFhUoXu1EZSJTIy\
\3fTU53b412r4AEusD7tcdRgH47yTr5hMO63bJnYBbmNperLHfiT1lP0MLQLh1J1DfoYBs\
\auoJOzvtAgvjHo6UFttnK6vZ3Cknpuob6uMS2MkJKmuoQsqsAYcRDWbJ2Rgw4bm2ndTM4\
\zFfuRDKvdrL6sDkuPNPYqxMWlqnXjSbU0eLtceZuKgXLHR8cdvsEvywt4JaZUQhnbq3Vl\
\7nZqcXdoi4XGTCgSGcGp8N0SDVhvkVh0QF1RVpWPnOMyYISJvuaHfo1zXMdq9tEdtJfID"

getText :: Int -> Int -> Text
getText offset len = T.take len $ T.drop offset staticText
Loading

0 comments on commit 5bb23a8

Please sign in to comment.