Skip to content

Testing framework: add Test.random for generating "random" values

E. Rivas requested to merge er433/test/random-gen into dev

This MR adds a new primitive Test.random : unit -> 'a which generates a random value of type 'a (if possible).

It can be handy for generating parameters of contracts, here's an example of usage:

$ cat random_incr.mligo 
// This is testme.mligo
type storage = int

type parameter =
  Increment of int
| Decrement of int
| Reset

type return = operation list * storage

// Two entrypoints
let add (store, delta : storage * int) : storage = store + delta
let sub (store, delta : storage * int) : storage = store - delta

(* Main access point that dispatches to the entrypoints according to
   the smart contract parameter. *)
let main (action, store : parameter * storage) : return =
 ([] : operation list),    // No operations
 (match action with
   Increment (n) -> add (store, n)
 | Decrement (n) -> sub (store, n)
 | Reset         -> 0)

let test =
  let (ta, _, _) = Test.originate main 0 0mutez in
  let c = Test.to_contract ta in
  let _ = List.iter (fun (_ : unit) ->
    let p = (Test.random () : parameter) in
    let () = Test.log p in
    Test.transfer_to_contract_exn c p 0mutez) [(); (); ()] in
  let v = Test.get_storage ta in
  let () = Test.log v in
  ()
$ dune exec -- ligo test random_incr.mligo 
Increment (8)           
Decrement (5)
Decrement (84)
-81
Everything at the top-level was executed.
- test exited with value ().
  • has a changelog entry
Edited by E. Rivas

Merge request reports

Loading