Skip to content

Commit

Permalink
fix: build & refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Nov 18, 2018
1 parent 1c03a12 commit b4f437a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 98 deletions.
6 changes: 3 additions & 3 deletions lib/src/advices/sync_advices.ts
@@ -1,5 +1,5 @@
import { Advice } from "../core/advice";
import { Metadata } from "../core/metadata";
import { Advice } from '../core/advice';
import { Metadata } from '../core/metadata';

export class BeforeAdvice extends Advice {
wove(target: Function, metadata: Metadata) {
Expand Down Expand Up @@ -27,7 +27,7 @@ export class AroundAdvice extends Advice {
this.invoke(target, metadata);
this.advice.bind(this.context, metadata).apply(null, metadata.method.args);
} else {
metadata.method.result = result;
metadata.method.result = metadata.method.result || result;
}

return metadata.method.result;
Expand Down
29 changes: 29 additions & 0 deletions test/advices/around_advice_using_complete.spec.ts
@@ -0,0 +1,29 @@
import { Wove, resetRegistry, Metadata } from '../../lib/src/core';

import { expect } from 'chai';
import { aroundMethod } from '../../lib';

describe('around advices', () => {
it('should only get invoked once when using MethodMetadata.complete()', () => {
let count = 0;

class Aspect {
@aroundMethod({ classNamePattern: /^Test/, methodNamePattern: /^get$/ })
aroundMethod(meta: Metadata) {
return meta.method.complete(...meta.method.args) + ' appended from the aspect';
}
}

@Wove()
class Test {
get(it: string): string {
count++;
return it;
}
}

const test = new Test();
expect(test.get('test')).to.equal('test appended from the aspect');
expect(count).to.equal(1);
});
});
33 changes: 0 additions & 33 deletions test/advices/around_advice_using_complete.ts

This file was deleted.

18 changes: 9 additions & 9 deletions test/advices/async_advices.spec.ts
@@ -1,10 +1,7 @@
import {Metadata, MethodMetadata, Wove, resetRegistry} from '../../lib/src/core';
import {Advice} from '../../lib/src/core/advice';
import * as SyncAdvices from '../../lib/src/advices';
import { expect } from 'chai';

import {afterMethod, asyncOnThrowOfMethod, onThrowOfMethod, aroundMethod} from '../../lib/index';

import {expect} from 'chai';
import { Metadata, Wove } from '../../lib/src/core';
import { afterMethod, asyncOnThrowOfMethod, onThrowOfMethod, aroundMethod } from '../../lib/index';

@Wove()
class Target {
Expand All @@ -14,9 +11,11 @@ class Target {
}

async throwError() {
await new Promise((_, reject) => setTimeout(_ => {
reject(42);
}, 10))
await new Promise((_, reject) =>
setTimeout(_ => {
reject(42);
}, 10)
);
}

async directThrow() {
Expand Down Expand Up @@ -94,6 +93,7 @@ describe('async advices', () => {
it('should work with async targets', async () => {
let hasBeenCalled = false;
let calledTimes = 0;

class Advice {
@aroundMethod({ classNamePattern: /Target/, methodNamePattern: /foo/ })
async around(data: Metadata) {
Expand Down
19 changes: 8 additions & 11 deletions test/advices/external_advice.spec.ts
@@ -1,27 +1,24 @@
import {
Wove,
resetRegistry
} from '../../lib/src/core';
import { Wove, resetRegistry } from '../../lib/src/core';

import { expect } from 'chai';

import './external_aspect'
import './external_aspect';

describe('sync advices', () => {
afterEach(() => {
resetRegistry();
});
afterEach(resetRegistry);

describe('External AroundAdvice', () => {
it('should invoke the external advice with the appropriate metadata', () => {
let demo: any;

@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo' }
class ExternalAspectDemo {
get(_: any, __: any): string {
return 'ExternalAspectDemo';
}
}

demo = new Demo();
demo = new ExternalAspectDemo();
expect(demo.get(42, 1.618)).to.equal('ExternalAspect');
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/advices/external_aspect.ts
@@ -1,16 +1,16 @@
import { aroundMethod, Metadata } from "../../lib";
import { expect } from "chai";
import { aroundMethod, Metadata } from '../../lib';
import { expect } from 'chai';

export default class ExternalAspect {
@aroundMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
@aroundMethod({ classNamePattern: /^ExternalAspectDemo$/, methodNamePattern: /.*/ })
around(metadata: Metadata) {
expect(this).to.deep.equal(ExternalAspect.prototype);
expect(metadata.className).to.equal('Demo');
expect(metadata.className).to.equal('ExternalAspectDemo');
expect(metadata.method.name).to.equal('get');
expect(metadata.method.args).to.deep.equal([42, 1.618]);
expect(metadata.method.invoke).to.be.a('function');

metadata.method.proceed = false
metadata.method.result = 'ExternalAspect'
metadata.method.proceed = false;
metadata.method.result = 'ExternalAspect';
}
}
47 changes: 16 additions & 31 deletions test/advices/sync_advices.spec.ts
@@ -1,36 +1,21 @@
import {
Metadata,
MethodMetadata,
Wove,
resetRegistry
} from '../../lib/src/core';
import { Advice } from '../../lib/src/core/advice';
import * as SyncAdvices from '../../lib/src/advices';
import { Metadata, Wove, resetRegistry } from '../../lib/src/core';

import {
beforeMethod,
beforeStaticMethod,
beforeGetter,
beforeSetter,
afterMethod, aroundMethod
} from "../../lib/index";
afterMethod,
aroundMethod
} from '../../lib/index';

import { expect } from 'chai';

import './external_aspect'

describe('sync advices', () => {
beforeEach(() => {
resetRegistry();
});
afterEach(() => {
resetRegistry();
});
describe('BeforeAdvice', () => {
// beforeEach(() => {
// resetRegistry();
// });
beforeEach(resetRegistry);
afterEach(resetRegistry);

describe('BeforeAdvice', () => {
it('should invoke the advice with the appropriate metadata', done => {
let demo: any;
class Aspect {
Expand Down Expand Up @@ -82,9 +67,7 @@ describe('sync advices', () => {
@beforeMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
before(metadata: Metadata) {
metadata.method.proceed = false;
metadata.method.result = metadata.method.invoke(
...metadata.method.args
);
metadata.method.result = metadata.method.invoke(...metadata.method.args);
expect(metadata.method.result).to.be.equal(6);
}
}
Expand All @@ -109,9 +92,7 @@ describe('sync advices', () => {
@beforeStaticMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
before(metadata: Metadata) {
metadata.method.proceed = false;
metadata.method.result = metadata.method.invoke(
...metadata.method.args
);
metadata.method.result = metadata.method.invoke(...metadata.method.args);
expect(metadata.method.result).to.be.equal(3);
}
}
Expand Down Expand Up @@ -298,7 +279,9 @@ describe('sync advices', () => {
}
@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo'; }
get(foo: any, bar: any): string {
return 'Demo';
}
}

demo = new Demo();
Expand All @@ -310,11 +293,13 @@ describe('sync advices', () => {

@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo' }
get(foo: any, bar: any): string {
return 'Demo';
}
}

demo = new Demo();
expect(demo.get(42, 1.618)).to.equal('ExternalAspect');
expect(demo.get(42, 1.618)).to.equal('Demo');
});
});

Expand Down
16 changes: 11 additions & 5 deletions test/core/wove.spec.ts
Expand Up @@ -11,7 +11,9 @@ const o = 42;
class ClassA {
foo() {}

overridden() { return o; }
overridden() {
return o;
}
}

@Wove()
Expand All @@ -27,7 +29,9 @@ class ClassB extends ClassA {

qux() {}

overridden() { return super.overridden() + 1; }
overridden() {
return super.overridden() + 1;
}
}

const methods: string[] = [];
Expand All @@ -43,14 +47,16 @@ class LoggerAspect {
}

describe('@Wove', () => {
beforeEach(() => (methods.length = 0));
beforeEach(() => {
methods.length = 0;
});

it('should work with subclasses', () => {
const fooSpy = spy(ClassA.prototype, 'foo');
const barSpy = spy(ClassB.prototype, 'bar');
const quxSpy = spy(ClassB.prototype, 'qux');
const overriddenSpyA = spy(ClassA.prototype, 'overridden')
const overriddenSpyB = spy(ClassB.prototype, 'overridden')
const overriddenSpyA = spy(ClassA.prototype, 'overridden');
const overriddenSpyB = spy(ClassB.prototype, 'overridden');

const b = new ClassB();

Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Expand Up @@ -13,6 +13,8 @@
"lib/index.ts",
"test/advices/sync_advices.spec.ts",
"test/advices/async_advices.spec.ts",
"test/advices/external_advice.spec.ts",
"test/advices/around_advice_using_complete.spec.ts",
"test/core/pointcut.spec.ts",
"test/core/preconditions.spec.ts",
"test/core/wove.spec.ts",
Expand Down

0 comments on commit b4f437a

Please sign in to comment.