Skip to content
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

feat: add indentDSL to improve checkDSL parsing reliability #68

Merged
merged 11 commits into from
Oct 14, 2022
29 changes: 29 additions & 0 deletions src/indent-dsl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const indentDSL = (rawDsl: string, removeEmptyLines = false) => {
const indentType = "";
const indentRelation = " ";
const indentDefine = " ";

return rawDsl
.split("\n")
.filter((line: string) => (removeEmptyLines ? line.trim().length > 0 : true))
.map((line: string) => {
const selectedLine = line.trim();
const keyword = selectedLine.split(" ")[0];
let indentation = "";
switch (keyword) {
case "type":
indentation = indentType;
break;
case "relations":
indentation = indentRelation;
break;
case "define":
indentation = indentDefine;
break;
default:
break;
}
return `${indentation}${selectedLine}`;
})
.join("\n");
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { friendlySyntaxToApiSyntax } from "./friendly-to-api";
export { apiSyntaxToFriendlySyntax } from "./api-to-friendly";
export { checkDSL } from "./check-dsl";
export { indentDSL } from "./indent-dsl";
62 changes: 62 additions & 0 deletions tests/indent-dsl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { friendlySyntaxToApiSyntax, indentDSL } from "../src";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { testModels } from "./data";

const getRandomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};

const generateRandomNumberOfSpaces = () => {
return " ".repeat(getRandomNumber(0, 30));
};

const generateRandomNewLines = () => {
return "\n".repeat(getRandomNumber(0, 15));
};

describe("indent-dsl", () => {
testModels.forEach((testCase) => {
it(`should indent ${testCase.name}`, () => {
const rawFriendly = testCase.friendly
.split("\n")
.map((line) => generateRandomNumberOfSpaces() + line.trim())
.join("\n");
const apiSyntax = friendlySyntaxToApiSyntax(indentDSL(rawFriendly));
expect(apiSyntax).toEqual(testCase.json);
});
});

testModels.forEach((testCase) => {
it(`should indent ${testCase.name} with random spaces and empty lines removed`, () => {
const rawFriendly = testCase.friendly
.split("\n")
.map((line) => generateRandomNumberOfSpaces() + line.trim() + generateRandomNewLines())
.join("\n");
const apiSyntax = friendlySyntaxToApiSyntax(indentDSL(rawFriendly, true));
expect(apiSyntax).toEqual(testCase.json);
});
});

testModels.forEach((testCase) => {
it(`should indent ${testCase.name} with empty lines removed`, () => {
const rawFriendly = testCase.friendly
.split("\n")
.map((line) => line.trim() + generateRandomNewLines())
.join("\n");
const apiSyntax = friendlySyntaxToApiSyntax(indentDSL(rawFriendly, true));
expect(apiSyntax).toEqual(testCase.json);
});
});

testModels.forEach((testCase) => {
it(`should indent ${testCase.name} with empty lines not removed`, () => {
const rawFriendly = testCase.friendly
.split("\n")
.map((line) => line.trim() + generateRandomNewLines())
.join("\n");
const apiSyntax = friendlySyntaxToApiSyntax(indentDSL(rawFriendly, false));
expect(apiSyntax).toEqual(testCase.json);
});
});
});