Fix tests

This commit is contained in:
Pieter Vander Vennet 2022-02-19 02:45:15 +01:00
parent d9d6e96fed
commit 04a2cc9bc7
18 changed files with 71 additions and 72 deletions

View file

@ -612,7 +612,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -610,7 +610,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -456,7 +456,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -454,7 +454,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -256,7 +256,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -254,7 +254,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -260,7 +260,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -258,7 +258,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -96,7 +96,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -96,7 +96,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -204,7 +204,7 @@
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -202,7 +202,7 @@ export default {
] ]
}, },
"then": { "then": {
"description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\ntype: rendered" "description": "If the condition `if` is met, the text `then` will be rendered.\nIf not known yet, the user will be presented with `then` as an option\nType: rendered"
}, },
"icon": { "icon": {
"description": "An icon supporting this mapping; typically shown pretty small\nType: icon", "description": "An icon supporting this mapping; typically shown pretty small\nType: icon",

View file

@ -7,6 +7,11 @@ import * as tagrenderingmetapaths from "../../../assets/tagrenderingconfigmeta.j
export class ExtractImages extends Conversion<LayoutConfigJson, string[]> { export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
private _isOfficial: boolean; private _isOfficial: boolean;
private _sharedTagRenderings: Map<string, any>; private _sharedTagRenderings: Map<string, any>;
private static readonly layoutMetaPaths = (metapaths["default"] ?? metapaths).filter(mp => mp.typeHint !== undefined && (mp.typeHint === "image" || mp.typeHint === "icon"))
private static readonly tagRenderingMetaPaths = (tagrenderingmetapaths["default"] ?? tagrenderingmetapaths).filter(trpath => trpath.typeHint !== "rendered")
constructor(isOfficial: boolean, sharedTagRenderings: Map<string, any>) { constructor(isOfficial: boolean, sharedTagRenderings: Map<string, any>) {
super("Extract all images from a layoutConfig using the meta paths",[],"ExctractImages"); super("Extract all images from a layoutConfig using the meta paths",[],"ExctractImages");
this._isOfficial = isOfficial; this._isOfficial = isOfficial;
@ -14,25 +19,17 @@ export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
} }
convert(json: LayoutConfigJson, context: string): { result: string[], errors: string[], warnings: string[] } { convert(json: LayoutConfigJson, context: string): { result: string[], errors: string[], warnings: string[] } {
const paths = metapaths["default"] ?? metapaths const allFoundImages = []
const trpaths = tagrenderingmetapaths["default"] ?? tagrenderingmetapaths
const allFoundImages = []
const errors = [] const errors = []
const warnings = [] const warnings = []
for (const metapath of paths) { for (const metapath of ExtractImages.layoutMetaPaths) {
if (metapath.typeHint === undefined) {
continue
}
if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") {
continue
}
const mightBeTr = Array.isArray(metapath.type) && metapath.type.some(t => t["$ref"] == "#/definitions/TagRenderingConfigJson") const mightBeTr = Array.isArray(metapath.type) && metapath.type.some(t => t["$ref"] == "#/definitions/TagRenderingConfigJson")
const found = Utils.CollectPath(metapath.path, json) const found = Utils.CollectPath(metapath.path, json)
if (mightBeTr) { if (mightBeTr) {
// We might have tagRenderingConfigs containing icons here // We might have tagRenderingConfigs containing icons here
for (const {path, leaf} of found) { for (const el of found) {
const foundImage = leaf; const path = el.path
const foundImage = el.leaf;
if (typeof foundImage === "string") { if (typeof foundImage === "string") {
if(foundImage == ""){ if(foundImage == ""){
@ -47,17 +44,14 @@ export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
allFoundImages.push(foundImage) allFoundImages.push(foundImage)
} else{ } else{
// This is a tagRendering where every rendered value might be an icon! // This is a tagRendering where every rendered value might be an icon!
for (const trpath of trpaths) { for (const trpath of ExtractImages.tagRenderingMetaPaths) {
if (trpath.typeHint !== "rendered") {
continue
}
const fromPath = Utils.CollectPath(trpath.path, foundImage) const fromPath = Utils.CollectPath(trpath.path, foundImage)
for (const img of fromPath) { for (const img of fromPath) {
if (typeof img !== "string") { if (typeof img.leaf !== "string") {
(this._isOfficial ? errors: warnings).push(context+": found an image path that is not a path at " + context + "." + metapath.path.join(".") + ": " + JSON.stringify(img)) (this._isOfficial ? errors: warnings).push(context+": found an image path that is not a path at " + context + "." + img.path.join(".") + ": " + JSON.stringify(img))
} }
} }
allFoundImages.push(...fromPath.filter(i => typeof i === "string")) allFoundImages.push(...fromPath.map(i => i.leaf).filter(i => typeof i=== "string"))
for (const pathAndImg of fromPath) { for (const pathAndImg of fromPath) {
if(pathAndImg.leaf === "" || pathAndImg.leaf["path"] == ""){ if(pathAndImg.leaf === "" || pathAndImg.leaf["path"] == ""){
errors.push(context+[...path,...pathAndImg.path].join(".")+": Found an empty image at ") errors.push(context+[...path,...pathAndImg.path].join(".")+": Found an empty image at ")
@ -68,7 +62,7 @@ export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
} }
} }
} else { } else {
allFoundImages.push(...found) allFoundImages.push(...found.map(i => i.leaf))
} }
} }

View file

@ -115,7 +115,7 @@ export interface TagRenderingConfigJson {
/** /**
* If the condition `if` is met, the text `then` will be rendered. * If the condition `if` is met, the text `then` will be rendered.
* If not known yet, the user will be presented with `then` as an option * If not known yet, the user will be presented with `then` as an option
* type: rendered * Type: rendered
*/ */
then: string | any, then: string | any,
/** /**

View file

@ -304,7 +304,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
if (target === null) { if (target === null) {
return source return source
} }
for (const key in source) { for (const key in source) {
if (!source.hasOwnProperty(key)) { if (!source.hasOwnProperty(key)) {
continue continue
@ -364,9 +364,9 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
// We have reached the leaf // We have reached the leaf
const leaf = object[head]; const leaf = object[head];
if (leaf !== undefined) { if (leaf !== undefined) {
if(Array.isArray(leaf)){ if (Array.isArray(leaf)) {
object[head] = leaf.map(o => replaceLeaf(o, travelledPath)) object[head] = leaf.map(o => replaceLeaf(o, travelledPath))
}else{ } else {
object[head] = replaceLeaf(leaf, travelledPath) object[head] = replaceLeaf(leaf, travelledPath)
} }
} }
@ -381,10 +381,10 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return; return;
} }
if (Array.isArray(sub)) { if (Array.isArray(sub)) {
sub.forEach((el, i) => Utils.WalkPath(path.slice(1), el, replaceLeaf, [...travelledPath, head, ""+i])) sub.forEach((el, i) => Utils.WalkPath(path.slice(1), el, replaceLeaf, [...travelledPath, head, "" + i]))
return; return;
} }
Utils.WalkPath(path.slice(1), sub, replaceLeaf, [...travelledPath,head]) Utils.WalkPath(path.slice(1), sub, replaceLeaf, [...travelledPath, head])
} }
/** /**
@ -393,22 +393,26 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
* *
* The leaf objects are collected in the list * The leaf objects are collected in the list
*/ */
public static CollectPath(path: string[], object: any, collectedList: {leaf: any, path: string[]}[] = [], travelledPath: string[] = []): {leaf: any, path: string[]}[] { public static CollectPath(path: string[], object: any, collectedList: { leaf: any, path: string[] }[] = [], travelledPath: string[] = []): { leaf: any, path: string[] }[] {
if (object === undefined || object === null) { if (object === undefined || object === null) {
return collectedList; return collectedList;
} }
const head = path[0] const head = path[0]
travelledPath = [...travelledPath, head]
if (path.length === 1) { if (path.length === 1) {
// We have reached the leaf // We have reached the leaf
const leaf = object[head]; const leaf = object[head];
if (leaf === undefined || leaf === null) { if (leaf === undefined || leaf === null) {
return collectedList return collectedList
} }
if (Array.isArray(leaf)) { if (Array.isArray(leaf)) {
collectedList.push(...leaf) for (let i = 0; i < (<any[]>leaf).length; i++){
} else { const l = (<any[]>leaf)[i];
collectedList.push(leaf) collectedList.push({leaf: l, path: [...travelledPath, ""+i]})
} }
} else {
collectedList.push({leaf, path: travelledPath})
}
return collectedList return collectedList
} }
const sub = object[head] const sub = object[head]
@ -417,13 +421,13 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
} }
if (Array.isArray(sub)) { if (Array.isArray(sub)) {
sub.forEach((el, i) => Utils.CollectPath(path.slice(1), el, collectedList, [...travelledPath,head,""+i])) sub.forEach((el, i) => Utils.CollectPath(path.slice(1), el, collectedList, [...travelledPath, "" + i]))
return collectedList; return collectedList;
} }
if (typeof sub !== "object") { if (typeof sub !== "object") {
return collectedList; return collectedList;
} }
return Utils.CollectPath(path.slice(1), sub, collectedList,[...travelledPath, head]) return Utils.CollectPath(path.slice(1), sub, collectedList, travelledPath)
} }
/** /**
@ -725,6 +729,28 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return new Date(str) return new Date(str)
} }
public static levenshteinDistance(str1: string, str2: string) {
const track = Array(str2.length + 1).fill(null).map(() =>
Array(str1.length + 1).fill(null));
for (let i = 0; i <= str1.length; i += 1) {
track[0][i] = i;
}
for (let j = 0; j <= str2.length; j += 1) {
track[j][0] = j;
}
for (let j = 1; j <= str2.length; j += 1) {
for (let i = 1; i <= str1.length; i += 1) {
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
track[j][i] = Math.min(
track[j][i - 1] + 1, // deletion
track[j - 1][i] + 1, // insertion
track[j - 1][i - 1] + indicator, // substitution
);
}
}
return track[str2.length][str1.length];
}
private static colorDiff(c0: { r: number, g: number, b: number }, c1: { r: number, g: number, b: number }) { private static colorDiff(c0: { r: number, g: number, b: number }, c1: { r: number, g: number, b: number }) {
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b); return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b);
} }
@ -751,27 +777,5 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
b: parseInt(hex.substr(5, 2), 16), b: parseInt(hex.substr(5, 2), 16),
} }
} }
public static levenshteinDistance (str1: string, str2: string) {
const track = Array(str2.length + 1).fill(null).map(() =>
Array(str1.length + 1).fill(null));
for (let i = 0; i <= str1.length; i += 1) {
track[0][i] = i;
}
for (let j = 0; j <= str2.length; j += 1) {
track[j][0] = j;
}
for (let j = 1; j <= str2.length; j += 1) {
for (let i = 1; i <= str1.length; i += 1) {
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
track[j][i] = Math.min(
track[j][i - 1] + 1, // deletion
track[j - 1][i] + 1, // insertion
track[j - 1][i - 1] + indicator, // substitution
);
}
}
return track[str2.length][str1.length];
}
} }

View file

@ -10,7 +10,7 @@ export default class ImageAttributionSpec extends T {
[ [
"Should find all the images", "Should find all the images",
() => { () => {
const images = new Set(new ExtractImages(true).convertStrict(<any> cyclofix, "test")) const images = new Set(new ExtractImages(true, new Map<string, any>()).convertStrict(<any> cyclofix, "test"))
const expectedValues = [ const expectedValues = [
'./assets/layers/bike_repair_station/repair_station.svg', './assets/layers/bike_repair_station/repair_station.svg',
'./assets/layers/bike_repair_station/repair_station_pump.svg', './assets/layers/bike_repair_station/repair_station_pump.svg',

View file

@ -470,7 +470,7 @@ export default class LegacyThemeLoaderSpec extends T {
T.isTrue(r.warnings.some(msg => msg.indexOf("./assets/layers/bike_parking/staple.svg") >= 0), "staple.svg not mentioned"); T.isTrue(r.warnings.some(msg => msg.indexOf("./assets/layers/bike_parking/staple.svg") >= 0), "staple.svg not mentioned");
}], }],
["Images in 'thens' icons are detected", () => { ["Images in 'thens' icons are detected", () => {
const r = new ExtractImages(true).convert(<any>{ const r = new ExtractImages(true, new Map<string, any>()).convert(<any>{
"layers": [ "layers": [
{ {
tagRenderings: [ tagRenderings: [

View file

@ -59,7 +59,7 @@ async function main() {
let args = [...process.argv] let args = [...process.argv]
args.splice(0, 2) args.splice(0, 2)
args = args.map(a => a.toLowerCase()) args = args.map(a => a.toLowerCase().replace(/"/g, ""))
const allFailures: { testsuite: string, name: string, msg: string } [] = [] const allFailures: { testsuite: string, name: string, msg: string } [] = []
let testsToRun = allTests let testsToRun = allTests
@ -72,6 +72,7 @@ async function main() {
} }
}) })
testsToRun = allTests.filter(t => args.indexOf(t.name.toLowerCase()) >= 0) testsToRun = allTests.filter(t => args.indexOf(t.name.toLowerCase()) >= 0)
console.log("Only running test "+testsToRun.join(", "))
} }
if (testsToRun.length == 0) { if (testsToRun.length == 0) {