diff --git a/packages/doenetml-worker/src/test/tagSpecific/selectprimenumbers.test.ts b/packages/doenetml-worker/src/test/tagSpecific/selectprimenumbers.test.ts index 3dd2df742..8f07d0471 100644 --- a/packages/doenetml-worker/src/test/tagSpecific/selectprimenumbers.test.ts +++ b/packages/doenetml-worker/src/test/tagSpecific/selectprimenumbers.test.ts @@ -1,13 +1,9 @@ import { describe, expect, it, vi } from "vitest"; import { createTestCore, returnAllStateVariables } from "../utils/test-core"; -import { cleanLatex } from "../utils/math"; import { updateBooleanInputValue, updateMathInputValue, - updateMatrixInputValue, - updateTextInputValue, } from "../utils/actions"; -import me from "math-expressions"; const Mock = vi.fn(); vi.stubGlobal("postMessage", Mock); diff --git a/packages/doenetml-worker/src/test/tagSpecific/selectrandomnumbers.test.ts b/packages/doenetml-worker/src/test/tagSpecific/selectrandomnumbers.test.ts new file mode 100644 index 000000000..01afc1aeb --- /dev/null +++ b/packages/doenetml-worker/src/test/tagSpecific/selectrandomnumbers.test.ts @@ -0,0 +1,887 @@ +import { describe, expect, it, vi } from "vitest"; +import { createTestCore, returnAllStateVariables } from "../utils/test-core"; +import { updateMathInputValue } from "../utils/actions"; +import me from "math-expressions"; + +const Mock = vi.fn(); +vi.stubGlobal("postMessage", Mock); + +describe("SelectRandomNumbers tag tests", async () => { + async function test_combined_statistics({ + doenetML, + componentName, + numSamplesPerComponent, + numRepetitions, + minValue, + strictMin = true, + maxValue, + strictMax = true, + validValues, + allowedMeanMid, + allowedMeanSpread, + allowedVarianceMid, + allowedVarianceSpread, + expectedMean, + expectedVariance, + }: { + doenetML: string; + componentName: string; + numSamplesPerComponent: number; + numRepetitions: number; + minValue?: number; + strictMin?: boolean; + maxValue?: number; + strictMax?: boolean; + validValues?: number[]; + allowedMeanMid?: number; + allowedMeanSpread?: number; + allowedVarianceMid?: number; + allowedVarianceSpread?: number; + expectedMean?: number; + expectedVariance?: number; + }) { + let samples: number[] = []; + for (let i = 0; i < numRepetitions; i++) { + let core = await createTestCore({ + doenetML, + requestedVariantIndex: i, + }); + const stateVariables = await returnAllStateVariables(core); + samples.push( + ...stateVariables[componentName].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ), + ); + + if (expectedMean !== undefined && i == 0) { + expect(stateVariables[componentName].stateValues.mean).closeTo( + expectedMean, + 1e-10, + ); + } + + if (expectedVariance !== undefined && i == 0) { + expect( + stateVariables[componentName].stateValues.variance, + ).closeTo(expectedVariance, 1e-10); + expect( + stateVariables[componentName].stateValues.standardDeviation, + ).closeTo(Math.sqrt(expectedVariance), 1e-10); + } + } + + expect(samples.length).eq(numSamplesPerComponent * numRepetitions); + + if (minValue !== undefined) { + for (let sample of samples) { + if (strictMin) { + expect(sample).gt(minValue); + } else { + expect(sample).gte(minValue); + } + } + } + + if (maxValue !== undefined) { + for (let sample of samples) { + if (strictMax) { + expect(sample).lt(maxValue); + } else { + expect(sample).lte(maxValue); + } + } + } + + if (validValues !== undefined) { + for (let sample of samples) { + expect(validValues.includes(sample)).eq(true); + } + } + + if (allowedMeanMid !== undefined && allowedMeanSpread !== undefined) { + let meanX = me.math.mean(samples); + expect(meanX).closeTo(allowedMeanMid, allowedMeanSpread); + } + + if ( + allowedVarianceMid !== undefined && + allowedVarianceSpread !== undefined + ) { + let varX = me.math.variance(samples, "uncorrected"); + expect(varX).closeTo(allowedVarianceMid, allowedVarianceSpread); + } + } + + it("no parameters, select single uniform random number from 0 to 1", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 1, + numRepetitions: 100, + minValue: 0, + strictMin: true, + maxValue: 1, + strictMax: false, + allowedMeanMid: 0.5, + allowedMeanSpread: 0.1, + allowedVarianceMid: 1 / 12, + allowedVarianceSpread: 0.02, + expectedMean: 0.5, + expectedVariance: 1 / 12, + }); + }); + + it("select five uniform random numbers from 0 to 8, only to specified", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 5, + numRepetitions: 20, + minValue: 0, + strictMin: true, + maxValue: 8, + strictMax: false, + allowedMeanMid: 4, + allowedMeanSpread: 0.5, + allowedVarianceMid: 8 ** 2 / 12, + allowedVarianceSpread: 1.5, + expectedMean: 4, + expectedVariance: 8 ** 2 / 12, + }); + }); + + it("select five uniform random numbers from -5 to -4, only from specified", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 5, + numRepetitions: 20, + minValue: -5, + strictMin: true, + maxValue: -4, + strictMax: false, + allowedMeanMid: -4.5, + allowedMeanSpread: 0.05, + allowedVarianceMid: 1 / 12, + allowedVarianceSpread: 0.02, + expectedMean: -4.5, + expectedVariance: 1 / 12, + }); + }); + + it("select ten uniform random numbers from -4 to -2", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 10, + numRepetitions: 10, + minValue: -4, + strictMin: true, + maxValue: -2, + strictMax: false, + allowedMeanMid: -3, + allowedMeanSpread: 0.5, + allowedVarianceMid: 2 ** 2 / 12, + allowedVarianceSpread: 0.5, + expectedMean: -3, + expectedVariance: 2 ** 2 / 12, + }); + }); + + it("select ten uniform random numbers from -2 to -4", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 10, + numRepetitions: 10, + minValue: -4, + strictMin: true, + maxValue: -2, + strictMax: false, + allowedMeanMid: -3, + allowedMeanSpread: 0.5, + allowedVarianceMid: 2 ** 2 / 12, + allowedVarianceSpread: 0.5, + expectedMean: -3, + expectedVariance: 2 ** 2 / 12, + }); + }); + + it("select twenty continuous standard normals, no parameters", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 20, + numRepetitions: 5, + allowedMeanMid: 0, + allowedMeanSpread: 0.3, + allowedVarianceMid: 1, + allowedVarianceSpread: 0.5, + expectedMean: 0, + expectedVariance: 1, + }); + }); + + it("select five continuous standard normals, unspecified mean 0, standard deviation 10", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 5, + numRepetitions: 20, + allowedMeanMid: 0, + allowedMeanSpread: 3, + allowedVarianceMid: 100, + allowedVarianceSpread: 25, + expectedMean: 0, + expectedVariance: 100, + }); + }); + + it("select single continuous standard normal, mean -50, unspecified standard deviation 1", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 1, + numRepetitions: 100, + allowedMeanMid: -50, + allowedMeanSpread: 0.5, + allowedVarianceMid: 1, + allowedVarianceSpread: 0.4, + expectedMean: -50, + expectedVariance: 1, + }); + }); + + it("select twenty continuous standard normals, mean -3, variance 0.01", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 20, + numRepetitions: 5, + allowedMeanMid: -3, + allowedMeanSpread: 0.1, + allowedVarianceMid: 0.01, + allowedVarianceSpread: 0.005, + expectedMean: -3, + expectedVariance: 0.01, + }); + }); + + it("select single discrete uniform, no parameters, integer from 0 to 1", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 1, + numRepetitions: 100, + validValues: [0, 1], + allowedMeanMid: 0.5, + allowedMeanSpread: 0.15, + allowedVarianceMid: (2 ** 2 - 1) / 12, + allowedVarianceSpread: 0.05, + expectedMean: 0.5, + expectedVariance: (2 ** 2 - 1) / 12, + }); + }); + + it("select single discrete uniform, from 0.5 to 5.5, only to specified", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 1, + numRepetitions: 100, + validValues: [0.5, 1.5, 2.5, 3.5, 4.5, 5.5], + allowedMeanMid: 3, + allowedMeanSpread: 0.2, + allowedVarianceMid: (6 ** 2 - 1) / 12, + allowedVarianceSpread: 1, + expectedMean: 3, + expectedVariance: (6 ** 2 - 1) / 12, + }); + }); + + it("select single discrete uniform, from 8.5 to 9.5, only from specified", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 1, + numRepetitions: 100, + validValues: [8.5, 9.5], + allowedMeanMid: 9, + allowedMeanSpread: 0.1, + allowedVarianceMid: (2 ** 2 - 1) / 12, + allowedVarianceSpread: 0.05, + expectedMean: 9, + expectedVariance: (2 ** 2 - 1) / 12, + }); + }); + + it("select five integers from -3 to 5", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 5, + numRepetitions: 20, + validValues: [-3, -2, -1, 0, 1, 2, 3, 4, 5], + allowedMeanMid: 1, + allowedMeanSpread: 0.5, + allowedVarianceMid: (9 ** 2 - 1) / 12, + allowedVarianceSpread: 1.5, + expectedMean: 1, + expectedVariance: (9 ** 2 - 1) / 12, + }); + }); + + it("select five integers from 5 to -3 gives nothing", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 0, + numRepetitions: 20, + }); + }); + + it("select 10 odd integers from -3 to 5", async () => { + const doenetML = ``; + + await test_combined_statistics({ + doenetML, + componentName: "/sel", + numSamplesPerComponent: 10, + numRepetitions: 10, + validValues: [-3, -1, 1, 3, 5], + allowedMeanMid: 1, + allowedMeanSpread: 0.8, + allowedVarianceMid: ((5 ** 2 - 1) * 2 ** 2) / 12, + allowedVarianceSpread: 2, + expectedMean: 1, + expectedVariance: ((5 ** 2 - 1) * 2 ** 2) / 12, + }); + }); + + it("asList", async () => { + let core = await createTestCore({ + doenetML: ` +

+

+ `, + }); + + let results: number[] = []; + + let stateVariables = await returnAllStateVariables(core); + + results.push(stateVariables["/u"].stateValues.value); + results.push(stateVariables["/v"].stateValues.value); + results.push(stateVariables["/w"].stateValues.value); + results.push(stateVariables["/x"].stateValues.value); + results.push(stateVariables["/y"].stateValues.value); + + for (let num of results) { + expect(num).gte(175).lte(205); + } + + let roundedResults = results.map((x) => Math.round(x * 100) / 100); + expect(stateVariables["/p1"].stateValues.text).eq( + roundedResults.join(", "), + ); + expect(stateVariables["/p2"].stateValues.text).eq( + roundedResults.join(""), + ); + }); + + it("select doesn't change dynamically", async () => { + let core = await createTestCore({ + doenetML: ` + + +

+ +

+ + + +

+ +

+

$maxNum2.value{assignNames="maxNum2a"}

+ `, + }); + + let stateVariables = await returnAllStateVariables(core); + let sample1replacements = stateVariables["/sample1"].replacements!; + let sample2replacements = stateVariables["/sample2"].replacements!; + expect(sample1replacements.length).eq(20); + expect(sample2replacements.length).eq(10); + let sample1numbers = sample1replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let sample2numbers = sample2replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + for (let num of sample1numbers) { + expect(num).gte(0); + expect(num).lt(10); + } + for (let num of sample2numbers) { + expect([0, 1, 2, 3, 4].includes(num)).eq(true); + } + + // Nothing changes when change mathInputs + await updateMathInputValue({ + latex: "7", + componentName: "/numToSelect", + core, + }); + await updateMathInputValue({ + latex: "11", + componentName: "/maxNum", + core, + }); + await updateMathInputValue({ + latex: "16", + componentName: "/numToSelect2", + core, + }); + await updateMathInputValue({ + latex: "18", + componentName: "/maxNum2", + core, + }); + + stateVariables = await returnAllStateVariables(core); + sample1replacements = stateVariables["/sample1"].replacements!; + sample2replacements = stateVariables["/sample2"].replacements!; + + expect( + sample1replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ), + ).eqls(sample1numbers); + expect( + sample2replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ), + ).eqls(sample2numbers); + }); + + it("select doesn't resample in dynamic map", async () => { + let core = await createTestCore({ + doenetML: ` + How many numbers do you want? +

+ + + + +

+ +

$map1

+ + $p1{name="p3"} + $p2{name="p4"} + + $p3{name="p5"} + $p4{name="p6"} + `, + }); + + async function check_sampled_numbers(sampledNumbers: number[]) { + const stateVariables = await returnAllStateVariables(core); + + expect( + stateVariables["/p1"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p2"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p3"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p4"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p5"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p6"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + } + + let sampledNumbers: number[] = []; + + // initially nothing + await check_sampled_numbers([]); + + // sample one variable + await updateMathInputValue({ latex: "1", componentName: "/mi1", core }); + + let stateVariables = await returnAllStateVariables(core); + sampledNumbers.push(stateVariables["/a/n"].stateValues.value); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get same number back + await updateMathInputValue({ latex: "1", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers); + + // get two more samples + await updateMathInputValue({ latex: "3", componentName: "/mi1", core }); + + stateVariables = await returnAllStateVariables(core); + let n1 = stateVariables["/a/n"].stateValues.value; + let n2 = stateVariables["/b/n"].stateValues.value; + let n3 = stateVariables["/c/n"].stateValues.value; + expect(n1).eq(sampledNumbers[0]); + sampledNumbers.push(n2); + sampledNumbers.push(n3); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get first two numbers back + await updateMathInputValue({ latex: "2", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers.slice(0, 2)); + + // get six total samples + await updateMathInputValue({ latex: "6", componentName: "/mi1", core }); + + stateVariables = await returnAllStateVariables(core); + n1 = stateVariables["/a/n"].stateValues.value; + n2 = stateVariables["/b/n"].stateValues.value; + n3 = stateVariables["/c/n"].stateValues.value; + let n4 = stateVariables["/d/n"].stateValues.value; + let n5 = stateVariables["/e/n"].stateValues.value; + let n6 = stateVariables["/f/n"].stateValues.value; + expect(n1).eq(sampledNumbers[0]); + expect(n2).eq(sampledNumbers[1]); + expect(n3).eq(sampledNumbers[2]); + sampledNumbers.push(n4); + sampledNumbers.push(n5); + sampledNumbers.push(n6); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get all six back + await updateMathInputValue({ latex: "6", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers); + }); + + it("select single discrete uniform number, assign name", async () => { + let core = await createTestCore({ + doenetML: ` +

+

+

+

$u{name="u2"}

+

$v{name="v2"}

+

$w{name="w2"}

+ `, + }); + + let options = [3, 10]; + + let stateVariables = await returnAllStateVariables(core); + + let u = stateVariables["/u"]; + let u2 = stateVariables["/u2"]; + + expect(options.includes(u.stateValues.value)).eq(true); + expect(u.stateValues.value).eq(u2.stateValues.value); + + let v = stateVariables["/v"]; + let v2 = stateVariables["/v2"]; + expect(options.includes(v.stateValues.value)).eq(true); + expect(v.stateValues.value).eq(v2.stateValues.value); + + let w = stateVariables["/w"]; + let w2 = stateVariables["/w2"]; + expect(options.includes(w.stateValues.value)).eq(true); + expect(w.stateValues.value).eq(w2.stateValues.value); + }); + + it("select multiple uniform random numbers, assign names", async () => { + let core = await createTestCore({ + doenetML: ` +

+ +

+

$u{name="u2"}

+

$v{name="v2"}

+

$w{name="w2"}

+ `, + }); + + let results: number[] = []; + + let stateVariables = await returnAllStateVariables(core); + let s = stateVariables["/s"]; + expect(s.replacements!.length).eq(6); + for (let ind = 0; ind < 6; ind++) { + let num = + stateVariables[s.replacements![ind].componentName].stateValues + .value; + results[ind] = num; + expect(num).gte(3); + expect(num).lt(13); + } + + let u = stateVariables["/u"]; + let u2 = stateVariables["/u2"]; + expect(u.stateValues.value).eq(results[0]); + expect(u2.stateValues.value).eq(results[0]); + + let v = stateVariables["/v"]; + let v2 = stateVariables["/v2"]; + expect(v.stateValues.value).eq(results[1]); + expect(v2.stateValues.value).eq(results[1]); + + let w = stateVariables["/w"]; + let w2 = stateVariables["/w2"]; + expect(w.stateValues.value).eq(results[2]); + expect(w2.stateValues.value).eq(results[2]); + }); + + it("select multiple uniform random numbers, assign names, newNamespace", async () => { + let core = await createTestCore({ + doenetML: ` +

+ +

+

$(s/u{name="u2"})

+

$(s/v{name="v2"})

+

$(s/w{name="w2"})

+ `, + }); + + let results: number[] = []; + + let stateVariables = await returnAllStateVariables(core); + let s = stateVariables["/s"]; + expect(s.replacements!.length).eq(6); + for (let ind = 0; ind < 6; ind++) { + let num = + stateVariables[s.replacements![ind].componentName].stateValues + .value; + results[ind] = num; + expect(num).gte(3); + expect(num).lt(13); + } + + let u = stateVariables["/s/u"]; + let u2 = stateVariables["/u2"]; + expect(u.stateValues.value).eq(results[0]); + expect(u2.stateValues.value).eq(results[0]); + + let v = stateVariables["/s/v"]; + let v2 = stateVariables["/v2"]; + expect(v.stateValues.value).eq(results[1]); + expect(v2.stateValues.value).eq(results[1]); + + let w = stateVariables["/s/w"]; + let w2 = stateVariables["/w2"]; + expect(w.stateValues.value).eq(results[2]); + expect(w2.stateValues.value).eq(results[2]); + }); + + it("numToSelect from selectFromSequence", async () => { + let core = await createTestCore({ + doenetML: ` +

n1 =

+

nums =

+

a1=$a1, b1=$b1, c1=$c1, d1=$d1, e1=$e1

+ +

n2 =

+

nums =

+

a2=$a2, b2=$b2, c2=$c2, d2=$d2, e2=$e2

+ +

n3 =

+

nums =

+

a3=$a3, b3=$b3, c3=$c3, d3=$d3, e3=$e3

+ +

n4 =

+

nums =

+

a4=$a4, b4=$b4, c4=$c4, d4=$d4, e4=$e4

+ +

n5 =

+

nums =

+

a5=$a5, b5=$b5, c5=$c5, d5=$d5, e5=$e5

+ `, + }); + + let stateVariables = await returnAllStateVariables(core); + let n1 = stateVariables["/n1"].stateValues.value; + let n2 = stateVariables["/n2"].stateValues.value; + let n3 = stateVariables["/n3"].stateValues.value; + let n4 = stateVariables["/n4"].stateValues.value; + let n5 = stateVariables["/n5"].stateValues.value; + + let nums1 = stateVariables["/nums1"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let nums2 = stateVariables["/nums2"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let nums3 = stateVariables["/nums3"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let nums4 = stateVariables["/nums4"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let nums5 = stateVariables["/nums5"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + expect(nums1.length).eq(n1); + expect(nums2.length).eq(n2); + expect(nums3.length).eq(n3); + expect(nums4.length).eq(n4); + expect(nums5.length).eq(n5); + + nums1.length = 5; + nums2.length = 5; + nums3.length = 5; + nums4.length = 5; + nums5.length = 5; + + nums1.fill("", n1); + nums2.fill("", n2); + nums3.fill("", n3); + nums4.fill("", n4); + nums5.fill("", n5); + + let l = ["a", "b", "c", "d", "e"]; + + expect(stateVariables["/p1"].stateValues.text).eq( + nums1 + .map((v, i) => `${l[i]}1=${v ? Math.round(v * 1e9) / 1e9 : ""}`) + .join(", "), + ); + expect(stateVariables["/p2"].stateValues.text).eq( + nums2 + .map((v, i) => `${l[i]}2=${v ? Math.round(v * 1e9) / 1e9 : ""}`) + .join(", "), + ); + expect(stateVariables["/p3"].stateValues.text).eq( + nums3 + .map((v, i) => `${l[i]}3=${v ? Math.round(v * 1e9) / 1e9 : ""}`) + .join(", "), + ); + expect(stateVariables["/p4"].stateValues.text).eq( + nums4 + .map((v, i) => `${l[i]}4=${v ? Math.round(v * 1e9) / 1e9 : ""}`) + .join(", "), + ); + expect(stateVariables["/p5"].stateValues.text).eq( + nums5 + .map((v, i) => `${l[i]}5=${v ? Math.round(v * 1e9) / 1e9 : ""}`) + .join(", "), + ); + }); + + it("rounding", async () => { + let core = await createTestCore({ + doenetML: ` +

+

+

+

+ +

$n1

+

$n2

+

$n3

+

$n4

+ + `, + }); + + let stateVariables = await returnAllStateVariables(core); + + let n1 = stateVariables["/n1"].stateValues.value; + let n2 = stateVariables["/n2"].stateValues.value; + let n3 = stateVariables["/n3"].stateValues.value; + let n4 = stateVariables["/n4"].stateValues.value; + + expect(stateVariables["/n1"].stateValues.text).eq( + String(Math.round(n1 * 10 ** 8) / 10 ** 8), + ); + expect(stateVariables["/n2"].stateValues.text).eq( + String(Math.round(n2 * 10 ** 1) / 10 ** 1), + ); + expect(stateVariables["/n3"].stateValues.text).eq( + String(Math.round(n3 * 10 ** 3) / 10 ** 3), + ); + expect(stateVariables["/n4"].stateValues.text).eq(String(n4) + ".0"); + + expect(stateVariables["/n1a"].stateValues.text).eq( + String(Math.round(n1 * 10 ** 8) / 10 ** 8), + ); + expect(stateVariables["/n2a"].stateValues.text).eq( + String(Math.round(n2 * 10 ** 1) / 10 ** 1), + ); + expect(stateVariables["/n3a"].stateValues.text).eq( + String(Math.round(n3 * 10 ** 3) / 10 ** 3), + ); + expect(stateVariables["/n4a"].stateValues.text).eq(String(n4) + ".0"); + }); +}); diff --git a/packages/test-cypress/cypress/e2e/tagSpecific/selectrandomnumbers.cy.js b/packages/test-cypress/cypress/e2e/tagSpecific/selectrandomnumbers.cy.js deleted file mode 100644 index f4cafdef6..000000000 --- a/packages/test-cypress/cypress/e2e/tagSpecific/selectrandomnumbers.cy.js +++ /dev/null @@ -1,3012 +0,0 @@ -import me from "math-expressions"; -import { cesc, cesc2 } from "@doenet/utils"; - -describe("SelectRandomNumbers Tag Tests", function () { - beforeEach(() => { - cy.clearIndexedDB(); - cy.visit("/"); - }); - - it("no parameters, select single uniform random number from 0 to 1", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.map( - (x) => - stateVariables[ - stateVariables[ - stateVariables[x.componentName].replacements[0] - .componentName - ].replacements[0].componentName - ].stateValues.value, - ); - - expect(samples.length).eq(100); - - for (let sample of samples) { - expect(sample).gt(0); - expect(sample).lte(1); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(0.5, 0.1); - expect(varX).closeTo(1 / 12, 0.02); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(0.5, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(1 / 12, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(1 / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/map2"].replacements.map( - (x) => - stateVariables[ - stateVariables[ - stateVariables[x.componentName].replacements[0] - .componentName - ].replacements[0].componentName - ].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select five uniform random numbers from 0 to 8, only to specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect(sample).gt(0); - expect(sample).lte(8); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(4, 0.5); - expect(varX).closeTo(8 ** 2 / 12, 1); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(4, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - 8 ** 2 / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(8 ** 2 / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/map2"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select five uniform random numbers from -5 to -4, only from specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect(sample).gt(-5); - expect(sample).lte(-4); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(-4.5, 0.05); - expect(varX).closeTo(1 / 12, 0.02); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(-4.5, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(1 / 12, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(1 / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/map2"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select ten uniform random numbers from -4 to -2", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect(sample).gt(-4); - expect(sample).lte(-2); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(-3, 0.5); - expect(varX).closeTo(2 ** 2 / 12, 0.5); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(-3, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - 2 ** 2 / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(2 ** 2 / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select ten uniform random numbers from -2 to -4", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect(sample).gt(-4); - expect(sample).lte(-2); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(-3, 0.5); - expect(varX).closeTo(2 ** 2 / 12, 0.5); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(-3, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - 2 ** 2 / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(2 ** 2 / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select twenty continuous standard normals, no parameters", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(0, 0.3); - expect(varX).closeTo(1, 0.5); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(0, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(1, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(1), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select five continuous standard normals, unspecified mean 0, standard deviation 10", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(0, 3); - expect(varX).closeTo(100, 25); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(0, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(100, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(100), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select single continuous standard normal, mean -50, unspecified standard deviation 1", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(-50, 0.5); - expect(varX).closeTo(1, 0.4); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(-50, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(1, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(1), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select twenty continuous standard normals, mean 100, standard deviation 10", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(100, 5); - expect(varX).closeTo(100, 40); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(100, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(100, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(100), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select twenty continuous standard normals, mean -3, variance 0.01", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(-3, 0.1); - expect(varX).closeTo(0.01, 0.005); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(-3, 1e-10); - expect(firstSelect.stateValues.variance).closeTo(0.01, 1e-10); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(0.01), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select single discrete uniform, no parameters, integer from 0 to 1", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect([0, 1].includes(sample)).eq(true); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(0.5, 0.15); - expect(varX).closeTo((2 ** 2 - 1) / 12, 0.05); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(0.5, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - (2 ** 2 - 1) / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt((2 ** 2 - 1) / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select single discrete uniform, from 0.5 to 5.5, only to specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect([0.5, 1.5, 2.5, 3.5, 4.5, 5.5].includes(sample)).eq( - true, - ); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(3, 0.2); - expect(varX).closeTo((6 ** 2 - 1) / 12, 1); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(3, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - (6 ** 2 - 1) / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt((6 ** 2 - 1) / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select single discrete uniform, from 8.5 to 9.5, only from specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect([8.5, 9.5].includes(sample)).eq(true); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(9, 0.1); - expect(varX).closeTo((2 ** 2 - 1) / 12, 0.05); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(9, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - (2 ** 2 - 1) / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt((2 ** 2 - 1) / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select five integers from -3 to 5", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect([-3, -2, -1, 0, 1, 2, 3, 4, 5].includes(sample)).eq( - true, - ); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(1, 0.5); - expect(varX).closeTo((9 ** 2 - 1) / 12, 1.5); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(1, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - (9 ** 2 - 1) / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt((9 ** 2 - 1) / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select five integers from 5 to -3 gives nothing", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(0); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("select 10 odd integers from -3 to 5", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1{name="map2"} -

- - $_p1{name="p"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(samples.length).eq(100); - - for (let sample of samples) { - expect([-3, -1, 1, 3, 5].includes(sample)).eq(true); - } - - let meanX = me.math.mean(samples); - let varX = me.math.variance(samples, "uncorrected"); - - expect(meanX).closeTo(1, 0.8); - expect(varX).closeTo(((5 ** 2 - 1) * 2 ** 2) / 12, 2); - - let firstSelect = - stateVariables[ - stateVariables[ - stateVariables["/_map1"].replacements[0].componentName - ].replacements[0].componentName - ]; - expect(firstSelect.stateValues.mean).closeTo(1, 1e-10); - expect(firstSelect.stateValues.variance).closeTo( - ((5 ** 2 - 1) * 2 ** 2) / 12, - 1e-10, - ); - expect(firstSelect.stateValues.standardDeviation).closeTo( - Math.sqrt(((5 ** 2 - 1) * 2 ** 2) / 12), - 1e-10, - ); - - let copiedSamples = stateVariables["/_map1"].replacements.reduce( - (a, c) => [ - ...a, - ...stateVariables[ - stateVariables[c.componentName].replacements[0] - .componentName - ].replacements.map( - (y) => - stateVariables[y.componentName].stateValues.value, - ), - ], - [], - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("asList", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

-

- - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - results.push(stateVariables["/u"].stateValues.value); - results.push(stateVariables["/v"].stateValues.value); - results.push(stateVariables["/w"].stateValues.value); - results.push(stateVariables["/x"].stateValues.value); - results.push(stateVariables["/y"].stateValues.value); - - for (let num of results) { - expect(num).gte(175).lte(205); - } - - let roundedResults = results.map((x) => Math.round(x * 100) / 100); - cy.get(cesc2("#/_p1")).should( - "have.text", - roundedResults.join(", "), - ); - cy.get(cesc2("#/_p2")).should("have.text", roundedResults.join("")); - }); - }); - - it("selected number doesn't change dynamically", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a - - -

- -

- - - -

- -

-

$maxnum2{name="maxnum2a"}

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let sample1numbers, sample2numbers; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let sample1replacements = stateVariables["/sample1"].replacements; - let sample2replacements = stateVariables["/sample2"].replacements; - expect(sample1replacements.length).eq(20); - expect(sample2replacements.length).eq(10); - sample1numbers = sample1replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - sample2numbers = sample2replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - for (let num of sample1numbers) { - expect(num).gte(0); - expect(num).lt(10); - } - for (let num of sample2numbers) { - expect([0, 1, 2, 3, 4].includes(num)).eq(true); - } - }); - - cy.log("Nothing changes when change mathinputs"); - cy.get(cesc("#\\/numToSelect") + " textarea").type( - `{end}{backspace}{backspace}7{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/maxnum") + " textarea").type( - `{end}{backspace}{backspace}11{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/numToSelect2") + " textarea").type( - `{end}{backspace}{backspace}15{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/maxnum2") + " textarea").type( - `{end}{backspace}{backspace}18{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/maxnum2a")).should("contain.text", "18"); - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let sample1replacements = stateVariables["/sample1"].replacements; - let sample2replacements = stateVariables["/sample2"].replacements; - - expect( - sample1replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ), - ).eqls(sample1numbers); - expect( - sample2replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ), - ).eqls(sample2numbers); - }); - }); - - it("random number doesn't resample in dynamic map", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a - How many numbers do you want? -

- - - - - - -

- -

$_map1{name="map2"}

-

$_aslist1

- - $p1{name="p4"} - $p2{name="p5"} - $p3{name="p6"} - - $p4{name="p7"} - $p5{name="p8"} - $p6{name="p9"} -

$_mathinput1{name="m1"}

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let samplednumbers = []; - - cy.log("initially nothing"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("sample one variable"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}1{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "1"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - samplednumbers.push(n1); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - for (let ind = 0; ind < 1; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get same number back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}1{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "1"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - - for (let ind = 0; ind < 1; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("get two more samples"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}3{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "3"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - samplednumbers.push(n2); - samplednumbers.push(n3); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - for (let ind = 0; ind < 3; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get first two numbers back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}2{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "2"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - - for (let ind = 0; ind < 2; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("get six total samples"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}6{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "6"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - let n4 = stateVariables["/d/n"].stateValues.value; - let n5 = stateVariables["/e/n"].stateValues.value; - let n6 = stateVariables["/f/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect(n3).eq(samplednumbers[2]); - samplednumbers.push(n4); - samplednumbers.push(n5); - samplednumbers.push(n6); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - for (let ind = 0; ind < 6; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get all six back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}6{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "6"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - let n4 = stateVariables["/d/n"].stateValues.value; - let n5 = stateVariables["/e/n"].stateValues.value; - let n6 = stateVariables["/f/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect(n3).eq(samplednumbers[2]); - expect(n4).eq(samplednumbers[3]); - expect(n5).eq(samplednumbers[4]); - expect(n6).eq(samplednumbers[5]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - for (let ind = 0; ind < 6; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - }); - - it("select single discrete uniform number, assign name", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

-

-

-

$u{name="u2"}

-

$v{name="v2"}

-

$w{name="w2"}

- `, - }, - "*", - ); - }); - - let options = [3, 10]; - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/u"]; - let u2 = stateVariables["/u2"]; - - expect(options.includes(u.stateValues.value)).eq(true); - expect(u.stateValues.value).eq(u2.stateValues.value); - - let v = stateVariables["/v"]; - let v2 = stateVariables["/v2"]; - expect(options.includes(v.stateValues.value)).eq(true); - expect(v.stateValues.value).eq(v2.stateValues.value); - - let w = stateVariables["/w"]; - let w2 = stateVariables["/w2"]; - expect(options.includes(w.stateValues.value)).eq(true); - expect(w.stateValues.value).eq(w2.stateValues.value); - }); - }); - - it("select multiple uniform random numbers, assign names", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- -

-

$u{name="u2"}

-

$v{name="v2"}

-

$w{name="w2"}

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - for (let ind = 0; ind < 6; ind++) { - cy.get(cesc("#\\/_p1") + " > :nth-child(" + (2 * ind + 4) + ")") - .invoke("text") - .then((text) => { - let num = Number(text); - results[ind] = num; - expect(num).gte(3); - expect(num).lt(13); - }); - } - - cy.log("check by name").then(() => { - cy.get(cesc("#\\/u")).should("have.text", results[0]); - cy.get(cesc("#\\/u2")).should("have.text", results[0]); - cy.get(cesc("#\\/v")).should("have.text", results[1]); - cy.get(cesc("#\\/v2")).should("have.text", results[1]); - cy.get(cesc("#\\/w")).should("have.text", results[2]); - cy.get(cesc("#\\/w2")).should("have.text", results[2]); - }); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/u"]; - let u2 = stateVariables["/u2"]; - expect(u.stateValues.value).closeTo(results[0], 1e-8); - expect(u2.stateValues.value).closeTo(results[0], 1e-8); - - let v = stateVariables["/v"]; - let v2 = stateVariables["/v2"]; - expect(v.stateValues.value).closeTo(results[1], 1e-8); - expect(v2.stateValues.value).closeTo(results[1], 1e-8); - - let w = stateVariables["/w"]; - let w2 = stateVariables["/w2"]; - expect(w.stateValues.value).closeTo(results[2], 1e-8); - expect(w2.stateValues.value).closeTo(results[2], 1e-8); - - let s = stateVariables["/s"]; - expect(s.replacements.length).eq(6); - for (let ind = 0; ind < 6; ind++) { - let r = stateVariables[s.replacements[ind].componentName]; - expect(r.stateValues.value).closeTo(results[ind], 1e-8); - } - }); - }); - - it("select multiple uniform random numbers, assign names, newNamespace", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- -

-

$(s/u{name="u2"})

-

$(s/v{name="v2"})

-

$(s/w{name="w2"})

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - for (let ind = 0; ind < 6; ind++) { - cy.get(cesc("#\\/_p1") + " > :nth-child(" + (2 * ind + 4) + ")") - .invoke("text") - .then((text) => { - let num = Number(text); - results[ind] = num; - expect(num).gte(3); - expect(num).lt(13); - }); - } - - cy.log("check by name").then(() => { - cy.get(cesc("#\\/s\\/u")).should("have.text", results[0]); - cy.get(cesc("#\\/u2")).should("have.text", results[0]); - cy.get(cesc("#\\/s\\/v")).should("have.text", results[1]); - cy.get(cesc("#\\/v2")).should("have.text", results[1]); - cy.get(cesc("#\\/s\\/w")).should("have.text", results[2]); - cy.get(cesc("#\\/w2")).should("have.text", results[2]); - }); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/s/u"]; - let u2 = stateVariables["/u2"]; - expect(u.stateValues.value).closeTo(results[0], 1e-8); - expect(u2.stateValues.value).closeTo(results[0], 1e-8); - - let v = stateVariables["/s/v"]; - let v2 = stateVariables["/v2"]; - expect(v.stateValues.value).closeTo(results[1], 1e-8); - expect(v2.stateValues.value).closeTo(results[1], 1e-8); - - let w = stateVariables["/s/w"]; - let w2 = stateVariables["/w2"]; - expect(w.stateValues.value).closeTo(results[2], 1e-8); - expect(w2.stateValues.value).closeTo(results[2], 1e-8); - - let s = stateVariables["/s"]; - expect(s.replacements.length).eq(6); - for (let ind = 0; ind < 6; ind++) { - let r = stateVariables[s.replacements[ind].componentName]; - expect(r.stateValues.value).closeTo(results[ind], 1e-8); - } - }); - }); - - it("numToSelect from selectfromsequence", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a - -

n1 =

-

nums =

-

a1=$a1, b1=$b1, c1=$c1, d1=$d1, e1=$e1

- -

n2 =

-

nums =

-

a2=$a2, b2=$b2, c2=$c2, d2=$d2, e2=$e2

- -

n3 =

-

nums =

-

a3=$a3, b3=$b3, c3=$c3, d3=$d3, e3=$e3

- -

n4 =

-

nums =

-

a4=$a4, b4=$b4, c4=$c4, d4=$d4, e4=$e4

- -

n5 =

-

nums =

-

a5=$a5, b5=$b5, c5=$c5, d5=$d5, e5=$e5

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); // to wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/n1"].stateValues.value; - let n2 = stateVariables["/n2"].stateValues.value; - let n3 = stateVariables["/n3"].stateValues.value; - let n4 = stateVariables["/n4"].stateValues.value; - let n5 = stateVariables["/n5"].stateValues.value; - - let nums1 = stateVariables["/nums1"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - let nums2 = stateVariables["/nums2"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - let nums3 = stateVariables["/nums3"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - let nums4 = stateVariables["/nums4"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - let nums5 = stateVariables["/nums5"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - expect(nums1.length).eq(n1); - expect(nums2.length).eq(n2); - expect(nums3.length).eq(n3); - expect(nums4.length).eq(n4); - expect(nums5.length).eq(n5); - - nums1.length = 5; - nums2.length = 5; - nums3.length = 5; - nums4.length = 5; - nums5.length = 5; - - nums1.fill("", n1); - nums2.fill("", n2); - nums3.fill("", n3); - nums4.fill("", n4); - nums5.fill("", n5); - - let l = ["a", "b", "c", "d", "e"]; - - cy.get(cesc("#\\/p1")).should( - "have.text", - nums1 - .map( - (v, i) => - `${l[i]}1=${v ? Math.round(v * 1e9) / 1e9 : ""}`, - ) - .join(", "), - ); - cy.get(cesc("#\\/p2")).should( - "have.text", - nums2 - .map( - (v, i) => - `${l[i]}2=${v ? Math.round(v * 1e9) / 1e9 : ""}`, - ) - .join(", "), - ); - cy.get(cesc("#\\/p3")).should( - "have.text", - nums3 - .map( - (v, i) => - `${l[i]}3=${v ? Math.round(v * 1e9) / 1e9 : ""}`, - ) - .join(", "), - ); - cy.get(cesc("#\\/p4")).should( - "have.text", - nums4 - .map( - (v, i) => - `${l[i]}4=${v ? Math.round(v * 1e9) / 1e9 : ""}`, - ) - .join(", "), - ); - cy.get(cesc("#\\/p5")).should( - "have.text", - nums5 - .map( - (v, i) => - `${l[i]}5=${v ? Math.round(v * 1e9) / 1e9 : ""}`, - ) - .join(", "), - ); - }); - }); - - it("rounding", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

-

-

-

- -

$n1

-

$n2

-

$n3

-

$n4

- - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let n1 = stateVariables["/n1"].stateValues.value; - let n2 = stateVariables["/n2"].stateValues.value; - let n3 = stateVariables["/n3"].stateValues.value; - let n4 = stateVariables["/n4"].stateValues.value; - - cy.get(cesc("#\\/n1")).should( - "have.text", - String(Math.round(n1 * 10 ** 8) / 10 ** 8), - ); - cy.get(cesc("#\\/n2")).should( - "have.text", - String(Math.round(n2 * 10 ** 1) / 10 ** 1), - ); - cy.get(cesc("#\\/n3")).should( - "have.text", - String(Math.round(n3 * 10 ** 3) / 10 ** 3), - ); - cy.get(cesc("#\\/n4")).should("have.text", String(n4) + ".0"); - - cy.get(cesc("#\\/n1a")).should( - "have.text", - String(Math.round(n1 * 10 ** 8) / 10 ** 8), - ); - cy.get(cesc("#\\/n2a")).should( - "have.text", - String(Math.round(n2 * 10 ** 1) / 10 ** 1), - ); - cy.get(cesc("#\\/n3a")).should( - "have.text", - String(Math.round(n3 * 10 ** 3) / 10 ** 3), - ); - cy.get(cesc("#\\/n4a")).should("have.text", String(n4) + ".0"); - }); - }); -});