Skip to content

Commit

Permalink
added more tests and created a setup for mocking wow functions like C…
Browse files Browse the repository at this point in the history
…reateFrame in the timer class
  • Loading branch information
wartoshika committed Nov 17, 2018
1 parent 3cc0e98 commit 0a57847
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 43 deletions.
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,26 @@
"args": [
"-r",
"ts-node/register",
"-r",
"test/mocks/enableMocks.ts",
"${file}"
],
"internalConsoleOptions": "neverOpen"
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Debug All Test Files",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-r",
"ts-node/register",
"-r",
"test/mocks/enableMocks.ts",
"./test/**/*.spec.ts"
],
"internalConsoleOptions": "openOnSessionStart",
"cwd": "${workspaceFolder}"
}
]
}
2 changes: 2 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"args": [
"-r",
"ts-node/register",
"-r",
"test/mocks/enableMocks.ts",
"${file}"
],
"options": {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "A Framework written in Typescript for World of Warcraft Addons",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha -r ts-node/register \"test/**/*.spec.ts\"",
"coverage": "./node_modules/.bin/nyc ./node_modules/.bin/mocha -r ts-node/register \"test/**/*.spec.ts\"",
"test": "./node_modules/.bin/mocha -r ts-node/register -r test/mocks/enableMocks.ts \"test/**/*.spec.ts\"",
"coverage": "./node_modules/.bin/nyc ./node_modules/.bin/mocha -r ts-node/register -r test/mocks/enableMocks.ts \"test/**/*.spec.ts\"",
"coverage:send:coveralls": "nyc report --reporter=text-lcov | coveralls",
"build": "node_modules/.bin/qhun-transpiler -p ./qhun-transpiler.json",
"build-dev": "node ../../qhun-transpiler/dist/index.js -p ./qhun-transpiler.json",
Expand Down Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.9",
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"dependency-cruiser": "^4.6.0",
Expand Down
6 changes: 0 additions & 6 deletions src/core/AddonOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Singleton } from "./decorators/Singleton";
import { Debugger } from "./debug/Debugger";
import { ModuleConfig } from "./data/ModuleConfig";

