Lines
100 %
Functions
Branches
use super::define_struct;
use crate::ast::Expr;
use crate::compiler::native::{STRUCT_COPY, STRUCT_NEW, STRUCT_P, STRUCT_REF, STRUCT_SET};
use crate::runtime::{Head, SymbolTable};
fn defined(fields: &[&str]) -> SymbolTable {
let mut table = SymbolTable::with_builtins();
let fields = fields.iter().map(ToString::to_string).collect();
define_struct(&mut table, "PT", fields).expect("struct defines");
table
}
fn calls(table: &SymbolTable, name: &str, native: &str) -> bool {
matches!(table.resolve_head(name), Head::Function(_, Expr::List(body))
if matches!(body.first(), Some(Expr::Symbol(head)) if head == native))
#[test]
fn every_function_of_a_struct_is_a_lambda_over_a_native() {
let table = defined(&["X", "Y"]);
assert!(calls(&table, "MAKE-PT", STRUCT_NEW));
assert!(calls(&table, "PT-P", STRUCT_P));
assert!(calls(&table, "COPY-PT", STRUCT_COPY));
assert!(calls(&table, "PT-X", STRUCT_REF));
assert!(calls(&table, "PT-Y", STRUCT_REF));
assert!(calls(&table, "(SETF PT-X)", STRUCT_SET));
assert!(calls(&table, "(SETF PT-Y)", STRUCT_SET));
fn a_field_named_p_makes_the_predicate_name_an_accessor() {
define_struct(&mut table, "BOX", vec!["P".to_string()]).expect("struct defines");
assert!(calls(&table, "BOX-P", STRUCT_REF));
fn defining_a_struct_again_with_other_fields_is_an_error() {
let mut table = defined(&["X"]);
let err = define_struct(&mut table, "PT", vec!["X".to_string(), "Y".to_string()]);
assert!(err.is_err());
assert!(!calls(&table, "PT-Y", STRUCT_REF));
fn defining_a_struct_again_with_its_fields_keeps_its_layout() {
let fields = vec!["X".to_string()];
let before = table.define_struct("PT", fields.clone()).expect("known");
define_struct(&mut table, "PT", fields.clone()).expect("same fields");
let after = table.define_struct("PT", fields.clone()).expect("known");
assert_eq!(after, before);
assert_eq!(
table.struct_layout(after).map(|l| l.fields.clone()),
Some(fields)
);