|
| 1 | +module Codec.Binary.Bech32.THSpec |
| 2 | + ( spec |
| 3 | + ) where |
| 4 | + |
| 5 | +import Prelude |
| 6 | + |
| 7 | +import Codec.Binary.Bech32 |
| 8 | + ( CharPosition (..) |
| 9 | + , HumanReadablePartError (..) |
| 10 | + , humanReadableCharMaxBound |
| 11 | + , humanReadableCharMinBound |
| 12 | + , humanReadablePartMaxLength |
| 13 | + , humanReadablePartMinLength |
| 14 | + ) |
| 15 | +import Codec.Binary.Bech32.TH |
| 16 | + ( humanReadablePart ) |
| 17 | +import Control.Monad |
| 18 | + ( forM_ ) |
| 19 | +import Language.Haskell.TH.Quote |
| 20 | + ( QuasiQuoter (quoteExp) ) |
| 21 | +import Language.Haskell.TH.Syntax |
| 22 | + ( Exp (..), runQ ) |
| 23 | +import Test.Hspec |
| 24 | + ( Spec, describe, it, shouldSatisfy, shouldThrow ) |
| 25 | + |
| 26 | +spec :: Spec |
| 27 | +spec = |
| 28 | + describe "Quasi-Quotations" $ |
| 29 | + |
| 30 | + describe "Human-Readable Prefixes" $ do |
| 31 | + let mkHumanReadablePartExp = runQ . quoteExp humanReadablePart |
| 32 | + |
| 33 | + describe "Parsing valid human-readable prefixes should succeed." $ |
| 34 | + forM_ validHumanReadableParts $ \hrp -> |
| 35 | + it (show hrp) $ |
| 36 | + mkHumanReadablePartExp hrp >>= |
| 37 | + (`shouldSatisfy` isAppE) |
| 38 | + |
| 39 | + describe "Parsing invalid human-readable prefixes should fail." $ |
| 40 | + forM_ invalidHumanReadableParts $ \(hrp, expectedError) -> |
| 41 | + it (show hrp) $ |
| 42 | + mkHumanReadablePartExp hrp |
| 43 | + `shouldThrow` (== expectedError) |
| 44 | + |
| 45 | +-- | Matches only function application expressions. |
| 46 | +-- |
| 47 | +isAppE :: Exp -> Bool |
| 48 | +isAppE AppE {} = True |
| 49 | +isAppE _ = False |
| 50 | + |
| 51 | +-- | A selection of valid human-readable prefixes, that when parsed with the |
| 52 | +-- 'humanReadablePart' quasiquoter should not result in an exception. |
| 53 | +-- |
| 54 | +-- Note that this is not by any means intended to be an exhaustive list. |
| 55 | +-- The underlying parsing logic, provided by `humanReadablePartFromText`, |
| 56 | +-- is already tested in the `bech32` package. |
| 57 | +-- |
| 58 | +validHumanReadableParts :: [String] |
| 59 | +validHumanReadableParts = |
| 60 | + [ replicate humanReadablePartMinLength humanReadableCharMinBound |
| 61 | + , replicate humanReadablePartMaxLength humanReadableCharMaxBound |
| 62 | + , "addr" |
| 63 | + ] |
| 64 | + |
| 65 | +-- | A selection of invalid human-readable prefixes, along with the errors that |
| 66 | +-- we expect to see if we attempt to parse them with the 'humanReadablePart' |
| 67 | +-- quasi-quoter. |
| 68 | +-- |
| 69 | +-- Note that this is not by any means intended to be an exhaustive list. |
| 70 | +-- The underlying parsing logic, provided by `humanReadablePartFromText`, |
| 71 | +-- is already tested in the `bech32` package. |
| 72 | +-- |
| 73 | +invalidHumanReadableParts :: [(String, HumanReadablePartError)] |
| 74 | +invalidHumanReadableParts = |
| 75 | + [ ( replicate (pred minLen) minChar |
| 76 | + , HumanReadablePartTooShort |
| 77 | + ) |
| 78 | + , ( replicate (succ maxLen) maxChar |
| 79 | + , HumanReadablePartTooLong |
| 80 | + ) |
| 81 | + , ( replicate (succ minLen) (pred minChar) |
| 82 | + , HumanReadablePartContainsInvalidChars (CharPosition <$> [0 .. minLen]) |
| 83 | + ) |
| 84 | + , ( replicate (succ minLen) (succ maxChar) |
| 85 | + , HumanReadablePartContainsInvalidChars (CharPosition <$> [0 .. minLen]) |
| 86 | + ) |
| 87 | + ] |
| 88 | + where |
| 89 | + minChar = humanReadableCharMinBound |
| 90 | + maxChar = humanReadableCharMaxBound |
| 91 | + minLen = humanReadablePartMinLength |
| 92 | + maxLen = humanReadablePartMaxLength |
0 commit comments