리펙토링: 코드 최적화
This commit is contained in:
@@ -14,22 +14,26 @@ describe("parenthesis sign problem engine", () => {
|
||||
expect(Number.isInteger(problem.multiplier)).toBe(true);
|
||||
expect(problem.multiplier).toBeGreaterThanOrEqual(-9);
|
||||
expect(problem.multiplier).toBeLessThanOrEqual(-1);
|
||||
expect(problem.a).toBeGreaterThanOrEqual(1);
|
||||
expect(problem.a).toBeLessThanOrEqual(9);
|
||||
expect(problem.b).toBeGreaterThanOrEqual(1);
|
||||
expect(problem.b).toBeLessThanOrEqual(9);
|
||||
expect(problem.a).not.toBe(problem.b);
|
||||
expect(["+", "-"]).toContain(problem.operator);
|
||||
expect(problem.terms).toHaveLength(2);
|
||||
expect(problem.operators).toHaveLength(1);
|
||||
expect(problem.terms.every((term) => term >= 1 && term <= 9)).toBe(true);
|
||||
expect(new Set(problem.terms).size).toBe(problem.terms.length);
|
||||
expect(["+", "-"]).toContain(problem.operators[0]);
|
||||
}
|
||||
});
|
||||
|
||||
it("distributes the multiplier correctly for both operators", () => {
|
||||
for (let index = 0; index < 100; index += 1) {
|
||||
const problem = generateProblem();
|
||||
const signedB = problem.operator === "-" ? -problem.b : problem.b;
|
||||
const signedTerms = problem.terms.map((term, termIndex) => {
|
||||
if (termIndex === 0) return term;
|
||||
|
||||
expect(problem.distributedA).toBe(problem.multiplier * problem.a);
|
||||
expect(problem.distributedB).toBe(problem.multiplier * signedB);
|
||||
return problem.operators[termIndex - 1] === "-" ? -term : term;
|
||||
});
|
||||
|
||||
expect(problem.distributedTerms).toEqual(
|
||||
signedTerms.map((term) => problem.multiplier * term),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -37,20 +41,15 @@ describe("parenthesis sign problem engine", () => {
|
||||
const problem = generateProblem();
|
||||
|
||||
const correctResult = gradeAnswer(problem, {
|
||||
a: problem.distributedA,
|
||||
b: problem.distributedB,
|
||||
terms: problem.distributedTerms,
|
||||
});
|
||||
|
||||
expect(correctResult.correct).toBe(true);
|
||||
expect(correctResult.userAnswer).toEqual([
|
||||
problem.distributedA,
|
||||
problem.distributedB,
|
||||
]);
|
||||
expect(correctResult.userAnswer).toEqual(problem.distributedTerms);
|
||||
|
||||
expect(
|
||||
gradeAnswer(problem, {
|
||||
a: problem.distributedB,
|
||||
b: problem.distributedA,
|
||||
terms: [...problem.distributedTerms].reverse(),
|
||||
}).correct,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user