Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 1ed28e6

Browse files
committed
test: use describe blocks
1 parent 132399e commit 1ed28e6

File tree

1 file changed

+151
-143
lines changed

1 file changed

+151
-143
lines changed

src/token_position.test.ts

Lines changed: 151 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -35,164 +35,172 @@ describe('token_positions', () => {
3535
testcases = dom.createCodeViews()
3636
})
3737

38-
it('convertNode tokenizes text properly', () => {
39-
const elems = [
40-
{
41-
content: '<div>Hello, World!</div>',
42-
nodeValues: ['Hello', ',', ' ', 'World', '!'],
43-
},
44-
{
45-
content: `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`,
46-
nodeValues: [
47-
tabChar,
48-
'if',
49-
' ',
50-
'rv',
51-
' ',
52-
':',
53-
'=',
54-
' ',
55-
'contextGet',
56-
'(',
57-
'r',
58-
',',
59-
' ',
60-
'routeKey',
61-
')',
62-
';',
63-
' ',
64-
'rv',
65-
' ',
66-
'!',
67-
'=',
68-
' ',
69-
'nil',
70-
' ',
71-
'{',
72-
],
73-
},
74-
]
75-
76-
for (const { content, nodeValues } of elems) {
77-
const elem = dom.createElementFromString(content)
38+
describe('covertNode()', () => {
39+
it('tokenizes text properly', () => {
40+
const elems = [
41+
{
42+
content: '<div>Hello, World!</div>',
43+
nodeValues: ['Hello', ',', ' ', 'World', '!'],
44+
},
45+
{
46+
content: `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`,
47+
nodeValues: [
48+
tabChar,
49+
'if',
50+
' ',
51+
'rv',
52+
' ',
53+
':',
54+
'=',
55+
' ',
56+
'contextGet',
57+
'(',
58+
'r',
59+
',',
60+
' ',
61+
'routeKey',
62+
')',
63+
';',
64+
' ',
65+
'rv',
66+
' ',
67+
'!',
68+
'=',
69+
' ',
70+
'nil',
71+
' ',
72+
'{',
73+
],
74+
},
75+
]
76+
77+
for (const { content, nodeValues } of elems) {
78+
const elem = dom.createElementFromString(content)
79+
80+
convertNode(elem)
81+
82+
const nodes = getTextNodes(elem)
83+
84+
expect(nodes.length).to.equal(nodeValues.length)
85+
86+
for (const [i, val] of nodeValues.entries()) {
87+
expect(nodes[i].nodeValue).to.equal(val)
88+
}
89+
}
90+
})
91+
})
7892

79-
convertNode(elem)
93+
describe('findElementWithOffset()', () => {
94+
it('finds the correct token', () => {
95+
const content = `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`
96+
97+
const elems = [
98+
{
99+
offset: 11,
100+
token: 'contextGet',
101+
},
102+
{
103+
offset: 21,
104+
token: '(',
105+
},
106+
{
107+
offset: 2,
108+
token: 'if',
109+
},
110+
{
111+
offset: 4,
112+
token: ' ',
113+
},
114+
]
80115

81-
const nodes = getTextNodes(elem)
116+
const elem = dom.createElementFromString(content)
82117

83-
expect(nodes.length).to.equal(nodeValues.length)
118+
for (const { offset, token } of elems) {
119+
const tokenElem = findElementWithOffset(elem, offset)
84120

85-
for (const [i, val] of nodeValues.entries()) {
86-
expect(nodes[i].nodeValue).to.equal(val)
87-
}
88-
}
89-
})
121+
expect(tokenElem).to.not.equal(undefined)
90122

91-
it('findElementWithOffset finds the correct token', () => {
92-
const content = `${tabChar}if rv := contextGet(r, routeKey); rv != nil {`
93-
94-
const elems = [
95-
{
96-
offset: 11,
97-
token: 'contextGet',
98-
},
99-
{
100-
offset: 21,
101-
token: '(',
102-
},
103-
{
104-
offset: 2,
105-
token: 'if',
106-
},
107-
{
108-
offset: 4,
109-
token: ' ',
110-
},
111-
]
112-
113-
const elem = dom.createElementFromString(content)
114-
115-
for (const { offset, token } of elems) {
116-
const tokenElem = findElementWithOffset(elem, offset)
117-
118-
expect(tokenElem).to.not.equal(undefined)
119-
120-
expect(tokenElem!.textContent).to.equal(token)
121-
}
122-
})
123+
expect(tokenElem!.textContent).to.equal(token)
124+
}
125+
})
123126

124-
it('findElementWithOffset returns undefined for invalid offsets', () => {
125-
const content = 'Hello, World!'
127+
it('returns undefined for invalid offsets', () => {
128+
const content = 'Hello, World!'
126129

127-
const offsets = [content.length + 1, 0]
130+
const offsets = [content.length + 1, 0]
128131

129-
const elem = dom.createElementFromString(content)
132+
const elem = dom.createElementFromString(content)
130133

131-
for (const offset of offsets) {
132-
const tokenElem = findElementWithOffset(elem, offset)
134+
for (const offset of offsets) {
135+
const tokenElem = findElementWithOffset(elem, offset)
133136

134-
expect(tokenElem).to.equal(undefined)
135-
}
137+
expect(tokenElem).to.equal(undefined)
138+
}
139+
})
136140
})
137141

138-
it('getTokenAtPosition finds the correct tokens', () => {
139-
const tokens = [
140-
{
141-
token: 'NewRouter',
142-
position: { line: 24, character: 7 },
143-
},
144-
{
145-
token: 'import',
146-
position: { line: 7, character: 3 },
147-
},
148-
{
149-
token: 'if',
150-
position: { line: 154, character: 2 },
151-
},
152-
{
153-
token: '=',
154-
position: { line: 257, character: 5 },
155-
},
156-
]
157-
158-
for (const { codeView, ...domOptions } of testcases) {
159-
for (const { token, position } of tokens) {
160-
const found = getTokenAtPosition(codeView, position, domOptions)
161-
162-
expect(found).to.not.equal(undefined)
163-
expect(found!.textContent).to.equal(token)
142+
describe('getTokenAtPosition()', () => {
143+
it('finds the correct tokens', () => {
144+
const tokens = [
145+
{
146+
token: 'NewRouter',
147+
position: { line: 24, character: 7 },
148+
},
149+
{
150+
token: 'import',
151+
position: { line: 7, character: 3 },
152+
},
153+
{
154+
token: 'if',
155+
position: { line: 154, character: 2 },
156+
},
157+
{
158+
token: '=',
159+
position: { line: 257, character: 5 },
160+
},
161+
]
162+
163+
for (const { codeView, ...domOptions } of testcases) {
164+
for (const { token, position } of tokens) {
165+
const found = getTokenAtPosition(codeView, position, domOptions)
166+
167+
expect(found).to.not.equal(undefined)
168+
expect(found!.textContent).to.equal(token)
169+
}
164170
}
165-
}
171+
})
166172
})
167173

168-
it('locateTarget finds the correct token for a target', () => {
169-
const positions: {
170-
/** A position within the expected token. */
171-
atPosition: Position
172-
/** The position that locateTarget found. If it works correctly, it is the position of the first character in the token. */
173-
foundPosition: Position
174-
}[] = [
175-
{ atPosition: { line: 24, character: 8 }, foundPosition: { line: 24, character: 6 } }, // NewRouter
176-
{ atPosition: { line: 7, character: 3 }, foundPosition: { line: 7, character: 1 } }, // import
177-
{ atPosition: { line: 154, character: 3 }, foundPosition: { line: 154, character: 2 } }, // if
178-
{ atPosition: { line: 257, character: 5 }, foundPosition: { line: 257, character: 5 } }, // =
179-
{ atPosition: { line: 121, character: 9 }, foundPosition: { line: 121, character: 9 } }, // *
180-
{ atPosition: { line: 128, character: 8 }, foundPosition: { line: 128, character: 8 } }, // :
181-
]
182-
183-
for (const { codeView, ...domOptions } of testcases) {
184-
for (const { atPosition, foundPosition } of positions) {
185-
const target = getTokenAtPosition(codeView, atPosition, domOptions)
186-
187-
const found = locateTarget(target!, domOptions)
188-
189-
expect(found).to.not.equal(undefined)
190-
191-
const token = found as HoveredToken
192-
193-
expect(token.line).to.equal(foundPosition.line)
194-
expect(token.character).to.equal(foundPosition.character)
174+
describe('locateTarget()', () => {
175+
it('finds the correct token for a target', () => {
176+
const positions: {
177+
/** A position within the expected token. */
178+
atPosition: Position
179+
/** The position that locateTarget found. If it works correctly, it is the position of the first character in the token. */
180+
foundPosition: Position
181+
}[] = [
182+
{ atPosition: { line: 24, character: 8 }, foundPosition: { line: 24, character: 6 } }, // NewRouter
183+
{ atPosition: { line: 7, character: 3 }, foundPosition: { line: 7, character: 1 } }, // import
184+
{ atPosition: { line: 154, character: 3 }, foundPosition: { line: 154, character: 2 } }, // if
185+
{ atPosition: { line: 257, character: 5 }, foundPosition: { line: 257, character: 5 } }, // =
186+
{ atPosition: { line: 121, character: 9 }, foundPosition: { line: 121, character: 9 } }, // *
187+
{ atPosition: { line: 128, character: 8 }, foundPosition: { line: 128, character: 8 } }, // :
188+
]
189+
190+
for (const { codeView, ...domOptions } of testcases) {
191+
for (const { atPosition, foundPosition } of positions) {
192+
const target = getTokenAtPosition(codeView, atPosition, domOptions)
193+
194+
const found = locateTarget(target!, domOptions)
195+
196+
expect(found).to.not.equal(undefined)
197+
198+
const token = found as HoveredToken
199+
200+
expect(token.line).to.equal(foundPosition.line)
201+
expect(token.character).to.equal(foundPosition.character)
202+
}
195203
}
196-
}
204+
})
197205
})
198206
})

0 commit comments

Comments
 (0)