From a91f0f6b82f340cfde5a01bc328d2723dc4248f0 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 18 Sep 2020 12:11:00 -0700 Subject: [PATCH] refactor(compiler): refactor ast spans test to be more human readable (#38902) The current tests print out the span numbers, which are really difficult to verify since it requires manually going to the template string and looking at what characters appear within those indexes. The better humanization would be to use the toString method of the spans, which prints the span text itself PR Close #38902 --- .../test/render3/r3_ast_spans_spec.ts | 147 ++++++++++-------- 1 file changed, 83 insertions(+), 64 deletions(-) diff --git a/packages/compiler/test/render3/r3_ast_spans_spec.ts b/packages/compiler/test/render3/r3_ast_spans_spec.ts index 791b36021fd5f..fac18efac94b1 100644 --- a/packages/compiler/test/render3/r3_ast_spans_spec.ts +++ b/packages/compiler/test/render3/r3_ast_spans_spec.ts @@ -95,7 +95,7 @@ function humanizeSpan(span: ParseSourceSpan|null|undefined): string { if (span === null || span === undefined) { return ``; } - return `${span.start.offset}:${span.end.offset}`; + return span.toString(); } function expectFromHtml(html: string) { @@ -113,21 +113,21 @@ describe('R3 AST source spans', () => { describe('nodes without binding', () => { it('is correct for text nodes', () => { expectFromHtml('a').toEqual([ - ['Text', '0:1'], + ['Text', 'a'], ]); }); it('is correct for elements with attributes', () => { expectFromHtml('
').toEqual([ - ['Element', '0:17', '0:11', '11:17'], - ['TextAttribute', '5:10', '8:9'], + ['Element', '
', '
', '
'], + ['TextAttribute', 'a="b"', 'b'], ]); }); it('is correct for elements with attributes without value', () => { expectFromHtml('
').toEqual([ - ['Element', '0:13', '0:7', '7:13'], - ['TextAttribute', '5:6', ''], + ['Element', '
', '
', '
'], + ['TextAttribute', 'a', ''], ]); }); }); @@ -135,7 +135,7 @@ describe('R3 AST source spans', () => { describe('bound text nodes', () => { it('is correct for bound text nodes', () => { expectFromHtml('{{a}}').toEqual([ - ['BoundText', '0:5'], + ['BoundText', '{{a}}'], ]); }); }); @@ -143,29 +143,29 @@ describe('R3 AST source spans', () => { describe('bound attributes', () => { it('is correct for bound properties', () => { expectFromHtml('
').toEqual([ - ['Element', '0:26', '0:20', '20:26'], - ['BoundAttribute', '5:19', '17:18'], + ['Element', '
', '
', '
'], + ['BoundAttribute', '[someProp]="v"', 'v'], ]); }); it('is correct for bound properties without value', () => { expectFromHtml('
').toEqual([ - ['Element', '0:22', '0:16', '16:22'], - ['BoundAttribute', '5:15', ''], + ['Element', '
', '
', '
'], + ['BoundAttribute', '[someProp]', ''], ]); }); it('is correct for bound properties via bind- ', () => { expectFromHtml('
').toEqual([ - ['Element', '0:25', '0:19', '19:25'], - ['BoundAttribute', '5:18', '16:17'], + ['Element', '
', '
', '
'], + ['BoundAttribute', 'bind-prop="v"', 'v'], ]); }); it('is correct for bound properties via {{...}}', () => { expectFromHtml('
').toEqual([ - ['Element', '0:24', '0:18', '18:24'], - ['BoundAttribute', '5:17', '11:16'], + ['Element', '
', '
', '
'], + ['BoundAttribute', 'prop="{{v}}"', '{{v}}'], ]); }); }); @@ -173,57 +173,68 @@ describe('R3 AST source spans', () => { describe('templates', () => { it('is correct for * directives', () => { expectFromHtml('
').toEqual([ - ['Template', '0:17', '0:11', '11:17'], - ['TextAttribute', '6:10', ''], // ngIf - ['Element', '0:17', '0:11', '11:17'], + ['Template', '
', '
', '
'], + ['TextAttribute', 'ngIf', ''], + ['Element', '
', '
', '
'], ]); }); it('is correct for ', () => { expectFromHtml('').toEqual([ - ['Template', '0:27', '0:13', '13:27'], + ['Template', '', '', ''], ]); }); it('is correct for reference via #...', () => { expectFromHtml('').toEqual([ - ['Template', '0:30', '0:16', '16:30'], - ['Reference', '13:15', ''], + ['Template', '', '', ''], + ['Reference', '#a', ''], ]); }); it('is correct for reference with name', () => { expectFromHtml('').toEqual([ - ['Template', '0:34', '0:20', '20:34'], - ['Reference', '13:19', '17:18'], + [ + 'Template', '', '', '' + ], + ['Reference', '#a="b"', 'b'], ]); }); it('is correct for reference via ref-...', () => { expectFromHtml('').toEqual([ - ['Template', '0:33', '0:19', '19:33'], - ['Reference', '13:18', ''], + ['Template', '', '', ''], + ['Reference', 'ref-a', ''], ]); }); it('is correct for variables via let-...', () => { expectFromHtml('').toEqual([ - ['Template', '0:37', '0:23', '23:37'], - ['Variable', '13:22', '20:21'], + [ + 'Template', '', '', + '' + ], + ['Variable', 'let-a="b"', 'b'], ]); }); it('is correct for attributes', () => { expectFromHtml('').toEqual([ - ['Template', '0:35', '0:21', '21:35'], - ['TextAttribute', '13:20', '17:19'], + [ + 'Template', '', '', + '' + ], + ['TextAttribute', 'k1="v1"', 'v1'], ]); }); it('is correct for bound attributes', () => { expectFromHtml('').toEqual([ - ['Template', '0:37', '0:23', '23:37'], - ['BoundAttribute', '13:22', '19:21'], + [ + 'Template', '', '', + '' + ], + ['BoundAttribute', '[k1]="v1"', 'v1'], ]); }); }); @@ -236,11 +247,17 @@ describe('R3 AST source spans', () => { //
//
expectFromHtml('
').toEqual([ - ['Template', '0:38', '0:32', '32:38'], - ['TextAttribute', '6:11', ''], // ngFor - ['BoundAttribute', '5:31', '25:30'], // *ngFor="let item of items" -> items - ['Variable', '13:22', ''], // let item - ['Element', '0:38', '0:32', '32:38'], + [ + 'Template', '
', '
', + '
' + ], + ['TextAttribute', 'ngFor', ''], + ['BoundAttribute', '*ngFor="let item of items"', 'items'], + ['Variable', 'let item ', ''], + [ + 'Element', '
', '
', + '
' + ], ]); // Note that this test exercises an *incorrect* usage of the ngFor @@ -250,28 +267,30 @@ describe('R3 AST source spans', () => { //
// expectFromHtml('
').toEqual([ - ['Template', '0:34', '0:28', '28:34'], - ['BoundAttribute', '5:27', '13:17'], // ngFor="item of items" -> item - ['BoundAttribute', '5:27', '21:26'], // ngFor="item of items" -> items - ['Element', '0:34', '0:28', '28:34'], + [ + 'Template', '
', '
', '
' + ], + ['BoundAttribute', '*ngFor="item of items"', 'item'], + ['BoundAttribute', '*ngFor="item of items"', 'items'], + ['Element', '
', '
', '
'], ]); }); it('is correct for variables via let ...', () => { expectFromHtml('
').toEqual([ - ['Template', '0:27', '0:21', '21:27'], - ['TextAttribute', '6:10', ''], // ngIf - ['Variable', '12:19', '18:19'], // let a=b -> b - ['Element', '0:27', '0:21', '21:27'], + ['Template', '
', '
', '
'], + ['TextAttribute', 'ngIf', ''], + ['Variable', 'let a=b', 'b'], + ['Element', '
', '
', '
'], ]); }); it('is correct for variables via as ...', () => { expectFromHtml('
').toEqual([ - ['Template', '0:33', '0:27', '27:33'], - ['BoundAttribute', '5:26', '12:16'], // ngIf="expr as local" -> expr - ['Variable', '6:25', '6:10'], // ngIf="expr as local -> ngIf - ['Element', '0:33', '0:27', '27:33'], + ['Template', '
', '
', '
'], + ['BoundAttribute', '*ngIf="expr as local"', 'expr'], + ['Variable', 'ngIf="expr as local', 'ngIf'], + ['Element', '
', '
', '
'], ]); }); }); @@ -279,31 +298,31 @@ describe('R3 AST source spans', () => { describe('events', () => { it('is correct for event names case sensitive', () => { expectFromHtml('
').toEqual([ - ['Element', '0:27', '0:21', '21:27'], - ['BoundEvent', '5:20', '18:19'], + ['Element', '
', '
', '
'], + ['BoundEvent', '(someEvent)="v"', 'v'], ]); }); it('is correct for bound events via on-', () => { expectFromHtml('
').toEqual([ - ['Element', '0:24', '0:18', '18:24'], - ['BoundEvent', '5:17', '15:16'], + ['Element', '
', '
', '
'], + ['BoundEvent', 'on-event="v"', 'v'], ]); }); it('is correct for bound events and properties via [(...)]', () => { expectFromHtml('
').toEqual([ - ['Element', '0:24', '0:18', '18:24'], - ['BoundAttribute', '5:17', '15:16'], - ['BoundEvent', '5:17', '15:16'], + ['Element', '
', '
', '
'], + ['BoundAttribute', '[(prop)]="v"', 'v'], + ['BoundEvent', '[(prop)]="v"', 'v'], ]); }); it('is correct for bound events and properties via bindon-', () => { expectFromHtml('
').toEqual([ - ['Element', '0:27', '0:21', '21:27'], - ['BoundAttribute', '5:20', '18:19'], - ['BoundEvent', '5:20', '18:19'], + ['Element', '
', '
', '
'], + ['BoundAttribute', 'bindon-prop="v"', 'v'], + ['BoundEvent', 'bindon-prop="v"', 'v'], ]); }); }); @@ -311,22 +330,22 @@ describe('R3 AST source spans', () => { describe('references', () => { it('is correct for references via #...', () => { expectFromHtml('
').toEqual([ - ['Element', '0:14', '0:8', '8:14'], - ['Reference', '5:7', ''], + ['Element', '
', '
', '
'], + ['Reference', '#a', ''], ]); }); it('is correct for references with name', () => { expectFromHtml('
').toEqual([ - ['Element', '0:18', '0:12', '12:18'], - ['Reference', '5:11', '9:10'], + ['Element', '
', '
', '
'], + ['Reference', '#a="b"', 'b'], ]); }); it('is correct for references via ref-', () => { expectFromHtml('
').toEqual([ - ['Element', '0:17', '0:11', '11:17'], - ['Reference', '5:10', ''], + ['Element', '
', '
', '
'], + ['Reference', 'ref-a', ''], ]); }); });