Skip to content

Commit 8fe880d

Browse files
committed
Add tests
1 parent 5237814 commit 8fe880d

File tree

13 files changed

+1769
-31
lines changed

13 files changed

+1769
-31
lines changed

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "Stale issues and PRs"
1+
name: Check for stale issues and PRs
22
on:
33
schedule:
44
- cron: "30 1 * * *"

.github/workflows/tests.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Run tests suite
2+
3+
on:
4+
pull_request:
5+
push:
6+
jobs:
7+
tests:
8+
name: Tests Suite
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
- name: Install Node
14+
uses: actions/setup-node@v1
15+
with:
16+
node-version: 16
17+
- name: Install Dependencies
18+
uses: borales/actions-yarn@v3
19+
with:
20+
cmd: install
21+
- name: Run Tests
22+
uses: borales/actions-yarn@v3
23+
with:
24+
cmd: test

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dist
33
node_modules
44
tsconfig.tsbuildinfo
55
*.vsix
6-
test/
76
server/resources/nwscript.nss
87
yarn-error.log
98
.DS_Store

.vscodeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ webpack.config.js
1010
test/**
1111
server/resources/nwscript.nss
1212
server/scripts
13+
server/test
1314
images/**
1415
TODO.txt

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@
139139
"watch": "tsc -b -w",
140140
"package": "yarn compile",
141141
"lint": "eslint ./client/src ./server/src --ext .ts",
142-
"postinstall": "cd client && yarn install && cd ../server && yarn install && cd .."
142+
"postinstall": "cd client && yarn install && cd ../server && yarn install && cd ..",
143+
"test": "cd server && yarn test"
143144
},
144145
"devDependencies": {
145146
"@types/node": "14.x",

server/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"scripts": {
1818
"postinstall": "patch-package",
19-
"generate-lib-defs": "ts-node scripts/GenerateLibDefinitions.ts"
19+
"generate-lib-defs": "ts-node scripts/GenerateLibDefinitions.ts",
20+
"test": "mocha -r ts-node/register 'test/**/*.ts'"
2021
},
2122
"dependencies": {
2223
"glob": "^8.0.1",
@@ -30,10 +31,14 @@
3031
"zeromq": "6.0.0-beta.6"
3132
},
3233
"devDependencies": {
34+
"@types/chai": "^4.3.3",
3335
"@types/glob": "^7.2.0",
36+
"@types/mocha": "^9.1.1",
3437
"@types/node": "^17.0.35",
3538
"@types/sax": "^1.2.4",
3639
"@types/zeromq": "^5.2.1",
40+
"chai": "^4.3.6",
41+
"mocha": "^10.0.0",
3742
"ts-node": "^10.7.0"
3843
}
3944
}

server/src/Tokenizer/Tokenizer.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ export default class Tokenizer {
8383
return tokensArray.findIndex((token) => token.startIndex === targetToken.startIndex);
8484
}
8585

