38 lines
1.6 KiB
TypeScript
38 lines
1.6 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { getAST } from "@gram-lang/parser";
|
|
import { compile, WarningCode } from "../src/index";
|
|
|
|
const undefinedRefs = (source: string) =>
|
|
compile(getAST(source)).warnings.filter(
|
|
(w) => w.code === WarningCode.UNDEFINED_REFERENCE,
|
|
);
|
|
|
|
// `@&flour` mentions an ingredient written earlier. It finds it by registry
|
|
// id, as the shopping list merges `@Flour` and `@flour`, and as `gram format`
|
|
// lowercases ids: an error that formatting makes vanish would be a change of
|
|
// meaning (found by the fuzzer, `format-preserves-meaning`).
|
|
describe("@& reference to an ingredient written with other capitals", () => {
|
|
it("finds the ingredient whatever its case", () => {
|
|
expect(undefinedRefs("Add @Flour{100 g}.\n\nMix @&flour.\n")).toEqual([]);
|
|
expect(undefinedRefs("Add @flour{100 g}.\n\nMix @&Flour{20 g}.\n")).toEqual(
|
|
[],
|
|
);
|
|
expect(
|
|
undefinedRefs("Add @Sucre Glace{50 g}.\n\nDust with @&sucre glace{}.\n"),
|
|
).toEqual([]);
|
|
});
|
|
|
|
it("reads the same before and after the ids are lowercased", () => {
|
|
const written = "Add @Flour{100 g}.\n\nMix @&flour{20 g}.\n";
|
|
const lowered = "Add @flour{100 g}.\n\nMix @&flour{20 g}.\n";
|
|
const shopping = (s: string) =>
|
|
JSON.stringify(compile(getAST(s)).shoppingList ?? null);
|
|
expect(undefinedRefs(written)).toEqual(undefinedRefs(lowered));
|
|
expect(shopping(written).toLowerCase()).toBe(shopping(lowered));
|
|
});
|
|
|
|
it("still reports an ingredient never written", () => {
|
|
const [w] = undefinedRefs("Add @flour{100 g}.\n\nMix @&sugar.\n");
|
|
expect(w?.message).toContain("@&sugar");
|
|
});
|
|
});
|