Skip to content

Commit cc02408

Browse files
Deep Furiyasatyakigh
authored andcommitted
updated tests with modified Document constructor
1 parent b80bfa8 commit cc02408

File tree

9 files changed

+145
-132
lines changed

9 files changed

+145
-132
lines changed

tools/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function discoverTemplateFiles(paths: string[]): TemplateFile[] {
174174
const content = readFileSync(path, 'utf8');
175175
const { extension, type } = detectDocumentType(path, content);
176176
const textDocument = TextDocument.create(path, type === DocumentType.JSON ? 'json' : 'yaml', 1, content);
177-
const document = new Document(textDocument);
177+
const document = new Document(textDocument.uri, () => textDocument);
178178

179179
return {
180180
name: uriToPath(path).base,

tst/unit/codeLens/ManagedResourceCodeLens.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { describe, it, expect, beforeEach, vi } from 'vitest';
2-
import { TextDocument } from 'vscode-languageserver-textdocument';
32
import { ManagedResourceCodeLens } from '../../../src/codeLens/ManagedResourceCodeLens';
43
import { getEntityMap } from '../../../src/context/SectionContextBuilder';
5-
import { Document } from '../../../src/document/Document';
64
import { createMockSyntaxTreeManager } from '../../utils/MockServerComponents';
5+
import { createTestDocument } from '../../utils/Utils';
76

87
// Mock the SectionContextBuilder module
98
vi.mock('../../../src/context/SectionContextBuilder', () => ({
@@ -25,8 +24,11 @@ describe('ManagedResourceCodeLens', () => {
2524
it('should return empty array when syntax tree is not found', () => {
2625
mockSyntaxTreeManager.getSyntaxTree.returns({} as any);
2726

28-
const document = new Document(
29-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'Resources:\n Bucket:\n Type: AWS::S3::Bucket'),
27+
const document = createTestDocument(
28+
'file:///test.yaml',
29+
'yaml',
30+
1,
31+
'Resources:\n Bucket:\n Type: AWS::S3::Bucket',
3032
);
3133
const result = codeLens.getCodeLenses('file:///test.yaml', document);
3234

@@ -37,9 +39,7 @@ describe('ManagedResourceCodeLens', () => {
3739
mockSyntaxTreeManager.getSyntaxTree.returns({} as any);
3840
mockGetEntityMap.mockReturnValue(undefined);
3941

40-
const document = new Document(
41-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'AWSTemplateFormatVersion: "2010-09-09"'),
42-
);
42+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, 'AWSTemplateFormatVersion: "2010-09-09"');
4343
const result = codeLens.getCodeLenses('file:///test.yaml', document);
4444

4545
expect(result).toEqual([]);
@@ -54,7 +54,7 @@ describe('ManagedResourceCodeLens', () => {
5454
StackName: test-stack
5555
PrimaryIdentifier: bucket-id`;
5656

57-
const document = new Document(TextDocument.create('file:///test.yaml', 'yaml', 1, yamlContent));
57+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, yamlContent);
5858

5959
const mockResourceContext = {
6060
entity: {
@@ -88,7 +88,7 @@ describe('ManagedResourceCodeLens', () => {
8888
StackName: test-stack
8989
PrimaryIdentifier: bucket-id`;
9090

91-
const document = new Document(TextDocument.create('file:///test.yaml', 'yaml', 1, yamlContent));
91+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, yamlContent);
9292

9393
const mockResourceContext = {
9494
entity: {
@@ -118,7 +118,7 @@ describe('ManagedResourceCodeLens', () => {
118118
ManagedByStack: true
119119
StackName: test-stack`;
120120

121-
const document = new Document(TextDocument.create('file:///test.yaml', 'yaml', 1, yamlContent));
121+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, yamlContent);
122122

123123
const mockResourceContext = {
124124
entity: {

tst/unit/codeLens/StackActionsCodeLens.test.ts

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { describe, it, expect, beforeEach, vi } from 'vitest';
2-
import { TextDocument } from 'vscode-languageserver-textdocument';
32
import { getStackActionsCodeLenses } from '../../../src/codeLens/StackActionsCodeLens';
43
import { getEntityMap } from '../../../src/context/SectionContextBuilder';
5-
import { Document } from '../../../src/document/Document';
64
import { createMockSyntaxTreeManager } from '../../utils/MockServerComponents';
5+
import { createTestDocument } from '../../utils/Utils';
76

87
vi.mock('../../../src/context/SectionContextBuilder', () => ({
98
getEntityMap: vi.fn(),
@@ -22,8 +21,11 @@ describe('StackActionsCodeLens', () => {
2221
it('should return empty array when syntax tree is not found', () => {
2322
mockSyntaxTreeManager.getSyntaxTree.returns(undefined);
2423

25-
const document = new Document(
26-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'Resources:\n Bucket:\n Type: AWS::S3::Bucket'),
24+
const document = createTestDocument(
25+
'file:///test.yaml',
26+
'yaml',
27+
1,
28+
'Resources:\n Bucket:\n Type: AWS::S3::Bucket',
2729
);
2830

2931
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -35,9 +37,7 @@ describe('StackActionsCodeLens', () => {
3537
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
3638
mockGetEntityMap.mockReturnValue(undefined);
3739

38-
const document = new Document(
39-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'AWSTemplateFormatVersion: "2010-09-09"'),
40-
);
40+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, 'AWSTemplateFormatVersion: "2010-09-09"');
4141

4242
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
4343

@@ -48,7 +48,7 @@ describe('StackActionsCodeLens', () => {
4848
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
4949
mockGetEntityMap.mockReturnValue(new Map());
5050

51-
const document = new Document(TextDocument.create('file:///test.yaml', 'yaml', 1, 'Resources: {}'));
51+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, 'Resources: {}');
5252

5353
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
5454

@@ -59,9 +59,7 @@ describe('StackActionsCodeLens', () => {
5959
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
6060
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
6161

62-
const document = new Document(
63-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'some: random\nyaml: content'),
64-
);
62+
const document = createTestDocument('file:///test.yaml', 'yaml', 1, 'some: random\nyaml: content');
6563

6664
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
6765

@@ -72,8 +70,11 @@ describe('StackActionsCodeLens', () => {
7270
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
7371
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
7472

75-
const document = new Document(
76-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'Resources:\n Bucket:\n Type: AWS::S3::Bucket'),
73+
const document = createTestDocument(
74+
'file:///test.yaml',
75+
'yaml',
76+
1,
77+
'Resources:\n Bucket:\n Type: AWS::S3::Bucket',
7778
);
7879

7980
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -88,8 +89,11 @@ describe('StackActionsCodeLens', () => {
8889
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
8990
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
9091

91-
const document = new Document(
92-
TextDocument.create('file:///test.yaml', 'yaml', 1, 'Resources:\n Bucket:\n Type: AWS::S3::Bucket'),
92+
const document = createTestDocument(
93+
'file:///test.yaml',
94+
'yaml',
95+
1,
96+
'Resources:\n Bucket:\n Type: AWS::S3::Bucket',
9397
);
9498

9599
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -102,8 +106,11 @@ describe('StackActionsCodeLens', () => {
102106
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
103107
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
104108

105-
const document = new Document(
106-
TextDocument.create('file:///test.yaml', 'yaml', 1, '\n\nResources:\n Bucket:\n Type: AWS::S3::Bucket'),
109+
const document = createTestDocument(
110+
'file:///test.yaml',
111+
'yaml',
112+
1,
113+
'\n\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
107114
);
108115

109116
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -115,13 +122,11 @@ describe('StackActionsCodeLens', () => {
115122
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
116123
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
117124

118-
const document = new Document(
119-
TextDocument.create(
120-
'file:///test.yaml',
121-
'yaml',
122-
1,
123-
'# This is a comment\n# Another comment\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
124-
),
125+
const document = createTestDocument(
126+
'file:///test.yaml',
127+
'yaml',
128+
1,
129+
'# This is a comment\n# Another comment\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
125130
);
126131

127132
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -133,13 +138,11 @@ describe('StackActionsCodeLens', () => {
133138
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
134139
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
135140

136-
const document = new Document(
137-
TextDocument.create(
138-
'file:///test.yaml',
139-
'yaml',
140-
1,
141-
'\n# Comment\n\n# Another comment\n\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
142-
),
141+
const document = createTestDocument(
142+
'file:///test.yaml',
143+
'yaml',
144+
1,
145+
'\n# Comment\n\n# Another comment\n\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
143146
);
144147

145148
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -151,13 +154,11 @@ describe('StackActionsCodeLens', () => {
151154
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
152155
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
153156

154-
const document = new Document(
155-
TextDocument.create(
156-
'file:///test.yaml',
157-
'yaml',
158-
1,
159-
'AWSTemplateFormatVersion: "2010-09-09"\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
160-
),
157+
const document = createTestDocument(
158+
'file:///test.yaml',
159+
'yaml',
160+
1,
161+
'AWSTemplateFormatVersion: "2010-09-09"\nResources:\n Bucket:\n Type: AWS::S3::Bucket',
161162
);
162163

163164
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -170,13 +171,11 @@ describe('StackActionsCodeLens', () => {
170171
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
171172
mockGetEntityMap.mockReturnValue(new Map([['Function', {}]]));
172173

173-
const document = new Document(
174-
TextDocument.create(
175-
'file:///test.yaml',
176-
'yaml',
177-
1,
178-
'Transform: AWS::Serverless-2016-10-31\nResources:\n Function:\n Type: AWS::Serverless::Function',
179-
),
174+
const document = createTestDocument(
175+
'file:///test.yaml',
176+
'yaml',
177+
1,
178+
'Transform: AWS::Serverless-2016-10-31\nResources:\n Function:\n Type: AWS::Serverless::Function',
180179
);
181180

182181
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -188,8 +187,11 @@ describe('StackActionsCodeLens', () => {
188187
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
189188
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
190189

191-
const document = new Document(
192-
TextDocument.create('file:///test.json', 'json', 1, '{"Resources":{"Bucket":{"Type":"AWS::S3::Bucket"}}}'),
190+
const document = createTestDocument(
191+
'file:///test.json',
192+
'json',
193+
1,
194+
'{"Resources":{"Bucket":{"Type":"AWS::S3::Bucket"}}}',
193195
);
194196

195197
const result = getStackActionsCodeLenses('file:///test.json', document, mockSyntaxTreeManager);
@@ -207,13 +209,11 @@ describe('StackActionsCodeLens', () => {
207209
]),
208210
);
209211

210-
const document = new Document(
211-
TextDocument.create(
212-
'file:///test.yaml',
213-
'yaml',
214-
1,
215-
'Resources:\n Bucket:\n Type: AWS::S3::Bucket\n Table:\n Type: AWS::DynamoDB::Table',
216-
),
212+
const document = createTestDocument(
213+
'file:///test.yaml',
214+
'yaml',
215+
1,
216+
'Resources:\n Bucket:\n Type: AWS::S3::Bucket\n Table:\n Type: AWS::DynamoDB::Table',
217217
);
218218

219219
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);
@@ -225,13 +225,11 @@ describe('StackActionsCodeLens', () => {
225225
mockSyntaxTreeManager.getSyntaxTree.returns({ rootNode: {} } as any);
226226
mockGetEntityMap.mockReturnValue(new Map([['Bucket', {}]]));
227227

228-
const document = new Document(
229-
TextDocument.create(
230-
'file:///test.yaml',
231-
'yaml',
232-
1,
233-
' \n\t\n \nResources:\n Bucket:\n Type: AWS::S3::Bucket',
234-
),
228+
const document = createTestDocument(
229+
'file:///test.yaml',
230+
'yaml',
231+
1,
232+
' \n\t\n \nResources:\n Bucket:\n Type: AWS::S3::Bucket',
235233
);
236234

237235
const result = getStackActionsCodeLenses('file:///test.yaml', document, mockSyntaxTreeManager);

0 commit comments

Comments
 (0)