forked from MapComplete/MapComplete
Add some regression tests
This commit is contained in:
parent
f6cbb44454
commit
35ecc21e09
2 changed files with 152 additions and 78 deletions
190
test/Tag.spec.ts
190
test/Tag.spec.ts
|
@ -1,4 +1,5 @@
|
|||
import {Utils} from "../Utils";
|
||||
|
||||
Utils.runningFromConsole = true;
|
||||
import {equal} from "assert";
|
||||
import T from "./TestHelper";
|
||||
|
@ -14,6 +15,7 @@ import PublicHolidayInput from "../UI/OpeningHours/PublicHolidayInput";
|
|||
import {SubstitutedTranslation} from "../UI/SubstitutedTranslation";
|
||||
import {Tag} from "../Logic/Tags/Tag";
|
||||
import {And} from "../Logic/Tags/And";
|
||||
import * as Assert from "assert";
|
||||
|
||||
|
||||
new T("Tags", [
|
||||
|
@ -28,25 +30,24 @@ new T("Tags", [
|
|||
const tag = FromJSON.Tag("key=value") as Tag;
|
||||
equal(tag.key, "key");
|
||||
equal(tag.value, "value");
|
||||
equal(tag.matchesProperties({"key":"value"}), true)
|
||||
equal(tag.matchesProperties({"key":"z"}), false)
|
||||
equal(tag.matchesProperties({"key":""}), false)
|
||||
equal(tag.matchesProperties({"other_key":""}), false)
|
||||
equal(tag.matchesProperties({"other_key":"value"}), false)
|
||||
equal(tag.matchesProperties({"key": "value"}), true)
|
||||
equal(tag.matchesProperties({"key": "z"}), false)
|
||||
equal(tag.matchesProperties({"key": ""}), false)
|
||||
equal(tag.matchesProperties({"other_key": ""}), false)
|
||||
equal(tag.matchesProperties({"other_key": "value"}), false)
|
||||
|
||||
const isEmpty = FromJSON.Tag("key=") as Tag;
|
||||
equal(isEmpty.matchesProperties({"key":"value"}), false)
|
||||
equal(isEmpty.matchesProperties({"key":""}), true)
|
||||
equal(isEmpty.matchesProperties({"other_key":""}), true)
|
||||
equal(isEmpty.matchesProperties({"other_key":"value"}), true)
|
||||
equal(isEmpty.matchesProperties({"key": "value"}), false)
|
||||
equal(isEmpty.matchesProperties({"key": ""}), true)
|
||||
equal(isEmpty.matchesProperties({"other_key": ""}), true)
|
||||
equal(isEmpty.matchesProperties({"other_key": "value"}), true)
|
||||
|
||||
const isNotEmpty = FromJSON.Tag("key!=");
|
||||
equal(isNotEmpty.matchesProperties({"key":"value"}), true)
|
||||
equal(isNotEmpty.matchesProperties({"key":"other_value"}), true)
|
||||
equal(isNotEmpty.matchesProperties({"key":""}), false)
|
||||
equal(isNotEmpty.matchesProperties({"other_key":""}), false)
|
||||
equal(isNotEmpty.matchesProperties({"other_key":"value"}), false)
|
||||
|
||||
equal(isNotEmpty.matchesProperties({"key": "value"}), true)
|
||||
equal(isNotEmpty.matchesProperties({"key": "other_value"}), true)
|
||||
equal(isNotEmpty.matchesProperties({"key": ""}), false)
|
||||
equal(isNotEmpty.matchesProperties({"other_key": ""}), false)
|
||||
equal(isNotEmpty.matchesProperties({"other_key": "value"}), false)
|
||||
|
||||
|
||||
const and = FromJSON.Tag({"and": ["key=value", "x=y"]}) as And;
|
||||
|
@ -55,50 +56,50 @@ new T("Tags", [
|
|||
|
||||
|
||||
const notReg = FromJSON.Tag("x!~y") as And;
|
||||
equal(notReg.matchesProperties({"x":"y"}), false)
|
||||
equal(notReg.matchesProperties({"x":"z"}), true)
|
||||
equal(notReg.matchesProperties({"x":""}), true)
|
||||
equal(notReg.matchesProperties({"x": "y"}), false)
|
||||
equal(notReg.matchesProperties({"x": "z"}), true)
|
||||
equal(notReg.matchesProperties({"x": ""}), true)
|
||||
equal(notReg.matchesProperties({}), true)
|
||||
|
||||
const noMatch = FromJSON.Tag("key!=value") as Tag;
|
||||
equal(noMatch.matchesProperties({"key":"value"}), false)
|
||||
equal(noMatch.matchesProperties({"key":"otherValue"}), true)
|
||||
equal(noMatch.matchesProperties({"key":""}), true)
|
||||
equal(noMatch.matchesProperties({"otherKey":""}), true)
|
||||
|
||||
|
||||
const noMatch = FromJSON.Tag("key!=value") as Tag;
|
||||
equal(noMatch.matchesProperties({"key": "value"}), false)
|
||||
equal(noMatch.matchesProperties({"key": "otherValue"}), true)
|
||||
equal(noMatch.matchesProperties({"key": ""}), true)
|
||||
equal(noMatch.matchesProperties({"otherKey": ""}), true)
|
||||
|
||||
|
||||
const multiMatch = FromJSON.Tag("vending~.*bicycle_tube.*") as Tag;
|
||||
equal(multiMatch.matchesProperties({"vending":"bicycle_tube"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending":"something;bicycle_tube"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending":"bicycle_tube;something"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending":"xyz;bicycle_tube;something"}), true)
|
||||
|
||||
equal(multiMatch.matchesProperties({"vending": "bicycle_tube"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending": "something;bicycle_tube"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending": "bicycle_tube;something"}), true)
|
||||
equal(multiMatch.matchesProperties({"vending": "xyz;bicycle_tube;something"}), true)
|
||||
|
||||
const nameStartsWith = FromJSON.Tag("name~[sS]peelbos.*")
|
||||
equal(nameStartsWith.matchesProperties({"name": "Speelbos Sint-Anna"}), true)
|
||||
equal(nameStartsWith.matchesProperties({"name": "speelbos Sint-Anna"}), true)
|
||||
equal(nameStartsWith.matchesProperties({"name": "Sint-Anna"}), false)
|
||||
equal(nameStartsWith.matchesProperties({"name": ""}), false)
|
||||
|
||||
|
||||
|
||||
|
||||
const assign = FromJSON.Tag("survey:date:={_date:now}")
|
||||
equal(assign.matchesProperties({"survey:date":"2021-03-29", "_date:now":"2021-03-29"}), true);
|
||||
equal(assign.matchesProperties({"survey:date":"2021-03-29", "_date:now":"2021-01-01"}), false);
|
||||
equal(assign.matchesProperties({"survey:date":"2021-03-29"}), false);
|
||||
equal(assign.matchesProperties({"_date:now":"2021-03-29"}), false);
|
||||
equal(assign.matchesProperties({"some_key":"2021-03-29"}), false);
|
||||
equal(assign.matchesProperties({"survey:date": "2021-03-29", "_date:now": "2021-03-29"}), true);
|
||||
equal(assign.matchesProperties({"survey:date": "2021-03-29", "_date:now": "2021-01-01"}), false);
|
||||
equal(assign.matchesProperties({"survey:date": "2021-03-29"}), false);
|
||||
equal(assign.matchesProperties({"_date:now": "2021-03-29"}), false);
|
||||
equal(assign.matchesProperties({"some_key": "2021-03-29"}), false);
|
||||
|
||||
})],
|
||||
["Is equivalent test", (() => {
|
||||
|
||||
const t0 = new And([
|
||||
new Tag("valves:special","A"),
|
||||
new Tag("valves","A")
|
||||
new Tag("valves:special", "A"),
|
||||
new Tag("valves", "A")
|
||||
])
|
||||
const t1 = new And([
|
||||
new Tag("valves","A")
|
||||
new Tag("valves", "A")
|
||||
])
|
||||
const t2 = new And([
|
||||
new Tag("valves","B")
|
||||
new Tag("valves", "B")
|
||||
])
|
||||
equal(true, t0.isEquivalent(t0))
|
||||
equal(true, t1.isEquivalent(t1))
|
||||
|
@ -107,7 +108,7 @@ new T("Tags", [
|
|||
equal(false, t0.isEquivalent(t1))
|
||||
equal(false, t0.isEquivalent(t2))
|
||||
equal(false, t1.isEquivalent(t0))
|
||||
|
||||
|
||||
equal(false, t1.isEquivalent(t2))
|
||||
equal(false, t2.isEquivalent(t0))
|
||||
equal(false, t2.isEquivalent(t1))
|
||||
|
@ -124,7 +125,7 @@ new T("Tags", [
|
|||
["Parse tag rendering", (() => {
|
||||
Locale.language.setData("nl");
|
||||
const tr = new TagRenderingConfig({
|
||||
render: ({"en":"Name is {name}", "nl":"Ook een {name}"} as any),
|
||||
render: ({"en": "Name is {name}", "nl": "Ook een {name}"} as any),
|
||||
question: "Wat is de naam van dit object?",
|
||||
freeform: {
|
||||
key: "name",
|
||||
|
@ -137,22 +138,22 @@ new T("Tags", [
|
|||
}
|
||||
],
|
||||
condition: "x="
|
||||
}, undefined,"");
|
||||
}, undefined, "");
|
||||
|
||||
equal(undefined, tr.GetRenderValue({"foo": "bar"}));
|
||||
equal("Has no name", tr.GetRenderValue({"noname": "yes"})?.txt);
|
||||
equal("Ook een {name}", tr.GetRenderValue({"name": "xyz"})?.txt);
|
||||
equal("Ook een xyz", SubstitutedTranslation.construct( tr.GetRenderValue({"name": "xyz"}),
|
||||
new UIEventSource<any>({"name":"xyz"})).InnerRender());
|
||||
equal("Ook een xyz", SubstitutedTranslation.construct(tr.GetRenderValue({"name": "xyz"}),
|
||||
new UIEventSource<any>({"name": "xyz"})).InnerRender());
|
||||
equal(undefined, tr.GetRenderValue({"foo": "bar"}));
|
||||
|
||||
})],
|
||||
|
||||
|
||||
[
|
||||
"Empty match test",
|
||||
() => {
|
||||
const t = new Tag("key","");
|
||||
equal(false, t.matchesProperties({ "key":"somevalue"}))
|
||||
const t = new Tag("key", "");
|
||||
equal(false, t.matchesProperties({"key": "somevalue"}))
|
||||
}
|
||||
],
|
||||
[
|
||||
|
@ -190,7 +191,7 @@ new T("Tags", [
|
|||
]
|
||||
};
|
||||
|
||||
const constr = new TagRenderingConfig(def, undefined,"test");
|
||||
const constr = new TagRenderingConfig(def, undefined, "test");
|
||||
const uiEl = new EditableTagRendering(new UIEventSource<any>(
|
||||
{leisure: "park", "access": "no"}), constr
|
||||
);
|
||||
|
@ -215,11 +216,11 @@ new T("Tags", [
|
|||
endHour: 12,
|
||||
endMinutes: 0
|
||||
};
|
||||
|
||||
|
||||
const merged = OH.MergeTimes([oh0, oh1]);
|
||||
const r = merged[0];
|
||||
equal( merged.length, 1);
|
||||
equal(r.startHour,10 );
|
||||
equal(merged.length, 1);
|
||||
equal(r.startHour, 10);
|
||||
equal(r.endHour, 12)
|
||||
|
||||
}
|
||||
|
@ -243,12 +244,12 @@ new T("Tags", [
|
|||
|
||||
const merged = OH.MergeTimes([oh0, oh1]);
|
||||
const r = merged[0];
|
||||
equal( merged.length, 1);
|
||||
equal(r.startHour,10 );
|
||||
equal(merged.length, 1);
|
||||
equal(r.startHour, 10);
|
||||
equal(r.endHour, 12)
|
||||
|
||||
}],
|
||||
["Parse OH 1",() => {
|
||||
["Parse OH 1", () => {
|
||||
const rules = OH.ParseRule("11:00-19:00");
|
||||
equal(rules.length, 7);
|
||||
equal(rules[0].weekday, 0);
|
||||
|
@ -256,14 +257,14 @@ new T("Tags", [
|
|||
equal(rules[3].endHour, 19);
|
||||
|
||||
}],
|
||||
["Parse OH 2",() => {
|
||||
["Parse OH 2", () => {
|
||||
const rules = OH.ParseRule("Mo-Th 11:00-19:00");
|
||||
equal(rules.length, 4);
|
||||
equal(rules[0].weekday, 0);
|
||||
equal(rules[0].startHour, 11);
|
||||
equal(rules[3].endHour, 19);
|
||||
}],
|
||||
["JOIN OH 1",() => {
|
||||
["JOIN OH 1", () => {
|
||||
const rules = OH.ToString([
|
||||
{
|
||||
weekday: 0,
|
||||
|
@ -287,7 +288,7 @@ new T("Tags", [
|
|||
endMinutes: 0,
|
||||
startHour: 13,
|
||||
startMinutes: 0
|
||||
},{
|
||||
}, {
|
||||
weekday: 1,
|
||||
endHour: 12,
|
||||
endMinutes: 0,
|
||||
|
@ -298,7 +299,7 @@ new T("Tags", [
|
|||
]);
|
||||
equal(rules, "Mo-Tu 10:00-12:00, 13:00-17:00");
|
||||
}],
|
||||
["JOIN OH 2",() => {
|
||||
["JOIN OH 2", () => {
|
||||
const rules = OH.ToString([
|
||||
|
||||
{
|
||||
|
@ -307,7 +308,7 @@ new T("Tags", [
|
|||
endMinutes: 0,
|
||||
startHour: 13,
|
||||
startMinutes: 0
|
||||
},{
|
||||
}, {
|
||||
weekday: 1,
|
||||
endHour: 12,
|
||||
endMinutes: 0,
|
||||
|
@ -318,7 +319,7 @@ new T("Tags", [
|
|||
]);
|
||||
equal(rules, "Tu 10:00-12:00, 13:00-17:00");
|
||||
}],
|
||||
["JOIN OH 3",() => {
|
||||
["JOIN OH 3", () => {
|
||||
const rules = OH.ToString([
|
||||
|
||||
{
|
||||
|
@ -327,7 +328,7 @@ new T("Tags", [
|
|||
endMinutes: 0,
|
||||
startHour: 13,
|
||||
startMinutes: 0
|
||||
},{
|
||||
}, {
|
||||
weekday: 1,
|
||||
endHour: 12,
|
||||
endMinutes: 0,
|
||||
|
@ -338,7 +339,7 @@ new T("Tags", [
|
|||
]);
|
||||
equal(rules, "Tu 10:00-12:00; Th 13:00-17:00");
|
||||
}],
|
||||
["JOIN OH 3",() => {
|
||||
["JOIN OH 3", () => {
|
||||
const rules = OH.ToString([
|
||||
|
||||
{
|
||||
|
@ -347,7 +348,7 @@ new T("Tags", [
|
|||
endMinutes: 0,
|
||||
startHour: 13,
|
||||
startMinutes: 0
|
||||
},{
|
||||
}, {
|
||||
weekday: 1,
|
||||
endHour: 12,
|
||||
endMinutes: 0,
|
||||
|
@ -358,14 +359,14 @@ new T("Tags", [
|
|||
]);
|
||||
equal(rules, "Tu 10:00-12:00; Su 13:00-17:00");
|
||||
}],
|
||||
["OH 24/7",() => {
|
||||
["OH 24/7", () => {
|
||||
const rules = OH.Parse("24/7");
|
||||
equal(rules.length, 7);
|
||||
equal(rules[0].startHour, 0);
|
||||
const asStr = OH.ToString(rules);
|
||||
equal(asStr, "24/7");
|
||||
}],
|
||||
["OH Th[-1] off",() => {
|
||||
["OH Th[-1] off", () => {
|
||||
const rules = OH.ParseRule("Th[-1] off");
|
||||
equal(rules, null);
|
||||
}],
|
||||
|
@ -378,17 +379,50 @@ new T("Tags", [
|
|||
equal(rules.mode, " ");
|
||||
}],
|
||||
["Round", () => {
|
||||
equal(Utils.Round(15), "15.0")
|
||||
equal(Utils.Round(1), "1.0")
|
||||
equal(Utils.Round(1.5), "1.5")
|
||||
equal(Utils.Round(0.5), "0.5")
|
||||
equal(Utils.Round(1.6), "1.6")
|
||||
equal(Utils.Round(15), "15.0")
|
||||
equal(Utils.Round(1), "1.0")
|
||||
equal(Utils.Round(1.5), "1.5")
|
||||
equal(Utils.Round(0.5), "0.5")
|
||||
equal(Utils.Round(1.6), "1.6")
|
||||
|
||||
equal(Utils.Round(-15), "-15.0")
|
||||
equal(Utils.Round(-1), "-1.0")
|
||||
equal(Utils.Round(-1.5), "-1.5")
|
||||
equal(Utils.Round(-0.5), "-0.5")
|
||||
equal(Utils.Round(-1.6), "-1.6")
|
||||
equal(Utils.Round(-15), "-15.0")
|
||||
equal(Utils.Round(-1), "-1.0")
|
||||
equal(Utils.Round(-1.5), "-1.5")
|
||||
equal(Utils.Round(-0.5), "-0.5")
|
||||
equal(Utils.Round(-1.6), "-1.6")
|
||||
|
||||
}
|
||||
]]);
|
||||
],
|
||||
["Regression", () => {
|
||||
|
||||
const config = {
|
||||
"#": "Bottle refill",
|
||||
"question": {
|
||||
"en": "How easy is it to fill water bottles?",
|
||||
"nl": "Hoe gemakkelijk is het om drinkbussen bij te vullen?",
|
||||
"de": "Wie einfach ist es, Wasserflaschen zu füllen?"
|
||||
},
|
||||
"mappings": [
|
||||
{
|
||||
"if": "bottle=yes",
|
||||
"then": {
|
||||
"en": "It is easy to refill water bottles",
|
||||
"nl": "Een drinkbus bijvullen gaat makkelijk",
|
||||
"de": "Es ist einfach, Wasserflaschen nachzufüllen"
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": "bottle=no",
|
||||
"then": {
|
||||
"en": "Water bottles may not fit",
|
||||
"nl": "Een drinkbus past moeilijk",
|
||||
"de": "Wasserflaschen passen möglicherweise nicht"
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const tagRendering = new TagRenderingConfig(config, null, "test");
|
||||
equal(true, tagRendering.IsKnown({bottle: "yes"}))
|
||||
equal(false, tagRendering.IsKnown({}))
|
||||
}]]);
|
||||
|
|
|
@ -5,6 +5,7 @@ Utils.runningFromConsole = true;
|
|||
import TagRenderingQuestion from "../UI/Popup/TagRenderingQuestion";
|
||||
import {UIEventSource} from "../Logic/UIEventSource";
|
||||
import TagRenderingConfig from "../Customizations/JSON/TagRenderingConfig";
|
||||
import EditableTagRendering from "../UI/Popup/EditableTagRendering";
|
||||
|
||||
|
||||
new T("TagQuestionElement",
|
||||
|
@ -53,6 +54,45 @@ new T("TagQuestionElement",
|
|||
T.assertContains("What is the name of this bookcase?", html);
|
||||
T.assertContains("This bookcase has no name", html);
|
||||
T.assertContains("<input type='text'", html);
|
||||
}],
|
||||
["Regression: has drinking water is asked", () => {
|
||||
const tags = new UIEventSource({
|
||||
id: "node/123",
|
||||
amenity: 'drinking_water'
|
||||
});
|
||||
const jsonConfig = {
|
||||
"#": "Bottle refill",
|
||||
"question": {
|
||||
"en": "How easy is it to fill water bottles?",
|
||||
"nl": "Hoe gemakkelijk is het om drinkbussen bij te vullen?",
|
||||
"de": "Wie einfach ist es, Wasserflaschen zu füllen?"
|
||||
},
|
||||
"mappings": [
|
||||
{
|
||||
"if": "bottle=yes",
|
||||
"then": {
|
||||
"en": "It is easy to refill water bottles",
|
||||
"nl": "Een drinkbus bijvullen gaat makkelijk",
|
||||
"de": "Es ist einfach, Wasserflaschen nachzufüllen"
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": "bottle=no",
|
||||
"then": {
|
||||
"en": "Water bottles may not fit",
|
||||
"nl": "Een drinkbus past moeilijk",
|
||||
"de": "Wasserflaschen passen möglicherweise nicht"
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const config = new TagRenderingConfig(
|
||||
jsonConfig, null, "test");
|
||||
const questionElement = new EditableTagRendering(tags, config);
|
||||
const html = questionElement.InnerRender();
|
||||
T.assertContains("How easy is it to fill water bottles?", html);
|
||||
T.assertContains("It is easy to refill water bottles", html);
|
||||
T.assertContains("<input type='radio'", html);
|
||||
}]
|
||||
]
|
||||
);
|
Loading…
Reference in a new issue