Skip to content

Commit

Permalink
numListItems alias for math
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp committed Oct 30, 2024
1 parent 9b02472 commit d258a25
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/doenetml-worker/src/test/tagSpecific/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9130,4 +9130,43 @@ describe("Math tag tests", async () => {
]);
}
});

it("numListItems and list", async () => {
let core = await createTestCore({
doenetML: `
<p><math name="m">2x, 3y, 4z</math>!</p>
<p name="p2">Number of list items is $m.numListItems.</p>
<p name="p3">list items: $m.list.</p>
<p name="p4">math list from items: <mathList name="ml">$m.list</mathList>.</p>
`,
});

let stateVariables = await returnAllStateVariables(core);

expect(stateVariables["/p2"].stateValues.text).eq(
"Number of list items is 3.",
);
expect(stateVariables["/p3"].stateValues.text).eq(
"list items: 2 x, 3 y, 4 z.",
);
expect(stateVariables["/p4"].stateValues.text).eq(
"math list from items: 2 x, 3 y, 4 z.",
);

expect(stateVariables["/m"].stateValues.numDimensions).eq(3);
expect(stateVariables["/m"].stateValues.list.map((v) => v.tree)).eqls([
["*", 2, "x"],
["*", 3, "y"],
["*", 4, "z"],
]);

expect(stateVariables["/ml"].stateValues.maths.map((v) => v.tree)).eqls(
[
["*", 2, "x"],
["*", 3, "y"],
["*", 4, "z"],
],
);
});
});
66 changes: 66 additions & 0 deletions packages/doenetml-worker/src/test/tagSpecific/mathinput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9396,6 +9396,10 @@ describe("MathInput tag tests", async () => {
<p name="p14">Matrix: $mi.matrix</p>
<p name="p15">Matrix[$i]: $mi.matrix[$i]</p>
<p name="p16">Matrix[$i][1]: $mi.matrix[$i][1]</p>
<p name="p17">Number of list items: $mi.numListItems</p>
<p name="p18">List: $mi.list</p>
<p name="p19">Math list from list: <mathList name="ml">$mi.list</mathList></p>
<p name="p20">Number list from list: <numberList name="nl">$mi.list</numberList></p>
`,
});
Expand Down Expand Up @@ -9434,6 +9438,11 @@ describe("MathInput tag tests", async () => {
asVec = me.fromAst(["vector", ...mathTree.slice(1)]);
}

let asList = math;
if (mathTree[0] === "vector") {
asList = me.fromAst(["list", ...mathTree.slice(1)]);
}

expect(stateVariables["/p1"].stateValues.text).eq(
`Number of dimensions: ${numDimensions}`,
);
Expand Down Expand Up @@ -9558,6 +9567,55 @@ describe("MathInput tag tests", async () => {
`Matrix[${i}][1]: `,
);
}

expect(stateVariables["/p17"].stateValues.text).eq(
`Number of list items: ${numDimensions}`,
);

expect(stateVariables["/p18"].stateValues.text).eq(
`List: ${asList}`,
);
if (numDimensions === 1) {
expect(
stateVariables["/mi"].stateValues.list.map((v) => v.tree),
).eqls([math.tree]);
} else {
expect(
stateVariables["/mi"].stateValues.list.map((v) => v.tree),
).eqls(math.tree.slice(1));
}

expect(stateVariables["/p19"].stateValues.text).eq(
`Math list from list: ${asList}`,
);
if (numDimensions === 1) {
expect(
stateVariables["/ml"].stateValues.maths.map((v) => v.tree),
).eqls([math.tree]);
} else {
expect(
stateVariables["/ml"].stateValues.maths.map((v) => v.tree),
).eqls(math.tree.slice(1));
}

if (numDimensions === 1) {
let num = math.evaluate_to_constant();

expect(stateVariables["/p20"].stateValues.text).eq(
`Number list from list: ${num}`,
);
expect(stateVariables["/nl"].stateValues.numbers).eqls([num]);
} else {
let nums = math.tree
.slice(1)
.map((v) => me.fromAst(v).evaluate_to_constant());

expect(stateVariables["/p20"].stateValues.text).eq(
`Number list from list: ${nums.join(", ")}`,
);

expect(stateVariables["/nl"].stateValues.numbers).eqls(nums);
}
}

let math = me.fromAst("\uff3f");
Expand Down Expand Up @@ -9635,5 +9693,13 @@ describe("MathInput tag tests", async () => {
});
math = me.fromAst(["list", "p", "q"]);
await check_items(math, i);

await updateMathInputValue({
latex: "5,4,3",
componentName: "/mi",
core,
});
math = me.fromAst(["list", 5, 4, 3]);
await check_items(math, i);
});
});
9 changes: 9 additions & 0 deletions packages/doenetml-worker/src/test/tagSpecific/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ describe("Text tag tests", async () => {
<p name="p2">Number of list items is $t.numListItems.</p>
<p name="p3">list items: $t.list.</p>
<p name="p4">text list from items: <textList name="tl">$t.list</textList>.</p>
`,
});

Expand All @@ -999,11 +1000,19 @@ describe("Text tag tests", async () => {
expect(stateVariables["/p3"].stateValues.text).eq(
"list items: Hello there, friend!.",
);
expect(stateVariables["/p4"].stateValues.text).eq(
"text list from items: Hello there, friend!.",
);

expect(stateVariables["/t"].stateValues.numListItems).eq(2);
expect(stateVariables["/t"].stateValues.list).eqls([
"Hello there",
"friend!",
]);

expect(stateVariables["/tl"].stateValues.texts).eqls([
"Hello there",
"friend!",
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,7 @@ describe("TextInput tag tests", async () => {
<p name="p5">Words: $ti.words.</p>
<p name="p6">Number of list items is $ti.numListItems.</p>
<p name="p7">List: $ti.list.</p>
<p name="p8">Text list from list: <textList name="tl">$ti.list</textList>.</p>
`,
});

Expand Down Expand Up @@ -1627,6 +1628,9 @@ describe("TextInput tag tests", async () => {
expect(stateVariables["/p7"].stateValues.text).eq(
`List: ${list.join(", ")}.`,
);
expect(stateVariables["/p8"].stateValues.text).eq(
`Text list from list: ${list.join(", ")}.`,
);

expect(stateVariables["/ti"].stateValues.numCharacters).eq(
numCharacters,
Expand All @@ -1640,6 +1644,7 @@ describe("TextInput tag tests", async () => {
numListItems,
);
expect(stateVariables["/ti"].stateValues.list).eqls(list);
expect(stateVariables["/tl"].stateValues.texts).eqls(list);
}

let string = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export function returnMathVectorMatrixStateVariableDefinitions() {
targetVariableName: "x3",
};

stateVariableDefinitions.numListItems = {
isAlias: true,
targetVariableName: "numDimensions",
};

stateVariableDefinitions.list = {
public: true,
shadowingInstructions: {
Expand Down

0 comments on commit d258a25

Please sign in to comment.