From 9dedb624944d16c09e1648086abd65ca64288f0b Mon Sep 17 00:00:00 2001 From: Zach Arend Date: Thu, 10 Dec 2020 11:30:50 -0800 Subject: [PATCH] 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 --- .../errors/GOLDEN_PARTIAL.js | 1 - .../errors/TEST_CASES.json | 62 ----------------- .../errors/nested_i18n_msg.ts | 16 ----- .../nested_i18n_msg_with_ng-containers.ts | 18 ----- .../errors/nested_i18n_msg_with_tags.ts | 18 ----- .../r3_view_compiler_i18n_spec.ts | 55 --------------- .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 68 +++++++++++++++++++ 7 files changed, 68 insertions(+), 170 deletions(-) delete mode 100644 packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/GOLDEN_PARTIAL.js delete mode 100644 packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/TEST_CASES.json delete mode 100644 packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg.ts delete mode 100644 packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_ng-containers.ts delete mode 100644 packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_tags.ts diff --git a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/GOLDEN_PARTIAL.js b/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/GOLDEN_PARTIAL.js deleted file mode 100644 index 8b13789179..0000000000 --- a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/GOLDEN_PARTIAL.js +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/TEST_CASES.json b/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/TEST_CASES.json deleted file mode 100644 index ef9628442d..0000000000 --- a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/TEST_CASES.json +++ /dev/null @@ -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 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\\)" - } - ] - } - ] - } - ] -} diff --git a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg.ts b/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg.ts deleted file mode 100644 index 533f1f9060..0000000000 --- a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {Component, NgModule} from '@angular/core'; - -@Component({ - selector: 'my-component', - template: ` -
-
Some content
-
-`, -}) -export class MyComponent { -} - -@NgModule({declarations: [MyComponent]}) -export class MyModule { -} \ No newline at end of file diff --git a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_ng-containers.ts b/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_ng-containers.ts deleted file mode 100644 index e5690686f2..0000000000 --- a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_ng-containers.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {Component, NgModule} from '@angular/core'; - -@Component({ - selector: 'my-component', - template: ` - -
- Some content -
-
-`, -}) -export class MyComponent { -} - -@NgModule({declarations: [MyComponent]}) -export class MyModule { -} \ No newline at end of file diff --git a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_tags.ts b/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_tags.ts deleted file mode 100644 index e8942340c2..0000000000 --- a/packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_i18n/errors/nested_i18n_msg_with_tags.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {Component, NgModule} from '@angular/core'; - -@Component({ - selector: 'my-component', - template: ` -
-
-
Some content
-
-
-`, -}) -export class MyComponent { -} - -@NgModule({declarations: [MyComponent]}) -export class MyModule { -} \ No newline at end of file diff --git a/packages/compiler-cli/test/compliance_old/r3_view_compiler_i18n_spec.ts b/packages/compiler-cli/test/compliance_old/r3_view_compiler_i18n_spec.ts index e87b65c0a5..f0cf9ac3a5 100644 --- a/packages/compiler-cli/test/compliance_old/r3_view_compiler_i18n_spec.ts +++ b/packages/compiler-cli/test/compliance_old/r3_view_compiler_i18n_spec.ts @@ -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(` -
-
Some content
-
- `); - try { - compile(files, angularFiles); - } catch (error) { - verifyNestedSectionsError(error, '[ERROR ->]
Some content
'); - } - }); - - it('should throw on nested i18n sections with tags in between', () => { - const files = getAppFilesWithTemplate(` -
-
-
Some content
-
-
- `); - try { - compile(files, angularFiles); - } catch (error) { - verifyNestedSectionsError(error, '[ERROR ->]
Some content
'); - } - }); - - it('should throw on nested i18n sections represented with s', () => { - const files = getAppFilesWithTemplate(` - -
- Some content -
-
- `); - try { - compile(files, angularFiles); - } catch (error) { - verifyNestedSectionsError( - error, '[ERROR ->]Some content'); - } - }); - }); - describe('namespaces', () => { it('should handle namespaces inside i18n blocks', () => { const input = ` diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index b613f5ee9b..b44016d070 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -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: '
Content
' + }) + 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('
Content
'); + }); + + 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: '
Content
' + }) + 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('
Content
'); + }); + + it('report a diagnostic on nested i18n sections represented with s', () => { + env.write('test.ts', ` + import {Component} from '@angular/core'; + @Component({ + selector: 'test-component', + template: '
Content
' + }) + 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('Content'); + }); + }); }); });