86-
private getTokenLanguageType(line: string, token: IToken) {
87-
const rawContent = this.getRawTokenContent(line, token);
86+
private getTokenLanguageType(line: string, tokens: IToken[], index: number) {
87+
const rawContent = this.getRawTokenContent(line, tokens[index]);
8888

89-
return LanguageTypes[rawContent as keyof typeof LanguageTypes] || rawContent;
89+
const type = LanguageTypes[rawContent as keyof typeof LanguageTypes] || rawContent;
90+
return (type === LanguageTypes.struct ? this.getRawTokenContent(line, tokens[index + 2]) : type) as LanguageTypes;
9091
}
9192

9293
private getConstantValue(line: string, tokensArray: IToken[]) {
9394
const startIndex = tokensArray.findIndex((token) => token.scopes.includes(LanguageScopes.assignationStatement));
94-
const endIndex = tokensArray.length - 1;
95+
const endIndex = tokensArray.findIndex((token) => token.scopes.includes(LanguageScopes.terminatorStatement));
9596

9697
return tokensArray
9798
.filter((_, index) => index > startIndex && index < endIndex)
@@ -145,7 +146,7 @@ export default class Tokenizer {
145146
position: { line: lineIndex, character: token.startIndex },
146147
identifier: this.getRawTokenContent(line, token),
147148
tokenType: CompletionItemKind.TypeParameter,
148-
valueType: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
149+
valueType: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
149150
defaultValue: defaultValue.trim() || undefined,
150151
};
151152
});
@@ -155,7 +156,13 @@ export default class Tokenizer {
155156
const comments: string[] = [];
156157

157158
let errorSafeIndex = Math.max(index, 0);
158-
while (tokensLines[errorSafeIndex]?.at(0)?.scopes.includes(LanguageScopes.commentStatement)) {
159+
while (
160+
tokensLines[errorSafeIndex]
161+
?.at(0)
162+
?.scopes.find(
163+
(scope) => scope === LanguageScopes.commentStatement || scope === LanguageScopes.documentationCommentStatement
164+
)
165+
) {
159166
comments.unshift(lines[errorSafeIndex]);
160167
errorSafeIndex--;
161168
}
@@ -218,12 +225,12 @@ export default class Tokenizer {
218225
if (token.scopes.includes(LanguageScopes.blockTermination)) {
219226
scope.structComplexTokens.push(currentStruct);
220227
currentStruct = null;
221-
} else if (lastIndex > 2 && !token.scopes.includes(LanguageScopes.blockDeclaraction)) {
228+
} else if (lastIndex > 0 && tokensArray[1].scopes.includes(LanguageScopes.type)) {
222229
currentStruct.properties.push({
223230
position: { line: lineIndex, character: tokensArray[3].startIndex },
224231
identifier: this.getRawTokenContent(line, tokensArray[3]),
225232
tokenType: CompletionItemKind.Property,
226-
valueType: this.getTokenLanguageType(line, tokensArray[1]),
233+
valueType: this.getTokenLanguageType(line, tokensArray, 1),
227234
});
228235
}
229236

@@ -248,7 +255,7 @@ export default class Tokenizer {
248255
position: { line: lineIndex, character: token.startIndex },
249256
identifier: this.getRawTokenContent(line, token),
250257
tokenType: CompletionItemKind.Constant,
251-
valueType: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
258+
valueType: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
252259
value: this.getConstantValue(line, tokensArray),
253260
});
254261
break;
@@ -267,8 +274,8 @@ export default class Tokenizer {
267274
tokenType: CompletionItemKind.Function,
268275
returnType:
269276
tokenIndex === 0
270-
? this.getTokenLanguageType(lines[lineIndex - 1], tokensArrays[lineIndex - 1]![0])
271-
: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
277+
? this.getTokenLanguageType(lines[lineIndex - 1], tokensArrays[lineIndex - 1]!, 0)
278+
: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
272279
params: this.getFunctionParams(lineIndex, lines, tokensArrays),
273280
comments: this.getFunctionComments(lines, tokensArrays, tokenIndex === 0 ? lineIndex - 2 : lineIndex - 1),
274281
});
@@ -279,7 +286,8 @@ export default class Tokenizer {
279286
// STRUCT
280287
if (
281288
token.scopes.includes(LanguageScopes.structIdentifier) &&
282-
(lastToken.scopes.includes(LanguageScopes.structIdentifier) ||
289+
((lastToken.scopes.includes(LanguageScopes.structIdentifier) &&
290+
tokensArrays[lineIndex + 1]?.at(0)?.scopes.includes(LanguageScopes.blockDeclaraction)) ||
283291
lastToken.scopes.includes(LanguageScopes.blockDeclaraction))
284292
) {
285293
currentStruct = {
@@ -355,7 +363,7 @@ export default class Tokenizer {
355363
position: { line: lineIndex, character: token.startIndex },
356364
identifier: this.getRawTokenContent(line, token),
357365
tokenType: CompletionItemKind.Variable,
358-
valueType: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
366+
valueType: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
359367
});
360368

361369
let nextVariableToken;
@@ -372,7 +380,7 @@ export default class Tokenizer {
372380
position: { line: lineIndex, character: nextVariableToken.startIndex },
373381
identifier: this.getRawTokenContent(line, nextVariableToken),
374382
tokenType: CompletionItemKind.Variable,
375-
valueType: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
383+
valueType: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
376384
});
377385
}
378386
}
@@ -383,7 +391,7 @@ export default class Tokenizer {
383391
position: { line: lineIndex, character: token.startIndex },
384392
identifier: this.getRawTokenContent(line, token),
385393
tokenType: CompletionItemKind.TypeParameter,
386-
valueType: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
394+
valueType: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
387395
});
388396
}
389397

@@ -400,8 +408,8 @@ export default class Tokenizer {
400408
tokenType: CompletionItemKind.Function,
401409
returnType:
402410
tokenIndex === 0
403-
? this.getTokenLanguageType(lines[lineIndex - 1], tokensArrays[lineIndex - 1]![0])
404-
: this.getTokenLanguageType(line, tokensArray[tokenIndex - 2]),
411+
? this.getTokenLanguageType(lines[lineIndex - 1], tokensArrays[lineIndex - 1]!, 0)
412+
: this.getTokenLanguageType(line, tokensArray, tokenIndex - 2),
405413
params: this.getFunctionParams(lineIndex, lines, tokensArrays),
406414
comments: this.getFunctionComments(lines, tokensArrays, tokenIndex === 0 ? lineIndex - 2 : lineIndex - 1),
407415
});

server/src/Tokenizer/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export enum LanguageScopes {
2929
terminatorStatement = "punctuation.terminator.statement.nss",
3030
dotAccessStatement = "punctuation.separator.dot-access.nss",
3131
commentStatement = "punctuation.definition.comment.nss",
32+
documentationCommentStatement = "punctuation.definition.comment.documentation.nss",
3233
assignationStatement = "keyword.operator.assignment.nss",
3334
functionIdentifier = "entity.name.function.nss",
3435
functionCall = "meta.function-call.nss",

0 commit comments

Comments
 (0)