Fix tests

This commit is contained in:
Pieter Vander Vennet 2022-07-29 21:09:58 +02:00
parent 04040b68b9
commit 1ff03d9bd0

View file

@ -366,7 +366,7 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
* // should warn for unexpected keys * // should warn for unexpected keys
* const errors = [] * const errors = []
* RewriteSpecial.convertIfNeeded({"special": {type: "image_carousel"}, "en": "xyz"}, errors, "test") // => {'*': "{image_carousel()}"} * RewriteSpecial.convertIfNeeded({"special": {type: "image_carousel"}, "en": "xyz"}, errors, "test") // => {'*': "{image_carousel()}"}
* errors // => ["At test: Unexpected key in a special block: en"] * errors // => ["The only keys allowed next to a 'special'-block are 'before' and 'after'. Perhaps you meant to put 'en' into the special block?"]
* *
* // should give an error on unknown visualisations * // should give an error on unknown visualisations
* const errors = [] * const errors = []
@ -378,24 +378,25 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
* const errors = [] * const errors = []
* RewriteSpecial.convertIfNeeded({"special": {}}, errors, "test") // => undefined * RewriteSpecial.convertIfNeeded({"special": {}}, errors, "test") // => undefined
* errors // => ["A 'special'-block should define 'type' to indicate which visualisation should be used"] * errors // => ["A 'special'-block should define 'type' to indicate which visualisation should be used"]
* *
* *
* // an actual test * // an actual test
* const special = {"special": { * const special = {
* "type": "multi", * "before": {
* "before": {
* "en": "<h3>Entrances</h3>This building has {_entrances_count} entrances:" * "en": "<h3>Entrances</h3>This building has {_entrances_count} entrances:"
* }, * },
* "after": { * "after": {
* "en": "{_entrances_count_without_width_count} entrances don't have width information yet" * "en": "{_entrances_count_without_width_count} entrances don't have width information yet"
* }, * },
* "special": {
* "type": "multi",
* "key": "_entrance_properties_with_width", * "key": "_entrance_properties_with_width",
* "tagrendering": { * "tagrendering": {
* "en": "An <a href='#{id}'>entrance</a> of {canonical(width)}" * "en": "An <a href='#{id}'>entrance</a> of {canonical(width)}"
* } * }
* }} * }}
* const errors = [] * const errors = []
* RewriteSpecial.convertIfNeeded(special, errors, "test") // => {"en": "<h3>Entrances</h3>This building has {_entrances_count} entrances: {multi(_entrance_properties_with_width,An <a href='#&LBRACEid&RBRACE'>entrance</a> of &LBRACEcanonical&LPARENSwidth&RPARENS&RBRACE)}An <a href='#{id}'>entrance</a> of {canonical(width)}"} * RewriteSpecial.convertIfNeeded(special, errors, "test") // => {"en": "<h3>Entrances</h3>This building has {_entrances_count} entrances:{multi(_entrance_properties_with_width,An <a href='#&LBRACEid&RBRACE'>entrance</a> of &LBRACEcanonical&LPARENSwidth&RPARENS&RBRACE)}{_entrances_count_without_width_count} entrances don't have width information yet"}
* errors // => [] * errors // => []
*/ */
private static convertIfNeeded(input: (object & { special: { type: string } }) | any, errors: string[], context: string): any { private static convertIfNeeded(input: (object & { special: { type: string } }) | any, errors: string[], context: string): any {
@ -409,12 +410,18 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
errors.push("A 'special'-block should define 'type' to indicate which visualisation should be used") errors.push("A 'special'-block should define 'type' to indicate which visualisation should be used")
return undefined return undefined
} }
const vis = SpecialVisualizations.specialVisualizations.find(sp => sp.funcName === type) const vis = SpecialVisualizations.specialVisualizations.find(sp => sp.funcName === type)
if (vis === undefined) { if (vis === undefined) {
const options = Utils.sortedByLevenshteinDistance(type, SpecialVisualizations.specialVisualizations, sp => sp.funcName) const options = Utils.sortedByLevenshteinDistance(type, SpecialVisualizations.specialVisualizations, sp => sp.funcName)
errors.push(`Special visualisation '${type}' not found. Did you perhaps mean ${options[0].funcName}, ${options[1].funcName} or ${options[2].funcName}?\n\tFor all known special visualisations, please see https://github.com/pietervdvn/MapComplete/blob/develop/Docs/SpecialRenderings.md`) errors.push(`Special visualisation '${type}' not found. Did you perhaps mean ${options[0].funcName}, ${options[1].funcName} or ${options[2].funcName}?\n\tFor all known special visualisations, please see https://github.com/pietervdvn/MapComplete/blob/develop/Docs/SpecialRenderings.md`)
return undefined return undefined
} }
errors.push(...
Array.from(Object.keys(input)).filter(k => k !== "special" && k !== "before" && k !== "after")
.map(k => {
return `The only keys allowed next to a 'special'-block are 'before' and 'after'. Perhaps you meant to put '${k}' into the special block?`;
}))
const argNamesList = vis.args.map(a => a.name) const argNamesList = vis.args.map(a => a.name)
const argNames = new Set<string>(argNamesList) const argNames = new Set<string>(argNamesList)
@ -471,20 +478,20 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
for (const ln of languages) { for (const ln of languages) {
const args = [] const args = []
for (const argName of argNamesList) { for (const argName of argNamesList) {
const v = special[argName] ?? "" let v = special[argName] ?? ""
if (Translations.isProbablyATranslation(v)) { if (Translations.isProbablyATranslation(v)) {
const txt = new Translation(v).textFor(ln) v = new Translation(v).textFor(ln)
.replace(/,/g, "&COMMA")
.replace(/\{/g, "&LBRACE") }
.replace(/}/g, "&RBRACE")
; if (typeof v === "string") {
args.push(txt)
} else if (typeof v === "string") {
const txt = v.replace(/,/g, "&COMMA") const txt = v.replace(/,/g, "&COMMA")
.replace(/\{/g, "&LBRACE") .replace(/\{/g, "&LBRACE")
.replace(/}/g, "&RBRACE") .replace(/}/g, "&RBRACE")
.replace(/\(/g, "&LPARENS")
.replace(/\)/g, '&RPARENS')
args.push(txt) args.push(txt)
} else if(typeof v === "object"){ } else if (typeof v === "object") {
args.push(JSON.stringify(v)) args.push(JSON.stringify(v))
} else { } else {
args.push(v) args.push(v)
@ -518,7 +525,7 @@ export class RewriteSpecial extends DesugaringStep<TagRenderingConfigJson> {
* const result = new RewriteSpecial().convert(tr,"test").result * const result = new RewriteSpecial().convert(tr,"test").result
* const expected = {render: {'en': "Some introduction{image_carousel(image)}"}} * const expected = {render: {'en': "Some introduction{image_carousel(image)}"}}
* result // => expected * result // => expected
* *
* // Should put text after if specified * // Should put text after if specified
* const tr = { * const tr = {
* render: {special: {type: "image_carousel", image_key: "image"}, after: {en: "Some footer"} }, * render: {special: {type: "image_carousel", image_key: "image"}, after: {en: "Some footer"} },