More tests

This commit is contained in:
pietervdvn 2022-03-15 01:53:08 +01:00
parent 82e59bc1eb
commit 50d383279d
5 changed files with 76 additions and 86 deletions

View file

@ -60,7 +60,27 @@ export class Denomination {
)
}
public canonicalValue(value: string, actAsDefault?: boolean) {
/**
* Create a representation of the given value
* @param value: the value from OSM
* @param actAsDefault: if set and the value can be parsed as number, will be parsed and trimmed
*
* const unit = new Denomination({
* canonicalDenomination: "m",
* alternativeDenomination: ["meter"],
* 'default': true,
* human: {
* en: "meter"
* }
* }, "test")
* unit.canonicalValue("42m") // =>"42 m"
* unit.canonicalValue("42") // =>"42 m"
* unit.canonicalValue("42 m") // =>"42 m"
* unit.canonicalValue("42 meter") // =>"42 m"
*
*
*/
public canonicalValue(value: string, actAsDefault?: boolean) : string {
if (value === undefined) {
return undefined;
}

View file

@ -1,31 +0,0 @@
import T from "./TestHelper";
import TileFreshnessCalculator from "../Logic/FeatureSource/TileFreshnessCalculator";
import {Tiles} from "../Models/TileRange";
import {equal} from "assert";
export default class TileFreshnessCalculatorSpec extends T {
constructor() {
super([
[
"TileFresnessTests",
() => {
const calc = new TileFreshnessCalculator();
// 19/266407/175535
const date = new Date()
date.setTime(42)
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534), date)
equal(42, calc.freshnessFor(19, 266406, 175534).getTime())
equal(42, calc.freshnessFor(20, 266406 * 2, 175534 * 2 + 1).getTime())
equal(undefined, calc.freshnessFor(19, 266406, 175535))
equal(undefined, calc.freshnessFor(18, 266406 / 2, 175534 / 2))
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534 + 1), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534 + 1), date)
equal(42, calc.freshnessFor(18, 266406 / 2, 175534 / 2).getTime())
}
]
])
}
}

View file

@ -1,54 +0,0 @@
import T from "./TestHelper";
import {equal} from "assert";
import {Unit} from "../Models/Unit";
import {Denomination} from "../Models/Denomination";
export default class UnitsSpec extends T {
constructor() {
super([
["Simple canonicalize", () => {
const unit = new Denomination({
canonicalDenomination: "m",
alternativeDenomination: ["meter"],
'default': true,
human: {
en: "meter"
}
}, "test")
equal(unit.canonicalValue("42m"), "42 m")
equal(unit.canonicalValue("42"), "42 m")
equal(unit.canonicalValue("42 m"), "42 m")
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)
}
]
]);
}
}

View file

@ -0,0 +1,25 @@
import {describe} from 'mocha'
import TileFreshnessCalculator from "../../../Logic/FeatureSource/TileFreshnessCalculator";
import {Tiles} from "../../../Models/TileRange";
import {expect} from "chai"
describe("TileFreshnessCalculator", () => {
it("should get the freshness for loaded tiles",
() => {
const calc = new TileFreshnessCalculator();
// 19/266407/175535
const date = new Date()
date.setTime(42)
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534), date)
expect(calc.freshnessFor(19, 266406, 175534).getTime()).eq(42)
expect(calc.freshnessFor(20, 266406 * 2, 175534 * 2 + 1).getTime()).eq(42)
expect(calc.freshnessFor(19, 266406, 175535)).undefined
expect(calc.freshnessFor(18, 266406 / 2, 175534 / 2)).undefined
calc.addTileLoad(Tiles.tile_index(19, 266406, 175534 + 1), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534), date)
calc.addTileLoad(Tiles.tile_index(19, 266406 + 1, 175534 + 1), date)
expect(calc.freshnessFor(18, 266406 / 2, 175534 / 2).getTime()).eq(42)
})
})

View file

@ -0,0 +1,30 @@
import {describe} from 'mocha'
import {expect} from 'chai'
import {Unit} from "../../Models/Unit";
import {Denomination} from "../../Models/Denomination";
describe("Unit", () => {
it("should convert a value back and forth", () => {
const unit = new Denomination({
"canonicalDenomination": "MW",
"alternativeDenomination": ["megawatts", "megawatt"],
"human": {
"en": " megawatts",
"nl": " megawatt"
},
"default": true
}, "test");
const canonical = unit.canonicalValue("5")
expect(canonical).eq( "5 MW")
const units = new Unit(["key"], [unit], false)
const [detected, detectedDenom] = units.findDenomination("5 MW")
expect(detected).eq( "5")
expect(detectedDenom).eq( unit)
}
)
})