Skip to content
Permalink
Browse files

use strict 'isLeft' for bech32 internal tests

  • Loading branch information...
KtorZ committed Jun 12, 2019
1 parent 5b4dfd9 commit 8efaf5ce5b353b21345bbce9344e5dbb4f900271
Showing with 18 additions and 7 deletions.
  1. +2 −1 lib/bech32/bech32.cabal
  2. +16 −6 lib/bech32/test/Codec/Binary/Bech32Spec.hs
@@ -58,10 +58,11 @@ test-suite bech32-test
build-depends:
base
, bech32
, bytestring
, containers
, deepseq
, extra
, hspec
, bytestring
, QuickCheck
, text
, vector
@@ -1,5 +1,6 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
@@ -27,6 +28,8 @@ import Codec.Binary.Bech32.Internal
, humanReadablePartToText
, separatorChar
)
import Control.DeepSeq
( deepseq )
import Control.Monad
( forM_, replicateM )
import Data.Bits
@@ -36,7 +39,7 @@ import Data.ByteString
import Data.Char
( chr, isUpper, ord, toLower, toUpper )
import Data.Either
( fromRight, isLeft, isRight )
( fromRight, isRight )
import Data.Either.Extra
( eitherToMaybe )
import Data.Functor.Identity
@@ -96,7 +99,7 @@ spec = do
let checksumCorrupted =
(hrp `T.snoc` (chr (ord first `xor` 1)))
`T.append` rest'
(Bech32.decode checksumCorrupted) `shouldSatisfy` isLeft
(Bech32.decode checksumCorrupted) `shouldSatisfy` isLeft'
-- test that re-encoding the decoded checksum results in
-- the same checksum.
let checksumEncoded = Bech32.encode resultHRP resultData
@@ -171,7 +174,7 @@ spec = do
char1 /= char2 ==>
(T.length corruptedString === T.length originalString)
.&&.
(Bech32.decode corruptedString `shouldSatisfy` isLeft)
(Bech32.decode corruptedString `shouldSatisfy` isLeft')

it "Decoding fails when a character is omitted." $
property $ withMaxSuccess 10000 $ \s -> do
@@ -189,7 +192,7 @@ spec = do
return $ counterexample description $
(T.length corruptedString === T.length originalString - 1)
.&&.
(Bech32.decode corruptedString `shouldSatisfy` isLeft)
(Bech32.decode corruptedString `shouldSatisfy` isLeft')
.||.
-- In the case where the tail of a valid Bech32 string is
-- composed of one or more consecutive 'q' characters
@@ -224,7 +227,7 @@ spec = do
"inserted into the middle" $
(T.length corruptedString === T.length originalString + 1)
.&&.
(Bech32.decode corruptedString `shouldSatisfy` isLeft)
(Bech32.decode corruptedString `shouldSatisfy` isLeft')
.||.
-- In the case where the last character of a valid Bech32
-- string is the character 'p', inserting any number of
@@ -256,7 +259,7 @@ spec = do
corruptedString /= originalString ==>
(T.length corruptedString === T.length originalString)
.&&.
(result `shouldSatisfy` isLeft)
(result `shouldSatisfy` isLeft')

it "Decoding fails for an upper-case string with a lower-case \
\character." $
@@ -625,3 +628,10 @@ isMixedCase :: Text -> Bool
isMixedCase t =
T.toUpper t /= t &&
T.toLower t /= t

-- | Strict `isLeft`, evaluate the left-side and return 'True' if 'Left'.
-- 'False' otherwise.
isLeft' :: Show e => Either e a -> Bool
isLeft' = \case
Left e -> show e `deepseq` True
Right _ -> False

0 comments on commit 8efaf5c

Please sign in to comment.
You can’t perform that action at this time.