11import * as assert from 'assert' ;
22import { ClassCompletionItemProvider } from '../../extension' ;
33import {
4- workspace ,
54 Uri ,
65 Event ,
76 CancellationToken ,
87 CompletionContext ,
98 CompletionTriggerKind ,
10- CompletionItem
9+ CompletionItem ,
10+ TextDocument ,
11+ EndOfLine ,
12+ Position ,
13+ Range ,
14+ TextLine
1115} from 'vscode' ;
1216
1317class MockCancellationToken implements CancellationToken {
@@ -24,8 +28,52 @@ class MockCompletionContext implements CompletionContext {
2428 triggerCharacter ?: string | undefined ;
2529}
2630
31+ class MockDocument implements TextDocument {
32+ uri ! : Uri ;
33+ fileName ! : string ;
34+ isUntitled ! : boolean ;
35+ languageId ! : string ;
36+ version ! : number ;
37+ isDirty ! : boolean ;
38+ isClosed ! : boolean ;
39+ eol ! : EndOfLine ;
40+ lineCount ! : number ;
41+ text : string ;
42+
43+ constructor ( text : string ) {
44+ this . text = text ;
45+ }
46+
47+ getText ( range ?: Range ) : string {
48+ return this . text ;
49+ }
50+
51+ save ( ) : Thenable < boolean > {
52+ throw new Error ( 'Method not implemented.' ) ;
53+ }
54+ lineAt ( position : Position | number | any ) : TextLine {
55+ throw new Error ( 'Method not implemented.' ) ;
56+ }
57+ offsetAt ( position : Position ) : number {
58+ throw new Error ( 'Method not implemented.' ) ;
59+ }
60+ positionAt ( offset : number ) : Position {
61+ throw new Error ( 'Method not implemented.' ) ;
62+ }
63+ getWordRangeAtPosition ( position : Position , regex ?: RegExp ) : Range | undefined {
64+ throw new Error ( 'Method not implemented.' ) ;
65+ }
66+ validateRange ( range : Range ) : Range {
67+ throw new Error ( 'Method not implemented.' ) ;
68+ }
69+ validatePosition ( position : Position ) : Position {
70+ throw new Error ( 'Method not implemented.' ) ;
71+ }
72+ }
73+
2774suite ( 'Extension Test Suite' , ( ) => {
2875
76+ const position = new Position ( 0 , 0 ) ;
2977 const token = new MockCancellationToken ( false ) ;
3078 const context = new MockCompletionContext ( ) ;
3179
@@ -87,32 +135,26 @@ suite('Extension Test Suite', () => {
87135 "` ) ?. [ 2 ] , "http://example.com/example.css" ) ;
88136 } ) ;
89137
90- test ( 'Rejects outside class attribute' , async ( ) => {
138+ test ( 'Rejects outside class attribute' , ( done ) => {
91139 const provider = new ClassCompletionItemProvider ( ) ;
92- const content = `<a class=""></a>` ;
93- const document = await workspace . openTextDocument ( { content, language : "html" } ) ;
94-
95- try {
96- const items = await ( provider . provideCompletionItems (
97- document ,
98- document . positionAt ( content . length ) ,
99- token ,
100- context ) as Thenable < CompletionItem [ ] > ) ;
101-
102- assert . strictEqual ( items . length , 0 ) ;
103- } catch ( e ) {
104- assert . strictEqual ( e , undefined ) ;
105- }
140+ const document = new MockDocument ( `<a class=""></a>` ) ;
141+
142+ const result = provider . provideCompletionItems (
143+ document ,
144+ position ,
145+ token ,
146+ context ) as Thenable < CompletionItem [ ] > ;
147+
148+ result . then ( items => done ( new Error ( "Should reject!" ) ) , ( ) => done ( ) ) ;
106149 } ) ;
107150
108151 test ( 'Completes from style tag' , async ( ) => {
109152 const provider = new ClassCompletionItemProvider ( ) ;
110- const content = `<style>.test{}</style><a class="` ;
111- const document = await workspace . openTextDocument ( { content, language : "html" } ) ;
153+ const document = new MockDocument ( `<style>.test{}</style><a class="` ) ;
112154
113155 const items = await ( provider . provideCompletionItems (
114156 document ,
115- document . positionAt ( content . length ) ,
157+ position ,
116158 token ,
117159 context ) as Thenable < CompletionItem [ ] > ) ;
118160
@@ -121,24 +163,22 @@ suite('Extension Test Suite', () => {
121163
122164 test ( 'Completes from link tag' , async ( ) => {
123165 const provider = new ClassCompletionItemProvider ( ) ;
124- const content = `
166+ const document = new MockDocument ( `
125167 <link
126168 href="https://cdn.jsdelivr.net/npm/[email protected] /dist/css/bootstrap.min.css" 127169 rel="stylesheet"
128170 >
129- <a class="` ;
130- const document = await workspace . openTextDocument ( { content, language : "html" } ) ;
171+ <a class="` ) ;
131172
132173 const items = await ( provider . provideCompletionItems (
133174 document ,
134- document . positionAt ( content . length ) ,
175+ position ,
135176 token ,
136177 context ) as Thenable < CompletionItem [ ] > ) ;
137178
138179 assert . notStrictEqual ( items . length , 0 ) ;
139180 } ) ;
140181
141-
142182 test ( 'Completes from remote style' , async ( ) => {
143183 const provider = new class extends ClassCompletionItemProvider {
144184 getRemoteStyleSheets ( uri : Uri ) : string [ ] {
@@ -148,12 +188,11 @@ suite('Extension Test Suite', () => {
148188 }
149189 } ( ) ;
150190
151- const content = `<a class="` ;
152- const document = await workspace . openTextDocument ( { content, language : "html" } ) ;
191+ const document = new MockDocument ( `<a class="` ) ;
153192
154193 const items = await ( provider . provideCompletionItems (
155194 document ,
156- document . positionAt ( content . length ) ,
195+ position ,
157196 token ,
158197 context ) as Thenable < CompletionItem [ ] > ) ;
159198
0 commit comments