This commit is contained in:
Pranav Gaddamadugu 2023-04-12 11:50:00 -07:00
parent 69f4a20aca
commit 64548699a9
6 changed files with 47 additions and 36 deletions

View File

@ -49,5 +49,3 @@ pub struct OutputOptions {
/// If enabled writes the AST after dead code elimination.
pub dce_ast: bool,
}

View File

@ -27,10 +27,10 @@ use leo_test_framework::{
use snarkvm::prelude::*;
use crate::utilities::{get_build_options, get_cwd_option, hash_asts, hash_content, setup_build_directory};
use leo_compiler::{CompilerOptions, OutputOptions};
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::{fs, path::Path, rc::Rc};
use leo_compiler::{CompilerOptions, OutputOptions};
struct CompileNamespace;
@ -79,11 +79,12 @@ fn run_test(test: Test, handler: &Handler) -> Result<Value, ()> {
flattened_ast: true,
inlined_ast: true,
dce_ast: true,
}
},
};
// Parse the program.
let mut parsed = handler.extend_if_error(parse_program(handler, &test.content, cwd.clone(), Some(compiler_options)))?;
let mut parsed =
handler.extend_if_error(parse_program(handler, &test.content, cwd.clone(), Some(compiler_options)))?;
// Compile the program to bytecode.
let program_name = format!("{}.{}", parsed.program_name, parsed.network);

View File

@ -37,12 +37,12 @@ use leo_test_framework::{
use snarkvm::{console, prelude::*};
use leo_compiler::{CompilerOptions, OutputOptions};
use leo_test_framework::test::TestExpectationMode;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::{collections::BTreeMap, fs, path::Path, rc::Rc};
use leo_compiler::{CompilerOptions, OutputOptions};
// TODO: Evaluate namespace.
struct ExecuteNamespace;
@ -99,19 +99,25 @@ fn run_test(test: Test, handler: &Handler, err_buf: &BufferEmitter) -> Result<Va
flattened_ast: true,
inlined_ast: true,
dce_ast: true,
}
},
};
// Parse the program.
let mut parsed = handler.extend_if_error(parse_program(handler, &test.content, cwd.clone(), Some(compiler_options)))?;
let mut parsed =
handler.extend_if_error(parse_program(handler, &test.content, cwd.clone(), Some(compiler_options)))?;
// Compile the program to bytecode.
let program_name = format!("{}.{}", parsed.program_name, parsed.network);
let bytecode = handler.extend_if_error(compile_and_process(&mut parsed))?;
// Extract the cases from the test config.
let all_cases =
test.config.extra.get("cases").expect("An `Execute` config must have a `cases` field.").as_mapping().unwrap();
let all_cases = test
.config
.extra
.get("cases")
.expect("An `Execute` config must have a `cases` field.")
.as_mapping()
.unwrap();
// Initialize a map for the expected results.
let mut results = BTreeMap::new();

View File

@ -26,6 +26,7 @@ use leo_test_framework::Test;
use snarkvm::prelude::*;
use leo_test_framework::test::TestConfig;
use snarkvm::{file::Manifest, package::Package};
use std::{
cell::RefCell,
@ -34,7 +35,6 @@ use std::{
path::{Path, PathBuf},
rc::Rc,
};
use leo_test_framework::test::TestConfig;
pub type Network = Testnet3;
#[allow(unused)]
@ -67,15 +67,28 @@ pub fn get_build_options(test_config: &TestConfig) -> Vec<BuildOptions> {
match test_config.extra.get("configs") {
Some(configs) => {
// Parse the sequence of compiler configurations.
configs.as_sequence().unwrap().iter().map(|config| {
let config = config.as_mapping().unwrap();
assert_eq!(config.len(), 1, "A compiler configuration must have exactly one key-value pair. e.g. `dce_enabled`: true");
configs
.as_sequence()
.unwrap()
.iter()
.map(|config| {
let config = config.as_mapping().expect("Expected the compiler configuration to be a mapping.");
assert_eq!(
config.len(),
1,
"A compiler configuration must have exactly one key-value pair. e.g. `dce_enabled`: true"
);
BuildOptions {
dce_enabled: config.get(&serde_yaml::Value::String("dce_enabled".to_string())).expect("Expected key `dce_enabled`").as_bool().unwrap(),
dce_enabled: config
.get(&serde_yaml::Value::String("dce_enabled".to_string()))
.expect("Expected key `dce_enabled`")
.as_bool()
.expect("Expected value to be a boolean."),
}
}).collect()
})
.collect()
}
None => vec![ BuildOptions { dce_enabled: true } ],
None => vec![BuildOptions { dce_enabled: true }],
}
}
@ -102,18 +115,15 @@ pub fn setup_build_directory(program_name: &str, bytecode: &String, handler: &Ha
handler.extend_if_error(Package::<Testnet3>::open(&directory).map_err(LeoError::Anyhow))
}
pub fn new_compiler(handler: &Handler, main_file_path: PathBuf, compiler_options: Option<CompilerOptions>) -> Compiler<'_> {
pub fn new_compiler(
handler: &Handler,
main_file_path: PathBuf,
compiler_options: Option<CompilerOptions>,
) -> Compiler<'_> {
let output_dir = PathBuf::from("/tmp/output/");
fs::create_dir_all(output_dir.clone()).unwrap();
Compiler::new(
String::from("test"),
String::from("aleo"),
handler,
main_file_path,
output_dir,
compiler_options
)
Compiler::new(String::from("test"), String::from("aleo"), handler, main_file_path, output_dir, compiler_options)
}
pub fn parse_program<'a>(

View File

@ -71,9 +71,7 @@ pub struct BuildOptions {
impl From<BuildOptions> for CompilerOptions {
fn from(options: BuildOptions) -> Self {
let mut out_options = Self {
build: leo_compiler::BuildOptions {
dce_enabled: options.enable_dce,
},
build: leo_compiler::BuildOptions { dce_enabled: options.enable_dce },
output: OutputOptions {
spans_enabled: options.enable_spans,
initial_input_ast: options.enable_initial_input_ast_snapshot,
@ -83,7 +81,7 @@ impl From<BuildOptions> for CompilerOptions {
flattened_ast: options.enable_flattened_ast_snapshot,
inlined_ast: options.enable_inlined_ast_snapshot,
dce_ast: options.enable_dce_ast_snapshot,
}
},
};
if options.enable_all_ast_snapshots {
out_options.output.initial_input_ast = true;

View File

@ -86,9 +86,7 @@ fn new_compiler(handler: &Handler) -> Compiler<'_> {
PathBuf::from(String::new()),
PathBuf::from(String::new()),
Some(CompilerOptions {
build: BuildOptions {
dce_enabled: true,
},
build: BuildOptions { dce_enabled: true },
output: OutputOptions {
spans_enabled: false,
initial_ast: false,
@ -98,7 +96,7 @@ fn new_compiler(handler: &Handler) -> Compiler<'_> {
flattened_ast: false,
inlined_ast: false,
dce_ast: false,
}
},
}),
)
}