feat: new setup for the gen_uplc testing

* new test only module aiken_project::tests
* move TestProject to tests/mod.rs
* new tests go in gen_uplc.rs
This commit is contained in:
microproofs
2023-04-18 22:45:36 -04:00
committed by Kasey
parent e4276d7f5a
commit c2ee631d07
6 changed files with 568 additions and 583 deletions

View File

@@ -5,6 +5,7 @@ use super::{
schema::{Annotated, Schema},
};
use crate::module::{CheckedModule, CheckedModules};
use aiken_lang::{
ast::{TypedArg, TypedFunction, TypedValidator},
gen_uplc::CodeGenerator,
@@ -176,19 +177,14 @@ impl Validator {
#[cfg(test)]
mod test {
use assert_json_diff::assert_json_eq;
use indexmap::IndexMap;
use pretty_assertions::assert_eq;
use serde_json::{self, json};
use std::{collections::HashMap, path::PathBuf};
use std::collections::HashMap;
use aiken_lang::{
self,
ast::{Definition, Function, ModuleKind, Tracing, TypedDataType, TypedFunction},
ast::{Definition, Function},
builtins,
gen_uplc::builder::{DataTypeKey, FunctionAccessKey},
parser,
tipo::TypeInfo,
IdGenerator,
};
use uplc::{
ast::{self as uplc_ast, Constant, Name},
@@ -196,6 +192,8 @@ mod test {
optimize, BigInt, Constr, PlutusData,
};
use crate::tests::TestProject;
use super::{
super::{
definitions::{Definitions, Reference},
@@ -204,95 +202,6 @@ mod test {
},
*,
};
use crate::{module::ParsedModule, PackageName};
// TODO: Possible refactor this out of the module and have it used by `Project`. The idea would
// be to make this struct below the actual project, and wrap it in another metadata struct
// which contains all the config and I/O stuff regarding the project.
struct TestProject {
package: PackageName,
id_gen: IdGenerator,
module_types: HashMap<String, TypeInfo>,
functions: IndexMap<FunctionAccessKey, TypedFunction>,
data_types: IndexMap<DataTypeKey, TypedDataType>,
}
impl TestProject {
fn new() -> Self {
let id_gen = IdGenerator::new();
let package = PackageName {
owner: "test".to_owned(),
repo: "project".to_owned(),
};
let mut module_types = HashMap::new();
module_types.insert("aiken".to_string(), builtins::prelude(&id_gen));
module_types.insert("aiken/builtin".to_string(), builtins::plutus(&id_gen));
let functions = builtins::prelude_functions(&id_gen);
let data_types = builtins::prelude_data_types(&id_gen);
TestProject {
package,
id_gen,
module_types,
functions,
data_types,
}
}
fn parse(&self, source_code: &str) -> ParsedModule {
let kind = ModuleKind::Validator;
let name = "test_module".to_owned();
let (mut ast, extra) =
parser::module(source_code, kind).expect("Failed to parse module");
ast.name = name.clone();
ParsedModule {
kind,
ast,
code: source_code.to_string(),
name,
path: PathBuf::new(),
extra,
package: self.package.to_string(),
}
}
fn check(&mut self, module: ParsedModule) -> CheckedModule {
let mut warnings = vec![];
let ast = module
.ast
.infer(
&self.id_gen,
module.kind,
&self.package.to_string(),
&self.module_types,
Tracing::NoTraces,
&mut warnings,
)
.expect("Failed to type-check module");
self.module_types
.insert(module.name.clone(), ast.type_info.clone());
let mut checked_module = CheckedModule {
kind: module.kind,
extra: module.extra,
name: module.name,
code: module.code,
package: module.package,
input_path: module.path,
ast,
};
checked_module.attach_doc_and_module_comments();
checked_module
}
}
fn assert_validator(source_code: &str, expected: serde_json::Value) {
let mut project = TestProject::new();