Added unit tests
This commit is contained in:
@@ -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();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user