2021-01-08 03:57:18 +01:00
|
|
|
import {Utils} from "../Utils";
|
|
|
|
Utils.runningFromConsole = true;
|
2020-08-30 01:13:18 +02:00
|
|
|
import {equal} from "assert";
|
2020-08-22 17:33:08 +02:00
|
|
|
import T from "./TestHelper";
|
2020-08-30 01:13:18 +02:00
|
|
|
import {FromJSON} from "../Customizations/JSON/FromJSON";
|
|
|
|
import Locale from "../UI/i18n/Locale";
|
2020-11-06 01:58:26 +01:00
|
|
|
import Translations from "../UI/i18n/Translations";
|
2020-09-17 13:13:02 +02:00
|
|
|
import {UIEventSource} from "../Logic/UIEventSource";
|
2020-10-28 11:19:47 +01:00
|
|
|
import TagRenderingConfig from "../Customizations/JSON/TagRenderingConfig";
|
|
|
|
import EditableTagRendering from "../UI/Popup/EditableTagRendering";
|
2020-11-06 01:58:26 +01:00
|
|
|
import {Translation} from "../UI/i18n/Translation";
|
2021-01-03 00:19:42 +01:00
|
|
|
import {OH, OpeningHour} from "../UI/OpeningHours/OpeningHours";
|
|
|
|
import PublicHolidayInput from "../UI/OpeningHours/PublicHolidayInput";
|
2021-02-05 16:32:37 +01:00
|
|
|
import {SubstitutedTranslation} from "../UI/SubstitutedTranslation";
|
2021-03-29 00:41:53 +02:00
|
|
|
import {Tag} from "../Logic/Tags/Tag";
|
|
|
|
import {And} from "../Logic/Tags/And";
|
2020-10-06 01:37:02 +02:00
|
|
|
|
2020-08-22 12:44:11 +02:00
|
|
|
|
2021-02-20 01:45:51 +01:00
|
|
|
new T("Tags", [
|
2020-08-22 17:33:08 +02:00
|
|
|
["Tag replacement works in translation", () => {
|
|
|
|
const tr = new Translation({
|
|
|
|
"en": "Test {key} abc"
|
|
|
|
}).replace("{key}", "value");
|
|
|
|
equal(tr.txt, "Test value abc");
|
2020-08-22 12:44:11 +02:00
|
|
|
|
2020-08-23 02:26:17 +02:00
|
|
|
}],
|
2020-08-30 01:13:18 +02:00
|
|
|
["Parse tag config", (() => {
|
|
|
|
const tag = FromJSON.Tag("key=value") as Tag;
|
|
|
|
equal(tag.key, "key");
|
|
|
|
equal(tag.value, "value");
|
2021-03-22 02:17:00 +01:00
|
|
|
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)
|
2021-03-14 01:40:35 +01:00
|
|
|
|
|
|
|
const isEmpty = FromJSON.Tag("key=") as Tag;
|
2021-03-22 02:17:00 +01:00
|
|
|
equal(isEmpty.matchesProperties({"key":"value"}), false)
|
|
|
|
equal(isEmpty.matchesProperties({"key":""}), true)
|
|
|
|
equal(isEmpty.matchesProperties({"other_key":""}), true)
|
|
|
|
equal(isEmpty.matchesProperties({"other_key":"value"}), true)
|
2021-03-14 01:40:35 +01:00
|
|
|
|
|
|
|
const isNotEmpty = FromJSON.Tag("key!=");
|
2021-03-22 02:17:00 +01:00
|
|
|
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)
|
2021-03-14 01:40:35 +01:00
|
|
|
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
|
|
|
|
const and = FromJSON.Tag({"and": ["key=value", "x=y"]}) as And;
|
|
|
|
equal((and.and[0] as Tag).key, "key");
|
|
|
|
equal((and.and[1] as Tag).value, "y");
|
|
|
|
|
2020-11-20 14:00:37 +01:00
|
|
|
|
|
|
|
const notReg = FromJSON.Tag("x!~y") as And;
|
2021-03-22 02:17:00 +01:00
|
|
|
equal(notReg.matchesProperties({"x":"y"}), false)
|
|
|
|
equal(notReg.matchesProperties({"x":"z"}), true)
|
|
|
|
equal(notReg.matchesProperties({"x":""}), true)
|
|
|
|
equal(notReg.matchesProperties({}), true)
|
2021-01-08 03:57:18 +01:00
|
|
|
|
|
|
|
const noMatch = FromJSON.Tag("key!=value") as Tag;
|
2021-03-22 02:17:00 +01:00
|
|
|
equal(noMatch.matchesProperties({"key":"value"}), false)
|
|
|
|
equal(noMatch.matchesProperties({"key":"otherValue"}), true)
|
|
|
|
equal(noMatch.matchesProperties({"key":""}), true)
|
|
|
|
equal(noMatch.matchesProperties({"otherKey":""}), true)
|
2020-11-20 14:00:37 +01:00
|
|
|
|
2021-01-09 02:11:43 +01:00
|
|
|
|
|
|
|
const multiMatch = FromJSON.Tag("vending~.*bicycle_tube.*") as Tag;
|
2021-03-22 02:17:00 +01:00
|
|
|
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)
|
2021-03-14 01:40:35 +01:00
|
|
|
|
|
|
|
const nameStartsWith = FromJSON.Tag("name~[sS]peelbos.*")
|
2021-03-22 02:17:00 +01:00
|
|
|
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)
|
2021-03-29 01:20:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
2020-11-20 14:00:37 +01:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
})],
|
2020-10-28 11:19:47 +01:00
|
|
|
["Is equivalent test", (() => {
|
|
|
|
|
|
|
|
const t0 = new And([
|
|
|
|
new Tag("valves:special","A"),
|
|
|
|
new Tag("valves","A")
|
|
|
|
])
|
|
|
|
const t1 = new And([
|
|
|
|
new Tag("valves","A")
|
|
|
|
])
|
|
|
|
const t2 = new And([
|
|
|
|
new Tag("valves","B")
|
|
|
|
])
|
|
|
|
equal(true, t0.isEquivalent(t0))
|
|
|
|
equal(true, t1.isEquivalent(t1))
|
|
|
|
equal(true, t2.isEquivalent(t2))
|
|
|
|
|
|
|
|
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))
|
|
|
|
})],
|
2020-08-30 01:13:18 +02:00
|
|
|
["Parse translation map", (() => {
|
|
|
|
|
|
|
|
const json: any = {"en": "English", "nl": "Nederlands"};
|
2020-10-28 11:19:47 +01:00
|
|
|
const translation = Translations.WT(new Translation(json));
|
2020-08-30 01:13:18 +02:00
|
|
|
Locale.language.setData("en");
|
|
|
|
equal(translation.txt, "English");
|
|
|
|
Locale.language.setData("nl");
|
|
|
|
equal(translation.txt, "Nederlands");
|
|
|
|
})],
|
|
|
|
["Parse tag rendering", (() => {
|
|
|
|
Locale.language.setData("nl");
|
2020-10-28 11:19:47 +01:00
|
|
|
const tr = new TagRenderingConfig({
|
2020-08-30 01:13:18 +02:00
|
|
|
render: ({"en":"Name is {name}", "nl":"Ook een {name}"} as any),
|
|
|
|
question: "Wat is de naam van dit object?",
|
|
|
|
freeform: {
|
|
|
|
key: "name",
|
|
|
|
},
|
2020-08-31 02:59:47 +02:00
|
|
|
|
|
|
|
mappings: [
|
|
|
|
{
|
|
|
|
if: "noname=yes",
|
|
|
|
"then": "Has no name"
|
|
|
|
}
|
|
|
|
],
|
2020-08-30 01:13:18 +02:00
|
|
|
condition: "x="
|
2021-01-08 03:57:18 +01:00
|
|
|
}, undefined,"");
|
2020-08-30 01:13:18 +02:00
|
|
|
|
2020-10-28 11:19:47 +01:00
|
|
|
equal(undefined, tr.GetRenderValue({"foo": "bar"}));
|
|
|
|
equal("Has no name", tr.GetRenderValue({"noname": "yes"})?.txt);
|
|
|
|
equal("Ook een {name}", tr.GetRenderValue({"name": "xyz"})?.txt);
|
2021-02-05 16:32:37 +01:00
|
|
|
equal("Ook een xyz", SubstitutedTranslation.construct( tr.GetRenderValue({"name": "xyz"}),
|
2020-10-28 11:19:47 +01:00
|
|
|
new UIEventSource<any>({"name":"xyz"})).InnerRender());
|
|
|
|
equal(undefined, tr.GetRenderValue({"foo": "bar"}));
|
2020-08-30 01:13:18 +02:00
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
})],
|
2020-09-17 13:13:02 +02:00
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
[
|
2020-09-17 13:13:02 +02:00
|
|
|
"Empty match test",
|
2020-09-09 18:42:13 +02:00
|
|
|
() => {
|
2020-09-17 13:13:02 +02:00
|
|
|
const t = new Tag("key","");
|
2021-03-22 02:17:00 +01:00
|
|
|
equal(false, t.matchesProperties({ "key":"somevalue"}))
|
2020-09-17 13:13:02 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"Tagrendering test",
|
|
|
|
() => {
|
|
|
|
|
|
|
|
const def = {
|
|
|
|
"render": {
|
|
|
|
"nl": "De toegankelijkheid van dit gebied is: {access:description}"
|
|
|
|
},
|
|
|
|
"question": {
|
|
|
|
"nl": "Is dit gebied toegankelijk?"
|
|
|
|
},
|
|
|
|
"freeform": {
|
|
|
|
"key": "access:description"
|
|
|
|
},
|
|
|
|
"mappings": [
|
|
|
|
{
|
|
|
|
"if": {
|
|
|
|
"and": [
|
|
|
|
"access:description=",
|
|
|
|
"access=",
|
|
|
|
"leisure=park"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"then": {
|
|
|
|
"nl": "Dit gebied is vrij toegankelijk"
|
|
|
|
},
|
|
|
|
"hideInAnswer": true
|
|
|
|
},
|
|
|
|
{
|
2020-10-27 01:01:34 +01:00
|
|
|
"if": "access=no",
|
|
|
|
"then": "Niet toegankelijk"
|
2020-09-17 13:13:02 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-01-08 03:57:18 +01:00
|
|
|
const constr = new TagRenderingConfig(def, undefined,"test");
|
2020-10-28 11:19:47 +01:00
|
|
|
const uiEl = new EditableTagRendering(new UIEventSource<any>(
|
|
|
|
{leisure: "park", "access": "no"}), constr
|
2020-10-27 01:01:34 +01:00
|
|
|
);
|
2020-09-17 13:13:02 +02:00
|
|
|
const rendered = uiEl.InnerRender();
|
|
|
|
equal(true, rendered.indexOf("Niet toegankelijk") > 0)
|
|
|
|
|
2020-09-09 18:42:13 +02:00
|
|
|
}
|
2020-10-04 01:04:46 +02:00
|
|
|
], [
|
|
|
|
"Merge touching opening hours",
|
|
|
|
() => {
|
|
|
|
const oh1: OpeningHour = {
|
|
|
|
weekday: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0,
|
|
|
|
endHour: 11,
|
|
|
|
endMinutes: 0
|
|
|
|
};
|
|
|
|
const oh0: OpeningHour = {
|
|
|
|
weekday: 0,
|
|
|
|
startHour: 11,
|
|
|
|
startMinutes: 0,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0
|
|
|
|
};
|
|
|
|
|
2020-10-06 01:37:02 +02:00
|
|
|
const merged = OH.MergeTimes([oh0, oh1]);
|
2020-10-04 01:04:46 +02:00
|
|
|
const r = merged[0];
|
|
|
|
equal( merged.length, 1);
|
|
|
|
equal(r.startHour,10 );
|
|
|
|
equal(r.endHour, 12)
|
|
|
|
|
|
|
|
}
|
|
|
|
], [
|
|
|
|
"Merge overlapping opening hours",
|
|
|
|
() => {
|
|
|
|
const oh1: OpeningHour = {
|
|
|
|
weekday: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0,
|
|
|
|
endHour: 11,
|
|
|
|
endMinutes: 0
|
|
|
|
};
|
|
|
|
const oh0: OpeningHour = {
|
|
|
|
weekday: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 30,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0
|
|
|
|
};
|
|
|
|
|
2020-10-06 01:37:02 +02:00
|
|
|
const merged = OH.MergeTimes([oh0, oh1]);
|
2020-10-04 01:04:46 +02:00
|
|
|
const r = merged[0];
|
|
|
|
equal( merged.length, 1);
|
|
|
|
equal(r.startHour,10 );
|
|
|
|
equal(r.endHour, 12)
|
|
|
|
|
2021-03-22 02:17:00 +01:00
|
|
|
}],
|
2020-10-06 01:37:02 +02:00
|
|
|
["Parse OH 1",() => {
|
|
|
|
const rules = OH.ParseRule("11:00-19:00");
|
|
|
|
equal(rules.length, 7);
|
|
|
|
equal(rules[0].weekday, 0);
|
|
|
|
equal(rules[0].startHour, 11);
|
|
|
|
equal(rules[3].endHour, 19);
|
|
|
|
|
|
|
|
}],
|
|
|
|
["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",() => {
|
|
|
|
const rules = OH.ToString([
|
|
|
|
{
|
|
|
|
weekday: 0,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
weekday: 0,
|
|
|
|
endHour: 17,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 13,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 17,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 13,
|
|
|
|
startMinutes: 0
|
|
|
|
},{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
]);
|
2020-10-08 19:03:00 +02:00
|
|
|
equal(rules, "Mo-Tu 10:00-12:00, 13:00-17:00");
|
2020-10-06 01:37:02 +02:00
|
|
|
}],
|
|
|
|
["JOIN OH 2",() => {
|
|
|
|
const rules = OH.ToString([
|
|
|
|
|
|
|
|
{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 17,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 13,
|
|
|
|
startMinutes: 0
|
|
|
|
},{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
]);
|
2020-10-08 19:03:00 +02:00
|
|
|
equal(rules, "Tu 10:00-12:00, 13:00-17:00");
|
2020-10-06 01:37:02 +02:00
|
|
|
}],
|
|
|
|
["JOIN OH 3",() => {
|
|
|
|
const rules = OH.ToString([
|
|
|
|
|
|
|
|
{
|
|
|
|
weekday: 3,
|
|
|
|
endHour: 17,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 13,
|
|
|
|
startMinutes: 0
|
|
|
|
},{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
]);
|
2020-10-08 19:03:00 +02:00
|
|
|
equal(rules, "Tu 10:00-12:00; Th 13:00-17:00");
|
2020-10-06 01:37:02 +02:00
|
|
|
}],
|
|
|
|
["JOIN OH 3",() => {
|
|
|
|
const rules = OH.ToString([
|
|
|
|
|
|
|
|
{
|
|
|
|
weekday: 6,
|
|
|
|
endHour: 17,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 13,
|
|
|
|
startMinutes: 0
|
|
|
|
},{
|
|
|
|
weekday: 1,
|
|
|
|
endHour: 12,
|
|
|
|
endMinutes: 0,
|
|
|
|
startHour: 10,
|
|
|
|
startMinutes: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
]);
|
2020-10-08 19:03:00 +02:00
|
|
|
equal(rules, "Tu 10:00-12:00; Su 13:00-17:00");
|
2020-10-06 02:09:09 +02:00
|
|
|
}],
|
|
|
|
["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");
|
2020-10-08 19:03:00 +02:00
|
|
|
}],
|
|
|
|
["OH Th[-1] off",() => {
|
|
|
|
const rules = OH.ParseRule("Th[-1] off");
|
|
|
|
equal(rules, null);
|
|
|
|
}],
|
2020-10-27 01:01:34 +01:00
|
|
|
["OHNo parsePH 12:00-17:00", () => {
|
2020-10-08 19:03:00 +02:00
|
|
|
const rules = OH.ParseRule("PH 12:00-17:00");
|
|
|
|
equal(rules, null);
|
|
|
|
}],
|
2020-10-27 01:01:34 +01:00
|
|
|
["OH Parse PH 12:00-17:00", () => {
|
2020-10-08 19:03:00 +02:00
|
|
|
const rules = PublicHolidayInput.LoadValue("PH 12:00-17:00");
|
|
|
|
equal(rules.mode, " ");
|
2020-10-30 00:56:46 +01:00
|
|
|
}],
|
|
|
|
["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")
|
|
|
|
|
2021-03-22 02:17:00 +01:00
|
|
|
}
|
|
|
|
]]);
|