/
Fuzz.elm
78 lines (63 loc) · 2.53 KB
/
Fuzz.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module Tests.EthABI.Fuzz exposing (..)
{-| Contains fuzzers to test the rest of EthABI with.
-}
import Fuzz exposing (Fuzzer)
import EthABI.Types.Int
import EthABI.Types.UInt
import EthABI.Types.Bytes32
import EthABI.Types.Bytes
import EthABI.Types.Hexstring exposing (Hexstring)
import EthABI.Encode as Encode
import EthABI.Decode as Decode
import BigInt
import Random
import String
int256 : Fuzzer (Result String EthABI.Types.Int.Int256)
int256 =
Fuzz.int |> Fuzz.map (BigInt.fromInt >> EthABI.Types.Int.int256)
uint256 : Fuzzer (Result String EthABI.Types.UInt.UInt256)
uint256 =
Fuzz.intRange 0 Random.maxInt |> Fuzz.map (BigInt.fromInt >> EthABI.Types.UInt.uint256)
{-| TODO: Probably more useful if we can read the actual length we created.
-}
static_bytes : Fuzzer (Result String EthABI.Types.Bytes32.Bytes32)
static_bytes =
let
bytes32Fuzzer str len =
str |> String.left len >> EthABI.Types.Bytes32.fromString len
in
Fuzz.map2 bytes32Fuzzer (Fuzz.string) (Fuzz.intRange 0 32)
bytes32 : Fuzzer (Result String EthABI.Types.Bytes32.Bytes32)
bytes32 =
let
bytes32Fuzzer len str =
str
|> String.padRight 32 ' ' -- map short strings to correct length
|> String.left len -- map long strings to correct length
>> EthABI.Types.Bytes32.fromString len
in
Fuzz.string |> Fuzz.map (bytes32Fuzzer 32)
bytes : Fuzzer EthABI.Types.Bytes.Bytes
bytes =
Fuzz.string |> Fuzz.map (EthABI.Types.Bytes.fromString)
encoder_decoder_triple : Fuzzer ( String, Result String Hexstring, String )
encoder_decoder_triple =
let
triple : Fuzzer (Result String a) -> (a -> Encode.Encoder) -> Decode.Decoder a -> Fuzzer ( String, Result String Hexstring, String )
triple fuzzer encoder decoder =
let
encoded val =
val |> Result.map (encoder >> Encode.encode)
decoded val =
encoded val |> (Result.andThen (Decode.decodeHexstring decoder)) |> toString
in
fuzzer |> Fuzz.map (\val -> ( toString val, encoded val, decoded val ))
in
Fuzz.oneOf
[ triple int256 Encode.int256 Decode.int256
, triple uint256 Encode.uint256 Decode.uint256
, triple (Fuzz.bool |> Fuzz.map Ok) Encode.bool Decode.bool
, triple bytes32 Encode.bytes32 Decode.bytes32
-- TODO: Rest of types, once built.
-- TODO: How to create a recursive fuzzer here, for the arrays?
]