From bd8f4d5d7452229050ad5de57a9358e0e2d63416 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 28 Mar 2022 16:44:59 +0200 Subject: [PATCH] [tests] Extract common examples into separate module --- tests/contracts/examples.nim | 8 ++++---- tests/dagger/examples.nim | 16 +++------------- tests/examples.nim | 12 ++++++++++++ 3 files changed, 19 insertions(+), 17 deletions(-) create mode 100644 tests/examples.nim diff --git a/tests/contracts/examples.nim b/tests/contracts/examples.nim index 752b353a..e817bf59 100644 --- a/tests/contracts/examples.nim +++ b/tests/contracts/examples.nim @@ -3,12 +3,12 @@ import pkg/stint import pkg/nimcrypto import pkg/ethers import dagger/contracts +import ../examples -proc randomBytes(amount: static int): array[amount, byte] = - doAssert randomBytes(result) == amount +export examples proc example*(_: type Address): Address = - Address(randomBytes(20)) + Address(array[20, byte].example) proc example*(_: type StorageRequest): StorageRequest = StorageRequest( @@ -19,7 +19,7 @@ proc example*(_: type StorageRequest): StorageRequest = proofProbability: 4.u256, # require a proof roughly once every 4 periods maxPrice: 84.u256, expiry: (getTime() + initDuration(hours=1)).toUnix.u256, - nonce: randomBytes(32) + nonce: array[32, byte].example ) proc example*(_: type StorageOffer): StorageOffer = diff --git a/tests/dagger/examples.nim b/tests/dagger/examples.nim index dceb099e..471f16bb 100644 --- a/tests/dagger/examples.nim +++ b/tests/dagger/examples.nim @@ -6,27 +6,17 @@ import pkg/stint import pkg/dagger/rng import pkg/dagger/stores import pkg/dagger/blocktype +import ../examples + +export examples proc example*(_: type EthAddress): EthAddress = EthPrivateKey.random().toPublicKey.toAddress -proc example*(_: type UInt256): UInt256 = - var bytes: array[32, byte] - for b in bytes.mitems: - b = rand(byte) - UInt256.fromBytes(bytes) - proc example*(_: type UInt48): UInt48 = # workaround for https://github.com/nim-lang/Nim/issues/17670 uint64.rand mod (UInt48.high + 1) -proc example*[T: SomeInteger](_: type T): T = - rand(T) - -proc example*[T,N](_: type array[N, T]): array[N, T] = - for item in result.mitems: - item = T.example - proc example*(_: type Wallet): Wallet = Wallet.init(EthPrivateKey.random()) diff --git a/tests/examples.nim b/tests/examples.nim new file mode 100644 index 00000000..63994083 --- /dev/null +++ b/tests/examples.nim @@ -0,0 +1,12 @@ +import std/random +import pkg/stint + +proc example*[T: SomeInteger](_: type T): T = + rand(T) + +proc example*[T,N](_: type array[N, T]): array[N, T] = + for item in result.mitems: + item = T.example + +proc example*(_: type UInt256): UInt256 = + UInt256.fromBytes(array[32, byte].example)