1+ /**
2+ * @license
3+ * Copyright Google Inc. All Rights Reserved.
4+ *
5+ * Use of this source code is governed by an MIT-style license that can be
6+ * found in the LICENSE file at https://angular.io/license
7+ */
8+ import * as vscode from 'vscode' ;
9+ import { DocumentUri , TextDocument } from 'vscode-languageserver-textdocument' ;
10+
11+ import { isInsideComponentDecorator , isInsideInlineTemplateRegion } from '../embedded_support' ;
12+
13+ describe ( 'embedded language support' , ( ) => {
14+ describe ( 'isInsideInlineTemplateRegion' , ( ) => {
15+ it ( 'empty file' , ( ) => {
16+ test ( '¦' , isInsideInlineTemplateRegion , false ) ;
17+ } ) ;
18+
19+ it ( 'just after template' , ( ) => {
20+ test ( `template: '<div></div>'¦` , isInsideInlineTemplateRegion , false ) ;
21+ } ) ;
22+
23+ it ( 'just before template' , ( ) => {
24+ // Note that while it seems that this should be `false`, we should still consider this inside
25+ // the string because the visual mode of vim appears to have a position on top of the open
26+ // quote while the cursor position is before it.
27+ test ( `template: ¦'<div></div>'` , isInsideInlineTemplateRegion , true ) ;
28+ } ) ;
29+
30+ it ( 'two spaces before template' , ( ) => {
31+ test ( `template:¦ '<div></div>'` , isInsideInlineTemplateRegion , false ) ;
32+ } ) ;
33+
34+ it ( 'at beginning of template' , ( ) => {
35+ test ( `template: '¦<div></div>'` , isInsideInlineTemplateRegion , true ) ;
36+ } ) ;
37+
38+ it ( 'at end of template' , ( ) => {
39+ test ( `template: '<div></div>¦'` , isInsideInlineTemplateRegion , true ) ;
40+ } ) ;
41+ } ) ;
42+
43+ describe ( 'isInsideAngularContext' , ( ) => {
44+ it ( 'empty file' , ( ) => {
45+ test ( '¦' , isInsideComponentDecorator , false ) ;
46+ } ) ;
47+
48+ it ( 'just after template' , ( ) => {
49+ test ( `template: '<div></div>'¦` , isInsideComponentDecorator , false ) ;
50+ } ) ;
51+
52+ it ( 'inside template' , ( ) => {
53+ test ( `template: '<div>¦</div>'` , isInsideComponentDecorator , true ) ;
54+ } ) ;
55+
56+ it ( 'just after templateUrl' , ( ) => {
57+ test ( `templateUrl: './abc.html'¦` , isInsideComponentDecorator , false ) ;
58+ } ) ;
59+
60+ it ( 'inside templateUrl' , ( ) => {
61+ test ( `templateUrl: './abc¦.html'` , isInsideComponentDecorator , true ) ;
62+ } ) ;
63+
64+ it ( 'just after styleUrls' , ( ) => {
65+ test ( `styleUrls: ['./abc.css']¦` , isInsideComponentDecorator , false ) ;
66+ } ) ;
67+
68+ it ( 'inside first item of styleUrls' , ( ) => {
69+ test ( `styleUrls: ['./abc.c¦ss', 'def.css']` , isInsideComponentDecorator , true ) ;
70+ } ) ;
71+
72+ it ( 'inside second item of styleUrls' , ( ) => {
73+ test ( `styleUrls: ['./abc.css', 'def¦.css']` , isInsideComponentDecorator , true ) ;
74+ } ) ;
75+
76+ it ( 'inside second item of styleUrls, when first is complicated function' , ( ) => {
77+ test (
78+ `styleUrls: [getCss({strict: true, dirs: ['apple', 'banana']}), 'def¦.css']` ,
79+ isInsideComponentDecorator , true ) ;
80+ } ) ;
81+
82+ it ( 'inside non-string item of styleUrls' , ( ) => {
83+ test (
84+ `styleUrls: [getCss({strict: true¦, dirs: ['apple', 'banana']}), 'def.css']` ,
85+ isInsideComponentDecorator , false ) ;
86+ } ) ;
87+ } ) ;
88+ } ) ;
89+
90+ function test (
91+ fileWithCursor : string ,
92+ testFn : ( doc : vscode . TextDocument , position : vscode . Position ) => boolean ,
93+ expectation : boolean ) : void {
94+ const { cursor, text} = extractCursorInfo ( fileWithCursor ) ;
95+ const vdoc = TextDocument . create ( 'test' as DocumentUri , 'typescript' , 0 , text ) as { } as
96+ vscode . TextDocument ;
97+ const actual = testFn ( vdoc , vdoc . positionAt ( cursor ) ) ;
98+ expect ( actual ) . toBe ( expectation ) ;
99+ }
100+
101+ /**
102+ * Given a text snippet which contains exactly one cursor symbol ('¦'), extract both the offset of
103+ * that cursor within the text as well as the text snippet without the cursor.
104+ */
105+ function extractCursorInfo ( textWithCursor : string ) : { cursor : number , text : string } {
106+ const cursor = textWithCursor . indexOf ( '¦' ) ;
107+ if ( cursor === - 1 || textWithCursor . indexOf ( '¦' , cursor + 1 ) !== - 1 ) {
108+ throw new Error ( `Expected to find exactly one cursor symbol '¦'` ) ;
109+ }
110+
111+ return {
112+ cursor,
113+ text : textWithCursor . substr ( 0 , cursor ) + textWithCursor . substr ( cursor + 1 ) ,
114+ } ;
115+ }
0 commit comments