Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement mangle friendly MemberPrecondition
- Loading branch information
Showing
8 changed files
with
157 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
export interface MethodSelector { | ||
classNamePattern?: RegExp; | ||
methodNamePattern?: RegExp; | ||
classes?: any[]; | ||
methods?: any[]; | ||
classes?: Function[]; | ||
methods?: Function[]; | ||
} | ||
|
||
export interface MemberSelector { | ||
classNamePattern: RegExp; | ||
fieldNamePattern: RegExp; | ||
classNamePattern?: RegExp; | ||
fieldNamePattern?: RegExp; | ||
classes?: Function[]; | ||
fields?: PropertyDescriptor[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { MemberSelector } from './../../lib/src/joint_points/selectors'; | ||
import { MemberPrecondition } from './../../lib/src/joint_points/preconditions'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
describe('Preconditions', () => { | ||
describe('MemberPrecondition', () => { | ||
it('should match by regex', () => { | ||
class Foo { | ||
get bar(): any { | ||
return null; | ||
} | ||
get baz(): any { | ||
return null; | ||
} | ||
set foobar(v: any) { | ||
// empty | ||
} | ||
} | ||
|
||
const selector: MemberSelector = { | ||
classNamePattern: /Foo/, | ||
fieldNamePattern: /bar/ | ||
}; | ||
const p = new MemberPrecondition(selector); | ||
expect(p.assert({ classDefinition: Foo, fieldName: 'bar' })).equal(true); | ||
expect(p.assert({ classDefinition: Foo, fieldName: 'baz' })).equal(false); | ||
}); | ||
|
||
it('should match by value', () => { | ||
class Foo { | ||
get bar(): any { | ||
return undefined; | ||
} | ||
get baz(): any { | ||
return null; | ||
} | ||
set foobar(v: any) { | ||
// empty | ||
} | ||
} | ||
|
||
const selector: MemberSelector = { | ||
classes: [Foo], | ||
fields: [Object.getOwnPropertyDescriptor(Foo.prototype, 'baz')] | ||
}; | ||
const p = new MemberPrecondition(selector); | ||
// expect(p.assert({ classDefinition: Foo, fieldName: 'baz' })).equal(true); | ||
expect(p.assert({ classDefinition: Foo, fieldName: 'bar' })).equal(false); | ||
}); | ||
|
||
it('should throw with invalid selector', () => { | ||
class Foo { | ||
get bar(): any { | ||
return null; | ||
} | ||
get baz(): any { | ||
return null; | ||
} | ||
set foobar(v: any) { | ||
// empty | ||
} | ||
} | ||
|
||
const selector: MemberSelector = { | ||
classes: [Foo], | ||
fields: [Foo.prototype.foobar] | ||
}; | ||
const p = new MemberPrecondition(selector); | ||
expect(() => { | ||
p.assert({ classDefinition: Foo, fieldName: 'baz' }); | ||
}).to.throw(); | ||
}); | ||
|
||
it('should match by value & regex', () => { | ||
class Foo { | ||
get bar(): any { | ||
return null; | ||
} | ||
get baz(): any { | ||
return null; | ||
} | ||
set foobar(v: any) { | ||
// empty | ||
} | ||
} | ||
|
||
const p1 = new MemberPrecondition({ | ||
classNamePattern: /Foo/, | ||
fields: [Object.getOwnPropertyDescriptor(Foo.prototype, 'baz')] | ||
}); | ||
expect(p1.assert({ classDefinition: Foo, fieldName: 'bar' })).equal(false); | ||
expect(p1.assert({ classDefinition: Foo, fieldName: 'baz' })).equal(true); | ||
|
||
const p2 = new MemberPrecondition({ | ||
classes: [Foo], | ||
fieldNamePattern: /bar/ | ||
}); | ||
expect(p2.assert({ classDefinition: Foo, fieldName: 'bar' })).equal(true); | ||
expect(p2.assert({ classDefinition: Foo, fieldName: 'foobar' })).equal(true); | ||
expect(p2.assert({ classDefinition: Foo, fieldName: 'baz' })).equal(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters