Added unit tests
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@ log.txt
|
||||
out
|
||||
node_modules
|
||||
client/server
|
||||
lib
|
||||
lib/
|
||||
6
.mocharc.json
Normal file
6
.mocharc.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"ui": "bdd",
|
||||
"color": true,
|
||||
"spec": "./lib",
|
||||
"recursive": true
|
||||
}
|
||||
26
.vscode/tasks.json
vendored
26
.vscode/tasks.json
vendored
@@ -27,29 +27,11 @@
|
||||
"problemMatcher": ["$tsc"]
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "Format workspace",
|
||||
"command": "Powershell.exe -executionpolicy remotesigned -File ./scripts/format_documents.ps1",
|
||||
"problemMatcher": "$tsc"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "build extension",
|
||||
"command": "./scripts/build.bat",
|
||||
"type": "npm",
|
||||
"script": "test",
|
||||
"isBackground": false,
|
||||
"group": "build",
|
||||
"problemMatcher": "$tsc"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "install project extension",
|
||||
"command": "Powershell.exe -executionpolicy remotesigned -File ./scripts/install.ps1",
|
||||
"problemMatcher": "$tsc"
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "update project extension",
|
||||
"command": "Powershell.exe -executionpolicy remotesigned -File ./scripts/update.ps1",
|
||||
"problemMatcher": "$tsc"
|
||||
"problemMatcher": ["$tsc"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"extensions": {
|
||||
"recommendations": [
|
||||
"vscode-icons-team.vscode-icons"
|
||||
]
|
||||
}
|
||||
"folders": [{ "path": "." }],
|
||||
"settings": {
|
||||
"editor.formatOnSave": true,
|
||||
"prettier.tabWidth": 2,
|
||||
|
||||
"prettier.useTabs": false,
|
||||
"prettier.printWidth": 180,
|
||||
|
||||
"editor.useTabStops": false,
|
||||
"editor.tabSize": 2,
|
||||
|
||||
"[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.useTabStops": false, "editor.tabSize": 2 },
|
||||
"[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.useTabStops": false, "editor.tabSize": 2 },
|
||||
"[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.useTabStops": false, "editor.tabSize": 2 }
|
||||
}
|
||||
}
|
||||
80
package-lock.json
generated
80
package-lock.json
generated
@@ -8,6 +8,9 @@
|
||||
"name": "minecraft-bedrock-schemas",
|
||||
"version": "1.0.0",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"vscode-json-languageservice": "^4.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.2.21",
|
||||
"@types/mocha": "^9.0.0",
|
||||
@@ -1174,6 +1177,11 @@
|
||||
"dev": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/jsonc-parser": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.0.0.tgz",
|
||||
"integrity": "sha512-fQzRfAbIBnR0IQvftw9FJveWiHp72Fg20giDrHz6TdfB12UH/uue0D3hm57UB5KgAVuniLMCaS8P1IMj9NR7cA=="
|
||||
},
|
||||
"node_modules/loader-runner": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.2.0.tgz",
|
||||
@@ -1806,6 +1814,41 @@
|
||||
"punycode": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/vscode-json-languageservice": {
|
||||
"version": "4.1.7",
|
||||
"resolved": "https://registry.npmjs.org/vscode-json-languageservice/-/vscode-json-languageservice-4.1.7.tgz",
|
||||
"integrity": "sha512-cwG5TwZyHYthsk2aS3W1dVgVP6Vwn3o+zscwN58uMgZt/nKuyxd9vdEB1F58Ix+S5kSKAnkUCP6hvulcoImQQQ==",
|
||||
"dependencies": {
|
||||
"jsonc-parser": "^3.0.0",
|
||||
"vscode-languageserver-textdocument": "^1.0.1",
|
||||
"vscode-languageserver-types": "^3.16.0",
|
||||
"vscode-nls": "^5.0.0",
|
||||
"vscode-uri": "^3.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"npm": ">=7.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/vscode-languageserver-textdocument": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.1.tgz",
|
||||
"integrity": "sha512-UIcJDjX7IFkck7cSkNNyzIz5FyvpQfY7sdzVy+wkKN/BLaD4DQ0ppXQrKePomCxTS7RrolK1I0pey0bG9eh8dA=="
|
||||
},
|
||||
"node_modules/vscode-languageserver-types": {
|
||||
"version": "3.16.0",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz",
|
||||
"integrity": "sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA=="
|
||||
},
|
||||
"node_modules/vscode-nls": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/vscode-nls/-/vscode-nls-5.0.0.tgz",
|
||||
"integrity": "sha512-u0Lw+IYlgbEJFF6/qAqG2d1jQmJl0eyAGJHoAJqr2HT4M2BNuQYSEiSE75f52pXHSJm8AlTjnLLbBFPrdz2hpA=="
|
||||
},
|
||||
"node_modules/vscode-uri": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.0.2.tgz",
|
||||
"integrity": "sha512-jkjy6pjU1fxUvI51P+gCsxg1u2n8LSt0W6KrCNQceaziKzff74GoWmjVG46KieVzybO1sttPQmYfrwSHey7GUA=="
|
||||
},
|
||||
"node_modules/watchpack": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.2.0.tgz",
|
||||
@@ -3038,6 +3081,11 @@
|
||||
"dev": true,
|
||||
"peer": true
|
||||
},
|
||||
"jsonc-parser": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.0.0.tgz",
|
||||
"integrity": "sha512-fQzRfAbIBnR0IQvftw9FJveWiHp72Fg20giDrHz6TdfB12UH/uue0D3hm57UB5KgAVuniLMCaS8P1IMj9NR7cA=="
|
||||
},
|
||||
"loader-runner": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.2.0.tgz",
|
||||
@@ -3471,6 +3519,38 @@
|
||||
"punycode": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"vscode-json-languageservice": {
|
||||
"version": "4.1.7",
|
||||
"resolved": "https://registry.npmjs.org/vscode-json-languageservice/-/vscode-json-languageservice-4.1.7.tgz",
|
||||
"integrity": "sha512-cwG5TwZyHYthsk2aS3W1dVgVP6Vwn3o+zscwN58uMgZt/nKuyxd9vdEB1F58Ix+S5kSKAnkUCP6hvulcoImQQQ==",
|
||||
"requires": {
|
||||
"jsonc-parser": "^3.0.0",
|
||||
"vscode-languageserver-textdocument": "^1.0.1",
|
||||
"vscode-languageserver-types": "^3.16.0",
|
||||
"vscode-nls": "^5.0.0",
|
||||
"vscode-uri": "^3.0.2"
|
||||
}
|
||||
},
|
||||
"vscode-languageserver-textdocument": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.1.tgz",
|
||||
"integrity": "sha512-UIcJDjX7IFkck7cSkNNyzIz5FyvpQfY7sdzVy+wkKN/BLaD4DQ0ppXQrKePomCxTS7RrolK1I0pey0bG9eh8dA=="
|
||||
},
|
||||
"vscode-languageserver-types": {
|
||||
"version": "3.16.0",
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0.tgz",
|
||||
"integrity": "sha512-k8luDIWJWyenLc5ToFQQMaSrqCHiLwyKPHKPQZ5zz21vM+vIVUSvsRpcbiECH4WR88K2XZqc4ScRcZ7nk/jbeA=="
|
||||
},
|
||||
"vscode-nls": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/vscode-nls/-/vscode-nls-5.0.0.tgz",
|
||||
"integrity": "sha512-u0Lw+IYlgbEJFF6/qAqG2d1jQmJl0eyAGJHoAJqr2HT4M2BNuQYSEiSE75f52pXHSJm8AlTjnLLbBFPrdz2hpA=="
|
||||
},
|
||||
"vscode-uri": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.0.2.tgz",
|
||||
"integrity": "sha512-jkjy6pjU1fxUvI51P+gCsxg1u2n8LSt0W6KrCNQceaziKzff74GoWmjVG46KieVzybO1sttPQmYfrwSHey7GUA=="
|
||||
},
|
||||
"watchpack": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.2.0.tgz",
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha -r ts-node/register '**/*.test.ts'",
|
||||
"compile": "tsc -b",
|
||||
"test": "mocha --debug-brk",
|
||||
"pretest": "npm run compile",
|
||||
"update": "npm update && npm audit fix"
|
||||
},
|
||||
"repository": {
|
||||
@@ -32,5 +34,8 @@
|
||||
"mocha": "^9.0.3",
|
||||
"ts-loader": "^9.2.5",
|
||||
"ts-node": "^10.2.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-json-languageservice": "^4.1.7"
|
||||
}
|
||||
}
|
||||
|
||||
0
src/index.ts
Normal file
0
src/index.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
getLanguageService,
|
||||
LanguageService,
|
||||
LanguageSettings,
|
||||
SchemaConfiguration,
|
||||
TextDocument,
|
||||
JSONDocument,
|
||||
Diagnostic,
|
||||
Thenable,
|
||||
} from "vscode-json-languageservice";
|
||||
import * as url from "url";
|
||||
import * as data from "../../vscode-settings.json";
|
||||
import { readFileSync } from "fs";
|
||||
import { Files } from "./Utillity";
|
||||
|
||||
export namespace Schema {
|
||||
const workspaceContext = {
|
||||
resolveRelativePath: (relativePath: string, resource: string) => {
|
||||
return url.resolve(resource, relativePath);
|
||||
},
|
||||
};
|
||||
|
||||
export function GetValidator(): Validator {
|
||||
const ls = GetLanguageService();
|
||||
ls.configure(GetLanguageSettings());
|
||||
|
||||
const out = new Validator(ls);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function GetLanguageService(): LanguageService {
|
||||
return getLanguageService({ workspaceContext });
|
||||
}
|
||||
|
||||
export function GetLanguageSettings(): LanguageSettings {
|
||||
const schemas: SchemaConfiguration[] = [];
|
||||
const settings: LanguageSettings = { schemas: schemas };
|
||||
let rootfolder = Files.RootFolder();
|
||||
|
||||
if (!rootfolder.endsWith("/")) rootfolder += "/";
|
||||
|
||||
data["json.schemas"].forEach((m) => {
|
||||
if (m) {
|
||||
const schema = m.url.replace("https://raw.githubusercontent.com/Blockception/Minecraft-bedrock-json-schemas/main/", rootfolder);
|
||||
|
||||
schemas.push({ uri: schema, fileMatch: m.fileMatch });
|
||||
}
|
||||
});
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
export class Validator {
|
||||
readonly ls: LanguageService;
|
||||
|
||||
constructor(ls: LanguageService) {
|
||||
this.ls = ls;
|
||||
}
|
||||
|
||||
ValidateFile(uri: string): Result {
|
||||
const content = readFileSync(uri).toString();
|
||||
return this.ValidateContent(content, uri, "json");
|
||||
}
|
||||
|
||||
ValidateJson(data: any, fakeuri: string = "", langid: string = "json"): Result {
|
||||
return this.ValidateContent(JSON.stringify(data), fakeuri, langid);
|
||||
}
|
||||
|
||||
ValidateContent(json: string, fakeuri: string = "", langid: string = "json"): Result {
|
||||
const doc = TextDocument.create(fakeuri, langid, 0, json);
|
||||
const jdoc = this.ls.parseJSONDocument(doc);
|
||||
|
||||
const p = this.ls.doValidation(doc, jdoc, { comments: "ignore" });
|
||||
|
||||
return new Result(doc, jdoc, p);
|
||||
}
|
||||
}
|
||||
|
||||
export class Result {
|
||||
readonly doc: TextDocument;
|
||||
readonly jdoc: JSONDocument;
|
||||
readonly promise: Thenable<Diagnostic[]>;
|
||||
|
||||
constructor(doc: TextDocument, jdoc: JSONDocument, promise: Thenable<Diagnostic[]>) {
|
||||
this.doc = doc;
|
||||
this.jdoc = jdoc;
|
||||
this.promise = promise;
|
||||
}
|
||||
}
|
||||
|
||||
20
test/src/Utillity.test.ts
Normal file
20
test/src/Utillity.test.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { expect } from "chai";
|
||||
import { Files } from "./Utillity";
|
||||
|
||||
describe("files", () => {
|
||||
it("Root", () => {
|
||||
const temp = Files.RootFolder();
|
||||
console.log(temp);
|
||||
|
||||
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
|
||||
expect(temp.endsWith("lib\\test"), "ended with lib\\test").to.be.false;
|
||||
expect(temp.endsWith("lib/test"), "ended with lib/test").to.be.false;
|
||||
});
|
||||
|
||||
it("Test", () => {
|
||||
const temp = Files.TestFolder();
|
||||
console.log(temp);
|
||||
|
||||
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,28 @@
|
||||
import path from "path";
|
||||
import FastGlob = require("fast-glob");
|
||||
import path = require("path");
|
||||
|
||||
export namespace Files {
|
||||
export function TestFolder(): string {
|
||||
return __dirname;
|
||||
}
|
||||
export function TestFolder(): string {
|
||||
return path.join(__dirname, "..", "..", "test");
|
||||
}
|
||||
|
||||
export function FilesFolder(): string {
|
||||
return path.join(TestFolder(), "files");
|
||||
}
|
||||
export function RootFolder(): string {
|
||||
return path.join(TestFolder(), "..");
|
||||
}
|
||||
|
||||
export function FilesFolder(): string {
|
||||
return path.join(TestFolder(), "files");
|
||||
}
|
||||
|
||||
export function CorrectFilesFolder(): string {
|
||||
return path.join(FilesFolder(), "correct");
|
||||
}
|
||||
|
||||
export function InCorrectFilesFolder(): string {
|
||||
return path.join(FilesFolder(), "incorrect");
|
||||
}
|
||||
|
||||
export function GetFiles(folder: string): string[] {
|
||||
return FastGlob.sync(folder, { absolute: true, onlyFiles: true });
|
||||
}
|
||||
}
|
||||
|
||||
30
test/src/files/validate.test.ts
Normal file
30
test/src/files/validate.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { expect } from "chai";
|
||||
import { Schema } from "../SchemaTester";
|
||||
import { Files } from "../Utillity";
|
||||
|
||||
describe("test correct files", () => {
|
||||
const folder = Files.CorrectFilesFolder();
|
||||
const files = Files.GetFiles(folder);
|
||||
const validator = Schema.GetValidator();
|
||||
|
||||
files.forEach((file) => {
|
||||
if (file.endsWith(".json")) {
|
||||
const testfolder = file.replace(folder, "");
|
||||
|
||||
test(testfolder, (done) => {
|
||||
let result = validator.ValidateFile(file);
|
||||
|
||||
result.promise.then(
|
||||
(succes) => {
|
||||
expect(succes.length, "Expected no errors got: " + JSON.stringify(succes)).to.equal(0);
|
||||
done();
|
||||
},
|
||||
(fail) => {
|
||||
expect.fail("Failed to validate");
|
||||
done();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
13
tsconfig.json
Normal file
13
tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"include": ["src", "test"],
|
||||
"exclude": ["node_modules", "**/__tests__/*"],
|
||||
"compilerOptions": {
|
||||
"target": "es2019",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"outDir": "./lib",
|
||||
"strict": true,
|
||||
"resolveJsonModule": true,
|
||||
"sourceMap": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user