-
Notifications
You must be signed in to change notification settings - Fork 37
/
preconditions.spec.ts
104 lines (94 loc) · 2.86 KB
/
preconditions.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { MemberSelector } from './../../lib/src/join_points/selectors';
import { MemberPrecondition } from './../../lib/src/join_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);
});
});
});