Feature/jest2 (#322)

* Adding more tests
* adding jest
* fixing tests
* fixing linting
* removing 1 slash less
* fixing more tests
* fixing last test
* more last fixes
This commit is contained in:
Daan Verstraten
2024-11-09 10:08:30 +01:00
committed by GitHub
parent b788f118ff
commit 8e59f63167
39 changed files with 8224 additions and 1158 deletions

View File

@@ -1,16 +0,0 @@
{
"format_version": "1.16.200",
"minecraft:block": {
"description": {
"identifier": "namespace:block",
"properties": {}
},
"components": {},
"events": {
"minecraft:on_fall_on": {
"add_mob_effect": {},
"sequence": [{ "die": {} }]
}
}
}
}

View File

@@ -1,10 +1,8 @@
{
"format_version": "1.20.41",
"minecraft:npc_dialogue": {
"format_version": "1.19.50",
"minecraft:camera_preset": {
"identifier": "example:custom",
"inherit_from": "minecraft:free",
"player_effects": true,
"listener": "player",
"pos_x": 30,
"pos_y": 90,
"pos_z": -20,

View File

@@ -11,11 +11,7 @@
"minecraft:equippable": {
"slots": [
{
"accepted_items": [
{
"tags": "query.any_tag('minecraft:is_tool')"
}
],
"accepted_items": ["minecraft:tool"],
"item": {
"tags": "query.any_tag('minecraft:is_tool')"
},

View File

@@ -1,9 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:scatter_feature": {
"description": {
"identifier": "namespace:entity_name"
}
}
}

View File

@@ -1,19 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:beards_and_shavers": {
"description": {
"identifier": "beards_and_shavers_features:beards_and_shavers_feature"
},
"places_feature": "beards_and_shavers_features:beards_and_shavers_feature_obsidian",
"y_delta": 0,
"bounding_box_min": [-4, 0, -4],
"bounding_box_max": [5, 12, 5],
"beard_raggedness_min": 0.25,
"beard_raggedness_max": 0.5,
"surface_block_type": "minecraft:grass",
"subsurface_block_type": "minecraft:dirt"
}
}

View File

@@ -1,17 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:conditional_list": {
"description": {
"identifier": "conditional_list_features:conditional_list_feature"
},
"conditional_features": [
{
"places_feature": "conditional_list_features:conditional_list_feature_obsidian",
"condition": "query.noise(v.originx, v.originz) < 0"
}
],
"early_out_scheme": "placement_success"
}
}

View File

@@ -1,11 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:hell_cave_carver_feature": {
"description": {
"identifier": "hell_cave_carver_features:hell_cave_carver_feature"
},
"fill_with": "minecraft:planks",
"width_modifier": 1
}
}

View File

@@ -1,21 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:rect_layout": {
"description": {
"identifier": "rect_layout_features:rect_layout_feature"
},
"ratio_of_empty_space": 0.5,
"feature_areas":[
{
"feature": "rect_layout_features:rect_layout_feature_obsidian",
"area_dimensions": [1, 1]
},
{
"feature": "rect_layout_features:rect_layout_feature_planks",
"area_dimensions": [1, 1]
}
]
}
}

View File

@@ -1,11 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:scan_surface": {
"description": {
"identifier": "scan_surface_features:scan_surface_feature"
},
"scan_surface_feature": "scan_surface_features:scan_surface_feature_obsidian"
}
}

View File

@@ -1,14 +1,14 @@
{
"format_version": "1.13.0",
"format_version": "1.13.0",
"minecraft:single_block_feature": {
"description": {
"identifier": "scan_surface_features:scan_surface_feature_obsidian"
},
"minecraft:single_block_feature": {
"description": {
"identifier": "scan_surface_features:scan_surface_feature_obsidian"
},
"places_block": "minecraft:obsidian",
"places_block": "minecraft:obsidian",
"enforce_placement_rules": false,
"enforce_survivability_rules": false
}
}
"enforce_placement_rules": false,
"enforce_survivability_rules": false
}
}

View File

@@ -1,31 +0,0 @@
{
"format_version": "1.13.0",
"minecraft:scatter_feature": {
"description": {
"identifier": "scatter_and_singleblock_features:scatter_feature"
},
"places_feature": "scatter_and_singleblock_features:scatter_feature_obsidian",
"scatter_chance": {
"numerator": 2,
"denominator": 3
},
"iterations": "math.pow(2, 4)",
"coordinate_eval_order": "zxy",
"project_input_to_floor": true,
"z": {
"distribution": "fixed_grid",
"extent": [10, 15],
"step_size": 2,
"grid_offset": 3
},
"x": {
"distribution": "gaussian",
"extent": ["(v.worldx < 12) * 2", 16]
},
"y": 0
}
}

View File

