adding tests

This commit is contained in:
Yanick Champoux 2022-04-26 14:37:16 -04:00
parent ebec834752
commit 85674731ad
4 changed files with 77 additions and 1 deletions

View File

@ -25,3 +25,7 @@ tasks:
-c ajv-keywords \
-s schemas-yaml/character.yml \
-d {{.CLI_ARGS}}
test:
deps: [ schemas ]
cmds:
- vitest run

View File

@ -15,9 +15,11 @@
"author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)",
"license": "ISC",
"devDependencies": {
"prettier": "^2.6.2"
"prettier": "^2.6.2",
"vitest": "^0.10.0"
},
"dependencies": {
"ajv": "^8.11.0",
"ajv-cli": "^5.0.0",
"ajv-keywords": "^5.1.0"
}

View File

@ -0,0 +1,50 @@
{
"$defs" : {
"statistic" : {
"maximum" : 20,
"minimum" : 1,
"type" : "number"
}
},
"$id" : "https://hyperboria.babyl.ca/character.json",
"additionalProperties" : false,
"properties" : {
"name" : {
"type" : "string"
},
"player" : {
"type" : "string"
},
"statistics" : {
"allRequired" : true,
"properties" : {
"charisma" : {
"$ref" : "#/$defs/statistic"
},
"constitution" : {
"$ref" : "#/$defs/statistic"
},
"dexterity" : {
"$ref" : "#/$defs/statistic"
},
"intelligence" : {
"$ref" : "#/$defs/statistic"
},
"strength" : {
"$ref" : "#/$defs/statistic"
},
"wisdom" : {
"$ref" : "#/$defs/statistic"
}
},
"type" : "object"
}
},
"required" : [
"name",
"player",
"statistics"
],
"title" : "Hyperboria character sheet",
"type" : "object"
}

20
src/statistics.test.js Normal file
View File

@ -0,0 +1,20 @@
import { test, expect } from "vitest";
import Ajv from "ajv";
import characterSchema from "../schemas-json/character.json";
const ajv = new Ajv();
const validate = ajv.compile(characterSchema.$defs.statistic);
test("good statistic", () => {
expect(validate(12)).toBeTruthy();
expect(validate.errors).toBeNull();
});
test("bad statistic", () => {
expect(validate(21)).toBeFalsy();
expect(validate.errors[0]).toMatchObject({
message: "must be <= 20",
});
});