More unit tests

This commit is contained in:
Pieter Vander Vennet 2022-03-15 01:42:38 +01:00
parent 4f4fc650b1
commit 82e59bc1eb
16 changed files with 433 additions and 375 deletions

View file

@ -67,6 +67,30 @@ export class RegexTag extends TagsFilter {
return false;
}
/**
* Checks if this tag matches the given properties
*
* const isNotEmpty = new RegexTag("key","^$", true);
* isNotEmpty.matchesProperties({"key": "value"}) // => true
* isNotEmpty.matchesProperties({"key": "other_value"}) // => true
* isNotEmpty.matchesProperties({"key": ""}) // => false
* isNotEmpty.matchesProperties({"other_key": ""}) // => false
* isNotEmpty.matchesProperties({"other_key": "value"}) // => false
*
* const isNotEmpty = new RegexTag("key","^..*$", true);
* isNotEmpty.matchesProperties({"key": "value"}) // => false
* isNotEmpty.matchesProperties({"key": "other_value"}) // => false
* isNotEmpty.matchesProperties({"key": ""}) // => true
* isNotEmpty.matchesProperties({"other_key": ""}) // => true
* isNotEmpty.matchesProperties({"other_key": "value"}) // => true
*
* const notRegex = new RegexTag("x", /^y$/, true)
* notRegex.matchesProperties({"x": "y"}) // => false
* notRegex.matchesProperties({"x": "z"}) // => true
* notRegex.matchesProperties({"x": ""}) // => true
* notRegex.matchesProperties({}) // => true
*/
matchesProperties(tags: any): boolean {
if (typeof this.key === "string") {
const value = tags[this.key] ?? ""

View file

@ -22,6 +22,20 @@ export class Tag extends TagsFilter {
}
/**
* const tag = new Tag("key","value")
* tag.matchesProperties({"key": "value"}) // => true
* tag.matchesProperties({"key": "z"}) // => false
* tag.matchesProperties({"key": ""}) // => false
* tag.matchesProperties({"other_key": ""}) // => false
* tag.matchesProperties({"other_key": "value"}) // => false
*
* const isEmpty = new Tag("key","")
* isEmpty.matchesProperties({"key": "value"}) // => false
* isEmpty.matchesProperties({"key": ""}) // => true
* isEmpty.matchesProperties({"other_key": ""}) // => true
* isEmpty.matchesProperties({"other_key": "value"}) // => true
*/
matchesProperties(properties: any): boolean {
const foundValue = properties[this.key]
if (foundValue === undefined && (this.value === "" || this.value === undefined)) {

View file

@ -266,6 +266,9 @@ export class TagUtils {
if (tag.indexOf("!=") >= 0) {
const split = Utils.SplitFirst(tag, "!=");
if (split[1] === "*") {
throw "At "+context+": invalid tag "+tag+". To indicate a missing tag, use '"+split[0]+"!=' instead"
}
if(split[1] === "") {
split[1] = "..*"
}
return new RegexTag(