Refactoring: use more accurate context in conversion, fix tests

This commit is contained in:
Pieter Vander Vennet 2023-10-12 16:55:26 +02:00
parent 86d0de3806
commit f77d99f8ed
43 changed files with 999 additions and 367 deletions

View file

@ -1,27 +1,31 @@
import { Utils } from "../../../../src/Utils"
import { DesugaringContext } from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
import {
ConversionContext,
DesugaringContext,
} from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
import { LayerConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayerConfigJson"
import { TagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/TagRenderingConfigJson"
import { PrepareLayer } from "../../../../src/Models/ThemeConfig/Conversion/PrepareLayer"
import * as bookcases from "../../../../assets/layers/public_bookcase/public_bookcase.json"
import CreateNoteImportLayer from "../../../../src/Models/ThemeConfig/Conversion/CreateNoteImportLayer"
import { describe, expect, it } from "vitest"
import { QuestionableTagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
describe("CreateNoteImportLayer", () => {
it("should generate a layerconfig", () => {
const desugaringState: DesugaringContext = {
sharedLayers: new Map<string, LayerConfigJson>(),
tagRenderings: new Map<string, TagRenderingConfigJson>(),
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
}
const layerPrepare = new PrepareLayer(desugaringState)
const layer = layerPrepare.convertStrict(
bookcases,
"ImportLayerGeneratorTest:Parse bookcases"
ConversionContext.test("parse bookcases")
)
const generator = new CreateNoteImportLayer()
const generatedLayer: LayerConfigJson = generator.convertStrict(
layer,
"ImportLayerGeneratorTest: convert"
ConversionContext.test("convert")
)
expect(generatedLayer.isShown["and"][1].or[0].and[0]).toEqual(
"_tags~(^|.*;)amenity=public_bookcase($|;.*)"

View file

@ -1,6 +1,7 @@
import LayoutConfig from "../../../../src/Models/ThemeConfig/LayoutConfig"
import { FixLegacyTheme } from "../../../../src/Models/ThemeConfig/Conversion/LegacyJsonConvert"
import { describe, expect, it } from "vitest"
import { ConversionContext } from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
describe("FixLegacyTheme", () => {
it("should create a working theme config", () => {
@ -133,10 +134,11 @@ describe("FixLegacyTheme", () => {
},
],
}
const fixed = new FixLegacyTheme().convert(<any>walking_node_theme, "While testing")
const context = ConversionContext.test()
const fixed = new FixLegacyTheme().convert(<any>walking_node_theme, context)
// "Could not fix the legacy theme"
expect(fixed.errors).empty
const theme = new LayoutConfig(fixed.result, false)
expect(!context.hasErrors())
const theme = new LayoutConfig(fixed, false)
expect(theme).toBeDefined()
})
})

View file

@ -1,5 +1,4 @@
import { LayerConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayerConfigJson"
import { TagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/TagRenderingConfigJson"
import LineRenderingConfigJson from "../../../../src/Models/ThemeConfig/Json/LineRenderingConfigJson"
import {
ExpandRewrite,
@ -9,6 +8,7 @@ import {
import { QuestionableTagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import RewritableConfigJson from "../../../../src/Models/ThemeConfig/Json/RewritableConfigJson"
import { describe, expect, it } from "vitest"
import { ConversionContext } from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
describe("ExpandRewrite", () => {
it("should not allow overlapping keys", () => {
@ -20,19 +20,19 @@ describe("ExpandRewrite", () => {
renderings: "The value of xyz is longer_xyz",
}
const rewrite = new ExpandRewrite()
expect(() => rewrite.convert(spec, "test")).to.throw
expect(() => rewrite.convertStrict(spec, ConversionContext.test())).to.throw
})
})
describe("PrepareLayer", () => {
it("should expand rewrites in map renderings", () => {
const exampleLayer: LayerConfigJson = {
const exampleLayer: LayerConfigJson = <any>{
id: "testlayer",
source: {
osmTags: "key=value",
},
mapRendering: [
{
lineRendering: [
<any>{
rewrite: {
sourceString: ["left|right", "lr_offset"],
into: [
@ -60,15 +60,15 @@ describe("PrepareLayer", () => {
],
}
const prep = new PrepareLayer({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers: new Map<string, LayerConfigJson>(),
})
const result = prep.convertStrict(exampleLayer, "test")
const result = prep.convertStrict(exampleLayer, ConversionContext.test())
const expected = {
id: "testlayer",
source: { osmTags: "key=value" },
mapRendering: [
lineRendering: [
{
color: {
render: "#888",
@ -123,7 +123,7 @@ describe("RewriteSpecial", function () {
},
},
}
const r = new RewriteSpecial().convert(tr, "test").result
const r = new RewriteSpecial().convertStrict(tr, ConversionContext.test())
expect(r).toEqual({
id: "uk_addresses_import_button",
render: {

View file

@ -1,16 +1,20 @@
import { LayoutConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayoutConfigJson"
import { LayerConfigJson } from "../../../../src/Models/ThemeConfig/Json/LayerConfigJson"
import { PrepareTheme } from "../../../../src/Models/ThemeConfig/Conversion/PrepareTheme"
import { TagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/TagRenderingConfigJson"
import LayoutConfig from "../../../../src/Models/ThemeConfig/LayoutConfig"
import bookcaseLayer from "../../../../src/assets/generated/layers/public_bookcase.json"
import LayerConfig from "../../../../src/Models/ThemeConfig/LayerConfig"
import { ExtractImages } from "../../../../src/Models/ThemeConfig/Conversion/FixImages"
import cyclofix from "../../../../src/assets/generated/themes/cyclofix.json"
import { Tag } from "../../../../src/Logic/Tags/Tag"
import { DesugaringContext } from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
import {
ConversionContext,
DesugaringContext,
} from "../../../../src/Models/ThemeConfig/Conversion/Conversion"
import { And } from "../../../../src/Logic/Tags/And"
import { describe, expect, it } from "vitest"
import { QuestionableTagRenderingConfigJson } from "../../../../src/Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import Constants from "../../../../src/Models/Constants"
const themeConfigJson: LayoutConfigJson = {
description: "Descr",
@ -34,17 +38,40 @@ const themeConfigJson: LayoutConfigJson = {
id: "test",
}
function constructSharedLayers(): Map<string, LayerConfigJson> {
const sharedLayers = new Map<string, LayerConfigJson>()
sharedLayers.set("selected_element", <LayerConfigJson>{
id: "selected_element",
pointRendering: null,
tagRenderings: null,
lineRendering: null,
title: null,
source: "special",
})
for (const defaultLayer of Constants.added_by_default) {
sharedLayers.set(defaultLayer, <LayerConfigJson>{
id: defaultLayer,
pointRendering: null,
tagRenderings: null,
lineRendering: null,
title: null,
source: "special",
})
}
return sharedLayers
}
describe("PrepareTheme", () => {
it("should substitute layers", () => {
const sharedLayers = new Map<string, LayerConfigJson>()
sharedLayers.set("public_bookcase", bookcaseLayer)
const sharedLayers = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
const theme = { ...themeConfigJson, layers: ["public_bookcase"] }
const prepareStep = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
publicLayers: new Set<string>(),
})
let themeConfigJsonPrepared = prepareStep.convert(theme, "test").result
let themeConfigJsonPrepared = prepareStep.convertStrict(theme, ConversionContext.test())
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
@ -55,13 +82,13 @@ describe("PrepareTheme", () => {
})
it("should apply override", () => {
const sharedLayers = new Map<string, LayerConfigJson>()
sharedLayers.set("public_bookcase", bookcaseLayer)
const sharedLayers = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
publicLayers: new Set<string>(),
}).convert(themeConfigJson, "test").result
}).convertStrict(themeConfigJson, ConversionContext.test())
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
@ -70,19 +97,19 @@ describe("PrepareTheme", () => {
})
it("should apply override", () => {
const sharedLayers = new Map<string, LayerConfigJson>()
sharedLayers.set("public_bookcase", bookcaseLayer)
const sharedLayers = constructSharedLayers()
sharedLayers.set("public_bookcase", <any>bookcaseLayer)
let themeConfigJsonPrepared = new PrepareTheme({
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers: sharedLayers,
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
sharedLayers,
publicLayers: new Set<string>(),
}).convert(
}).convertStrict(
{
...themeConfigJson,
overrideAll: { source: { geoJson: "https://example.com/data.geojson" } },
},
"test"
).result
ConversionContext.test()
)
const themeConfig = new LayoutConfig(themeConfigJsonPrepared)
const layerUnderTest = <LayerConfig>(
themeConfig.layers.find((l) => l.id === "public_bookcase")
@ -100,11 +127,14 @@ describe("PrepareTheme", () => {
en: "Test layer - please ignore",
},
titleIcons: [],
mapRendering: null,
pointRendering: [{ location: ["point"], label: "xyz" }],
lineRendering: [{ width: 1 }],
}
const sharedLayers = constructSharedLayers()
sharedLayers.set("layer-example", testLayer)
const ctx: DesugaringContext = {
sharedLayers: new Map<string, LayerConfigJson>([["layer-example", testLayer]]),
tagRenderings: new Map<string, TagRenderingConfigJson>(),
sharedLayers,
tagRenderings: new Map<string, QuestionableTagRenderingConfigJson>(),
publicLayers: new Set<string>(),
}
const layout: LayoutConfigJson = {
@ -122,13 +152,15 @@ describe("PrepareTheme", () => {
},
],
startLat: 0,
pointRendering: null,
lineRendering: null,
startLon: 0,
startZoom: 0,
title: "Test theme",
}
const rewritten = new PrepareTheme(ctx, {
skipDefaultLayers: true,
}).convertStrict(layout, "test")
}).convertStrict(layout, ConversionContext.test())
expect(rewritten.layers[0]).toEqual(testLayer)
expect(rewritten.layers[1]).toEqual({
source: {
@ -137,7 +169,8 @@ describe("PrepareTheme", () => {
id: "layer-example",
name: null,
minzoom: 18,
mapRendering: null,
pointRendering: [{ location: ["point"], label: "xyz" }],
lineRendering: [{ width: 1 }],
titleIcons: [],
})
})
@ -147,7 +180,7 @@ describe("ExtractImages", () => {
it("should find all images in a themefile", () => {
const images = new Set<string>(
new ExtractImages(true, new Set<string>())
.convertStrict(<any>cyclofix, "test")
.convertStrict(<any>cyclofix, ConversionContext.test())
.map((x) => x.path)
)
const expectedValues = [