feat(compiler): support `// ...` and `// TODO` in mock compiler expectations (#23441)

PR Close #23441
This commit is contained in:
Victor Berchet 2018-04-16 00:15:12 -06:00
parent 1d1e75ee2b
commit c6b206ee4b
2 changed files with 39 additions and 3 deletions

View File

@ -69,6 +69,10 @@ function tokenize(text: string): Piece[] {
export function expectEmit( export function expectEmit(
source: string, expected: string, description: string, source: string, expected: string, description: string,
assertIdentifiers?: {[name: string]: RegExp}) { assertIdentifiers?: {[name: string]: RegExp}) {
// turns `// ...` into `…`
// remove `// TODO` comment lines
expected = expected.replace(/\/\/\s*\.\.\./g, ELLIPSIS).replace(/\/\/\s*TODO.*?\n/g, '');
const pieces = tokenize(expected); const pieces = tokenize(expected);
const {regexp, groups} = buildMatcher(pieces); const {regexp, groups} = buildMatcher(pieces);
const matches = source.match(regexp); const matches = source.match(regexp);

View File

@ -87,7 +87,7 @@ describe('mock_compiler', () => {
}); });
}); });
it('should be able to skip untested regions', () => { it('should be able to skip untested regions (… and // ...)', () => {
const files = { const files = {
app: { app: {
'hello.component.ts': ` 'hello.component.ts': `
@ -113,10 +113,43 @@ describe('mock_compiler', () => {
// The special character … means anything can be generated between the two sections allowing // The special character … means anything can be generated between the two sections allowing
// skipping sections of the output that are not under test. The ellipsis unicode char (…) is // skipping sections of the output that are not under test. The ellipsis unicode char (…) is
// used instead of '...' because '...' is legal JavaScript (the spread operator) and might // used instead of '...' because '...' is legal JavaScript (the spread operator) and might
// need to be tested. // need to be tested. `// ...` could also be used in place of `…`.
expectEmit(result.source, 'ctx.name … ctx.name.length', 'could not find correct length access'); expectEmit(result.source, 'ctx.name … ctx.name.length', 'could not find correct length access');
expectEmit(
result.source, 'ctx.name // ... ctx.name.length', 'could not find correct length access');
}); });
it('should be able to skip TODO comments (// TODO)', () => {
const files = {
app: {
'hello.component.ts': `
import {Component, Input} from '@angular/core';
@Component({template: 'Hello!'})
export class HelloComponent { }
`,
'hello.module.ts': `
import {NgModule} from '@angular/core';
import {HelloComponent} from './hello.component';
@NgModule({declarations: [HelloComponent]})
export class HelloModule {}
`
}
};
const result = compile(files, angularFiles);
expectEmit(
result.source, `
// TODO: this comment should not be taken into account
$r3$.ɵT(0, 'Hello!');
// TODO: this comment should not be taken into account
`,
'todo comments should be ignored');
});
it('should be able to enforce consistent identifiers', () => { it('should be able to enforce consistent identifiers', () => {
const files = { const files = {
app: { app: {
@ -184,5 +217,4 @@ describe('mock_compiler', () => {
result.source, '$ctx$.$n$ … $ctx$.$n$.length', 'Match names', {'$n$': /(not)_(\1)/}); result.source, '$ctx$.$n$ … $ctx$.$n$.length', 'Match names', {'$n$': /(not)_(\1)/});
}).toThrowError(/"\$n\$" is "name" which doesn't match \/\(not\)_\(\\1\)\//); }).toThrowError(/"\$n\$" is "name" which doesn't match \/\(not\)_\(\\1\)\//);
}); });
}); });