Lines
100 %
Functions
Branches
//! `MAKE-STRUCT-INSTANCE` / `STRUCT-FIELD` / `STRUCT-P` /
//! `STRUCT-SET-FIELD` / `MAKE-STRUCT-RUNTIME` natives — the dynamic
//! API DEFSTRUCT codegen targets.
use super::common::{compile_and_validate, compile_expect_error};
#[test]
fn make_struct_instance_string_fields() {
compile_and_validate("(make-struct-instance \"Point\" '(x y) '(1 2))");
}
fn make_struct_instance_wrong_arity_errors() {
let err = compile_expect_error("(make-struct-instance)");
assert!(err.contains("MAKE-STRUCT-INSTANCE"), "got: {err}");
fn make_struct_instance_non_string_name_errors() {
let err = compile_expect_error("(make-struct-instance 42 '() '())");
fn struct_field_lookup_by_name() {
compile_and_validate(
"(let* ((p (make-struct-instance \"Point\" '(x y) '(1 2)))) \
(struct-field p \"x\"))",
);
fn struct_p_returns_bool() {
"(let* ((p (make-struct-instance \"Point\" '(x) '(1)))) \
(struct-p p))",
fn struct_p_non_struct_returns_false() {
compile_and_validate("(struct-p 42)");
fn make_struct_instance_quoted_string_name() {
// Name as `'"Point"` — quoted-string branch in the name match.
compile_and_validate("(make-struct-instance '\"Point\" '(x) '(1))");
fn make_struct_instance_quoted_non_string_name_errors() {
let err = compile_expect_error("(make-struct-instance '42 '() '())");
fn make_struct_instance_non_string_field_errors() {
// Field list element is a number — bad-field-name branch.
let err = compile_expect_error("(make-struct-instance \"P\" (list 1) '(1))");
fn make_struct_instance_quoted_field_with_bad_element_errors() {
// Quoted list of fields, but one element is a number.
let err = compile_expect_error("(make-struct-instance \"P\" '(x 42) '(1 2))");
fn make_struct_instance_non_list_fields_errors() {
let err = compile_expect_error("(make-struct-instance \"P\" 42 '(1))");
fn make_struct_instance_non_list_values_errors() {
let err = compile_expect_error("(make-struct-instance \"P\" '(x) 42)");
fn make_struct_instance_quoted_values_with_bool_round_trip() {
// Quoted values branch with a Bool element — exercises the
// Expr::Bool arm of the quoted-list value lowering.
compile_and_validate("(make-struct-instance \"P\" '(flag) '(t))");
fn make_struct_instance_pads_with_nil_when_short() {
// 2 field names but only 1 value — pads with Nil via the
// `padded_values.push(Value::Nil)` loop.
compile_and_validate("(make-struct-instance \"P\" '(x y) '(1))");
fn struct_field_wrong_arity_errors() {
let err = compile_expect_error("(struct-field)");
assert!(err.contains("STRUCT-FIELD"), "got: {err}");
fn struct_set_field_wrong_arity_errors() {
let err = compile_expect_error("(struct-set-field)");
assert!(err.contains("STRUCT-SET-FIELD"), "got: {err}");
fn struct_p_wrong_arity_errors() {
let err = compile_expect_error("(struct-p)");
assert!(err.contains("STRUCT-P"), "got: {err}");
fn struct_set_field_unknown_struct_errors() {
// STRUCT-SET-FIELD requires the struct type to be defined via
// DEFSTRUCT first so the field-name table is registered. Using
// an ad-hoc make-struct-instance triggers an unknown-struct
// error, exercising the lookup-failure path.
let err = compile_expect_error(
(struct-set-field p \"x\" 99))",
assert!(
err.contains("STRUCT-SET-FIELD") || err.contains("Point"),
"got: {err}"