@@ -15,7 +15,9 @@
"minecraft:damage": 2,
"minecraft:stacked_by_data": true,
"minecraft:should_despawn": true,
"minecraft:hover_text_color": "aqua",
"minecraft:hover_text_color": {
},
"minecraft:use_animation": "brush",
"minecraft:use_modifiers": {
"use_duration": 2

View File

@@ -7,7 +7,7 @@
"type": "item",
"name": "minecraft:diamond_sword",
"weight": 1,
"functions": [{ "function": "specific_enchants", "enchants": [{ "id": "power", "level": 3 }, "power"] }]
"functions": [{ "function": "specific_enchants", "enchants": [{ "id": "power", "level": 3 }] }]
}
]
}

View File

@@ -1,18 +1,24 @@
{
"tiers": [
{
"trades": [
"total_exp_required": 5,
"groups": [
{
"wants": [
"num_to_select": 2,
"trades": [
{
"item": "minecraft:wheat",
"quantity": {
"min": 18,
"max": 22
}
"wants": [
{
"item": "minecraft:wheat",
"quantity": {
"min": 18,
"max": 22
}
}
],
"gives": [{ "item": "minecraft:emerald" }]
}
],
"gives": [{ "item": "minecraft:emerald" }]
]
}
]
}

View File

@@ -1,6 +1,6 @@
{
"Example": {
"textures": "example",
"sound": "obsidian"
"textures": "test",
"sound": "amethyst_block"
}
}
}

View File

@@ -6,7 +6,7 @@
"header": {
"name": "Hell Cave Carver Features",
"uuid": "459909b9-fdb8-4eb3-9b8d-55d09545b89d",
"uuid": "459909b9-fdb8-4eb3-9b8d-55d09545b89h",
"description": "Tests hell cave carver features",
"version": [0, 0, 1],
"min_engine_version": [1, 17, 0]

View File

@@ -6,7 +6,7 @@
"header": {
"name": "Hell Cave Carver Features",
"uuid": "459909b9-fdb8-4eb3-9b8d-55d09545b89d",
"uuid": "459909b9-fdb8-4eb3-9b8d-55d09545b89h",
"description": "Tests hell cave carver features",
"version": [1, 0, 0],
"min_engine_version": [1, 17, 0]

View File

@@ -1,45 +1,23 @@
import { expect } from "chai";
import { Github } from "../github";
import { Schema } from "../schema-tester";
import { Files } from "../utillity";
describe("test correct files", function () {
const folder = Files.CorrectFilesFolder().replace(/\\/gi, "/");
const files = Files.GetFiles(folder);
const files = Files.GetFiles(folder).filter((f) => f.endsWith(".json"));
const validator = Schema.GetValidator();
expect(files.length, "No files were returned").to.greaterThan(0);
expect(files.length).toBeGreaterThan(0);
files
.filter((f) => f.endsWith(".json"))
.forEach((file) => {
const testfolder = file.replace(folder + "/", "");
test.each(files)("File should have a schema & validate correctly: %s", async (file) => {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it(`File should have a schema & validate correctly: ${testfolder}`, async function () {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
const succes = await result.promise;
result.promise.then(
(succes) => {
expect(succes.length, "Expected no errors got: " + succes.length).to.equal(0);
succes.forEach((item) => console.log(item.message));
},
(fail) => {
Github.createError("Failed on validating", { file: file });
expect.fail("Failed to validate");
}
);
schemas.then(
(success) => {
expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
},
(fail) => {
Github.createError("Failed on retrieving schemas", { file: file });
expect.fail("failed on retrieving schemas");
}
);
expect(succes).toHaveLength(0);
succes.forEach((item) => console.log(item.message));
return Promise.all([result.promise, schemas]);
});
});
const s = await schemas;
expect(s.length).toBeGreaterThan(0);
});
});

View File

@@ -1,44 +1,20 @@
import { expect } from "chai";
import { Github } from "../github";
import { Schema } from "../schema-tester";
import { Files } from "../utillity";
describe("test incorrect files", function () {
const folder = Files.InCorrectFilesFolder().replace(/\\/gi, "/");
const files = Files.GetFiles(folder);
const files = Files.GetFiles(folder).filter((f) => f.endsWith(".json"));
const validator = Schema.GetValidator();
expect(files.length, "No files were returned").to.greaterThan(0);
expect(files.length).toBeGreaterThan(0);
files
.filter((f) => f.endsWith(".json"))
.forEach((file) => {
const testfolder = file.replace(folder + "/", "");
test.each(files)("File should invalidate & have a schema: %s", async (file) => {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it(`File should invalidate & have a schema: ${testfolder}`, async function () {
const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
result.promise.then(
(succes) => {
expect(succes.length, "Expected errors! but had none").to.greaterThan(0);
},
() => {
Github.createError("No errors where found", { file: file });
expect.fail("Failed to validate");
}
);
schemas.then(
(success) => {
expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
},
() => {
Github.createError("Found no schema", { file: file });
expect.fail("failed on retrieving schemas");
}
);
return Promise.all([schemas, result]);
});
});
const success = await result.promise;
expect(success.length).toBeGreaterThan(0);
const s = await schemas;
expect(s.length).toBeGreaterThan(0);
});
});

View File

@@ -9,7 +9,7 @@ export interface ErrorAnnotation {
export namespace Github {
export function createError(message: string, error: ErrorAnnotation = {}): void {
const data = Object.entries(error)
.filter(([key, value]) => value !== undefined)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${key}=${value}`)
.join(",");

View File

@@ -1,7 +1,8 @@
import { getLanguageService, LanguageService, LanguageSettings, SchemaConfiguration, TextDocument, JSONDocument, Diagnostic, Thenable } from "vscode-json-languageservice";
import { existsSync, readFileSync } from "fs";
import * as path from "path";
import * as url from "url";
import { Diagnostic, getLanguageService, JSONDocument, LanguageService, LanguageSettings, SchemaConfiguration, TextDocument, Thenable } from "vscode-json-languageservice";
import * as data from "../../vscode-settings.json";
import { readFileSync } from "fs";
import { Files } from "./utillity";
export namespace Schema {
@@ -21,26 +22,41 @@ export namespace Schema {
}
export function GetLanguageService(): LanguageService {
return getLanguageService({ workspaceContext });
return getLanguageService({
workspaceContext,
schemaRequestService: getSchema,
});
}
export async function getSchema(uri: string): Promise<string> {
const rootfolder = Files.RootFolder();
const filepath = uri.replace("https://raw.githubusercontent.com/Blockception/Minecraft-bedrock-json-schemas/main", rootfolder);
if (!existsSync(filepath)) {
throw new Error("file doesn't exist: " + filepath);
}
return readFileSync(filepath).toString();
}
export function GetLanguageSettings(): LanguageSettings {
const schemas: SchemaConfiguration[] = [];
const settings: LanguageSettings = { schemas: schemas };
const settings: LanguageSettings = {
schemas: schemas,
};
let rootfolder = Files.RootFolder();
if (!rootfolder.endsWith("/")) rootfolder += "/";
rootfolder = path.normalize(rootfolder);
data["json.schemas"].forEach((m) => {
if (m) {
const schema = m.url.replace("https://raw.githubusercontent.com/Blockception/Minecraft-bedrock-json-schemas/main/", rootfolder);
let matches = m.fileMatch;
if (typeof matches === "string") {
matches = [matches];
}
schemas.push({ uri: schema, fileMatch: matches });
schemas.push({ uri: m.url, fileMatch: matches });
}
});

View File

@@ -1,19 +1,17 @@
import { expect } from "chai";
import { DummyFiles } from "../../src/main";
import { Files } from "./utillity";
describe("Files", function () {
it("Root", function () {
const temp = Files.RootFolder();
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;
expect(temp.endsWith("lib")).toBeFalsy();
expect(temp.endsWith("lib\\test")).toBeFalsy();
expect(temp.endsWith("lib/test")).toBeFalsy();
});
it("Test", function () {
const temp = Files.TestFolder();
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
expect(temp.endsWith("lib")).toBeFalsy();
});
});

View File

@@ -1,5 +1,5 @@
import FastGlob = require("fast-glob");
import path = require("path");
import * as FastGlob from "fast-glob";
import * as path from "path";
export namespace Files {
export function TestFolder(): string {

View File

@@ -1,35 +1,24 @@
import path = require("path");
import { Files } from "./utillity";
import * as fs from "fs";
import * as JSONC from "comment-json";
import { expect } from "chai";
import * as fs from "fs";
import * as path from "path";
import { ErrorAnnotation, Github } from "./github";
import { Files } from "./utillity";
describe("Validate", function () {
const folder = path.join(Files.TestFolder(), "..", "source");
console.log(folder);
const files = Files.GetFiles(folder);
expect(files.length).toBeGreaterThan(0);
files.forEach((filepath) => {
const filename = filepath.slice(folder.length);
test.each(files)("Validating schema parts: %s", (filepath) => {
const data = fs.readFileSync(filepath, "utf8");
const object = JSONC.parse(data) as JsonSchema;
expect(object).toBeDefined();
if (!object) {
return;
}
it(`Validating schema parts: ${filename}`, function () {
let object: JsonSchema | undefined = undefined;
let data: string;
data = fs.readFileSync(filepath, "utf8");
object = <JsonSchema>JSONC.parse(data);
expect(object).to.not.be.undefined;
expect(object).to.not.be.null;
if (!object) {
this.skip();
return;
}
const explorer = new Explorer(data, filepath);
explorer.explore_refs(object, path.dirname(filepath));
});
const explorer = new Explorer(data, filepath);
explorer.explore_refs(object, path.dirname(filepath));
});
});
@@ -60,7 +49,7 @@ class Explorer {
anno.file = this.filepath;
Github.createError(`Ref not found: ${ref}`, anno);
expect.fail(`ref ${ref} does not exists`);
throw new Error(`ref ${ref} does not exists`);
}
}
}