forked from MapComplete/MapComplete
		
	Fix tests, fix units
This commit is contained in:
		
							parent
							
								
									8b1b843dbe
								
							
						
					
					
						commit
						fb3e3a040b
					
				
					 7 changed files with 52 additions and 17 deletions
				
			
		|  | @ -37,6 +37,7 @@ export class Unit { | ||||||
|          |          | ||||||
|         const possiblePostFixes = new Set<string>() |         const possiblePostFixes = new Set<string>() | ||||||
|         function addPostfixesOf(str){ |         function addPostfixesOf(str){ | ||||||
|  |             str = str.toLowerCase() | ||||||
|             for (let i = 0; i < str.length + 1; i++) { |             for (let i = 0; i < str.length + 1; i++) { | ||||||
|                 const substr = str.substring(0,i) |                 const substr = str.substring(0,i) | ||||||
|                 possiblePostFixes.add(substr) |                 possiblePostFixes.add(substr) | ||||||
|  | @ -178,11 +179,11 @@ export class Denomination { | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } else { |         } else { | ||||||
|             if (value.endsWith(this.canonical) && this.canonical !== "") { |             if (value.endsWith(this.canonical.toLowerCase()) && this.canonical !== "") { | ||||||
|                 return value.substring(0, value.length - this.canonical.length).trim(); |                 return value.substring(0, value.length - this.canonical.length).trim(); | ||||||
|             } |             } | ||||||
|             for (const alternativeValue of this.alternativeDenominations) { |             for (const alternativeValue of this.alternativeDenominations) { | ||||||
|                 if (value.endsWith(alternativeValue)) { |                 if (value.endsWith(alternativeValue.toLowerCase())) { | ||||||
|                     return value.substring(0, value.length - alternativeValue.length).trim(); |                     return value.substring(0, value.length - alternativeValue.length).trim(); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import TagRenderingConfig from "./JSON/TagRenderingConfig"; | import TagRenderingConfig from "./JSON/TagRenderingConfig"; | ||||||
| import * as questions from "../assets/tagRenderings/questions.json"; | import * as questions from "../assets/tagRenderings/questions.json"; | ||||||
| import * as icons from "../assets/tagRenderings/icons.json"; | import * as icons from "../assets/tagRenderings/icons.json"; | ||||||
|  | import {Utils} from "../Utils"; | ||||||
| 
 | 
 | ||||||
| export default class SharedTagRenderings { | export default class SharedTagRenderings { | ||||||
| 
 | 
 | ||||||
|  | @ -14,7 +15,10 @@ export default class SharedTagRenderings { | ||||||
|             try { |             try { | ||||||
|                 dict.set(key, new TagRenderingConfig(store[key], undefined, `SharedTagRenderings.${key}`)) |                 dict.set(key, new TagRenderingConfig(store[key], undefined, `SharedTagRenderings.${key}`)) | ||||||
|             } catch (e) { |             } catch (e) { | ||||||
|                 console.error("BUG: could not parse", key, " from questions.json or icons.json - this error happened during the build step of the SharedTagRenderings", e) |                 if(!Utils.runningFromConsole){ | ||||||
|  |                     console.error("BUG: could not parse", key, " from questions.json or icons.json - this error happened during the build step of the SharedTagRenderings", e) | ||||||
|  | 
 | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -94,7 +94,7 @@ export default class SimpleMetaTagger { | ||||||
|                     } |                     } | ||||||
|                     const value = feature.properties[key] |                     const value = feature.properties[key] | ||||||
|                     const [, denomination] = unit.findDenomination(value) |                     const [, denomination] = unit.findDenomination(value) | ||||||
|                     let canonical = denomination.canonicalValue(value) ?? undefined; |                     let canonical = denomination?.canonicalValue(value) ?? undefined; | ||||||
|                     console.log("Rewritten ", key, " from", value, "into", canonical) |                     console.log("Rewritten ", key, " from", value, "into", canonical) | ||||||
|                     if(canonical === undefined && !unit.eraseInvalid) { |                     if(canonical === undefined && !unit.eraseInvalid) { | ||||||
|                         break; |                         break; | ||||||
|  |  | ||||||
|  | @ -287,13 +287,21 @@ export default class ValidatedTextField { | ||||||
|             input = new CombinedInputElement( |             input = new CombinedInputElement( | ||||||
|                 input, |                 input, | ||||||
|                 unitDropDown, |                 unitDropDown, | ||||||
|  |                 // combine the value from the textfield and the dropdown into the resulting value that should go into OSM
 | ||||||
|                 (text, denom) => denom?.canonicalValue(text, true) ?? undefined,  |                 (text, denom) => denom?.canonicalValue(text, true) ?? undefined,  | ||||||
|                 (valueWithDenom: string) => { |                 (valueWithDenom: string) => { | ||||||
|                     const [text, denom] = unit.findDenomination(valueWithDenom) ?? [valueWithDenom, undefined]; |                     // Take the value from OSM and feed it into the textfield and the dropdown
 | ||||||
|                     if(text === undefined){ |                     const withDenom = unit.findDenomination(valueWithDenom); | ||||||
|                         return [valueWithDenom, undefined] |                     if(withDenom === undefined) | ||||||
|  |                     { | ||||||
|  |                         // Not a valid value at all - we give it undefined and leave the details up to the other elements
 | ||||||
|  |                         return [undefined, undefined] | ||||||
|                     } |                     } | ||||||
|                     return [text, denom] |                     const [strippedText, denom] = withDenom | ||||||
|  |                     if(strippedText === undefined){ | ||||||
|  |                         return [undefined, undefined] | ||||||
|  |                     } | ||||||
|  |                     return [strippedText, denom] | ||||||
|                 } |                 } | ||||||
|             ).SetClass("flex") |             ).SetClass("flex") | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | @ -19,7 +19,7 @@ export class SaveButton extends Toggle { | ||||||
| 
 | 
 | ||||||
|         const text = Translations.t.general.save |         const text = Translations.t.general.save | ||||||
|         const saveEnabled = text.Clone().SetClass(`btn`); |         const saveEnabled = text.Clone().SetClass(`btn`); | ||||||
|         const saveDisabled = text.SetClass(`btn btn-disabled`); |         const saveDisabled = text.Clone().SetClass(`btn btn-disabled`); | ||||||
|         const save = new Toggle( |         const save = new Toggle( | ||||||
|             saveEnabled, |             saveEnabled, | ||||||
|             saveDisabled, |             saveDisabled, | ||||||
|  |  | ||||||
|  | @ -11,6 +11,7 @@ import {FixedUiElement} from "../UI/Base/FixedUiElement"; | ||||||
| import Combine from "../UI/Base/Combine"; | import Combine from "../UI/Base/Combine"; | ||||||
| import OsmObjectSpec from "./OsmObject.spec"; | import OsmObjectSpec from "./OsmObject.spec"; | ||||||
| import ScriptUtils from "../scripts/ScriptUtils"; | import ScriptUtils from "../scripts/ScriptUtils"; | ||||||
|  | import UnitsSpec from "./Units.spec"; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -37,7 +38,8 @@ const allTests = [ | ||||||
|     new GeoOperationsSpec(), |     new GeoOperationsSpec(), | ||||||
|     new ImageSearcherSpec(), |     new ImageSearcherSpec(), | ||||||
|     new ThemeSpec(), |     new ThemeSpec(), | ||||||
|     new UtilsSpec() |     new UtilsSpec(), | ||||||
|  |     new UnitsSpec() | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import T from "./TestHelper"; | import T from "./TestHelper"; | ||||||
| import {Denomination} from "../Customizations/JSON/Denomination"; | import {Denomination, Unit} from "../Customizations/JSON/Denomination"; | ||||||
| import {equal} from "assert"; | import {equal} from "assert"; | ||||||
| 
 | 
 | ||||||
| export default class UnitsSpec extends T { | export default class UnitsSpec extends T { | ||||||
|  | @ -17,13 +17,33 @@ export default class UnitsSpec extends T { | ||||||
|                     } |                     } | ||||||
|                 }, "test") |                 }, "test") | ||||||
| 
 | 
 | ||||||
|                 equal(unit.canonicalValue("42m"), "42m") |                 equal(unit.canonicalValue("42m"), "42 m") | ||||||
|                 equal(unit.canonicalValue("42"), "42m") |                 equal(unit.canonicalValue("42"), "42 m") | ||||||
|                 equal(unit.canonicalValue("42 m"), "42m") |                 equal(unit.canonicalValue("42 m"), "42 m") | ||||||
|                 equal(unit.canonicalValue("42 meter"), "42m") |                 equal(unit.canonicalValue("42 meter"), "42 m") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|             }] |             }], | ||||||
|  |             ["Advanced canonicalize and back", () => { | ||||||
|  | 
 | ||||||
|  |                 const unit = new Denomination({ | ||||||
|  |                     "canonicalDenomination": "MW", | ||||||
|  |                     "alternativeDenomination": ["megawatts", "megawatt"], | ||||||
|  |                     "human": { | ||||||
|  |                         "en": " megawatts", | ||||||
|  |                         "nl": " megawatt" | ||||||
|  |                     }, | ||||||
|  |                     "default": true | ||||||
|  |                 }, "test"); | ||||||
|  | 
 | ||||||
|  |                 const canonical = unit.canonicalValue("5") | ||||||
|  |                 equal(canonical, "5 MW") | ||||||
|  |                 const units = new Unit(["key"], [unit], false) | ||||||
|  |                 const [detected, detectedDenom] = units.findDenomination("5 MW") | ||||||
|  |                 equal(detected, "5") | ||||||
|  |                 equal(detectedDenom, unit) | ||||||
|  |             } | ||||||
|  |             ] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|         ]); |         ]); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue