Skip to content

Commit 2bd383c

Browse files
tomblindPerryvw
authored andcommitted
New Tests for Code Coverage (#436)
* added tests to imrove coverage * tweaked hoisting test
1 parent 29fe5e4 commit 2bd383c

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

test/unit/bindingpatterns.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expect, Test, TestCase, FocusTest, TestCases } from "alsatian";
1+
import { Expect, Test, TestCase, TestCases } from "alsatian";
22

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

@@ -57,6 +57,20 @@ export class BindingPatternTests {
5757
Expect(result).toBe(true);
5858
}
5959

60+
@TestCases(testCases)
61+
@TestCases(testCasesDefault)
62+
public testBindingPatternExportDeclarations(
63+
bindingString: string,
64+
objectString: string,
65+
returnVariable: string
66+
): void {
67+
const result = util.transpileExecuteAndReturnExport(
68+
`export const ${bindingString} = ${objectString};`,
69+
returnVariable
70+
);
71+
Expect(result).toBe(true);
72+
}
73+
6074
@TestCases(testCases)
6175
@Test("Object bindings with call expressions")
6276
public testBindingPatternCallExpressions(
@@ -74,4 +88,4 @@ export class BindingPatternTests {
7488
Expect(result).toBe(true);
7589
}
7690

77-
}
91+
}

test/unit/expressions.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ExpressionTests {
1313
@TestCase("--i", "i = i - 1;")
1414
@TestCase("!a", "not a;")
1515
@TestCase("-a", "-a;")
16+
@TestCase("+a", "a;")
1617
@TestCase("let a = delete tbl['test']", "local a = (function()\n tbl.test = nil;\n return true;\nend)();")
1718
@TestCase("delete tbl['test']", "tbl.test = nil;")
1819
@TestCase("let a = delete tbl.test", "local a = (function()\n tbl.test = nil;\n return true;\nend)();")
@@ -365,6 +366,7 @@ export class ExpressionTests {
365366
}
366367

367368
@TestCase("x = y", "y")
369+
@TestCase("x += y", "xy")
368370
@Test("Assignment expressions")
369371
public assignmentExpression(expression: string, expected: string): void {
370372
const result = util.transpileAndExecute(`let x = "x"; let y = "y"; return ${expression};`);

test/unit/functions.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,19 @@ export class FunctionTests {
379379
Expect(result).toBe("foobar");
380380
}
381381

382+
@Test("Element access call no args")
383+
public elementAccessCallNoArgs(): void {
384+
const code = `class C {
385+
prop = "bar";
386+
method() { return this.prop; }
387+
}
388+
const c = new C();
389+
return c['method']();
390+
`;
391+
const result = util.transpileAndExecute(code);
392+
Expect(result).toBe("bar");
393+
}
394+
382395
@Test("Complex element access call")
383396
public elementAccessCallComplex(): void {
384397
const code = `class C {
@@ -392,6 +405,19 @@ export class FunctionTests {
392405
Expect(result).toBe("foobar");
393406
}
394407

408+
@Test("Complex element access call no args")
409+
public elementAccessCallComplexNoArgs(): void {
410+
const code = `class C {
411+
prop = "bar";
412+
method() { return this.prop; }
413+
}
414+
function getC() { return new C(); }
415+
return getC()['method']();
416+
`;
417+
const result = util.transpileAndExecute(code);
418+
Expect(result).toBe("bar");
419+
}
420+
395421
@Test("Complex element access call statement")
396422
public elementAccessCallComplexStatement(): void {
397423
const code = `let foo: string;
@@ -475,4 +501,14 @@ export class FunctionTests {
475501
export const result = bar(7);`;
476502
Expect(util.transpileExecuteAndReturnExport(code, "result")).toBe(7);
477503
}
504+
505+
@Test("Function using global as this")
506+
public functionUsingGlobalAsThis(): void {
507+
const code =
508+
`var foo = "foo";
509+
function bar(this: any) {
510+
return this.foo;
511+
}`;
512+
Expect(util.transpileAndExecute("return foo;", undefined, undefined, code)).toBe("foo");
513+
}
478514
}

test/unit/hoisting.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ export class HoistingTests {
123123
Expect(result).toBe(expectResult);
124124
}
125125

126+
@TestCase("", "foofoo")
127+
@TestCase(" = \"bar\"", "barbar")
128+
@Test("Var hoisting from child scope")
129+
public varHoistingFromChildScope(initializer: string, expectResult: string): void {
130+
const code =
131+
`foo = "foo";
132+
let result: string;
133+
if (true) {
134+
var foo${initializer};
135+
result = foo;
136+
}
137+
return foo + result;`;
138+
const result = util.transpileAndExecute(code);
139+
Expect(result).toBe(expectResult);
140+
}
141+
142+
@Test("Hoisting due to reference from hoisted function")
143+
public hoistingDueToReferenceFromHoistedFunction(): void {
144+
const code =
145+
`const foo = "foo";
146+
const result = bar();
147+
function bar() {
148+
return foo;
149+
}
150+
return result;`;
151+
const result = util.transpileAndExecute(code);
152+
Expect(result).toBe("foo");
153+
}
154+
126155
@Test("Namespace Hoisting")
127156
public namespaceHoisting(): void {
128157
const code =

0 commit comments

Comments
 (0)