[tests] Extract common examples into separate module

This commit is contained in:
Mark Spanbroek 2022-03-28 16:44:59 +02:00 committed by markspanbroek
parent 5b5f3335d6
commit bd8f4d5d74
3 changed files with 19 additions and 17 deletions

View File

@ -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 =

View File

@ -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())

12
tests/examples.nim Normal file
View File

@ -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)