Tests for various entity classes and update entity class implementations

This commit is contained in:
barsdeveloper
2024-06-03 00:11:30 +02:00
parent 5314228b33
commit 8fed17b20f
11 changed files with 309 additions and 210 deletions

View File

@@ -2,64 +2,44 @@ import P from "parsernostrum"
import Utility from "../Utility.js"
import Grammar from "../serialization/Grammar.js"
import IPrintableEntity from "./IPrintableEntity.js"
import StringEntity from "./StringEntity.js"
export default class LocalizedTextEntity extends IPrintableEntity {
static attributeSeparator = ", "
static printKey = k => ""
static lookbehind = "NSLOCTEXT"
static attributes = {
...super.attributes,
namespace: StringEntity.withDefault(),
key: StringEntity.withDefault(),
value: StringEntity.withDefault(),
}
static grammar = P.regArray(new RegExp(
String.raw`${this.lookbehind}\s*\(`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*`
+ String.raw`(,\s+)?`
String.raw`${LocalizedTextEntity.lookbehind}\s*\(`
+ String.raw`\s*"(?<namespace>${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(?<key>${Grammar.Regex.InsideString.source})"\s*,`
+ String.raw`\s*"(?<value>${Grammar.Regex.InsideString.source})"\s*`
+ String.raw`(?<trailing>,\s+)?`
+ String.raw`\)`,
"m"
)).map(matchResult => {
const self = matchResult[4] ? this.flagTrailing() : this
return new self(
Utility.unescapeString(matchResult[1]),
Utility.unescapeString(matchResult[2]),
Utility.unescapeString(matchResult[3]),
)
)).map(({ groups: { namespace, key, value, trailing } }) => {
const self = trailing ? this.flagTrailing() : this
return new self({
namespace: new (this.attributes.namespace)(Utility.unescapeString(namespace)),
key: new (this.attributes.namespace)(Utility.unescapeString(key)),
value: new (this.attributes.namespace)(Utility.unescapeString(value)),
})
}).label("LocalizedTextEntity")
#namespace
get namespace() {
return this.#namespace
}
set namespace(value) {
this.#namespace = value
}
#key
get key() {
return this.#key
}
set key(value) {
this.#key = value
}
#value
get value() {
return this.#value
}
set value(value) {
this.#value = value
}
constructor(namespace = "", key = "", value = "") {
super()
this.namespace = namespace
this.key = key
this.value = value
constructor(values = {}) {
super(values)
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.namespace>} */ this.namespace
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.key>} */ this.key
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.value>} */ this.value
}
print() {
return Utility.capitalFirstLetter(this.value)
}
toString() {
const trailer = this.Self().trailing ? ", " : ""
return `${this.lookbehind}(${this.namespace}, ${this.key}, ${this.value}${trailer})`
return Utility.capitalFirstLetter(this.value.valueOf())
}
}