Skip to content

Commit

Permalink
chore: Add more tests and fix some issues with transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 6, 2018
1 parent 844b8d2 commit d9e3487
Show file tree
Hide file tree
Showing 10 changed files with 533 additions and 155 deletions.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/dist/tests
/coverage
/setup
/temp
*.js.map
*.v12.suo
*.csproj
Expand Down
29 changes: 0 additions & 29 deletions src/tests/issues.ts

This file was deleted.

172 changes: 142 additions & 30 deletions src/tests/mainTests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import * as assert from "assert";
import { replaceInText } from "./../main";
import { runTestOnAllMethods } from "./testMethods";
import { runTestOnAllMethods } from "./testMethods";

runTestOnAllMethods(doTestsForMethod);

function doTestsForMethod(runTest: (text: string, expected: string) => void) {
describe("arrays", () => {
describe("nameof", () => {
function doTestsForMethod(runTest: (text: string, expected: string) => void, runThrowsTest: (text: string) => void) {
describe("nameof", () => {
describe("argument", () => {
it("should get the result of an identifier", () => {
runTest(`nameof(myObj);`, `"myObj";`);
});

it("should get the result of a property access expression", () => {
runTest(`nameof(myObj.prop);`, `"prop";`);
});

it("should get the result of a property access expression with null assertion operators", () => {
runTest(`nameof(myObj!.prop!);`, `"prop";`);
});
});

describe("type parameter", () => {
it("should get the result of an identifier", () => {
runTest(`nameof<Test>();`, `"Test";`);
});

it("should get the result of a fully qualified name", () => {
runTest(`nameof<This.Is.A.Test>();`, `"Test";`);
});
});

describe("arrays", () => {
it("should include the brackets", () => {
runTest(`nameof(anyProp[0]);`, `"anyProp[0]";`);
});
Expand All @@ -24,43 +46,133 @@ function doTestsForMethod(runTest: (text: string, expected: string) => void) {
});
});

describe("nameof.full", () => {
it("should include the brackets", () => {
runTest(`nameof.full(anyProp[0].myProp);`, `"anyProp[0].myProp";`);
describe("with function", () => {
it("should get the last string", () => {
runTest(`nameof<MyInterface>(i => i.prop1.prop2);`, `"prop2";`);
});

it("should throw when the function doesn't have a period", () => {
runThrowsTest(`nameof<MyInterface>(i => i);`);
});
});
});

describe("nameof with function", () => {
it("should get the last string", () => {
runTest(`nameof<MyInterface>(i => i.prop1.prop2);`, `"prop2";`);
});
describe("nameof.full", () => {
describe("argument", () => {
it("should include everything when no count arg is provided", () => {
runTest(`nameof.full(obj.prop.other);`, `"obj.prop.other";`);
});

it("should throw when the function doesn't have a period", () => {
assert.throws(() => {
replaceInText(`nameof<MyInterface>(i => i);`);
it("should not include null assertion operators", () => {
runTest(`nameof.full(obj!.prop!.other!);`, `"obj.prop.other";`);
});

it("should not include null assertion operators when also using element access expressions", () => {
runTest(`nameof.full(obj!.prop![0].other!);`, `"obj.prop[0].other";`);
});

it("should allow using a period index", () => {
runTest("nameof.full(MyTest.Test.This, 1);", `"Test.This";`);
});

it("should allow using a period index of 0", () => {
runTest("nameof.full(MyTest.Test.This, 0);", `"MyTest.Test.This";`);
});

it("should allow using a period index up to its max value", () => {
runTest("nameof.full(MyTest.Test.This, 2);", `"This";`);
});

it("should allow using a negative period index", () => {
runTest("nameof.full(MyTest.Test.This, -1);", `"This";`);
});

it("should allow using a negative period index to its max value", () => {
runTest("nameof.full(MyTest.Test.This, -3);", `"MyTest.Test.This";`);
});

it("should throw when the periodIndex is not a number literal", () => {
runThrowsTest("nameof.full(MyTest.Test, 'test')");
});
});
});

describe("nameof.full with function", () => {
it("should get the text", () => {
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2);`, `"prop1.prop2";`);
it("should throw when the periodIndex is greater than the number of periods", () => {
runThrowsTest("nameof.full(MyTest.Test, 2)");
});

it("should throw when the absolute value of the negative periodIndex is greater than the number of periods + 1", () => {
runThrowsTest("nameof.full(MyTest.Test, -3)");
});
});

it("should get the when using a function", () => {
runTest(`nameof.full<MyInterface>(function(i) { return i.prop1.prop2; });`, `"prop1.prop2";`);
describe("type parameter", () => {
it("should include everything when no count arg is provided", () => {
runTest(`nameof.full<Some.Test.Name>();`, `"Some.Test.Name";`);
});

it("should allow using a period index", () => {
runTest("nameof.full<MyTest.Test.This>(1);", `"Test.This";`);
});

it("should allow using a period index of 0", () => {
runTest("nameof.full<MyTest.Test.This>(0);", `"MyTest.Test.This";`);
});

it("should allow using a period index up to its max value", () => {
runTest("nameof.full<MyTest.Test.This>(2);", `"This";`);
});

it("should allow using a negative period index", () => {
runTest("nameof.full<MyTest.Test.This>(-1);", `"This";`);
});

it("should allow using a negative period index to its max value", () => {
runTest("nameof.full<MyTest.Test.This>(-3);", `"MyTest.Test.This";`);
});

it("should throw when the periodIndex is not a number literal", () => {
runThrowsTest("nameof.full<MyTest.Test>('test')");
});

it("should throw when the periodIndex is greater than the number of periods", () => {
runThrowsTest("nameof.full<MyTest.Test>(2)");
});

it("should throw when the absolute value of the negative periodIndex is greater than the number of periods + 1", () => {
runThrowsTest("nameof.full<MyTest.Test>(-3)");
});
});

it("should get the text when providing a period", () => {
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2, 0);`, `"prop1.prop2";`);
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2, 1);`, `"prop2";`);
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2.prop3, -1);`, `"prop3";`);
describe("arrays", () => {
it("should include the brackets", () => {
runTest(`nameof.full(anyProp[0].myProp);`, `"anyProp[0].myProp";`);
});
});

it("should throw when the function doesn't have a period", () => {
assert.throws(() => {
replaceInText(`nameof.full<MyInterface>(i => i);`);
describe("with function", () => {
it("should get the text", () => {
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2);`, `"prop1.prop2";`);
});

it("should get the text without the null assertion operator", () => {
runTest(`nameof.full<MyInterface>(i => i.prop1!.prop2!);`, `"prop1.prop2";`);
});

it("should get the text when there's a trailing comma with whitespace", () => {
runTest("nameof.full<IState>(state => state.field.dates, );", `"field.dates";`);
});

it("should get the text when using a function", () => {
runTest(`nameof.full<MyInterface>(function(i) { return i.prop1.prop2; });`, `"prop1.prop2";`);
});

it("should get the text when providing a period", () => {
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2, 0);`, `"prop1.prop2";`);
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2, 1);`, `"prop2";`);
runTest(`nameof.full<MyInterface>(i => i.prop1.prop2.prop3, -1);`, `"prop3";`);
});

it("should throw when the function doesn't have a period", () => {
runThrowsTest(`nameof.full<MyInterface>(i => i);`);
});
});
});
Expand Down
4 changes: 4 additions & 0 deletions src/tests/testMethods/replaceInTextTestMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export function runTest(text: string, expected: string) {
const result = replaceInText(text);
assert.equal(result.fileText || text, expected);
}

export function runThrowTest(text: string) {
assert.throws(() => replaceInText(text));
}
13 changes: 8 additions & 5 deletions src/tests/testMethods/runTestOnAllMethods.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { runTest as runTest1 } from "./replaceInTextTestMethod";
import { runTest as runTest2 } from "./transformationTestMethod";
import * as replaceInTextTestMethod from "./replaceInTextTestMethod";
import * as transformationTestMethod from "./transformationTestMethod";

const testMethods = [{ name: "replaceInText", runTest: runTest1 }, { name: "transformation", runTest: runTest2 }];
const testMethods = [
{ name: "replaceInText", ...replaceInTextTestMethod },
{ name: "transformation", ...transformationTestMethod }
];

export function runTestOnAllMethods(runTest: (method: (text: string, expected: string) => void) => void) {
export function runTestOnAllMethods(runTest: (method: (text: string, expected: string) => void, throwTestMethod: (text: string) => void) => void) {
for (const testMethod of testMethods) {
describe(testMethod.name, () => {
runTest(testMethod.runTest);
runTest(testMethod.runTest, testMethod.runThrowTest);
});
}
}
12 changes: 10 additions & 2 deletions src/tests/testMethods/transformationTestMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import * as ts from "typescript";
import { transformerFactory } from "../../transformation";

export function runTest(text: string, expected: string) {
const results = run(text);
assert.equal(results[0].fileText.trim(), expected.trim());
}

export function runThrowTest(text: string) {
assert.throws(() => run(text));
}

function run(text: string) {
const results: { fileName: string; fileText: string; }[] = [];
const compilerOptions: ts.CompilerOptions = {
strictNullChecks: true,
Expand Down Expand Up @@ -32,6 +41,5 @@ export function runTest(text: string, expected: string) {
};
const program = ts.createProgram(["/file.ts"], compilerOptions, host);
program.emit(undefined, (fileName, fileText) => results.push({ fileName, fileText }), undefined, false, transformers);

assert.equal(results[0].fileText.trim(), expected.trim());
return results;
}
22 changes: 0 additions & 22 deletions src/tests/text/exceptionsTests.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/text/replaceCallExpressionReplacesInText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function replaceCallExpressionReplacesInText(callExpressionReplaces: Repl
const text = getReplaceInfoText(currentReplace, periodIndexArgIndex);
const newText = (currentReplace.showFull
? `"${getFullText(text, getPeriodIndexArgText(currentReplace, periodIndexArgIndex))}"`
: `"${text.substring(text.lastIndexOf(".") + 1)}"`).replace(/ /g, "");
: `"${text.substring(text.lastIndexOf(".") + 1)}"`).replace(/ /g, "").replace(/!/g, "");
const offset = newText.length - (currentReplace.end - currentReplace.pos);

data = data.substring(0, currentReplace.pos) + newText + data.substring(currentReplace.end);
Expand Down

0 comments on commit d9e3487

Please sign in to comment.