Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 16 additions & 2 deletions test/unit/bindingpatterns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Expect, Test, TestCase, FocusTest, TestCases } from "alsatian";
import { Expect, Test, TestCase, TestCases } from "alsatian";

import * as util from "../src/util";

Expand Down Expand Up @@ -57,6 +57,20 @@ export class BindingPatternTests {
Expect(result).toBe(true);
}

@TestCases(testCases)
@TestCases(testCasesDefault)
public testBindingPatternExportDeclarations(
bindingString: string,
objectString: string,
returnVariable: string
): void {
const result = util.transpileExecuteAndReturnExport(
`export const ${bindingString} = ${objectString};`,
returnVariable
);
Expect(result).toBe(true);
}

@TestCases(testCases)
@Test("Object bindings with call expressions")
public testBindingPatternCallExpressions(
Expand All @@ -74,4 +88,4 @@ export class BindingPatternTests {
Expect(result).toBe(true);
}

}
}
2 changes: 2 additions & 0 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class ExpressionTests {
@TestCase("--i", "i = i - 1;")
@TestCase("!a", "not a;")
@TestCase("-a", "-a;")
@TestCase("+a", "a;")
@TestCase("let a = delete tbl['test']", "local a = (function()\n tbl.test = nil;\n return true;\nend)();")
@TestCase("delete tbl['test']", "tbl.test = nil;")
@TestCase("let a = delete tbl.test", "local a = (function()\n tbl.test = nil;\n return true;\nend)();")
Expand Down Expand Up @@ -365,6 +366,7 @@ export class ExpressionTests {
}

@TestCase("x = y", "y")
@TestCase("x += y", "xy")
@Test("Assignment expressions")
public assignmentExpression(expression: string, expected: string): void {
const result = util.transpileAndExecute(`let x = "x"; let y = "y"; return ${expression};`);
Expand Down
36 changes: 36 additions & 0 deletions test/unit/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,19 @@ export class FunctionTests {
Expect(result).toBe("foobar");
}

@Test("Element access call no args")
public elementAccessCallNoArgs(): void {
const code = `class C {
prop = "bar";
method() { return this.prop; }
}
const c = new C();
return c['method']();
`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("bar");
}

@Test("Complex element access call")
public elementAccessCallComplex(): void {
const code = `class C {
Expand All @@ -392,6 +405,19 @@ export class FunctionTests {
Expect(result).toBe("foobar");
}

@Test("Complex element access call no args")
public elementAccessCallComplexNoArgs(): void {
const code = `class C {
prop = "bar";
method() { return this.prop; }
}
function getC() { return new C(); }
return getC()['method']();
`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("bar");
}

@Test("Complex element access call statement")
public elementAccessCallComplexStatement(): void {
const code = `let foo: string;
Expand Down Expand Up @@ -475,4 +501,14 @@ export class FunctionTests {
export const result = bar(7);`;
Expect(util.transpileExecuteAndReturnExport(code, "result")).toBe(7);
}

@Test("Function using global as this")
public functionUsingGlobalAsThis(): void {
const code =
`var foo = "foo";
function bar(this: any) {
return this.foo;
}`;
Expect(util.transpileAndExecute("return foo;", undefined, undefined, code)).toBe("foo");
}
}
29 changes: 29 additions & 0 deletions test/unit/hoisting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ export class HoistingTests {
Expect(result).toBe(expectResult);
}

@TestCase("", "foofoo")
@TestCase(" = \"bar\"", "barbar")
@Test("Var hoisting from child scope")
public varHoistingFromChildScope(initializer: string, expectResult: string): void {
const code =
`foo = "foo";
let result: string;
if (true) {
var foo${initializer};
result = foo;
}
return foo + result;`;
const result = util.transpileAndExecute(code);
Expect(result).toBe(expectResult);
}

@Test("Hoisting due to reference from hoisted function")
public hoistingDueToReferenceFromHoistedFunction(): void {
const code =
`const foo = "foo";
const result = bar();
function bar() {
return foo;
}
return result;`;
const result = util.transpileAndExecute(code);
Expect(result).toBe("foo");
}

@Test("Namespace Hoisting")
public namespaceHoisting(): void {
const code =
Expand Down