Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Native tests. #28449

Open
wants to merge 25 commits into
base: mainnet
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**/target
/tmp/
/temp/
**.idea/
*.DS_Store
.vscode
Expand All @@ -23,4 +24,4 @@ sccache*/
*.bat

# environment
.env
.env
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ workspace = true
[dependencies.leo-compiler]
workspace = true

[dependencies.leo-disassembler]
workspace = true

[dependencies.leo-errors]
workspace = true

Expand Down Expand Up @@ -190,6 +193,9 @@ version = "0.15.7"
[dependencies.indexmap]
workspace = true

[dependencies.num_cpus]
version = "1.16.0"

[dependencies.rand]
workspace = true

Expand Down Expand Up @@ -229,6 +235,9 @@ features = [ "fmt" ]
[dependencies.crossterm]
version = "0.28.1"

[dependencies.rayon]
version = "1.10.0"

[dependencies.rpassword]
version = "7.3.1"

Expand Down
18 changes: 17 additions & 1 deletion compiler/ast/src/functions/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{Identifier, Node, NodeID, simple_node_impl};

use leo_span::Span;

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt;

Expand All @@ -27,6 +28,8 @@ pub struct Annotation {
// TODO: Consider using a symbol instead of an identifier.
/// The name of the annotation.
pub identifier: Identifier,
/// The data associated with the annotation.
pub data: IndexMap<Identifier, Option<String>>,
/// A span locating where the annotation occurred in the source.
pub span: Span,
/// The ID of the node.
Expand All @@ -37,6 +40,19 @@ simple_node_impl!(Annotation);

impl fmt::Display for Annotation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "@{}", self.identifier)
let data = match self.data.is_empty() {
true => "".to_string(),
false => {
let mut string = String::new();
for (key, value) in self.data.iter() {
match value {
None => string.push_str(&format!("{key},")),
Some(value) => string.push_str(&format!("{key} = \"{value}\",")),
}
}
format!("({string})")
}
};
write!(f, "@{}{}", self.identifier, data)
}
}
13 changes: 13 additions & 0 deletions compiler/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub use self::program::*;
pub mod statement;
pub use self::statement::*;

pub mod test;
pub use self::test::*;

pub mod types;
pub use self::types::*;

Expand All @@ -70,6 +73,7 @@ use leo_errors::{AstError, Result};
///
/// The [`Ast`] type represents a Leo program as a series of recursive data types.
/// These data types form a tree that begins from a [`Program`] type root.
// TODO: Clean up by removing the `Ast` type and renaming the exiting `Program` type to `Ast`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Ast {
pub ast: Program,
Expand All @@ -81,6 +85,15 @@ impl Ast {
Self { ast: program }
}

/// Combines the two ASTs into a single AST.
/// The ASTs are combined by extending the components of the first AST with the components of the second AST.
pub fn combine(&mut self, other: Self) {
let Program { imports, stubs, program_scopes } = other.ast;
self.ast.imports.extend(imports);
self.ast.stubs.extend(stubs);
self.ast.program_scopes.extend(program_scopes);
}

/// Returns a reference to the inner program AST representation.
pub fn as_repr(&self) -> &Program {
&self.ast
Expand Down
6 changes: 3 additions & 3 deletions compiler/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,6 @@ pub trait ProgramReconstructor: StatementReconstructor {
fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
ProgramScope {
program_id: input.program_id,
structs: input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect(),
mappings: input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect(),
functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(),
consts: input
.consts
.into_iter()
Expand All @@ -454,6 +451,9 @@ pub trait ProgramReconstructor: StatementReconstructor {
_ => unreachable!("`reconstruct_const` can only return `Statement::Const`"),
})
.collect(),
structs: input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect(),
mappings: input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect(),
functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(),
span: input.span,
}
}
Expand Down
5 changes: 1 addition & 4 deletions compiler/ast/src/passes/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,10 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> {
}

fn visit_program_scope(&mut self, input: &'a ProgramScope) {
input.consts.iter().for_each(|(_, c)| (self.visit_const(c)));
input.structs.iter().for_each(|(_, c)| (self.visit_struct(c)));

input.mappings.iter().for_each(|(_, c)| (self.visit_mapping(c)));

input.functions.iter().for_each(|(_, c)| (self.visit_function(c)));

input.consts.iter().for_each(|(_, c)| (self.visit_const(c)));
}

fn visit_stub(&mut self, _input: &'a Stub) {}
Expand Down
56 changes: 56 additions & 0 deletions compiler/ast/src/test/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::ProgramId;
use snarkvm::prelude::{Network, PrivateKey};

use serde::{Deserialize, Serialize};

/// A manifest describing the tests to be run and their associated metadata.
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct TestManifest<N: Network> {
/// The program ID.
pub program_id: String,
/// The tests to be run.
pub tests: Vec<TestMetadata<N>>,
}

impl<N: Network> TestManifest<N> {
/// Create a new test manifest.
pub fn new(program_id: &ProgramId) -> Self {
Self { program_id: program_id.to_string(), tests: Vec::new() }
}

/// Add a test to the manifest.
pub fn add_test(&mut self, test: TestMetadata<N>) {
self.tests.push(test);
}
}

/// Metadata associated with a test.
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct TestMetadata<N: Network> {
/// The name of the function.
pub function_name: String,
/// The private key to run the test with.
pub private_key: Option<PrivateKey<N>>,
/// The seed for the RNG.
pub seed: Option<u64>,
/// Whether or not the test is expected to fail.
pub should_fail: bool,
}
18 changes: 18 additions & 0 deletions compiler/ast/src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

mod manifest;
pub use manifest::*;
Loading