From 04a2cc9bc796b99cda2c9ec8b1a90f5429c0ed58 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Sat, 19 Feb 2022 02:45:15 +0100 Subject: [PATCH] Fix tests --- Docs/Schemas/LayerConfigJson.schema.json | 2 +- Docs/Schemas/LayerConfigJsonJSC.ts | 2 +- Docs/Schemas/LayoutConfigJson.schema.json | 2 +- Docs/Schemas/LayoutConfigJsonJSC.ts | 2 +- .../LineRenderingConfigJson.schema.json | 2 +- Docs/Schemas/LineRenderingConfigJsonJSC.ts | 2 +- .../PointRenderingConfigJson.schema.json | 2 +- Docs/Schemas/PointRenderingConfigJsonJSC.ts | 2 +- .../TagRenderingConfigJson.schema.json | 2 +- Docs/Schemas/TagRenderingConfigJsonJSC.ts | 2 +- Docs/Schemas/TilesourceConfigJson.schema.json | 2 +- Docs/Schemas/TilesourceConfigJsonJSC.ts | 2 +- Models/ThemeConfig/Conversion/FixImages.ts | 36 ++++----- .../Json/TagRenderingConfigJson.ts | 2 +- Utils.ts | 74 ++++++++++--------- test/ImageAttribution.spec.ts | 2 +- test/LegacyThemeLoader.spec.ts | 2 +- test/TestAll.ts | 3 +- 18 files changed, 71 insertions(+), 72 deletions(-) diff --git a/Docs/Schemas/LayerConfigJson.schema.json b/Docs/Schemas/LayerConfigJson.schema.json index 9e72ff1a3a..72a8a69e0f 100644 --- a/Docs/Schemas/LayerConfigJson.schema.json +++ b/Docs/Schemas/LayerConfigJson.schema.json @@ -612,7 +612,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/LayerConfigJsonJSC.ts b/Docs/Schemas/LayerConfigJsonJSC.ts index 0348fcb911..481e127360 100644 --- a/Docs/Schemas/LayerConfigJsonJSC.ts +++ b/Docs/Schemas/LayerConfigJsonJSC.ts @@ -610,7 +610,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/LayoutConfigJson.schema.json b/Docs/Schemas/LayoutConfigJson.schema.json index 3321c1eed9..362a59e45a 100644 --- a/Docs/Schemas/LayoutConfigJson.schema.json +++ b/Docs/Schemas/LayoutConfigJson.schema.json @@ -456,7 +456,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/LayoutConfigJsonJSC.ts b/Docs/Schemas/LayoutConfigJsonJSC.ts index 49ca6c05ea..92e8e24fb4 100644 --- a/Docs/Schemas/LayoutConfigJsonJSC.ts +++ b/Docs/Schemas/LayoutConfigJsonJSC.ts @@ -454,7 +454,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/LineRenderingConfigJson.schema.json b/Docs/Schemas/LineRenderingConfigJson.schema.json index 278e857e43..2b8cac100b 100644 --- a/Docs/Schemas/LineRenderingConfigJson.schema.json +++ b/Docs/Schemas/LineRenderingConfigJson.schema.json @@ -256,7 +256,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/LineRenderingConfigJsonJSC.ts b/Docs/Schemas/LineRenderingConfigJsonJSC.ts index f4a2e76d3b..b0037110ac 100644 --- a/Docs/Schemas/LineRenderingConfigJsonJSC.ts +++ b/Docs/Schemas/LineRenderingConfigJsonJSC.ts @@ -254,7 +254,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/PointRenderingConfigJson.schema.json b/Docs/Schemas/PointRenderingConfigJson.schema.json index 7159884592..89d2c3c417 100644 --- a/Docs/Schemas/PointRenderingConfigJson.schema.json +++ b/Docs/Schemas/PointRenderingConfigJson.schema.json @@ -260,7 +260,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/PointRenderingConfigJsonJSC.ts b/Docs/Schemas/PointRenderingConfigJsonJSC.ts index 1f37ef54a5..599e6c5c19 100644 --- a/Docs/Schemas/PointRenderingConfigJsonJSC.ts +++ b/Docs/Schemas/PointRenderingConfigJsonJSC.ts @@ -258,7 +258,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/TagRenderingConfigJson.schema.json b/Docs/Schemas/TagRenderingConfigJson.schema.json index 1c595208d7..21ee45e1ef 100644 --- a/Docs/Schemas/TagRenderingConfigJson.schema.json +++ b/Docs/Schemas/TagRenderingConfigJson.schema.json @@ -96,7 +96,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/TagRenderingConfigJsonJSC.ts b/Docs/Schemas/TagRenderingConfigJsonJSC.ts index 172740e9cb..cf2358570e 100644 --- a/Docs/Schemas/TagRenderingConfigJsonJSC.ts +++ b/Docs/Schemas/TagRenderingConfigJsonJSC.ts @@ -96,7 +96,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/TilesourceConfigJson.schema.json b/Docs/Schemas/TilesourceConfigJson.schema.json index a7ae626b5b..ebac532924 100644 --- a/Docs/Schemas/TilesourceConfigJson.schema.json +++ b/Docs/Schemas/TilesourceConfigJson.schema.json @@ -204,7 +204,7 @@ ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Docs/Schemas/TilesourceConfigJsonJSC.ts b/Docs/Schemas/TilesourceConfigJsonJSC.ts index 21d48703ce..bc487ece2c 100644 --- a/Docs/Schemas/TilesourceConfigJsonJSC.ts +++ b/Docs/Schemas/TilesourceConfigJsonJSC.ts @@ -202,7 +202,7 @@ export default { ] }, "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": { "description": "An icon supporting this mapping; typically shown pretty small\nType: icon", diff --git a/Models/ThemeConfig/Conversion/FixImages.ts b/Models/ThemeConfig/Conversion/FixImages.ts index ebeb0223dd..08318b4bc7 100644 --- a/Models/ThemeConfig/Conversion/FixImages.ts +++ b/Models/ThemeConfig/Conversion/FixImages.ts @@ -7,6 +7,11 @@ import * as tagrenderingmetapaths from "../../../assets/tagrenderingconfigmeta.j export class ExtractImages extends Conversion { private _isOfficial: boolean; private _sharedTagRenderings: Map; + + 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) { super("Extract all images from a layoutConfig using the meta paths",[],"ExctractImages"); this._isOfficial = isOfficial; @@ -14,25 +19,17 @@ export class ExtractImages extends Conversion { } convert(json: LayoutConfigJson, context: string): { result: string[], errors: string[], warnings: string[] } { - const paths = metapaths["default"] ?? metapaths - const trpaths = tagrenderingmetapaths["default"] ?? tagrenderingmetapaths - const allFoundImages = [] + const allFoundImages = [] const errors = [] const warnings = [] - for (const metapath of paths) { - if (metapath.typeHint === undefined) { - continue - } - if (metapath.typeHint !== "image" && metapath.typeHint !== "icon") { - continue - } - + for (const metapath of ExtractImages.layoutMetaPaths) { const mightBeTr = Array.isArray(metapath.type) && metapath.type.some(t => t["$ref"] == "#/definitions/TagRenderingConfigJson") const found = Utils.CollectPath(metapath.path, json) if (mightBeTr) { // We might have tagRenderingConfigs containing icons here - for (const {path, leaf} of found) { - const foundImage = leaf; + for (const el of found) { + const path = el.path + const foundImage = el.leaf; if (typeof foundImage === "string") { if(foundImage == ""){ @@ -47,17 +44,14 @@ export class ExtractImages extends Conversion { allFoundImages.push(foundImage) } else{ // This is a tagRendering where every rendered value might be an icon! - for (const trpath of trpaths) { - if (trpath.typeHint !== "rendered") { - continue - } + for (const trpath of ExtractImages.tagRenderingMetaPaths) { const fromPath = Utils.CollectPath(trpath.path, foundImage) for (const img of fromPath) { - if (typeof img !== "string") { - (this._isOfficial ? errors: warnings).push(context+": found an image path that is not a path at " + context + "." + metapath.path.join(".") + ": " + JSON.stringify(img)) + if (typeof img.leaf !== "string") { + (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) { if(pathAndImg.leaf === "" || pathAndImg.leaf["path"] == ""){ errors.push(context+[...path,...pathAndImg.path].join(".")+": Found an empty image at ") @@ -68,7 +62,7 @@ export class ExtractImages extends Conversion { } } } else { - allFoundImages.push(...found) + allFoundImages.push(...found.map(i => i.leaf)) } } diff --git a/Models/ThemeConfig/Json/TagRenderingConfigJson.ts b/Models/ThemeConfig/Json/TagRenderingConfigJson.ts index 36ccbf3cce..0b08218b50 100644 --- a/Models/ThemeConfig/Json/TagRenderingConfigJson.ts +++ b/Models/ThemeConfig/Json/TagRenderingConfigJson.ts @@ -115,7 +115,7 @@ export interface TagRenderingConfigJson { /** * 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 - * type: rendered + * Type: rendered */ then: string | any, /** diff --git a/Utils.ts b/Utils.ts index 9132347f48..41e4d141e1 100644 --- a/Utils.ts +++ b/Utils.ts @@ -304,7 +304,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be if (target === null) { return source } - + for (const key in source) { if (!source.hasOwnProperty(key)) { 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 const leaf = object[head]; if (leaf !== undefined) { - if(Array.isArray(leaf)){ + if (Array.isArray(leaf)) { object[head] = leaf.map(o => replaceLeaf(o, travelledPath)) - }else{ + } else { 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; } 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; } - 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 */ - 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) { return collectedList; } const head = path[0] + travelledPath = [...travelledPath, head] if (path.length === 1) { // We have reached the leaf const leaf = object[head]; if (leaf === undefined || leaf === null) { return collectedList - } - if (Array.isArray(leaf)) { - collectedList.push(...leaf) - } else { - collectedList.push(leaf) + } + if (Array.isArray(leaf)) { + for (let i = 0; i < (leaf).length; i++){ + const l = (leaf)[i]; + collectedList.push({leaf: l, path: [...travelledPath, ""+i]}) } + } else { + collectedList.push({leaf, path: travelledPath}) + } return collectedList } 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)) { - 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; } if (typeof sub !== "object") { 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) } + 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 }) { 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), } } - - 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]; - } } diff --git a/test/ImageAttribution.spec.ts b/test/ImageAttribution.spec.ts index 10883105a4..86b394a790 100644 --- a/test/ImageAttribution.spec.ts +++ b/test/ImageAttribution.spec.ts @@ -10,7 +10,7 @@ export default class ImageAttributionSpec extends T { [ "Should find all the images", () => { - const images = new Set(new ExtractImages(true).convertStrict( cyclofix, "test")) + const images = new Set(new ExtractImages(true, new Map()).convertStrict( cyclofix, "test")) const expectedValues = [ './assets/layers/bike_repair_station/repair_station.svg', './assets/layers/bike_repair_station/repair_station_pump.svg', diff --git a/test/LegacyThemeLoader.spec.ts b/test/LegacyThemeLoader.spec.ts index 85a3d5b178..d310887f7e 100644 --- a/test/LegacyThemeLoader.spec.ts +++ b/test/LegacyThemeLoader.spec.ts @@ -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"); }], ["Images in 'thens' icons are detected", () => { - const r = new ExtractImages(true).convert({ + const r = new ExtractImages(true, new Map()).convert({ "layers": [ { tagRenderings: [ diff --git a/test/TestAll.ts b/test/TestAll.ts index de92118643..5d43853cc7 100644 --- a/test/TestAll.ts +++ b/test/TestAll.ts @@ -59,7 +59,7 @@ async function main() { let args = [...process.argv] 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 } [] = [] let testsToRun = allTests @@ -72,6 +72,7 @@ async function main() { } }) testsToRun = allTests.filter(t => args.indexOf(t.name.toLowerCase()) >= 0) + console.log("Only running test "+testsToRun.join(", ")) } if (testsToRun.length == 0) {