Contents:
ignore this section. It is just my ramblings and notes
I wrote unit tests within the contract scope. Ref this file
beforeAll()
/beforeEach()
/afterEach
/afterAll
normal test functions:
priv_circ_pub_inputs
- example wrappers could be getReturnValues()
, getNewNullifiers()
, getNewReadRequests()
…Private/PublicContextInputs
e.g. set msg.sender()storageWrite
requires hash values to provided that isn't used.getNotes
which require 14+ values to be mocked:let mut returnVal = [0; VIEW_NOTE_ORACLE_RETURN_LENGTH];
returnVal[0] = 1; // return header - num_notes
returnVal[1] = this_address; // return header - contract_address
returnVal[2] = 1; // return header - nonce
returnVal[3] = 0; // return header - is_transient
returnVal[4] = note1.value1
returnVal[5] = note1.value2
....
OracleMock::mock("getNotes").withParams(...).returns(returnVal)
This is really powerful and great! A lot of Aztec.nr is currently untested and as is, we can use it for testing! However, there are some inconsistencies/bugs/faeture requests that I have based on my hacking:
let pub_circ_pub_inputs.return_values[0] = 11;
assert_eq(pub_circ_pub_inputs.return_values[0], 10);
this says Failed to solve brillig function, reason: explicit trap hit in brillig
.
Proposed Improvement: expected 10. got 11
Needed for checking that only x many storage slots were written or external function was only called x times. Current work around is mock.times(x) which can test if oracle was called more than x times, but not less than x times.
beforeEach/beforeAll
a.
#[oracle(notifyCreatedNote)]
fn notify_created_note_oracle<N>(_storage_slot: Field, _serialized_note: [Field; N], _inner_note_hash: Field) -> Field {}
We can expect devs to know _storage_slot
and _serialized_note
but not _inner_note_hash
b. Or consider this oracle with so many parameters:
#[oracle(getNotes)]
fn get_notes_oracle<N, S>(
_storage_slot: Field,
_num_selects: u8,
_select_by: [u8; N],
_select_values: [Field; N],
_sort_by: [u8; N],
_sort_order: [u2; N],
_limit: u32,
_offset: u32,
_return_size: u32,
_placeholder_fields: [Field; S]
) -> [Field; S] {}
[Unsure how this changes with the contract keyword being removed]
e.g. in a new file. May require new type, TestContract
. Depends on how we handle deprecation of the contract keyword…
#[aztec(private)]
fn add_rand(b: Field) -> Field {
rand() + b
}
#[test]
unconstrained fn test_add_rand() {
OracleMock::mock("getRandomField").returns(3).times(1);
let priv_circ_pub_inputs = add_rand(PrivateContextInputs::empty(), 2);
assert(2.lt(priv_circ_pub_inputs.return_values[0]), "Is not less than!!");
}
Crashes. but if I change times(2)
then works
enqueuePublicFunctionCall
, then the test works (just mocks callPrivateFunction
but if I uncomment, then it says callPrivateFunction
not mocked but it is with the expected params!)