Skip to content

Commit

Permalink
feat(random.ts): add support for generating random MayUseToken values
Browse files Browse the repository at this point in the history
This commit introduces a new function `mayUseTokenInvalid()` which generates random `MayUseToken` values. The function creates a record with `parentsOwnToken` and `inheritFromParent` fields of type boolean.

It also defines an `invalid` case where both `parentsOwnToken` and `inheritFromParent` are set to true, which is an invalid state. This invalid case is added to the `mayUseToken` generator using `Object.assign`.

The `mayUseToken` generator is then added to the `Generators` and `JsonGenerators` objects to make it available for generating random `MayUseToken` values in tests.

This change allows for more comprehensive testing of the `MayUseToken` type and its usage in the codebase.
  • Loading branch information
MartinMinkov committed Jul 9, 2024
1 parent d96cfd9 commit 48ba34d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/testing/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ZkappUri,
PublicKey,
StateHash,
MayUseToken,
} from '../../bindings/mina-transaction/transaction-leaves-bigint.js';
import { genericLayoutFold } from '../../bindings/lib/from-layout.js';
import { jsLayout } from '../../bindings/mina-transaction/gen/js-layout.js';
Expand Down Expand Up @@ -115,6 +116,7 @@ const actionState = oneOf(ActionState.empty(), field);
const verificationKeyHash = oneOf(VerificationKeyHash.empty(), field);
const receiptChainHash = oneOf(ReceiptChainHash.empty(), field);
const zkappUri = map(string(nat(50)), ZkappUri.fromJSON);
const mayUseToken = mayUseTokenInvalid();

type Types = typeof TypeMap & typeof customTypes & PrimitiveTypeMap<bigint>;
type Generators = {
Expand Down Expand Up @@ -143,6 +145,7 @@ const Generators: Generators = {
string: base58(nat(50)), // TODO replace various strings, like signature, with parsed types
number: nat(3),
TransactionVersion: uint32,
MayUseToken: mayUseToken,
};
let typeToBigintGenerator = new Map<Signable<any, any>, Random<any>>(
[TypeMap, primitiveTypeMap, customTypes]
Expand Down Expand Up @@ -251,6 +254,7 @@ const JsonGenerators: JsonGenerators = {
string: base58(nat(50)),
number: nat(3),
TransactionVersion: json_.uint32,
MayUseToken: mayUseToken,
};
let typeToJsonGenerator = new Map<Signable<any, any>, Random<any>>(
[TypeMap, primitiveTypeMap, customTypes]
Expand Down Expand Up @@ -884,6 +888,12 @@ function publicKeyWithInvalid() {
return Object.assign(publicKey, { invalid });
}

function mayUseTokenInvalid() {
let mayUseToken = record({ parentsOwnToken: bool, inheritFromParent: bool });
let both = map(mayUseToken, (m) => ({ ...m, parentsOwnToken: true }));
return Object.assign(mayUseToken, { invalid: oneOf(both) });
}

/**
* invalid arrays are sampled by generating an array with exactly one invalid input (and any number of valid inputs);
* (note: invalid arrays have the same length distribution as valid ones, except that they are never empty)
Expand Down

0 comments on commit 48ba34d

Please sign in to comment.