feat: add end to end tests to replace acceptance tests with strict uplc comparison.
Add acceptance tests 1,2, 6 as end to end tests
This commit is contained in:
@@ -3035,7 +3035,7 @@ impl<'a> CodeGenerator<'a> {
|
||||
} = ir else {
|
||||
let scope = ir.scope();
|
||||
|
||||
process_scope_updates(&mut to_be_defined_map, scope, func_index_map);
|
||||
process_scope_updates(&mut to_be_defined_map, &scope, func_index_map);
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -3043,7 +3043,7 @@ impl<'a> CodeGenerator<'a> {
|
||||
let ValueConstructorVariant::ModuleFn {name, module, builtin: None, ..} = &constructor.variant else {
|
||||
let scope = ir.scope();
|
||||
|
||||
process_scope_updates(&mut to_be_defined_map, scope, func_index_map);
|
||||
process_scope_updates(&mut to_be_defined_map, &scope, func_index_map);
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -3263,6 +3263,7 @@ impl<'a> CodeGenerator<'a> {
|
||||
} else {
|
||||
unreachable!("We found a function with no definitions");
|
||||
}
|
||||
process_scope_updates(&mut to_be_defined_map, scope, func_index_map);
|
||||
}
|
||||
|
||||
// Still to be defined
|
||||
@@ -5023,13 +5024,13 @@ impl<'a> CodeGenerator<'a> {
|
||||
|
||||
fn process_scope_updates(
|
||||
to_be_defined_map: &mut IndexMap<FunctionAccessKey, Scope>,
|
||||
scope: Scope,
|
||||
scope: &Scope,
|
||||
func_index_map: &mut IndexMap<FunctionAccessKey, Scope>,
|
||||
) {
|
||||
for func in to_be_defined_map.clone().iter() {
|
||||
if scope.common_ancestor(func.1) == scope.clone() {
|
||||
if &scope.common_ancestor(func.1) == scope {
|
||||
if let Some(index_scope) = func_index_map.get(func.0) {
|
||||
if index_scope.common_ancestor(func.1) == scope.clone() {
|
||||
if &index_scope.common_ancestor(func.1) == scope {
|
||||
func_index_map.insert(func.0.clone(), scope.clone());
|
||||
to_be_defined_map.shift_remove(func.0);
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,11 @@ edition = "2021"
|
||||
repository = "https://github.com/aiken-lang/aiken/crates/project"
|
||||
homepage = "https://github.com/aiken-lang/aiken"
|
||||
license = "Apache-2.0"
|
||||
authors = ["Lucas Rosa <x@rvcas.dev>", "Kasey White <kwhitemsg@gmail.com>", "KtorZ <matthias.benkort@gmail.com>"]
|
||||
authors = [
|
||||
"Lucas Rosa <x@rvcas.dev>",
|
||||
"Kasey White <kwhitemsg@gmail.com>",
|
||||
"KtorZ <matthias.benkort@gmail.com>",
|
||||
]
|
||||
rust-version = "1.66.1"
|
||||
|
||||
[dependencies]
|
||||
@@ -41,5 +45,11 @@ zip = "0.6.4"
|
||||
aiken-lang = { path = "../aiken-lang", version = "1.0.2-alpha" }
|
||||
uplc = { path = '../uplc', version = "1.0.2-alpha" }
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = "1.1.0"
|
||||
pretty_assertions = "1.3.0"
|
||||
|
||||
[features]
|
||||
default = ["uplc/default", "aiken-lang/default"]
|
||||
native-secp256k1 = ["uplc/native-secp256k1", "aiken-lang/native-secp256k1"]
|
||||
|
||||
@@ -175,6 +175,27 @@ 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 aiken_lang::{
|
||||
self,
|
||||
ast::{Definition, Function, ModuleKind, Tracing, TypedDataType, TypedFunction},
|
||||
builtins,
|
||||
gen_uplc::builder::{DataTypeKey, FunctionAccessKey},
|
||||
parser,
|
||||
tipo::TypeInfo,
|
||||
IdGenerator,
|
||||
};
|
||||
use uplc::{
|
||||
ast::{self as uplc_ast, Constant, Name},
|
||||
machine::cost_model::ExBudget,
|
||||
optimize, BigInt, PlutusData,
|
||||
};
|
||||
|
||||
use super::{
|
||||
super::{
|
||||
definitions::{Definitions, Reference},
|
||||
@@ -184,20 +205,6 @@ mod test {
|
||||
*,
|
||||
};
|
||||
use crate::{module::ParsedModule, PackageName};
|
||||
use aiken_lang::{
|
||||
self,
|
||||
ast::{ModuleKind, Tracing, TypedDataType, TypedFunction},
|
||||
builtins,
|
||||
gen_uplc::builder::{DataTypeKey, FunctionAccessKey},
|
||||
parser,
|
||||
tipo::TypeInfo,
|
||||
IdGenerator,
|
||||
};
|
||||
use assert_json_diff::assert_json_eq;
|
||||
use indexmap::IndexMap;
|
||||
use serde_json::{self, json};
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
use uplc::ast as uplc;
|
||||
|
||||
// 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
|
||||
@@ -319,6 +326,58 @@ mod test {
|
||||
assert_json_eq!(serde_json::to_value(validator).unwrap(), expected);
|
||||
}
|
||||
|
||||
fn assert_uplc(source_code: &str, expected: Term<Name>) {
|
||||
let mut project = TestProject::new();
|
||||
|
||||
let modules = CheckedModules::singleton(project.check(project.parse(source_code)));
|
||||
let mut generator = modules.new_generator(
|
||||
&project.functions,
|
||||
&project.data_types,
|
||||
&project.module_types,
|
||||
);
|
||||
|
||||
let Some(checked_module) = modules.values().next()
|
||||
else {
|
||||
unreachable!("There's got to be one right?")
|
||||
};
|
||||
|
||||
let mut scripts = vec![];
|
||||
|
||||
for def in checked_module.ast.definitions() {
|
||||
if let Definition::Test(func) = def {
|
||||
scripts.push((
|
||||
checked_module.input_path.clone(),
|
||||
checked_module.name.clone(),
|
||||
func,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(scripts.len(), 1);
|
||||
|
||||
let script = &scripts[0];
|
||||
|
||||
let Function { body, .. } = script.2;
|
||||
|
||||
let program = generator.generate_test(body);
|
||||
|
||||
let debruijn_program: Program<DeBruijn> = program.try_into().unwrap();
|
||||
|
||||
let expected = Program {
|
||||
version: (1, 0, 0),
|
||||
term: expected,
|
||||
};
|
||||
|
||||
let expected = optimize::aiken_optimize_and_intern(expected);
|
||||
let expected: Program<DeBruijn> = expected.try_into().unwrap();
|
||||
|
||||
assert_eq!(debruijn_program.to_pretty(), expected.to_pretty());
|
||||
|
||||
let eval = debruijn_program.eval(ExBudget::default());
|
||||
|
||||
assert!(!eval.failed())
|
||||
}
|
||||
|
||||
fn fixture_definitions() -> Definitions<Annotated<Schema>> {
|
||||
let mut definitions = Definitions::new();
|
||||
|
||||
@@ -412,6 +471,139 @@ mod test {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acceptance_test_6_if_else() {
|
||||
let src = r#"
|
||||
test bar() {
|
||||
let x = 1
|
||||
if x == 1 {
|
||||
True
|
||||
} else {
|
||||
False
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
assert_uplc(
|
||||
src,
|
||||
Term::equals_integer()
|
||||
.apply(Term::integer(1.into()))
|
||||
.apply(Term::integer(1.into()))
|
||||
.delayed_if_else(Term::bool(true), Term::bool(false)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acceptance_test_1_length() {
|
||||
let src = r#"
|
||||
pub fn length(xs: List<a>) -> Int {
|
||||
when xs is {
|
||||
[] ->
|
||||
0
|
||||
[_, ..rest] ->
|
||||
1 + length(rest)
|
||||
}
|
||||
}
|
||||
|
||||
test length_1() {
|
||||
length([1, 2, 3]) == 3
|
||||
}
|
||||
"#;
|
||||
|
||||
assert_uplc(
|
||||
src,
|
||||
Term::equals_integer()
|
||||
.apply(
|
||||
Term::var("length")
|
||||
.lambda("length")
|
||||
.apply(Term::var("length").apply(Term::var("length")))
|
||||
.lambda("length")
|
||||
.apply(
|
||||
Term::var("xs")
|
||||
.delayed_choose_list(
|
||||
Term::integer(0.into()),
|
||||
Term::add_integer()
|
||||
.apply(Term::integer(1.into()))
|
||||
.apply(
|
||||
Term::var("length")
|
||||
.apply(Term::var("length"))
|
||||
.apply(Term::var("rest")),
|
||||
)
|
||||
.lambda("rest")
|
||||
.apply(Term::tail_list().apply(Term::var("xs"))),
|
||||
)
|
||||
.lambda("xs")
|
||||
.lambda("length"),
|
||||
)
|
||||
.apply(Term::list_values(vec![
|
||||
Constant::Data(PlutusData::BigInt(BigInt::Int(1.into()))),
|
||||
Constant::Data(PlutusData::BigInt(BigInt::Int(2.into()))),
|
||||
Constant::Data(PlutusData::BigInt(BigInt::Int(3.into()))),
|
||||
])),
|
||||
)
|
||||
.apply(Term::integer(3.into())),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acceptance_test_2_repeat() {
|
||||
let src = r#"
|
||||
pub fn repeat(x: a, n: Int) -> List<a> {
|
||||
if n <= 0 {
|
||||
[]
|
||||
} else {
|
||||
[x, ..repeat(x, n - 1)]
|
||||
}
|
||||
}
|
||||
|
||||
test repeat_1() {
|
||||
repeat("aiken", 2) == ["aiken", "aiken"]
|
||||
}
|
||||
"#;
|
||||
|
||||
assert_uplc(
|
||||
src,
|
||||
Term::equals_data()
|
||||
.apply(
|
||||
Term::list_data().apply(
|
||||
Term::var("repeat")
|
||||
.lambda("repeat")
|
||||
.apply(Term::var("repeat").apply(Term::var("repeat")))
|
||||
.lambda("repeat")
|
||||
.apply(
|
||||
Term::less_than_equals_integer()
|
||||
.apply(Term::var("n"))
|
||||
.apply(Term::integer(0.into()))
|
||||
.delayed_if_else(
|
||||
Term::empty_list(),
|
||||
Term::mk_cons()
|
||||
.apply(Term::b_data().apply(Term::var("x")))
|
||||
.apply(
|
||||
Term::var("repeat")
|
||||
.apply(Term::var("repeat"))
|
||||
.apply(Term::var("x"))
|
||||
.apply(
|
||||
Term::sub_integer()
|
||||
.apply(Term::var("n"))
|
||||
.apply(Term::integer(1.into())),
|
||||
),
|
||||
),
|
||||
)
|
||||
.lambda("n")
|
||||
.lambda("x")
|
||||
.lambda("repeat"),
|
||||
)
|
||||
.apply(Term::byte_string("aiken".as_bytes().to_vec()))
|
||||
.apply(Term::integer(3.into())),
|
||||
),
|
||||
)
|
||||
.apply(Term::list_data().apply(Term::list_values(vec![
|
||||
Constant::Data(PlutusData::BoundedBytes("aiken".as_bytes().to_vec().into())),
|
||||
Constant::Data(PlutusData::BoundedBytes("aiken".as_bytes().to_vec().into())),
|
||||
]))),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mint_parameterized() {
|
||||
assert_validator(
|
||||
@@ -1165,7 +1357,7 @@ mod test {
|
||||
fn validate_arguments_integer() {
|
||||
let definitions = fixture_definitions();
|
||||
|
||||
let term = Term::data(uplc::Data::integer(42.into()));
|
||||
let term = Term::data(uplc_ast::Data::integer(42.into()));
|
||||
|
||||
let param = Parameter {
|
||||
title: None,
|
||||
@@ -1179,7 +1371,7 @@ mod test {
|
||||
fn validate_arguments_bytestring() {
|
||||
let definitions = fixture_definitions();
|
||||
|
||||
let term = Term::data(uplc::Data::bytestring(vec![102, 111, 111]));
|
||||
let term = Term::data(uplc_ast::Data::bytestring(vec![102, 111, 111]));
|
||||
|
||||
let param = Parameter {
|
||||
title: None,
|
||||
@@ -1208,9 +1400,9 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::list(vec![
|
||||
uplc::Data::integer(42.into()),
|
||||
uplc::Data::integer(14.into()),
|
||||
let term = Term::data(uplc_ast::Data::list(vec![
|
||||
uplc_ast::Data::integer(42.into()),
|
||||
uplc_ast::Data::integer(14.into()),
|
||||
]));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
@@ -1237,9 +1429,9 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::list(vec![uplc::Data::bytestring(vec![
|
||||
102, 111, 111,
|
||||
])]));
|
||||
let term = Term::data(uplc_ast::Data::list(vec![uplc_ast::Data::bytestring(
|
||||
vec![102, 111, 111],
|
||||
)]));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
|
||||
@@ -1269,9 +1461,9 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::list(vec![
|
||||
uplc::Data::integer(42.into()),
|
||||
uplc::Data::bytestring(vec![102, 111, 111]),
|
||||
let term = Term::data(uplc_ast::Data::list(vec![
|
||||
uplc_ast::Data::integer(42.into()),
|
||||
uplc_ast::Data::bytestring(vec![102, 111, 111]),
|
||||
]));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
@@ -1300,9 +1492,9 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::map(vec![(
|
||||
uplc::Data::bytestring(vec![102, 111, 111]),
|
||||
uplc::Data::integer(42.into()),
|
||||
let term = Term::data(uplc_ast::Data::map(vec![(
|
||||
uplc_ast::Data::bytestring(vec![102, 111, 111]),
|
||||
uplc_ast::Data::integer(42.into()),
|
||||
)]));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
@@ -1316,7 +1508,7 @@ mod test {
|
||||
|
||||
let definitions = fixture_definitions();
|
||||
|
||||
let term = Term::data(uplc::Data::constr(1, vec![]));
|
||||
let term = Term::data(uplc_ast::Data::constr(1, vec![]));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
|
||||
@@ -1351,7 +1543,10 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::constr(0, vec![uplc::Data::constr(0, vec![])]));
|
||||
let term = Term::data(uplc_ast::Data::constr(
|
||||
0,
|
||||
vec![uplc_ast::Data::constr(0, vec![])],
|
||||
));
|
||||
|
||||
let param: Parameter = schema.into();
|
||||
|
||||
@@ -1404,15 +1599,15 @@ mod test {
|
||||
.into(),
|
||||
);
|
||||
|
||||
let term = Term::data(uplc::Data::constr(
|
||||
let term = Term::data(uplc_ast::Data::constr(
|
||||
1,
|
||||
vec![
|
||||
uplc::Data::integer(14.into()),
|
||||
uplc::Data::constr(
|
||||
uplc_ast::Data::integer(14.into()),
|
||||
uplc_ast::Data::constr(
|
||||
1,
|
||||
vec![
|
||||
uplc::Data::integer(42.into()),
|
||||
uplc::Data::constr(0, vec![]),
|
||||
uplc_ast::Data::integer(42.into()),
|
||||
uplc_ast::Data::constr(0, vec![]),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
@@ -404,7 +404,7 @@ impl PartialEq for NamedDeBruijn {
|
||||
/// It allows for injecting fake textual names while also using Debruijn for decoding
|
||||
/// without having to loop through twice.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FakeNamedDeBruijn(pub NamedDeBruijn);
|
||||
pub struct FakeNamedDeBruijn(pub(crate) NamedDeBruijn);
|
||||
|
||||
impl From<DeBruijn> for FakeNamedDeBruijn {
|
||||
fn from(d: DeBruijn) -> Self {
|
||||
|
||||
@@ -53,6 +53,10 @@ impl<T> Term<T> {
|
||||
Term::Constant(Constant::ProtoList(Type::Data, vec![]).into())
|
||||
}
|
||||
|
||||
pub fn list_values(vals: Vec<Constant>) -> Self {
|
||||
Term::Constant(Constant::ProtoList(Type::Data, vals).into())
|
||||
}
|
||||
|
||||
pub fn empty_map() -> Self {
|
||||
Term::Constant(
|
||||
Constant::ProtoList(Type::Pair(Type::Data.into(), Type::Data.into()), vec![]).into(),
|
||||
@@ -103,6 +107,14 @@ impl<T> Term<T> {
|
||||
Term::Builtin(DefaultFunction::EqualsInteger)
|
||||
}
|
||||
|
||||
pub fn less_than_integer() -> Self {
|
||||
Term::Builtin(DefaultFunction::LessThanInteger)
|
||||
}
|
||||
|
||||
pub fn less_than_equals_integer() -> Self {
|
||||
Term::Builtin(DefaultFunction::LessThanEqualsInteger)
|
||||
}
|
||||
|
||||
pub fn equals_string() -> Self {
|
||||
Term::Builtin(DefaultFunction::EqualsString)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ const TERM_TAG_WIDTH: u32 = 4;
|
||||
pub trait Binder<'b>: Encode + Decode<'b> {
|
||||
fn binder_encode(&self, e: &mut Encoder) -> Result<(), en::Error>;
|
||||
fn binder_decode(d: &mut Decoder) -> Result<Self, de::Error>;
|
||||
fn text(&self) -> &str;
|
||||
fn text(&self) -> String;
|
||||
}
|
||||
|
||||
impl<'b, T> Flat<'b> for Program<T> where T: Binder<'b> + Debug {}
|
||||
@@ -255,7 +255,7 @@ where
|
||||
let var_option = T::binder_decode(d);
|
||||
match var_option {
|
||||
Ok(var) => {
|
||||
state_log.push(var.text().to_string());
|
||||
state_log.push(var.text());
|
||||
let term_option = Term::decode_debug(d, state_log);
|
||||
match term_option {
|
||||
Ok(term) => {
|
||||
@@ -650,8 +650,8 @@ impl<'b> Binder<'b> for Name {
|
||||
Name::decode(d)
|
||||
}
|
||||
|
||||
fn text(&self) -> &str {
|
||||
&self.text
|
||||
fn text(&self) -> String {
|
||||
self.text.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -687,8 +687,8 @@ impl<'b> Binder<'b> for NamedDeBruijn {
|
||||
})
|
||||
}
|
||||
|
||||
fn text(&self) -> &str {
|
||||
&self.text
|
||||
fn text(&self) -> String {
|
||||
format!("{}_{}", &self.text, self.index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -715,8 +715,8 @@ impl<'b> Binder<'b> for DeBruijn {
|
||||
Ok(DeBruijn::new(0))
|
||||
}
|
||||
|
||||
fn text(&self) -> &str {
|
||||
"i"
|
||||
fn text(&self) -> String {
|
||||
format!("i_{}", self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -749,8 +749,8 @@ impl<'b> Binder<'b> for FakeNamedDeBruijn {
|
||||
Ok(index.into())
|
||||
}
|
||||
|
||||
fn text(&self) -> &str {
|
||||
&self.0.text
|
||||
fn text(&self) -> String {
|
||||
format!("{}_{}", self.0.text, self.0.index)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user