test(compiler-cli): fix i18n error tests (#40026)

Refactors the i18n error tests to be unit tests in ngtsc_spec.ts. There
is two reasons for doing this.

First is that the tests in compliace_old expected an expection to be be
thrown but did not fail the test if no exception was thrown. That means
that this test could miss catching a bug. It is also a big hacky to call
compile directly and expect an exception to be thrown for diagnostics.

Also, this can easily be unit tested and an end-to-end test is not
necessary since we are not making use of the goldfiles for these tests.

It is easier to maintain and less hacky to validate that we get helpful
error messages when nesting i18n sections by calling getDiagnostics
directly.

PR Close #40026
This commit is contained in:
Zach Arend 2020-12-10 11:30:50 -08:00 committed by Alex Rickabaugh
parent 973bb403a5
commit 9dedb62494
7 changed files with 68 additions and 170 deletions

View File

@ -1,62 +0,0 @@
{
"$schema": "../../test_case_schema.json",
"cases": [
{
"description": "should throw on nested i18n sections",
"inputFiles": [
"nested_i18n_msg.ts"
],
"compilationModeFilter": [
"full compile"
],
"expectations": [
{
"expectedErrors": [
{
"message": "Cannot mark an element as translatable inside of a translatable section\\. Please remove the nested i18n marker\\.",
"location": "nested_i18n_msg\\.ts \\(7,5\\)"
}
]
}
]
},
{
"description": "should throw on nested i18n sections with tags in between",
"inputFiles": [
"nested_i18n_msg_with_tags.ts"
],
"compilationModeFilter": [
"full compile"
],
"expectations": [
{
"expectedErrors": [
{
"message": "Cannot mark an element as translatable inside of a translatable section\\. Please remove the nested i18n marker\\.",
"location": "nested_i18n_msg_with_tags\\.ts \\(8,7\\)"
}
]
}
]
},
{
"description": "should throw on nested i18n sections represented with <ng-container>s",
"inputFiles": [
"nested_i18n_msg_with_ng-containers.ts"
],
"compilationModeFilter": [
"full compile"
],
"expectations": [
{
"expectedErrors": [
{
"message": "Cannot mark an element as translatable inside of a translatable section\\. Please remove the nested i18n marker\\.",
"location": "nested_i18n_msg_with_ng-containers\\.ts \\(8,7\\)"
}
]
}
]
}
]
}

View File

@ -1,16 +0,0 @@
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'my-component',
template: `
<div i18n>
<div i18n>Some content</div>
</div>
`,
})
export class MyComponent {
}
@NgModule({declarations: [MyComponent]})
export class MyModule {
}

View File

@ -1,18 +0,0 @@
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'my-component',
template: `
<ng-container i18n>
<div>
<ng-container i18n>Some content</ng-container>
</div>
</ng-container>
`,
})
export class MyComponent {
}
@NgModule({declarations: [MyComponent]})
export class MyModule {
}

View File

@ -1,18 +0,0 @@
import {Component, NgModule} from '@angular/core';
@Component({
selector: 'my-component',
template: `
<div i18n>
<div>
<div i18n>Some content</div>
</div>
</div>
`,
})
export class MyComponent {
}
@NgModule({declarations: [MyComponent]})
export class MyModule {
}

View File

@ -3325,61 +3325,6 @@ $` + String.raw`{$I18N_4$}:ICU:\`;
});
});
describe('errors', () => {
const verifyNestedSectionsError = (errorThrown: any, expectedErrorText: string) => {
expect(errorThrown.ngParseErrors.length).toBe(1);
const msg = errorThrown.ngParseErrors[0].toString();
expect(msg).toContain(
'Cannot mark an element as translatable inside of a translatable section. Please remove the nested i18n marker.');
expect(msg).toContain(expectedErrorText);
expect(msg).toMatch(/app\/spec\.ts\@\d+\:\d+/);
};
it('should throw on nested i18n sections', () => {
const files = getAppFilesWithTemplate(`
<div i18n>
<div i18n>Some content</div>
</div>
`);
try {
compile(files, angularFiles);
} catch (error) {
verifyNestedSectionsError(error, '[ERROR ->]<div i18n>Some content</div>');
}
});
it('should throw on nested i18n sections with tags in between', () => {
const files = getAppFilesWithTemplate(`
<div i18n>
<div>
<div i18n>Some content</div>
</div>
</div>
`);
try {
compile(files, angularFiles);
} catch (error) {
verifyNestedSectionsError(error, '[ERROR ->]<div i18n>Some content</div>');
}
});
it('should throw on nested i18n sections represented with <ng-container>s', () => {
const files = getAppFilesWithTemplate(`
<ng-container i18n>
<div>
<ng-container i18n>Some content</ng-container>
</div>
</ng-container>
`);
try {
compile(files, angularFiles);
} catch (error) {
verifyNestedSectionsError(
error, '[ERROR ->]<ng-container i18n>Some content</ng-container>');
}
});
});
describe('namespaces', () => {
it('should handle namespaces inside i18n blocks', () => {
const input = `

View File

@ -7601,6 +7601,74 @@ export const Foo = Foo__PRE_R3__;
expect(getDiagnosticSourceCode(diags[0])).toBe('\'');
});
});
describe('i18n errors', () => {
it('should report helpful error message on nested i18n sections', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-component',
template: '<div i18n><div i18n>Content</div></div>'
})
class TestComponent {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toEqual(1);
expect(diags[0].messageText)
.toEqual(
'Cannot mark an element as translatable inside of a translatable section.' +
' Please remove the nested i18n marker.');
expect(diags[0].file?.fileName).toEqual(absoluteFrom('/test.ts'));
expect(diags[0].file?.text.substr(diags[0].start!, diags[0].length))
.toEqual('<div i18n>Content</div>');
});
it('report a diagnostic on nested i18n sections with tags in between', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-component',
template: '<div i18n><div><div i18n>Content</div></div></div>'
})
class TestComponent {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toEqual(1);
expect(diags[0].messageText)
.toEqual(
'Cannot mark an element as translatable inside of a translatable section.' +
' Please remove the nested i18n marker.');
expect(diags[0].file?.fileName).toEqual(absoluteFrom('/test.ts'));
expect(diags[0].file?.text.substr(diags[0].start!, diags[0].length))
.toEqual('<div i18n>Content</div>');
});
it('report a diagnostic on nested i18n sections represented with <ng-continers>s', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test-component',
template: '<div i18n><div><ng-container i18n>Content</ng-container></div></div>'
})
class TestComponent {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toEqual(1);
expect(diags[0].messageText)
.toEqual(
'Cannot mark an element as translatable inside of a translatable section.' +
' Please remove the nested i18n marker.');
expect(diags[0].file?.fileName).toEqual(absoluteFrom('/test.ts'));
expect(diags[0].file?.text.substr(diags[0].start!, diags[0].length))
.toEqual('<ng-container i18n>Content</ng-container>');
});
});
});
});