Add test for lambda and some edge cases for constant
This commit is contained in:
parent
e667fc3613
commit
41a7b73877
|
@ -1,32 +1,66 @@
|
||||||
use uplc::ast::{Constant, DeBruijn, Name, Program, Term};
|
#![cfg_attr(test, allow(non_snake_case))]
|
||||||
|
|
||||||
|
use uplc::ast::{Constant, Name, Program, Term, Unique};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
// version: (usize, usize, usize),
|
version: (usize, usize, usize),
|
||||||
|
term_builder: TermBuilder,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum TermBuilder {
|
||||||
|
Null,
|
||||||
|
Constant(Constant),
|
||||||
|
Lambda(Box<TermBuilder>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TermBuilder {
|
||||||
|
pub fn build_named(&self) -> Term<Name> {
|
||||||
|
match self.clone() {
|
||||||
|
Self::Null => todo!("Make fallible?"),
|
||||||
|
Self::Constant(c) => Term::Constant(c),
|
||||||
|
Self::Lambda(term) => Term::Lambda {
|
||||||
|
parameter_name: Name {
|
||||||
|
text: "i".to_string(),
|
||||||
|
unique: Unique::new(0),
|
||||||
|
},
|
||||||
|
body: Box::new(term.build_named()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TermBuilder {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
|
pub fn with_version(&mut self, maj: usize, min: usize, patch: usize) -> &mut Self {
|
||||||
|
self.version = (maj, min, patch);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_constant_int(&mut self, int: isize) -> &mut Self {
|
||||||
|
self.term_builder = TermBuilder::Constant(Constant::Integer(int));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_lambda(&mut self, int: isize) -> &mut Self {
|
||||||
|
let inner = TermBuilder::Constant(Constant::Integer(int));
|
||||||
|
self.term_builder = TermBuilder::Lambda(Box::new(inner));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn build_named(&self) -> Program<Name> {
|
pub fn build_named(&self) -> Program<Name> {
|
||||||
Program {
|
Program {
|
||||||
version: (11, 22, 33),
|
version: self.version,
|
||||||
term: Term::Constant(Constant::Integer(11)),
|
term: self.term_builder.build_named(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::Builder;
|
|
||||||
use uplc::parser;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let code = r"(program
|
|
||||||
11.22.33
|
|
||||||
(con integer 11)
|
|
||||||
)";
|
|
||||||
let expected = parser::program(code).unwrap();
|
|
||||||
let actual = Builder::default().build_named();
|
|
||||||
assert_eq!(expected, actual);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
use crate::Builder;
|
||||||
|
use uplc::parser;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_named__with_const() {
|
||||||
|
let code = r"(program
|
||||||
|
11.22.33
|
||||||
|
(con integer 11)
|
||||||
|
)";
|
||||||
|
let expected = parser::program(code).unwrap();
|
||||||
|
let actual = Builder::default()
|
||||||
|
.with_version(11, 22, 33)
|
||||||
|
.with_constant_int(11)
|
||||||
|
.build_named();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_named__with_different_const() {
|
||||||
|
let code = r"(program
|
||||||
|
11.22.33
|
||||||
|
(con integer 22)
|
||||||
|
)";
|
||||||
|
let expected = parser::program(code).unwrap();
|
||||||
|
let actual = Builder::default()
|
||||||
|
.with_version(11, 22, 33)
|
||||||
|
.with_constant_int(22)
|
||||||
|
.build_named();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_named__with_const_different_version() {
|
||||||
|
let code = r"(program
|
||||||
|
44.55.66
|
||||||
|
(con integer 11)
|
||||||
|
)";
|
||||||
|
let expected = parser::program(code).unwrap();
|
||||||
|
let actual = Builder::default()
|
||||||
|
.with_version(44, 55, 66)
|
||||||
|
.with_constant_int(11)
|
||||||
|
.build_named();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_named__with_lam() {
|
||||||
|
let code = r"(program
|
||||||
|
1.2.3
|
||||||
|
(lam i_0 (con integer 1))
|
||||||
|
)";
|
||||||
|
let expected = parser::program(code).unwrap();
|
||||||
|
let actual = Builder::default()
|
||||||
|
.with_version(1, 2, 3)
|
||||||
|
.with_lambda(1)
|
||||||
|
.build_named();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
}
|
Loading…
Reference in New Issue