@Singleton()
Expand All @@ -24,11 +23,6 @@ export class AddonOptions {
*/
public addonName: string;

/**
* the debugger instance if debigging is enabled
*/
public debuggerInstance: Debugger;

/**
* module specific configuration
*/
Expand Down
7 changes: 3 additions & 4 deletions src/core/async/Promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AddonOption } from "../decorators/AddonOption";
import { Logger } from "../debug/Logger";
import { Inject } from "../decorators/Inject";
import { Timer } from "./Timer";
Expand Down Expand Up @@ -28,7 +27,7 @@ enum PromiseState {
*/
export class Promise<T> implements PromiseLike<T> {

@AddonOption("debuggerInstance")
@Inject(Logger)
private logger: Logger;

@Inject(Timer)
Expand Down Expand Up @@ -72,7 +71,7 @@ export class Promise<T> implements PromiseLike<T> {
return this.doneInternal((_, value?: T) => {
if (typeof onFulfilled === "function") {
try {
return resolve(onFulfilled(value) as any);
return resolve(onFulfilled(_ !== undefined ? _ : value) as any);
} catch (e) {
return reject(e);
}
Expand All @@ -83,7 +82,7 @@ export class Promise<T> implements PromiseLike<T> {

if (typeof onRejected === "function") {
try {
return resolve(onRejected(reason) as any);
return resolve(onRejected(_ !== undefined ? _ : reason) as any);
} catch (e) {
return reject(e);
}
Expand Down
20 changes: 10 additions & 10 deletions src/core/async/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ export class Timer {
return uuid;
}

/**
* removes the timeout/interval request by its identifier
* @param identifier the identifier to remove
*/
public remove(identifier: string): void {

delete this.listeners[identifier];
this.checkStartStopTimer();
}

/**
* updates the timer based on the event frame update
* @param timePassed the time passed in milliseconds
Expand All @@ -161,16 +171,6 @@ export class Timer {
});
}

/**
* removes the timeout/interval request by its identifier
* @param identifier the identifier to remove
*/
private remove(identifier: string): void {

delete this.listeners[identifier];
this.checkStartStopTimer();
}

/**
* observes the listener stack and starts/stops the timer when
* the stack is full/empty
Expand Down
12 changes: 4 additions & 8 deletions src/core/debug/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Debugger } from "./Debugger";
import { AddonOption } from "../decorators/AddonOption";
import { Injectable } from "../decorators/Injectable";

/**
Expand All @@ -8,22 +7,19 @@ import { Injectable } from "../decorators/Injectable";
@Injectable()
export class Logger implements Debugger {

@AddonOption("debuggerInstance")
private debugger: Debugger;

public debug(...args: any[]): void {
this.debugger.debug(...args);
print("[DEBUG] ", ...args);
}

public info(...args: any[]): void {
this.debugger.info(...args);
print("[INFO] ", ...args);
}

public warning(...args: any[]): void {
this.debugger.warning(...args);
print("[WARNING] ", ...args);
}

public error(...args: any[]): void {
this.debugger.error(...args);
print("[ERROR] ", ...args);
}
}
5 changes: 0 additions & 5 deletions src/core/decorators/QhunAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export function QhunAddon(
addonOptionsInstance.addonName = __FILE_META[1];
addonOptionsInstance.moduleConfig = options.modules || {};

// bootstrap the debugger
if (options.debugger && options.debugger.instance) {
addonOptionsInstance.debuggerInstance = bootstrapDebugger(options.debugger.instance, [options.debugger.data]);
}

// go on with dependency injection
const injector = Injector.getInstance();
return <ClassDecorator>(<Target extends Function>(target: ClassConstructor<Target>) => {
Expand Down
35 changes: 32 additions & 3 deletions src/core/di/Injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class Injector {
*/
private instanceStorage: {
ctor: Function,
instance: Object
instance: Object,
manual: boolean
}[] = [];

constructor() {
Expand All @@ -43,7 +44,34 @@ export class Injector {
// create another instance
this.instanceStorage.push({
ctor: Injector,
instance: this
instance: this,
manual: false
});
}

/**
* adds the given instance to the internal stack
* @param ctor the constructor of the instance
* @param instance the instance itself
*/
public addInstance<T extends Object>(ctor: ClassConstructor<T>, instance: Partial<T>): void {

if (!this.findExistingInstance(ctor)) {
this.instanceStorage.push({
ctor: ctor,
instance: instance as Required<T>,
manual: true
});
}
}

/**
* removes all manually added instances from the internal stack
*/
public clearManual(): void {

this.instanceStorage = this.instanceStorage.filter(dependency => {
return dependency.manual === false;
});
}

Expand Down Expand Up @@ -94,7 +122,8 @@ export class Injector {
const instance = new (ctor as ClassConstructor)(...resolvedDependencies);
this.instanceStorage.push({
ctor: ctor,
instance: instance
instance: instance,
manual: false
});
return instance as T;
}
Expand Down
89 changes: 86 additions & 3 deletions test/core/async/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,94 @@ import { expect } from "chai";

@suite class PromiseSpec {


@test "Promise can be constructed"() {

/*const p = new Promise((resolve, reject) => {
const p = new Promise((resolve, reject) => {
resolve(true);
});*/
});

expect(p).to.be.an.instanceof(Promise);
}

@test(timeout(1000)) "Promise can resolve"(done: Function) {

new Promise((resolve, reject) => {

resolve(1);
}).done(() => {

done();
});
}

@test(timeout(1000)) "Promise can reject"(done: Function) {

new Promise((resolve, reject) => {
reject(1);
}).done(() => {
console.log("HERE");
}, () => {
done();
});
}

@test(timeout(1000)) "Promise is asynchronous"(done: Function) {

new Promise(resolve => {
setTimeout(resolve, 10);
}).done(() => done());
}

@test(timeout(1000)) "Promises can be chained"(done: Function) {

new Promise(resolve => {

resolve(1);
}).done(val => {

return new Promise<string>(resolve => {
resolve(val.toString());
});
}).done(stringVal => {

expect(stringVal).to.equal("1");
done();
});
}

@test(timeout(1000)) "Promises should reject when an exception has been thrown (1)"(done: Function) {

new Promise(() => {
throw "error";
}).done(() => { }, error => {
expect(error).to.equal("error");
done();
});
}

@test(timeout(1000)) "Promises should reject when an exception has been thrown (2)"(done: Function) {

new Promise(() => {
throw "error text";
}).done(() => { }, error => {
return new Promise(resolve => {
resolve(error);
});
}).done(message => {
expect(message).to.equal("error text");
done();
});
}

@test(timeout(1000)) "Promises should reject when an exception has been thrown (3)"(done: Function) {

new Promise((resolve, reject) => {
resolve("text");
}).done(val => {
throw "error " + val;
}).done(() => { }, err => {
expect(err).to.equal("error text");
done();
});
}
}

0 comments on commit 0a57847

Please sign in to comment.