@@ -95,3 +95,196 @@ describe('The constant-ness logic', () => {
95
95
}
96
96
} ) ;
97
97
} ) ;
98
+
99
+ describe ( 'test AbsoluteMatcher with file path' , ( ) => {
100
+ it ( 'matched path' , ( ) => {
101
+
102
+ const config = {
103
+ errorMessage : 'banned name with file path' ,
104
+ kind : PatternKind . BANNED_NAME ,
105
+ values : [ './file_0|Foo.bar' ]
106
+ } ;
107
+ const sources = [
108
+ `export class Foo { static bar(s: string) {return s + "abc";} }` ,
109
+ `import {Foo} from './file_0';
110
+ var a = Foo.bar("123");`
111
+ ] ;
112
+ const results =
113
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
114
+
115
+ expect ( results ) . toHaveFailuresMatching (
116
+ { matchedCode : `bar` , messageText : 'banned name with file path' } ) ;
117
+ } ) ;
118
+
119
+ it ( 'unmatched path' , ( ) => {
120
+ const config = {
121
+ errorMessage : 'banned name with file path' ,
122
+ kind : PatternKind . BANNED_NAME ,
123
+ values : [ './file_0|Foo.bar' ]
124
+ } ;
125
+ const sources = [
126
+ `export class Foo { static bar(s: string) {return s + "abc";} }` ,
127
+ `export class Foo { static bar(s: string) {return s + "abc";} }` ,
128
+ `import {Foo} from './file_1';
129
+ var a = Foo.bar("123");`
130
+ ] ;
131
+
132
+ const results =
133
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
134
+ expect ( results ) . toHaveNoFailures ( ) ;
135
+ } ) ;
136
+
137
+ it ( 'local exported definition' , ( ) => {
138
+ // This is a match because Foo.bar is an exported symbol.
139
+ const config = {
140
+ errorMessage : 'banned name with file path' ,
141
+ kind : PatternKind . BANNED_NAME ,
142
+ values : [ './file_0|Foo.bar' ]
143
+ } ;
144
+ const sources =
145
+ [ `export class Foo { static bar(s: string) {return s + "abc";} }
146
+ var a = Foo.bar("123");` ] ;
147
+
148
+ const results =
149
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
150
+ expect ( results ) . toHaveFailuresMatching (
151
+ { matchedCode : `bar` , messageText : 'banned name with file path' } ) ;
152
+ } ) ;
153
+
154
+ it ( 'local non-exported definition' , ( ) => {
155
+ // This is not a match because Foo.bar is a non-exported locally defined
156
+ // symbol.
157
+ const config = {
158
+ errorMessage : 'banned name with file path' ,
159
+ kind : PatternKind . BANNED_NAME ,
160
+ values : [ './file_0|Foo.bar' ]
161
+ } ;
162
+ const sources = [ `class Foo { static bar(s: string) {return s + "abc";} }
163
+ var a = Foo.bar("123");` ] ;
164
+ const results =
165
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
166
+ expect ( results ) . toHaveNoFailures ( ) ;
167
+ } ) ;
168
+
169
+ it ( 'property test 1' , ( ) => {
170
+ const config = {
171
+ errorMessage : 'banned name with file path' ,
172
+ kind : PatternKind . BANNED_NAME ,
173
+ values : [ './file_0|Foo.s' ]
174
+ } ;
175
+ const sources = [
176
+ `export class Foo { static s : string; }` ,
177
+ `import {Foo} from './file_0';
178
+ var a = Foo.s;` ,
179
+ ] ;
180
+
181
+ const results =
182
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
183
+ expect ( results ) . toHaveFailuresMatching (
184
+ { matchedCode : `s` , messageText : 'banned name with file path' } ) ;
185
+ } ) ;
186
+
187
+ it ( 'property test 2' , ( ) => {
188
+ // This is a match because Moo inherits s from Foo.
189
+ const config = {
190
+ errorMessage : 'banned name with file path' ,
191
+ kind : PatternKind . BANNED_NAME ,
192
+ values : [ './file_0|Foo.s' ]
193
+ } ;
194
+ const sources = [
195
+ `export class Foo { static s : string; }` ,
196
+ `import {Foo} from './file_0';
197
+ export class Moo extends Foo { static t : string; }` ,
198
+ `import {Moo} from './file_1';
199
+ var a = Moo.s;` ,
200
+ ] ;
201
+
202
+ const results =
203
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
204
+ expect ( results ) . toHaveFailuresMatching (
205
+ { matchedCode : `s` , messageText : 'banned name with file path' } ) ;
206
+ } ) ;
207
+
208
+ it ( 'property test 3' , ( ) => {
209
+ // This is not a match because Moo redefines s.
210
+ const config = {
211
+ errorMessage : 'banned name with file path' ,
212
+ kind : PatternKind . BANNED_NAME ,
213
+ values : [ './file_0|Foo.s' ]
214
+ } ;
215
+ const sources = [
216
+ `export class Foo { static s : string; }` ,
217
+ `import {Foo} from './file_0';
218
+ export class Moo extends Foo { static s : string; }` ,
219
+ `import {Moo} from './file_1';
220
+ var a = Moo.s;` ,
221
+ ] ;
222
+
223
+ const results =
224
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
225
+ expect ( results ) . toHaveNoFailures ( ) ;
226
+ } ) ;
227
+
228
+ it ( 'inheritance test 1' , ( ) => {
229
+ // This is a match because Moo inherits bar from Foo.
230
+ const config = {
231
+ errorMessage : 'banned name with file path' ,
232
+ kind : PatternKind . BANNED_NAME ,
233
+ values : [ './file_0|Foo.bar' ]
234
+ } ;
235
+ const sources = [
236
+ `export class Foo { static bar(s: string) {return s + "abc";} }` ,
237
+ `import {Foo} from './file_0';
238
+ export class Moo extends Foo { static far(s: string) {return s + "def";} }` ,
239
+ `import {Moo} from './file_1';
240
+ Moo.bar("abc");`
241
+ ] ;
242
+
243
+ const results =
244
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
245
+ expect ( results ) . toHaveFailuresMatching (
246
+ { matchedCode : `bar` , messageText : 'banned name with file path' } ) ;
247
+ } ) ;
248
+
249
+ it ( 'inheritance test 2' , ( ) => {
250
+ // This is not a match because Moo redefines bar.
251
+ const config = {
252
+ errorMessage : 'banned name with file path' ,
253
+ kind : PatternKind . BANNED_NAME ,
254
+ values : [ './file_0|Foo.bar' ]
255
+ } ;
256
+ const sources = [
257
+ `export class Foo { static bar(s: string) {return s + "abc";} }
258
+ export class Moo extends Foo { static bar(s: string) {return s + "def";} }` ,
259
+ `import {Foo, Moo} from './file_0';
260
+ Moo.bar("abc");`
261
+ ] ;
262
+
263
+ const results =
264
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
265
+ expect ( results ) . toHaveNoFailures ( ) ;
266
+ } ) ;
267
+
268
+ it ( 'interface' , ( ) => {
269
+ // This is not a match because even though bar specified is interface Moo,
270
+ // its actual definition is in class Boo.
271
+ const config = {
272
+ errorMessage : 'banned name with file path' ,
273
+ kind : PatternKind . BANNED_NAME ,
274
+ values : [ './file_1|Moo.bar' ]
275
+ } ;
276
+ const sources = [
277
+ `export class Foo { static bar(s: string) {return s + "abc";} }` ,
278
+ `import {Foo} from './file_0';
279
+ export interface Moo extends Foo { }` ,
280
+ `import {Moo} from './file_1';
281
+ export class Boo implements Moo { static bar(s: string) {return s + "def";} }` ,
282
+ `import {Boo} from './file_2';
283
+ Boo.bar("abc");` ,
284
+ ] ;
285
+
286
+ const results =
287
+ compileAndCheck ( new ConformancePatternRule ( config ) , ...sources ) ;
288
+ expect ( results ) . toHaveNoFailures ( ) ;
289
+ } ) ;
290
+ } ) ;
0 commit comments