Fixing/item descriptors (#122)

* Adding correct samples, but know to fail

* Renamed

* Updated validator
This commit is contained in:
Daan Verstraten
2022-10-06 21:32:49 +02:00
committed by GitHub
parent 15fccdad64
commit bcc47d1f41
8 changed files with 193 additions and 93 deletions

View File

@@ -1,7 +1,7 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
name: Unit Test name: Pull Request
on: on:
push: push:

View File

@@ -1,21 +1,16 @@
{ {
"$id": "blockception.minecraft.item.descriptor", "$id": "blockception.minecraft.item.descriptor",
"oneOf": [ "anyOf": [
{ { "$ref": "./identifier.json" },
"$ref": "./identifier.json" { "$ref": "#/definitions/object_item_descriptor" },
},
{
"$ref": "#/definitions/object_item_descriptor"
},
{ {
"type": "object", "type": "object",
"properties": { "properties": {
"item": { "item": {
"oneOf": [ "oneOf": [
{ "type": "string", "$ref": "./identifier.json" },
{ {
"$ref": "./identifier.json" "type": "object",
},
{
"$ref": "#/definitions/object_item_descriptor" "$ref": "#/definitions/object_item_descriptor"
} }
] ]
@@ -25,19 +20,26 @@
], ],
"definitions": { "definitions": {
"object_item_descriptor": { "object_item_descriptor": {
"title": "Item Descriptor",
"description": "An object that describes an item.",
"type": "object", "type": "object",
"additionalProperties": false,
"properties": { "properties": {
"tags": { "tags": {
"type": "string",
"$ref": "../../molang/string.json", "$ref": "../../molang/string.json",
"description": "[UNDOCUMENTED] A Molang expression ran against item or block to match.", "description": "[UNDOCUMENTED] A Molang expression ran against item or block to match.",
"$comment": "UNDOCUMENTED",
"examples": ["query.any_tag('minecraft:is_tool')"] "examples": ["query.any_tag('minecraft:is_tool')"]
}, },
"item_tag": { "item_tag": {
"type": "string", "type": "string",
"description": "[UNDOCUMENTED] A tag to lookup item or block by.", "description": "[UNDOCUMENTED] A tag to lookup item or block by.",
"$comment": "UNDOCUMENTED",
"examples": ["minecraft:is_tool"] "examples": ["minecraft:is_tool"]
} }
} },
"examples": [{ "tags": "query.any_tag('minecraft:is_tool')" }, { "item_tag": "minecraft:is_tool" }]
} }
} }
} }

View File

@@ -0,0 +1,23 @@
{
"format_version": "1.13.0",
"minecraft:entity": {
"description": {
"identifier": "minecraft:sheep",
"is_spawnable": true,
"is_summonable": true,
"is_experimental": false
},
"components": {
"minecraft:behavior.tempt": {
"priority": 4,
"speed_multiplier": 1.25,
"items": [
"wheat",
{
"tags": "query.any_tag('minecraft:is_tool')"
}
]
}
}
}
}

View File

@@ -0,0 +1,28 @@
{
"format_version": "1.13.0",
"minecraft:entity": {
"description": {
"identifier": "minecraft:sheep",
"is_spawnable": true,
"is_summonable": true,
"is_experimental": false
},
"components": {
"minecraft:equippable": {
"slots": [
{
"accepted_items": [
{
"tags": "query.any_tag('minecraft:is_tool')"
}
],
"item": {
"tags": "query.any_tag('minecraft:is_tool')"
},
"slot": 0
}
]
}
}
}
}

26
test/src/Github.ts Normal file
View File

@@ -0,0 +1,26 @@
export interface ErrorAnnotation {
file?: string;
line?: number;
column?: number;
endLine?: number;
title?: string;
}
export namespace Github {
export function createError(message: string, error: ErrorAnnotation = {}): void {
const data = Object.entries(error)
.filter(([key, value]) => value !== undefined)
.map(([key, value]) => `${key}=${value}`)
.join(",");
console.log(`::error ${data}::${message}`);
}
export function startGroup(title: string): void {
console.log(`::group::${title}`);
}
export function endGroup(): void {
console.log(`::endgroup::`);
}
}

View File

@@ -3,6 +3,7 @@ import { Files } from "./Utillity";
import * as fs from "fs"; import * as fs from "fs";
import * as JSONC from "comment-json"; import * as JSONC from "comment-json";
import { expect } from "chai"; import { expect } from "chai";
import { ErrorAnnotation, Github } from "./Github";
describe("Validate", function () { describe("Validate", function () {
const folder = path.join(Files.TestFolder(), "..", "source"); const folder = path.join(Files.TestFolder(), "..", "source");
@@ -12,31 +13,22 @@ describe("Validate", function () {
files.forEach((filepath) => { files.forEach((filepath) => {
const filename = filepath.slice(folder.length); const filename = filepath.slice(folder.length);
describe(filename, function () { it(`Validating schema parts: ${filename}`, function () {
let object: JsonSchema | undefined = undefined; let object: JsonSchema | undefined = undefined;
let data: string; let data: string;
it("Can read file", function () { data = fs.readFileSync(filepath, "utf8");
data = fs.readFileSync(filepath, "utf8"); object = <JsonSchema>JSONC.parse(data);
}); expect(object).to.not.be.undefined;
expect(object).to.not.be.null;
it("Can parse to json", function () { if (!object) {
object = <JsonSchema>JSONC.parse(data); this.skip();
}); return;
}
it("Not Undefined or null", function () { const explorer = new Explorer(data, filepath);
expect(object).to.not.be.undefined; explorer.explore_refs(object, path.dirname(filepath));
expect(object).to.not.be.null;
});
it("Check refs", function () {
if (!object) {
this.skip();
return;
}
explore_refs(object, path.dirname(filepath));
});
}); });
}); });
}); });
@@ -46,24 +38,61 @@ interface JsonSchema {
[key: string]: any; [key: string]: any;
} }
function explore_refs(data: JsonSchema, folder: string): void { class Explorer {
if (data.$ref) { text: string;
const ref = data.$ref; filepath: string;
if (!ref.startsWith("#")) { constructor(text: string, filepath: string) {
const filepath = path.isAbsolute(ref) ? ref : path.join(folder, ref); this.text = text;
this.filepath = filepath;
}
expect(fs.existsSync(filepath), `ref ${ref} exists`).to.be.true; public explore_refs(data: JsonSchema, folder: string): void {
if (data.$ref) {
const ref = data.$ref;
if (!ref.startsWith("#")) {
const filepath = path.isAbsolute(ref) ? ref : path.join(folder, ref);
if (!fs.existsSync(filepath)) {
const anno = this.find(ref);
anno.title = "Ref not found";
anno.file = this.filepath;
Github.createError(`Ref not found: ${ref}`, anno);
expect.fail(`ref ${ref} does not exists`);
}
}
}
for (const key in data) {
const element = data[key];
switch (typeof element) {
case "object":
this.explore_refs(element, folder);
break;
}
} }
} }
for (const key in data) { find(data: string): ErrorAnnotation {
const element = data[key]; const index = this.text.indexOf(data);
let lines = 1;
let lastindex = 0;
switch (typeof element) { for (let i = lastindex; i < index; i++) {
case "object": const char = this.text[i];
explore_refs(element, folder);
break; if (char === "\n") {
lastindex = i;
lines++;
}
} }
return {
line: lines,
column: index - lastindex,
};
} }
} }

View File

@@ -1,4 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { Github } from "../Github";
import { Schema } from "../SchemaTester"; import { Schema } from "../SchemaTester";
import { Files } from "../Utillity"; import { Files } from "../Utillity";
@@ -14,34 +15,29 @@ describe("test correct files", function () {
.forEach((file) => { .forEach((file) => {
const testfolder = file.replace(folder + "/", ""); const testfolder = file.replace(folder + "/", "");
describe(testfolder, function () { it(`File should have a schema & validate correctly: ${testfolder}`, async function () {
const result = validator.ValidateFile(file); const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc); const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it("validation", function () { result.promise.then(
result.promise.then( (succes) => {
(succes) => { expect(succes.length, "Expected no errors got: " + succes.length).to.equal(0);
expect(succes.length, "Expected no errors got: " + succes.length).to.equal(0); succes.forEach((item) => console.log(item.message));
succes.forEach((item) => console.log(item.message)); },
}, (fail) => {
(fail) => { Github.createError("Failed on validating", { file: file });
expect.fail("Failed to validate"); expect.fail("Failed to validate");
} }
); );
schemas.then(
return result.promise; (success) => {
}); expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
},
it("schemas", function () { (fail) => {
return schemas.then( Github.createError("Failed on retrieving schemas", { file: file });
(success) => { expect.fail("failed on retrieving schemas");
expect(success.length, "Expected schemas to be returned").to.greaterThan(0); }
}, );
(fail) => {
expect.fail("failed on retrieving schemas");
}
);
});
return Promise.all([result.promise, schemas]); return Promise.all([result.promise, schemas]);
}); });

View File

@@ -1,4 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { Github } from "../Github";
import { Schema } from "../SchemaTester"; import { Schema } from "../SchemaTester";
import { Files } from "../Utillity"; import { Files } from "../Utillity";
@@ -14,33 +15,28 @@ describe("test incorrect files", function () {
.forEach((file) => { .forEach((file) => {
const testfolder = file.replace(folder + "/", ""); const testfolder = file.replace(folder + "/", "");
describe(testfolder, function () { it(`File should invalidate & have a schema: ${testfolder}`, async function () {
const result = validator.ValidateFile(file); const result = validator.ValidateFile(file);
const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc); const schemas = validator.ls.getMatchingSchemas(result.doc, result.jdoc);
it("schemas", function () { result.promise.then(
return schemas.then( (succes) => {
(success) => { expect(succes.length, "Expected errors! but had none").to.greaterThan(0);
expect(success.length, "Expected schemas to be returned").to.greaterThan(0); },
}, (fail) => {
(fail) => { Github.createError("No errors where found", { file: file });
expect.fail("failed on retrieving schemas"); expect.fail("Failed to validate");
} }
); );
}); schemas.then(
(success) => {
it("validation", function () { expect(success.length, "Expected schemas to be returned").to.greaterThan(0);
result.promise.then( },
(succes) => { (fail) => {
expect(succes.length, "Expected errors! but had none").to.greaterThan(0); Github.createError("Found no schema", { file: file });
}, expect.fail("failed on retrieving schemas");
(fail) => { }
expect.fail("Failed to validate"); );
}
);
return result.promise;
});
return Promise.all([schemas, result]); return Promise.all([schemas, result]);
}); });