ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
85 lines (83 loc) • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const ts = require("typescript");
const errors = require("./../../errors");
const testHelpers_1 = require("./../compiler/testHelpers");
describe("helpers", () => {
describe("throwIfNotType", () => {
it("should throw when not the same type", () => {
chai_1.expect(() => errors.throwIfNotType(4, "string", "argName")).to.throw(errors.ArgumentTypeError, "Argument Error (argName): Expected type 'string', but was 'number'.");
});
it("should not throw when the same type", () => {
chai_1.expect(() => errors.throwIfNotType("", "string", "argName")).to.not.throw();
});
});
describe("throwIfNotStringOrWhitespace", () => {
it("should throw when not a string", () => {
chai_1.expect(() => errors.throwIfNotStringOrWhitespace(4, "argName")).to.throw(errors.ArgumentTypeError, "Argument Error (argName): Expected type 'string', but was 'number'.");
});
it("should throw when null", () => {
chai_1.expect(() => errors.throwIfNotStringOrWhitespace(null, "argName")).to.throw(errors.ArgumentTypeError, "Argument Error (argName): Expected type 'string', but was 'object'.");
});
it("should throw when whitespace string", () => {
chai_1.expect(() => errors.throwIfNotStringOrWhitespace(" ", "argName")).to.throw(errors.ArgumentNullOrWhitespaceError, "Argument Error (argName): Was null or whitespace.");
});
it("should throw when string that's not a whitespace string", () => {
chai_1.expect(() => errors.throwIfNotStringOrWhitespace("str", "argName")).to.not.throw();
});
});
describe("throwIfNotSyntaxKind", () => {
const { firstChild } = testHelpers_1.getInfoFromText("class Identifier {}");
it("should throw when not the expected syntax kind and no message is specified", () => {
chai_1.expect(() => errors.throwIfNotSyntaxKind(firstChild, ts.SyntaxKind.AbstractKeyword))
.to.throw(errors.NotImplementedError, "Expected node to be syntax kind AbstractKeyword, but was ClassDeclaration");
});
it("should throw when not the expected syntax kind and a message is specified", () => {
chai_1.expect(() => errors.throwIfNotSyntaxKind(firstChild, ts.SyntaxKind.AbstractKeyword, "message"))
.to.throw(errors.NotImplementedError, "message");
});
it("should not throw when is the expected syntax kind", () => {
chai_1.expect(() => errors.throwIfNotSyntaxKind(firstChild, ts.SyntaxKind.ClassDeclaration))
.to.not.throw();
});
});
describe("throwIfOutOfRange", () => {
it("should not throw when inside the bounds", () => {
chai_1.expect(() => errors.throwIfOutOfRange(5, [1, 10], "arg")).to.not.throw();
});
it("should throw when outside the inclusive lower bound", () => {
chai_1.expect(() => errors.throwIfOutOfRange(0, [1, 10], "arg")).to.throw();
});
it("should not throw when inside the inclusive lower bound", () => {
chai_1.expect(() => errors.throwIfOutOfRange(1, [1, 10], "arg")).to.not.throw();
});
it("should throw when outside the inclusive upper bound", () => {
chai_1.expect(() => errors.throwIfOutOfRange(11, [1, 10], "arg")).to.throw();
});
it("should not throw when inside the inclusive upper bound", () => {
chai_1.expect(() => errors.throwIfOutOfRange(10, [1, 10], "arg")).to.not.throw();
});
});
describe("throwIfNegative", () => {
it("should throw when negative", () => {
chai_1.expect(() => errors.throwIfNegative(-1, "arg")).to.throw();
});
it("should not throw when positive", () => {
chai_1.expect(() => errors.throwIfNegative(1, "arg")).to.not.throw();
});
it("should not throw when 0", () => {
chai_1.expect(() => errors.throwIfNegative(0, "arg")).to.not.throw();
});
});
describe("getNotImplementedForSyntaxKindError", () => {
const result = errors.getNotImplementedForSyntaxKindError(ts.SyntaxKind.EnumDeclaration);
it("should return a NotImplementedError", () => {
chai_1.expect(result).to.be.instanceOf(errors.NotImplementedError);
});
it("should have the correct message", () => {
chai_1.expect(result.message).to.equal("Not implemented feature for syntax kind 'EnumDeclaration'.");
});
});
});
//# sourceMappingURL=helpers.js.map