dope2/tests/enum.js
2025-06-03 10:42:36 +02:00

46 lines
1.4 KiB
JavaScript

import { makeTypeConstructor } from "../lib/meta/type_constructor.js";
import { Char, Double, Unit } from "../lib/primitives/primitive_types.js";
import { unit } from "../lib/primitives/unit.js";
import { makeModuleEnum } from "../lib/structures/enum.types.js";
import { newProduct } from "../lib/structures/product.js";
import { lsType } from "../lib/structures/type_constructors.types.js";
import assert from "node:assert";
const variants = [
newProduct("fever")(_ => Double),
newProduct("confused")(_ => Unit),
newProduct("other")(_ => lsType(_ => Char)),
];
const symbolSymptom = "Symptom__c0442736bb6f11ae99a51b14e4db44b4";
const Symptom = makeTypeConstructor(symbolSymptom)(0);
const [
// constructors
[, {i: newFever}],
[, {i: newConfused}],
[, {i: newOther}],
// match function
[, {i: matchSymptom}],
] = makeModuleEnum(Symptom)(variants);
const fever = newFever(38.5);
const confused = newConfused(unit);
const other = newOther("sweating");
console.log("observe the encoding of different variant instances:");
console.log(" ", fever);
console.log(" ", confused);
console.log(" ", other);
const description = symptom => matchSymptom(symptom)
(fever => `Fever (${fever} degrees)`)
(_confused => `Confused`)
(other => `Other: ${other}`);
assert.strictEqual("Fever (38.5 degrees)", description(fever));
assert.strictEqual("Confused" , description(confused));
assert.strictEqual("Other: sweating" , description(other));