Skip to content

Commit 248a856

Browse files
committed
add parsing & semantic token tests for comments
1 parent 2f3dde1 commit 248a856

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { createContextMapperDslServices } from '../../src/language/ContextMapperDslModule.js'
2+
import { clearDocuments, parseHelper } from 'langium/test'
3+
import { ContextMappingModel } from '../../src/language/generated/ast.js'
4+
import { EmptyFileSystem, LangiumDocument } from 'langium'
5+
import { afterEach, beforeAll, describe, test } from 'vitest'
6+
import { parseValidInput } from './ParsingTestHelper.js'
7+
8+
let services: ReturnType<typeof createContextMapperDslServices>
9+
let parse: ReturnType<typeof parseHelper<ContextMappingModel>>
10+
let document: LangiumDocument<ContextMappingModel> | undefined
11+
12+
beforeAll(async () => {
13+
services = createContextMapperDslServices(EmptyFileSystem)
14+
parse = parseHelper<ContextMappingModel>(services.ContextMapperDsl)
15+
})
16+
17+
afterEach(async () => {
18+
document && await clearDocuments(services.shared, [document])
19+
})
20+
21+
describe('Comment semantic token tests', () => {
22+
test('parse multiline comment at snippet start', async () => {
23+
document = await parseValidInput(parse, `
24+
/*
25+
This is a multiline comment
26+
*/
27+
ContextMap {}
28+
`)
29+
})
30+
31+
test('parse multiline comment at snippet end', async () => {
32+
document = await parseValidInput(parse, `
33+
ContextMap {}
34+
/*
35+
This is a multiline comment
36+
*/
37+
`)
38+
})
39+
40+
test('parse single-line comment at snippet start', async () => {
41+
document = await parseValidInput(parse, `
42+
// This is a single-line comment
43+
ContextMap {}
44+
`)
45+
})
46+
47+
test('parse single-line comment at snippet end', async () => {
48+
document = await parseValidInput(parse, `
49+
ContextMap {}
50+
// This is a single-line comment
51+
`)
52+
})
53+
54+
test('parse multiple multiline comments', async () => {
55+
document = await parseValidInput(parse, `
56+
/*
57+
TestContext description
58+
*/
59+
BoundedContext TestContext
60+
/*
61+
AnotherContext description
62+
*/
63+
BoundedContext AnotherContext
64+
`)
65+
})
66+
67+
test('parse multiple single-line comments', async () => {
68+
document = await parseValidInput(parse, `
69+
// TestContext description
70+
BoundedContext TestContext
71+
72+
// AnotherContext description
73+
BoundedContext AnotherContext
74+
`)
75+
})
76+
77+
test('parse multiline comment in nested structure', async () => {
78+
document = await parseValidInput(parse, `
79+
BoundedContext TestContext {
80+
/*
81+
This is a multiline comment
82+
*/
83+
Module TestModule
84+
}
85+
`)
86+
})
87+
88+
test('parse single-line comment in nested structure', async () => {
89+
document = await parseValidInput(parse, `
90+
BoundedContext TestContext {
91+
// This is a multiline comment
92+
Module TestModule
93+
}
94+
`)
95+
})
96+
97+
test('parse single-line comment after a field', async () => {
98+
document = await parseValidInput(parse, `
99+
BoundedContext TestContext {
100+
type TEAM // This is a single-line comment
101+
}
102+
`)
103+
})
104+
})
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { createContextMapperDslServices } from '../../src/language/ContextMapperDslModule.js'
2+
import { clearDocuments, parseHelper } from 'langium/test'
3+
import { ContextMappingModel } from '../../src/language/generated/ast.js'
4+
import { EmptyFileSystem, LangiumDocument } from 'langium'
5+
import { SemanticTokenProvider } from 'langium/lsp'
6+
import { afterEach, beforeAll, describe, test } from 'vitest'
7+
import {
8+
createSemanticTokenParams, expectSemanticTokensToEqual,
9+
expectSemanticTokensToHaveLength,
10+
extractSemanticTokens
11+
} from './SemanticTokenTestHelper.js'
12+
13+
let services: ReturnType<typeof createContextMapperDslServices>
14+
let parse: ReturnType<typeof parseHelper<ContextMappingModel>>
15+
let document: LangiumDocument<ContextMappingModel> | undefined
16+
let semanticTokenProvider: SemanticTokenProvider
17+
18+
beforeAll(async () => {
19+
services = createContextMapperDslServices(EmptyFileSystem)
20+
parse = parseHelper<ContextMappingModel>(services.ContextMapperDsl)
21+
semanticTokenProvider = services.ContextMapperDsl.lsp.SemanticTokenProvider!!
22+
})
23+
24+
afterEach(async () => {
25+
document && await clearDocuments(services.shared, [document])
26+
})
27+
28+
describe('Comment semantic token tests', () => {
29+
test('check semantic tokens for multiline comment at snippet start', async () => {
30+
document = await parse(`
31+
/*
32+
This is a multiline comment
33+
*/
34+
ContextMap {}
35+
`)
36+
const params = createSemanticTokenParams(document)
37+
38+
const result = await semanticTokenProvider.semanticHighlight(document, params)
39+
40+
const expectedNumberOfTokens = 2
41+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
42+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
43+
44+
expectSemanticTokensToEqual(tokens[0], 1, 6, 47, semanticTokenProvider.tokenTypes.comment, 0)
45+
})
46+
47+
test('check semantic tokens for multiline comment at snippet end', async () => {
48+
document = await parse(`
49+
ContextMap {}
50+
/*
51+
This is a multiline comment
52+
*/
53+
`)
54+
const params = createSemanticTokenParams(document)
55+
56+
const result = await semanticTokenProvider.semanticHighlight(document, params)
57+
58+
const expectedNumberOfTokens = 2
59+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
60+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
61+
62+
expectSemanticTokensToEqual(tokens[1], 1, 6, 47, semanticTokenProvider.tokenTypes.comment, 0)
63+
})
64+
65+
test('check semantic tokens for single-line comment at snippet start', async () => {
66+
document = await parse(`
67+
// This is a single-line comment
68+
ContextMap {}
69+
`)
70+
const params = createSemanticTokenParams(document)
71+
72+
const result = await semanticTokenProvider.semanticHighlight(document, params)
73+
74+
const expectedNumberOfTokens = 2
75+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
76+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
77+
78+
expectSemanticTokensToEqual(tokens[0], 1, 6, 32, semanticTokenProvider.tokenTypes.comment, 0)
79+
})
80+
81+
test('check semantic tokens for single-line comment at snippet end', async () => {
82+
document = await parse(`
83+
ContextMap {}
84+
// This is a single-line comment
85+
`)
86+
const params = createSemanticTokenParams(document)
87+
88+
const result = await semanticTokenProvider.semanticHighlight(document, params)
89+
90+
const expectedNumberOfTokens = 2
91+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
92+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
93+
94+
expectSemanticTokensToEqual(tokens[1], 1, 6, 32, semanticTokenProvider.tokenTypes.comment, 0)
95+
})
96+
97+
test('check semantic tokens for multiple multiline comments', async () => {
98+
document = await parse(`
99+
/*
100+
TestContext description
101+
*/
102+
BoundedContext TestContext
103+
/*
104+
AnotherContext description
105+
*/
106+
BoundedContext AnotherContext
107+
`)
108+
const params = createSemanticTokenParams(document)
109+
110+
const result = await semanticTokenProvider.semanticHighlight(document, params)
111+
112+
const expectedNumberOfTokens = 6
113+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
114+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
115+
116+
expectSemanticTokensToEqual(tokens[0], 1, 6, 43, semanticTokenProvider.tokenTypes.comment, 0)
117+
expectSemanticTokensToEqual(tokens[3], 1, 6, 46, semanticTokenProvider.tokenTypes.comment, 0)
118+
})
119+
120+
test('check semantic tokens for multiple single-line comments', async () => {
121+
document = await parse(`
122+
// TestContext description
123+
BoundedContext TestContext
124+
125+
// AnotherContext description
126+
BoundedContext AnotherContext
127+
`)
128+
const params = createSemanticTokenParams(document)
129+
130+
const result = await semanticTokenProvider.semanticHighlight(document, params)
131+
132+
const expectedNumberOfTokens = 6
133+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
134+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
135+
136+
expectSemanticTokensToEqual(tokens[0], 1, 6, 26, semanticTokenProvider.tokenTypes.comment, 0)
137+
expectSemanticTokensToEqual(tokens[3], 2, 6, 29, semanticTokenProvider.tokenTypes.comment, 0)
138+
})
139+
140+
test('check semantic tokens for multiline comment in nested structure', async () => {
141+
document = await parse(`
142+
BoundedContext TestContext {
143+
/*
144+
This is a multiline comment
145+
*/
146+
Module TestModule
147+
}
148+
`)
149+
const params = createSemanticTokenParams(document)
150+
151+
const result = await semanticTokenProvider.semanticHighlight(document, params)
152+
153+
const expectedNumberOfTokens = 5
154+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
155+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
156+
157+
expectSemanticTokensToEqual(tokens[2], 1, 8, 51, semanticTokenProvider.tokenTypes.comment, 0)
158+
})
159+
160+
test('check semantic tokens for single-line comment in nested structure', async () => {
161+
document = await parse(`
162+
BoundedContext TestContext {
163+
// This is a multiline comment
164+
Module TestModule
165+
}
166+
`)
167+
const params = createSemanticTokenParams(document)
168+
169+
const result = await semanticTokenProvider.semanticHighlight(document, params)
170+
171+
const expectedNumberOfTokens = 5
172+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
173+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
174+
175+
expectSemanticTokensToEqual(tokens[2], 1, 8, 30, semanticTokenProvider.tokenTypes.comment, 0)
176+
})
177+
178+
test('check semantic tokens for single-line comment after a field', async () => {
179+
document = await parse(`
180+
BoundedContext TestContext {
181+
type TEAM // This is a single-line comment
182+
}
183+
`)
184+
const params = createSemanticTokenParams(document)
185+
186+
const result = await semanticTokenProvider.semanticHighlight(document, params)
187+
188+
const expectedNumberOfTokens = 5
189+
expectSemanticTokensToHaveLength(result, expectedNumberOfTokens)
190+
const tokens = extractSemanticTokens(result, expectedNumberOfTokens)
191+
192+
expectSemanticTokensToEqual(tokens[4], 0, 5, 32, semanticTokenProvider.tokenTypes.comment, 0)
193+
})
194+
})

0 commit comments

Comments
 (0)