Skip to content

Commit f41fcfe

Browse files
committed
Add unit test for 'patternFromText'.
1 parent ac34b45 commit f41fcfe

File tree

8 files changed

+187
-11
lines changed

8 files changed

+187
-11
lines changed

.hlint.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
- ignore: {name: "Use newTMVarIO"}
5656
- ignore: {name: "Use newEmptyTMVarIO"}
5757
- ignore: {name: "Use newTVarIO"}
58+
- ignore: {name: "Use catMaybes"}
59+
- ignore: {name: "Use head"}
5860

5961
# Add custom hints for this project
6062
#

kupo.cabal

+60-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.yaml

+17-2
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ library:
3636
dependencies:
3737
- aeson
3838
- base16
39+
- base64
3940
- bech32
4041
- bech32-th
4142
- binary
4243
- bytestring
43-
- cardano-binary
4444
- cardano-crypto-class
4545
- cardano-ledger-alonzo
4646
- cardano-ledger-shelley
4747
- cardano-ledger-shelley-ma
4848
- cardano-ledger-byron
4949
- cardano-ledger-core
50-
- cardano-protocol-tpraos
5150
- cardano-slotting
5251
- containers
5352
- contra-tracer
@@ -73,6 +72,22 @@ library:
7372
- unix
7473
- yaml
7574

75+
tests:
76+
unit:
77+
main: Spec.hs
78+
source-dirs: test
79+
ghc-options: *ghc-options-test
80+
dependencies:
81+
- base
82+
- bytestring
83+
- containers
84+
- hspec
85+
- kupo
86+
- relude
87+
- QuickCheck
88+
build-tools:
89+
- hspec-discover
90+
7691
executables:
7792
kupo:
7893
main: Main.hs

src/Kupo/Configuration.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import Kupo.Data.ChainSync
5353
, Point (..)
5454
, SlotNo (..)
5555
)
56-
import Kupo.Data.Pattern.Address
56+
import Kupo.Data.Pattern
5757
( Pattern (..), patternFromText )
5858
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
5959
( SystemStart (..) )

src/Kupo/Data/Pattern/Address.hs src/Kupo/Data/Pattern.hs

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{-# LANGUAGE QuasiQuotes #-}
66

7-
module Kupo.Data.Pattern.Address
7+
module Kupo.Data.Pattern
88
( -- * Pattern
99
Pattern (..)
1010
, patternFromText
@@ -22,8 +22,6 @@ import Kupo.Prelude
2222

2323
import Codec.Binary.Bech32.TH
2424
( humanReadablePart )
25-
import Data.ByteString.Base16
26-
( decodeBase16 )
2725
import Kupo.Data.ChainSync
2826
( Address
2927
, Blake2b_224
@@ -90,10 +88,10 @@ patternFromText txt =
9088
if | payment == wildcard && delegation == wildcard ->
9189
pure (MatchAny onlyShelley)
9290
| payment == wildcard ->
93-
MatchPayment
91+
MatchDelegation
9492
<$> readerCredential delegation
9593
| delegation == wildcard ->
96-
MatchDelegation
94+
MatchPayment
9795
<$> readerCredential payment
9896
| otherwise -> do
9997
MatchPaymentAndDelegation

src/Kupo/Prelude.hs

+8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ module Kupo.Prelude
99
, view
1010
, (^.)
1111
, (^?)
12+
, encodeBase16
13+
, decodeBase16
14+
, encodeBase64
15+
, decodeBase64
1216
) where
1317

18+
import Data.ByteString.Base16
19+
( decodeBase16, encodeBase16 )
20+
import Data.ByteString.Base64
21+
( decodeBase64, encodeBase64 )
1422
import Data.Generics.Internal.VL.Lens
1523
( view, (^.) )
1624
import Data.Profunctor.Unsafe

test/Kupo/Data/PatternSpec.hs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
module Kupo.Data.PatternSpec
6+
( spec
7+
) where
8+
9+
import Kupo.Prelude
10+
11+
import Data.List
12+
( (!!) )
13+
import Kupo.Configuration
14+
( StandardCrypto )
15+
import Kupo.Data.ChainSync
16+
( Address, addressFromBytes )
17+
import Kupo.Data.Pattern
18+
( Pattern (..)
19+
, includingBootstrap
20+
, matching
21+
, onlyShelley
22+
, patternFromText
23+
)
24+
import Test.Hspec
25+
( Spec, context, parallel, shouldBe, specify )
26+
27+
spec :: Spec
28+
spec = parallel $ do
29+
context "patternFromText" $ forM_ patterns $ \(str, expectation) -> do
30+
specify (toString str) $ do
31+
patternFromText str `shouldBe` Just expectation
32+
33+
--
34+
-- Test Vectors
35+
--
36+
37+
patterns :: [(Text, Pattern StandardCrypto)]
38+
patterns =
39+
[ ( "*"
40+
, MatchAny includingBootstrap
41+
)
42+
, ( "*/*"
43+
, MatchAny onlyShelley
44+
)
45+
, ( "addr1vyc29pvl2uyzqt8nwxrcxnf558ffm27u3d9calxn8tdudjgz4xq9p"
46+
, MatchExact (addresses !! 0)
47+
)
48+
, ( "379bd7fd5493ebb21e199526b1a1b389ddb85cd70fccd4ca169bfdc4f59042ec/*"
49+
, MatchPayment (credentials !! 0)
50+
)
51+
, ( "adec17c2784d97ed403c011ab73aa32bdf74ee10ce44258bf72c256b/*"
52+
, MatchPayment (credentials !! 0)
53+
)
54+
, ( "*/379bd7fd5493ebb21e199526b1a1b389ddb85cd70fccd4ca169bfdc4f59042ec"
55+
, MatchDelegation (credentials !! 0)
56+
)
57+
, ( "*/stake_vkh14hkp0sncfkt76spuqydtww4r900hfmsseezztzlh9sjkkjx6d7q"
58+
, MatchDelegation (credentials !! 0)
59+
)
60+
, ( "addr_vkh14hkp0sncfkt76spuqydtww4r900hfmsseezztzlh9sjkkvjxtmr/\
61+
\script1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7r35m37"
62+
, MatchPaymentAndDelegation (credentials !! 0) (credentials !! 1)
63+
)
64+
, ( "stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw"
65+
, MatchDelegation (credentials !! 2)
66+
)
67+
, ( "script1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7r35m37/*"
68+
, MatchPayment (credentials !! 1)
69+
)
70+
]
71+
72+
addresses :: [Address StandardCrypto]
73+
addresses =
74+
[ addr
75+
| Just addr <- either (const Nothing) addressFromBytes . decodeBase16 <$>
76+
[ "6130a2859f5708202cf37187834d34a1d29dabdc8b4b8efcd33adbc6c9"
77+
]
78+
]
79+
80+
credentials :: [ByteString]
81+
credentials =
82+
[ credential
83+
| Right credential <- decodeBase16 <$>
84+
-- Verification Key Hash
85+
[ "adec17c2784d97ed403c011ab73aa32bdf74ee10ce44258bf72c256b"
86+
-- Script Hash
87+
, "c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f"
88+
-- Verification Key Hash
89+
, "337b62cfff6403a06a3acbc34f8c46003c69fe79a3628cefa9c47251"
90+
]
91+
]

test/Spec.hs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- This Source Code Form is subject to the terms of the Mozilla Public
2+
-- License, v. 2.0. If a copy of the MPL was not distributed with this
3+
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 commit comments

Comments
 (0)