Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature/markdown as master #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
validate step has tests && tests before solution
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 24, 2020
commit 0765174b98476ea5ab672b6696246f895e0ec9e8
25 changes: 19 additions & 6 deletions src/utils/validateCommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
export function validateCommitOrder(positions: string[]): boolean {
// loop over positions
const errors: number[] = [];
let previous = { level: 0, step: 0 };
let current = { level: 0, step: 0 };
let previous = { level: 0, step: 0, type: "" };
let current = { level: 0, step: 0, type: "" };
positions.forEach((position: string, index: number) => {
if (position === "INIT") {
if (previous.level !== 0 && previous.step !== 0) {
errors.push(index);
}
current = { level: 0, step: 0 };
current = { level: 0, step: 0, type: "" };
return;
} else {
// @deprecate - remove L|Q
const levelMatch = position.match(/^(?<level>[0-9]+)$/);
// @deprecate - remove S|Q|A
const stepMatch = position.match(
/^(?<level>[0-9]+)\.(?<step>[0-9]+):[T|S]$/
/^(?<level>[0-9]+)\.(?<step>[0-9]+):(?<stepType>[T|S])$/
);
if (levelMatch) {
// allows next level or step
Expand All @@ -27,7 +27,7 @@ export function validateCommitOrder(positions: string[]): boolean {
return;
}
const level = Number(levelString);
current = { level, step: 0 };
current = { level, step: 0, type: "" };
} else if (stepMatch) {
// allows next level or step
if (!stepMatch?.groups?.level || !stepMatch?.groups.step) {
Expand All @@ -38,13 +38,26 @@ export function validateCommitOrder(positions: string[]): boolean {

const level = Number(levelString);
const step = Number(stepString);
current = { level, step };
const type = stepMatch?.groups.stepType;

const sameStep = previous.level === level && previous.step === step;

if (
// tests should come before the solution
(sameStep && type === "T" && previous.type === "S") ||
// step should have tests
(!sameStep && type === "S")
) {
errors.push(index);
}
current = { level, step, type };
} else {
// error
console.warn(`Invalid commit position: ${position}`);
return;
}
if (
// levels or steps are out of order
current.level < previous.level ||
(current.level === previous.level && current.step < previous.step)
) {
Expand Down
12 changes: 12 additions & 0 deletions tests/commitOrder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("commitOrder", () => {
"1",
"1",
"1.1:T",
"1.1:T",
"1.1:S",
"1.1:S",
"1.2:T",
"1.2:S",
Expand Down Expand Up @@ -45,5 +47,15 @@ describe("commitOrder", () => {
const result = validateCommitOrder(positions);
expect(result).toBe(false);
});
it("should return false if solution is before step", () => {
const positions = ["INIT", "1", "1.1:S", "1.1:T", "1.2:T"];
const result = validateCommitOrder(positions);
expect(result).toBe(false);
});
it("should return false if solution but no test step", () => {
const positions = ["INIT", "1", "1.1:S", "1.2:T"];
const result = validateCommitOrder(positions);
expect(result).toBe(false);
});
});
});