Skip to content

Commit

Permalink
Convert Nuclear to Arrange/Act/Assert structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sjd210 committed Dec 5, 2024
1 parent 50ff875 commit bc54b94
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 41 deletions.
28 changes: 9 additions & 19 deletions test/models/Chemistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ describe("testCheck Term", () => {
// Arrange
const stateTerm = structuredClone(term);
stateTerm.state = "(aq)";
let stateTermCopy: Term = structuredClone(stateTerm);
const stateTermCopy: Term = structuredClone(stateTerm);

// Act
const testResponse = testCheck(stateTerm, stateTermCopy)
Expand Down Expand Up @@ -685,6 +685,14 @@ describe("testCheck Expression", () => {
});

describe("testCheck Statement", () => {
const chargedIon: Ion = structuredClone(augmentedIon);
chargedIon.molecules = [[structuredClone(element), 1]];

const chargedTerm: Term = augmentNode(structuredClone(term));
chargedTerm.value = chargedIon;

const chargedExpr: Expression = structuredClone(augmentedExpression);
chargedExpr.terms?.push(chargedTerm);
it("Returns truthy CheckerResponse when expressions match",
() => {
// Arrange
Expand Down Expand Up @@ -779,15 +787,6 @@ describe("testCheck Statement", () => {
it("Returns truthy CheckerResponse when statements charges are balanced",
() => {
// Arrange
const chargedIon: Ion = structuredClone(augmentedIon);
chargedIon.molecules = [[structuredClone(element), 1]];

const chargedTerm: Term = augmentNode(structuredClone(term));
chargedTerm.value = chargedIon;

const chargedExpr: Expression = structuredClone(augmentedExpression);
chargedExpr.terms?.push(chargedTerm);

const balancedCharges: Statement = augmentNode(structuredClone(statement));
balancedCharges.left = structuredClone(chargedExpr);
balancedCharges.right = structuredClone(chargedExpr);
Expand All @@ -804,15 +803,6 @@ describe("testCheck Statement", () => {
it("Returns falsy CheckerResponse when statements charges are unbalanced",
() => {
// Arrange
const chargedIon: Ion = structuredClone(augmentedIon);
chargedIon.molecules = [[structuredClone(element), 1]];

const chargedTerm: Term = augmentNode(structuredClone(term));
chargedTerm.value = chargedIon;

const chargedExpr: Expression = structuredClone(augmentedExpression);
chargedExpr.terms?.push(chargedTerm);

const unbalancedCharges: Statement = augmentNode(structuredClone(statement));
unbalancedCharges.left = structuredClone(chargedExpr);

Expand Down
77 changes: 55 additions & 22 deletions test/models/Nuclear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ function unaugmentedTestCheck<T extends ASTNode>(target: T, test: T): CheckerRes
describe("testCheck Particle", () => {
it("Returns truthy CheckerResponse when particles match",
() => {
// Act
// Arrange
const particleCopy: Particle = structuredClone(particle)

// Act
const testResponse = testCheck(particleCopy, particle);

// Assert
Expand All @@ -100,12 +101,13 @@ describe("testCheck Particle", () => {
);
it("Returns falsy CheckerResponse when particles don't match",
() => {
// Act
// Arrange
const valueMismatch: Particle = structuredClone(particle);
valueMismatch.particle = "betaparticle";
valueMismatch.mass = 0;
valueMismatch.atomic = -1;

// Act
const elementIncorrect = testCheck(valueMismatch, particle);

// Assert
Expand All @@ -115,10 +117,11 @@ describe("testCheck Particle", () => {
);
it("Returns falsy CheckerResponse when atomic number is invalid",
() => {
// Act
// Arrange
const nucleonMismatch: Particle = structuredClone(particle);
nucleonMismatch.particle = "betaparticle";

// Act
const nucleonIncorrect = testCheck(nucleonMismatch, particle);

// Assert
Expand All @@ -131,9 +134,10 @@ describe("testCheck Particle", () => {
describe("testCheck Isotope", () => {
it("Returns truthy CheckerResponse when isotope match",
() => {
// Act
// Arrange
const isotopeCopy: Isotope = structuredClone(isotope);

// Act
const testResponse = testCheck(isotopeCopy, isotope);

// Assert
Expand All @@ -143,11 +147,13 @@ describe("testCheck Isotope", () => {
);
it("Returns falsy CheckerResponse when isotope do not match",
() => {
// Arrange
const elementMismatch: Isotope = structuredClone(isotope);
elementMismatch.element = "Cl";
elementMismatch.mass = 35;
elementMismatch.atomic = 17;

// Act
const elementIncorrect = testCheck(elementMismatch, isotope);

// Assert
Expand All @@ -157,9 +163,11 @@ describe("testCheck Isotope", () => {
);
it("Returns falsy CheckerResponse when atomic number is invalid",
() => {
// Arrange
const nucleonMismatch: Isotope = structuredClone(isotope);
nucleonMismatch.element = "Cl";

// Act
const nucleonIncorrect = testCheck(nucleonMismatch, isotope);

// Assert
Expand All @@ -172,23 +180,32 @@ describe("testCheck Isotope", () => {
describe("testCheck Term", () => {
it("Returns truthy CheckerResponse when terms match",
() => {
let termCopy: Term = structuredClone(term);
// Arrange
const termCopy: Term = structuredClone(term);

let testResponse = testCheck(termCopy, term)
// Act
const testResponse = testCheck(termCopy, term)

// Assert
expect(testResponse.isEqual).toBeTruthy();
}
);
it("Returns falsy CheckerResponse when terms don't match",
() => {
// Mismatched coefficients
let mismatchTerm: Term = structuredClone(term);
// Arrange
const mismatchTerm: Term = structuredClone(term);
mismatchTerm.coeff = 3;

expect(testCheck(mismatchTerm, term).isEqual).toBeFalsy();
expect(testCheck(mismatchTerm, term).sameCoefficient).toBeFalsy();
// Act
const termIncorrect = testCheck(mismatchTerm, term);
const typeIncorrect = testCheck(particleTerm, term);

// Assert
expect(termIncorrect.isEqual).toBeFalsy();
expect(termIncorrect.sameCoefficient).toBeFalsy();

// Mismatched type
expect(testCheck(particleTerm, term).isEqual).toBeFalsy();
expect(typeIncorrect.isEqual).toBeFalsy();
}
);
it("Retains CheckerResponse properties",
Expand All @@ -205,6 +222,7 @@ describe("testCheck Term", () => {
const particleCopyTerm: Term = structuredClone(particleTerm);
particleCopyTerm.value = particleCopy;

// Act
const isotopeResponse = testCheck(isotopeError, isotope);
const particleResponse = testCheck(particleCopy, particle);

Expand All @@ -221,42 +239,51 @@ describe("testCheck Term", () => {
describe("testCheck Expression", () => {
it("Returns truthy CheckerResponse when expressions match",
() => {
// Act
// Arrange
const permutedExpression: Expression = structuredClone(expression);
permutedExpression.terms?.reverse;

// Act
const testResponse: CheckerResponse = testCheck(permutedExpression, expression);

// Assert
expect(testResponse.isEqual).toBeTruthy();
}
);
it("Returns falsy CheckerResponse when expressions do not match",
() => {
// Act
// Arrange
const lengthMismatch: Expression = structuredClone(expression);
lengthMismatch.terms?.push(structuredClone(term));

const termMismatch: Expression = structuredClone(expression);
if (termMismatch.terms) termMismatch.terms[1] = structuredClone(term);

// Act
const lengthIncorrect = testCheck(lengthMismatch, expression);
const termIncorrect = testCheck(termMismatch, expression);

// Assert
expect(testCheck(lengthMismatch, expression).isEqual).toBeFalsy();
expect(testCheck(termMismatch, expression).isEqual).toBeFalsy();
expect(lengthIncorrect.isEqual).toBeFalsy();
expect(termIncorrect.isEqual).toBeFalsy();
}
);
it("Returns an error if the AST is not augmented",
() => {
// Act
// Arrange
// This is the same as expression just unaugmented
const unaugmentedExpression: Expression = {
type: "expr",
term: structuredClone(term),
rest: structuredClone(particleTerm)
}

// Act
const testResponse = unaugmentedTestCheck(unaugmentedExpression, expression);

// Assert
expect(unaugmentedTestCheck(unaugmentedExpression, expression).containsError).toBeTruthy();
expect(unaugmentedTestCheck(unaugmentedExpression, expression).error).toEqual("Received unaugmented AST during checking process.");
expect(testResponse.containsError).toBeTruthy();
expect(testResponse.error).toEqual("Received unaugmented AST during checking process.");

expect(console.error).toHaveBeenCalled();
}
Expand All @@ -266,9 +293,10 @@ describe("testCheck Expression", () => {
describe("testCheck Statement", () => {
it("Returns truthy CheckerResponse when expressions match",
() => {
// Act
// Arrange
const copy: Statement = structuredClone(statement);

// Act
const copyResult: CheckerResponse = testCheck(copy, statement);

// Assert
Expand All @@ -277,12 +305,13 @@ describe("testCheck Statement", () => {
);
it("Returns falsy CheckerResponse when expressions do not match",
() => {
// Act
// Arrange
const swappedExpressions: Statement = structuredClone(statement);
const tempExpression = swappedExpressions.left;
swappedExpressions.left = swappedExpressions.right;
swappedExpressions.right = tempExpression;

// Act
const swapResult = testCheck(swappedExpressions, statement);

// Assert
Expand Down Expand Up @@ -323,7 +352,7 @@ describe("Check", () => {

it("Returns error message when given one",
() => {
// Act
// Arrange
const error: ParseError = {
type: "error",
value: "Sphinx of black quartz, judge my vow",
Expand All @@ -334,7 +363,9 @@ describe("Check", () => {
result: error
}

// Act
const response: CheckerResponse = check(errorAST, ast);

// Assert
expect(response.containsError).toBeTruthy();
expect(response.error).toBe("Sphinx of black quartz, judge my vow");
Expand All @@ -343,12 +374,14 @@ describe("Check", () => {
);
it("Returns type mismatch when appropriate",
() => {
// Act
// Arrange
const expressionAST: NuclearAST = {
result: structuredClone(expression)
}

// Act
const response: CheckerResponse = check(ast, expressionAST);

// Assert
expect(response.typeMismatch).toBeTruthy();
expect(response.expectedType).toBe("expr");
Expand Down

0 comments on commit bc54b94

Please sign in to comment.