forked from MapComplete/MapComplete
Fix tests
This commit is contained in:
parent
d9d6e96fed
commit
04a2cc9bc7
18 changed files with 71 additions and 72 deletions
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -7,6 +7,11 @@ import * as tagrenderingmetapaths from "../../../assets/tagrenderingconfigmeta.j
|
|||
export class ExtractImages extends Conversion<LayoutConfigJson, string[]> {
|
||||
private _isOfficial: boolean;
|
||||
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>) {
|
||||
super("Extract all images from a layoutConfig using the meta paths",[],"ExctractImages");
|
||||
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[] } {
|
||||
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<LayoutConfigJson, string[]> {
|
|||
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<LayoutConfigJson, string[]> {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
allFoundImages.push(...found)
|
||||
allFoundImages.push(...found.map(i => i.leaf))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
/**
|
||||
|
|
74
Utils.ts
74
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 < (<any[]>leaf).length; i++){
|
||||
const l = (<any[]>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];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ export default class ImageAttributionSpec extends T {
|
|||
[
|
||||
"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 = [
|
||||
'./assets/layers/bike_repair_station/repair_station.svg',
|
||||
'./assets/layers/bike_repair_station/repair_station_pump.svg',
|
||||
|
|
|
@ -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(<any>{
|
||||
const r = new ExtractImages(true, new Map<string, any>()).convert(<any>{
|
||||
"layers": [
|
||||
{
|
||||
tagRenderings: [
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue