-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Oracle mocker for nargo test (#2928)
Co-authored-by: Maxim Vezenov <[email protected]>
- Loading branch information
1 parent
285aa21
commit 0dd1e77
Showing
9 changed files
with
242 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#[oracle(create_mock)] | ||
unconstrained fn create_mock_oracle<N>(_name: str<N>) -> Field {} | ||
|
||
#[oracle(set_mock_params)] | ||
unconstrained fn set_mock_params_oracle<P>(_id: Field, _params: P) {} | ||
|
||
#[oracle(set_mock_returns)] | ||
unconstrained fn set_mock_returns_oracle<R>(_id: Field, _returns: R) {} | ||
|
||
#[oracle(set_mock_times)] | ||
unconstrained fn set_mock_times_oracle(_id: Field, _times: u64) {} | ||
|
||
#[oracle(clear_mock)] | ||
unconstrained fn clear_mock_oracle(_id: Field) {} | ||
|
||
struct OracleMock { | ||
id: Field, | ||
} | ||
|
||
impl OracleMock { | ||
unconstrained pub fn mock<N>(name: str<N>) -> Self { | ||
Self { | ||
id: create_mock_oracle(name), | ||
} | ||
} | ||
|
||
unconstrained pub fn with_params<P>(self, params: P) -> Self { | ||
set_mock_params_oracle(self.id, params); | ||
self | ||
} | ||
|
||
unconstrained pub fn returns<R>(self, returns: R) -> Self { | ||
set_mock_returns_oracle(self.id, returns); | ||
self | ||
} | ||
|
||
unconstrained pub fn times(self, times: u64) -> Self { | ||
set_mock_times_oracle(self.id, times); | ||
self | ||
} | ||
|
||
unconstrained pub fn clear(self) { | ||
clear_mock_oracle(self.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tooling/nargo_cli/tests/execution_success/mock_oracle/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "mock_oracle" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
tooling/nargo_cli/tests/execution_success/mock_oracle/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "10" | ||
|
30 changes: 30 additions & 0 deletions
30
tooling/nargo_cli/tests/execution_success/mock_oracle/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use dep::std::test::OracleMock; | ||
|
||
struct Point { | ||
x: Field, | ||
y: Field, | ||
} | ||
|
||
#[oracle(foo)] | ||
unconstrained fn foo_oracle(_point: Point, _array: [Field; 4]) -> Field {} | ||
|
||
unconstrained fn main() { | ||
let array = [1,2,3,4]; | ||
let another_array = [4,3,2,1]; | ||
let point = Point { | ||
x: 14, | ||
y: 27, | ||
}; | ||
|
||
OracleMock::mock("foo").returns(42).times(1); | ||
let mock = OracleMock::mock("foo").returns(0); | ||
assert_eq(42, foo_oracle(point, array)); | ||
assert_eq(0, foo_oracle(point, array)); | ||
mock.clear(); | ||
|
||
OracleMock::mock("foo").with_params((point, array)).returns(10); | ||
OracleMock::mock("foo").with_params((point, another_array)).returns(20); | ||
assert_eq(10, foo_oracle(point, array)); | ||
assert_eq(20, foo_oracle(point, another_array)); | ||
} | ||
|