mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-06-18 02:13:16 +08:00
Tests for various entity classes and update entity class implementations
This commit is contained in:
@@ -28,7 +28,9 @@ export default class AlternativesEntity extends IEntity {
|
|||||||
* @param {Types} types
|
* @param {Types} types
|
||||||
*/
|
*/
|
||||||
static accepting(...types) {
|
static accepting(...types) {
|
||||||
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(this.asUniqueClass())
|
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(
|
||||||
|
this.asUniqueClass()
|
||||||
|
)
|
||||||
result.alternatives = types
|
result.alternatives = types
|
||||||
result.grammar = result.createGrammar()
|
result.grammar = result.createGrammar()
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -2,19 +2,20 @@ import P from "parsernostrum"
|
|||||||
import Grammar from "../serialization/Grammar.js"
|
import Grammar from "../serialization/Grammar.js"
|
||||||
import IEntity from "./IEntity.js"
|
import IEntity from "./IEntity.js"
|
||||||
|
|
||||||
/** @template {typeof IEntity} T */
|
/** @template {IEntity} T */
|
||||||
export default class ArrayEntity extends IEntity {
|
export default class ArrayEntity extends IEntity {
|
||||||
|
|
||||||
/** @type {typeof IEntity} */
|
/** @type {typeof IEntity} */
|
||||||
static type
|
static type
|
||||||
static grammar = this.createGrammar()
|
static grammar = this.createGrammar()
|
||||||
|
|
||||||
/** @param {ExtractType<T>[]} values */
|
/** @param {T[]} values */
|
||||||
constructor(values = []) {
|
constructor(values = []) {
|
||||||
super()
|
super()
|
||||||
this.values = values
|
this.values = values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {P<ArrayEntity<IEntity>>} */
|
||||||
static createGrammar(elementGrammar = this.type?.grammar ?? P.lazy(() => Grammar.unknownValue)) {
|
static createGrammar(elementGrammar = this.type?.grammar ?? P.lazy(() => Grammar.unknownValue)) {
|
||||||
return this.inlined
|
return this.inlined
|
||||||
? elementGrammar
|
? elementGrammar
|
||||||
@@ -35,10 +36,10 @@ export default class ArrayEntity extends IEntity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @template {typeof IEntity} T
|
* @template {typeof IEntity} T
|
||||||
* @param {NonNullable<T>} type
|
* @param {T} type
|
||||||
*/
|
*/
|
||||||
static of(type) {
|
static of(type) {
|
||||||
const result = /** @type {typeof ArrayEntity<T> & { type: ExtractType<T>, grammar: P<ArrayEntity<T>> }} */(
|
const result = /** @type {{type: T, grammar: P<ArrayEntity<ExtractType<T>>> } & typeof ArrayEntity<ExtractType<T>>} */(
|
||||||
this.asUniqueClass()
|
this.asUniqueClass()
|
||||||
)
|
)
|
||||||
result.type = /** @type {ExtractType<T>} */(type)
|
result.type = /** @type {ExtractType<T>} */(type)
|
||||||
@@ -49,4 +50,16 @@ export default class ArrayEntity extends IEntity {
|
|||||||
valueOf() {
|
valueOf() {
|
||||||
return this.values
|
return this.values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toString(
|
||||||
|
insideString = false,
|
||||||
|
indentation = "",
|
||||||
|
printKey = this.Self().printKey,
|
||||||
|
) {
|
||||||
|
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator)
|
||||||
|
if (this.Self().trailing) {
|
||||||
|
result += this.Self().attributeSeparator
|
||||||
|
}
|
||||||
|
return `(${result})`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ export default class ByteEntity extends IntegerEntity {
|
|||||||
|
|
||||||
static grammar = P.numberByte.map(v => new this(v))
|
static grammar = P.numberByte.map(v => new this(v))
|
||||||
|
|
||||||
|
get value() {
|
||||||
|
return super.value
|
||||||
|
}
|
||||||
set value(value) {
|
set value(value) {
|
||||||
if (value % 1 == 0 && value >= 0 && value < 1 << 8) {
|
if (value % 1 == 0 && value >= 0 && value < 1 << 8) {
|
||||||
super.value = value
|
super.value = value
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import StringEntity from "./StringEntity.js"
|
|||||||
|
|
||||||
export default class FormatTextEntity extends IPrintableEntity {
|
export default class FormatTextEntity extends IPrintableEntity {
|
||||||
|
|
||||||
|
static attributeSeparator = ", "
|
||||||
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
|
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
|
||||||
/** @type {P<FormatTextEntity>} */
|
/** @type {P<FormatTextEntity>} */
|
||||||
static grammar = P.lazy(() => P.seq(
|
static grammar = P.lazy(() => P.seq(
|
||||||
@@ -53,4 +54,16 @@ export default class FormatTextEntity extends IPrintableEntity {
|
|||||||
: ""
|
: ""
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toString(
|
||||||
|
insideString = false,
|
||||||
|
indentation = "",
|
||||||
|
printKey = this.Self().printKey,
|
||||||
|
) {
|
||||||
|
const separator = this.Self().attributeSeparator
|
||||||
|
return this.lookbehind + "("
|
||||||
|
+ this.values.map(v => v.toString(insideString)).join(separator)
|
||||||
|
+ (this.Self().trailing ? separator : "")
|
||||||
|
+ ")"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export default class IEntity {
|
|||||||
static wrap = this.defaultWrapped
|
static wrap = this.defaultWrapped
|
||||||
|
|
||||||
static attributeSeparator = ","
|
static attributeSeparator = ","
|
||||||
|
static keySeparator = "="
|
||||||
|
|
||||||
/** @type {(k: String) => String} */
|
/** @type {(k: String) => String} */
|
||||||
static printKey = k => k
|
static printKey = k => k
|
||||||
@@ -103,7 +104,6 @@ export default class IEntity {
|
|||||||
*/
|
*/
|
||||||
static asUniqueClass() {
|
static asUniqueClass() {
|
||||||
if (this.name.length) {
|
if (this.name.length) {
|
||||||
// @ts-expect-error
|
|
||||||
return class extends this { }
|
return class extends this { }
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
@@ -281,9 +281,9 @@ export default class IEntity {
|
|||||||
if (Self.quoted) {
|
if (Self.quoted) {
|
||||||
keyValue = `"${keyValue}"`
|
keyValue = `"${keyValue}"`
|
||||||
}
|
}
|
||||||
result += Self.attributeSeparator.includes("\n") ? indentation : ""
|
result += (Self.attributeSeparator.includes("\n") ? indentation : "") + keyValue + Self.keySeparator
|
||||||
}
|
}
|
||||||
let serialization = value.toString(insideString, indentation, printKey)
|
let serialization = value?.toString(insideString, indentation, printKey)
|
||||||
if (Self.serialized) {
|
if (Self.serialized) {
|
||||||
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`
|
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,11 @@ export default class IntegerEntity extends NumberEntity {
|
|||||||
|
|
||||||
static grammar = P.numberInteger.map(v => new this(v))
|
static grammar = P.numberInteger.map(v => new this(v))
|
||||||
|
|
||||||
|
get value() {
|
||||||
|
return super.value
|
||||||
|
}
|
||||||
set value(value) {
|
set value(value) {
|
||||||
if (value >= -(1 << 31) && value < 1 << 31) {
|
if (value >= 1 << 31 && value < -(1 << 31)) {
|
||||||
value = Math.floor(value)
|
value = Math.floor(value)
|
||||||
super.value = value
|
super.value = value
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,64 +2,44 @@ import P from "parsernostrum"
|
|||||||
import Utility from "../Utility.js"
|
import Utility from "../Utility.js"
|
||||||
import Grammar from "../serialization/Grammar.js"
|
import Grammar from "../serialization/Grammar.js"
|
||||||
import IPrintableEntity from "./IPrintableEntity.js"
|
import IPrintableEntity from "./IPrintableEntity.js"
|
||||||
|
import StringEntity from "./StringEntity.js"
|
||||||
|
|
||||||
export default class LocalizedTextEntity extends IPrintableEntity {
|
export default class LocalizedTextEntity extends IPrintableEntity {
|
||||||
|
|
||||||
|
static attributeSeparator = ", "
|
||||||
|
static printKey = k => ""
|
||||||
static lookbehind = "NSLOCTEXT"
|
static lookbehind = "NSLOCTEXT"
|
||||||
|
static attributes = {
|
||||||
|
...super.attributes,
|
||||||
|
namespace: StringEntity.withDefault(),
|
||||||
|
key: StringEntity.withDefault(),
|
||||||
|
value: StringEntity.withDefault(),
|
||||||
|
}
|
||||||
static grammar = P.regArray(new RegExp(
|
static grammar = P.regArray(new RegExp(
|
||||||
String.raw`${this.lookbehind}\s*\(`
|
String.raw`${LocalizedTextEntity.lookbehind}\s*\(`
|
||||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
|
+ String.raw`\s*"(?<namespace>${Grammar.Regex.InsideString.source})"\s*,`
|
||||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
|
+ String.raw`\s*"(?<key>${Grammar.Regex.InsideString.source})"\s*,`
|
||||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*`
|
+ String.raw`\s*"(?<value>${Grammar.Regex.InsideString.source})"\s*`
|
||||||
+ String.raw`(,\s+)?`
|
+ String.raw`(?<trailing>,\s+)?`
|
||||||
+ String.raw`\)`,
|
+ String.raw`\)`,
|
||||||
"m"
|
"m"
|
||||||
)).map(matchResult => {
|
)).map(({ groups: { namespace, key, value, trailing } }) => {
|
||||||
const self = matchResult[4] ? this.flagTrailing() : this
|
const self = trailing ? this.flagTrailing() : this
|
||||||
return new self(
|
return new self({
|
||||||
Utility.unescapeString(matchResult[1]),
|
namespace: new (this.attributes.namespace)(Utility.unescapeString(namespace)),
|
||||||
Utility.unescapeString(matchResult[2]),
|
key: new (this.attributes.namespace)(Utility.unescapeString(key)),
|
||||||
Utility.unescapeString(matchResult[3]),
|
value: new (this.attributes.namespace)(Utility.unescapeString(value)),
|
||||||
)
|
})
|
||||||
}).label("LocalizedTextEntity")
|
}).label("LocalizedTextEntity")
|
||||||
|
|
||||||
#namespace
|
constructor(values = {}) {
|
||||||
get namespace() {
|
super(values)
|
||||||
return this.#namespace
|
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.namespace>} */ this.namespace
|
||||||
}
|
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.key>} */ this.key
|
||||||
set namespace(value) {
|
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.value>} */ this.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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print() {
|
print() {
|
||||||
return Utility.capitalFirstLetter(this.value)
|
return Utility.capitalFirstLetter(this.value.valueOf())
|
||||||
}
|
|
||||||
|
|
||||||
toString() {
|
|
||||||
const trailer = this.Self().trailing ? ", " : ""
|
|
||||||
return `${this.lookbehind}(${this.namespace}, ${this.key}, ${this.value}${trailer})`
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,11 @@ export default class NaturalNumberEntity extends IntegerEntity {
|
|||||||
|
|
||||||
static grammar = P.numberNatural.map(v => new this(v))
|
static grammar = P.numberNatural.map(v => new this(v))
|
||||||
|
|
||||||
|
get value() {
|
||||||
|
return super.value
|
||||||
|
}
|
||||||
set value(value) {
|
set value(value) {
|
||||||
value = Math.round(Utility.clamp(this.value, 0))
|
value = Math.round(Utility.clamp(this.value, 0))
|
||||||
super.value = value
|
super.value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(value = 0) {
|
|
||||||
super(value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
js/entity/NullEntity.js
Normal file
16
js/entity/NullEntity.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import P from "parsernostrum"
|
||||||
|
import IEntity from "./IEntity.js"
|
||||||
|
|
||||||
|
export default class NullEntity extends IEntity {
|
||||||
|
|
||||||
|
static grammar = P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
|
||||||
|
.map(v => new this())
|
||||||
|
|
||||||
|
toString(
|
||||||
|
insideString = false,
|
||||||
|
indentation = "",
|
||||||
|
printKey = this.Self().printKey,
|
||||||
|
) {
|
||||||
|
return "()"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,6 +47,7 @@ import NumberEntity from "../entity/NumberEntity.js"
|
|||||||
import StringEntity from "../entity/StringEntity.js"
|
import StringEntity from "../entity/StringEntity.js"
|
||||||
import ArrayEntity from "../entity/ArrayEntity.js"
|
import ArrayEntity from "../entity/ArrayEntity.js"
|
||||||
import AlternativesEntity from "../entity/AlternativesEntity.js"
|
import AlternativesEntity from "../entity/AlternativesEntity.js"
|
||||||
|
import NullEntity from "../entity/NullEntity.js"
|
||||||
|
|
||||||
export default function initializeSerializerFactory() {
|
export default function initializeSerializerFactory() {
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ export default function initializeSerializerFactory() {
|
|||||||
BooleanEntity.grammar,
|
BooleanEntity.grammar,
|
||||||
GuidEntity.grammar,
|
GuidEntity.grammar,
|
||||||
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
|
Parsernostrum.str("None").map(() => ObjectReferenceEntity.createNoneInstance()),
|
||||||
Grammar.null,
|
NullEntity.grammar,
|
||||||
NumberEntity.grammar,
|
NumberEntity.grammar,
|
||||||
ObjectReferenceEntity.fullReferenceGrammar,
|
ObjectReferenceEntity.fullReferenceGrammar,
|
||||||
StringEntity.grammar,
|
StringEntity.grammar,
|
||||||
|
|||||||
@@ -22,193 +22,262 @@ import VectorEntity from "../js/entity/VectorEntity.js"
|
|||||||
import Grammar from "../js/serialization/Grammar.js"
|
import Grammar from "../js/serialization/Grammar.js"
|
||||||
import SerializerFactory from "../js/serialization/SerializerFactory.js"
|
import SerializerFactory from "../js/serialization/SerializerFactory.js"
|
||||||
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
||||||
|
import NaturalNumberEntity from "../js/entity/NaturalNumberEntity.js"
|
||||||
|
import NullEntity from "../js/entity/NullEntity.js"
|
||||||
|
import AlternativesEntity from "../js/entity/AlternativesEntity.js"
|
||||||
|
|
||||||
test.beforeAll(() => initializeSerializerFactory())
|
test.beforeAll(() => initializeSerializerFactory())
|
||||||
|
|
||||||
test.describe.configure({ mode: "parallel" })
|
test.describe.configure({ mode: "parallel" })
|
||||||
|
|
||||||
test("ArrayEntity", () => {
|
test("ArrayEntity", () => {
|
||||||
let grammar = ArrayEntity.grammar
|
{
|
||||||
|
const grammar = ArrayEntity.grammar
|
||||||
expect(grammar.parse("()")).toEqual(new ArrayEntity([]))
|
let value = grammar.parse("()")
|
||||||
expect(grammar.parse("( )")).toEqual(new ArrayEntity([]))
|
expect(value).toEqual(new ArrayEntity([]))
|
||||||
expect(grammar.parse("(1, 2, 3, 4, 5, 6)")).toEqual(new ArrayEntity([
|
value = grammar.parse("( )")
|
||||||
new NumberEntity(1),
|
expect(value.toString()).toEqual("()")
|
||||||
new NumberEntity(2),
|
expect(value).toEqual(new ArrayEntity([]))
|
||||||
new NumberEntity(3),
|
value = grammar.parse("(1, 2, 3, 4, 5, 6)")
|
||||||
new NumberEntity(4),
|
expect(value.toString()).toEqual("(1,2,3,4,5,6)")
|
||||||
new NumberEntity(5),
|
expect(value).toEqual(new ArrayEntity([
|
||||||
new NumberEntity(6),
|
new NumberEntity(1),
|
||||||
]))
|
new NumberEntity(2),
|
||||||
expect(ArrayEntity.of(NumberEntity).grammar.parse("(2,4,6,8)")).toEqual(new ArrayEntity([
|
new NumberEntity(3),
|
||||||
new NumberEntity(2),
|
new NumberEntity(4),
|
||||||
new NumberEntity(4),
|
new NumberEntity(5),
|
||||||
new NumberEntity(6),
|
new NumberEntity(6),
|
||||||
new NumberEntity(8),
|
]))
|
||||||
]))
|
expect(value.Self().className()).toEqual("ArrayEntity")
|
||||||
expect(ArrayEntity.of(IntegerEntity).grammar.parse("(-0, -1, -2)")).toEqual(new ArrayEntity([
|
value.values.map(v => v.Self().className()).forEach(v => expect(v).toEqual("NumberEntity"))
|
||||||
new IntegerEntity(0),
|
value = grammar.parse("(1, 2, )")
|
||||||
new IntegerEntity(-1),
|
expect(value.toString()).toEqual("(1,2,)")
|
||||||
new IntegerEntity(-2),
|
expect(value).toEqual(new ArrayEntity([
|
||||||
]))
|
new NumberEntity(1),
|
||||||
expect(() => ArrayEntity.of(IntegerEntity).grammar.parse("(-1, -2.1, -3)")).toThrowError()
|
new NumberEntity(2),
|
||||||
expect(grammar.parse(`(
|
]))
|
||||||
"alpha",
|
}
|
||||||
"beta",
|
{
|
||||||
123,
|
const grammar = ArrayEntity.of(NumberEntity).grammar
|
||||||
3BEF2168446CAA32D5B54289FAB2F0BA,
|
let value = grammar.parse("(2,4,6,8)")
|
||||||
Some(a=1, b="2")
|
expect(value.toString()).toEqual("(2,4,6,8)")
|
||||||
)`)).toEqual(new ArrayEntity([
|
expect(value).toEqual(new ArrayEntity([
|
||||||
new StringEntity("alpha"),
|
new NumberEntity(2),
|
||||||
new StringEntity("beta"),
|
new NumberEntity(4),
|
||||||
new NumberEntity(123),
|
new NumberEntity(6),
|
||||||
new GuidEntity("3BEF2168446CAA32D5B54289FAB2F0BA"),
|
new NumberEntity(8),
|
||||||
new (UnknownKeysEntity.withLookbehind("Some"))({
|
]))
|
||||||
a: new NumberEntity(1),
|
}
|
||||||
b: new StringEntity("2"),
|
{
|
||||||
})
|
const grammar = ArrayEntity.of(IntegerEntity).grammar
|
||||||
]))
|
let value = grammar.parse("(-0, -1, -2)")
|
||||||
expect(grammar.parse(`(
|
expect(value.toString()).toEqual("(0,-1,-2)")
|
||||||
A(first = (9,8,7,6,5), second = 00000000000000000000000000000000),
|
expect(value).toEqual(new ArrayEntity([
|
||||||
B(key="hello"),
|
new IntegerEntity(0),
|
||||||
)`)).toEqual(new ArrayEntity([
|
new IntegerEntity(-1),
|
||||||
new UnknownKeysEntity({
|
new IntegerEntity(-2),
|
||||||
lookbehind: new StringEntity("A"),
|
]))
|
||||||
first: new ArrayEntity([
|
value.values.map(v => v.Self().className()).forEach(v => expect(v).toEqual("IntegerEntity"))
|
||||||
new NumberEntity(9),
|
value = grammar.parse("(-0, -1, -2,)")
|
||||||
new NumberEntity(8),
|
expect(value.toString()).toEqual("(0,-1,-2,)")
|
||||||
new NumberEntity(7),
|
expect(value).toEqual(new ArrayEntity([
|
||||||
new NumberEntity(6),
|
new IntegerEntity(0),
|
||||||
new NumberEntity(5),
|
new IntegerEntity(-1),
|
||||||
|
new IntegerEntity(-2),
|
||||||
|
]))
|
||||||
|
expect(() => grammar.parse("(-1, -2.1, -3)")).toThrowError()
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const grammar = ArrayEntity.grammar
|
||||||
|
let value = grammar.parse(`(
|
||||||
|
"alpha",
|
||||||
|
"beta",
|
||||||
|
123,
|
||||||
|
3BEF2168446CAA32D5B54289FAB2F0BA,
|
||||||
|
Some(a=1, b="number:\\"2\\"")
|
||||||
|
)`)
|
||||||
|
expect(value.toString()).toEqual('("alpha","beta",123,3BEF2168446CAA32D5B54289FAB2F0BA,Some(a=1,b="number:\\"2\\""))')
|
||||||
|
expect(value).toEqual(new ArrayEntity([
|
||||||
|
new StringEntity("alpha"),
|
||||||
|
new StringEntity("beta"),
|
||||||
|
new NumberEntity(123),
|
||||||
|
new GuidEntity("3BEF2168446CAA32D5B54289FAB2F0BA"),
|
||||||
|
new (UnknownKeysEntity.withLookbehind("Some"))({
|
||||||
|
a: new NumberEntity(1),
|
||||||
|
b: new StringEntity('number:"2"'),
|
||||||
|
})
|
||||||
|
]))
|
||||||
|
expect(value.values.map(v => v.Self().className())).toEqual([
|
||||||
|
"StringEntity",
|
||||||
|
"StringEntity",
|
||||||
|
"NumberEntity",
|
||||||
|
"GuidEntity",
|
||||||
|
"UnknownKeysEntity",
|
||||||
|
])
|
||||||
|
value = grammar.parse(`(
|
||||||
|
A(first = (9,8,7,6,5), second = 00000000000000000000000000000000),
|
||||||
|
B(key="hello"),
|
||||||
|
)`)
|
||||||
|
expect(value.toString()).toEqual('(A(first=(9,8,7,6,5),second=00000000000000000000000000000000),B(key="hello"),)')
|
||||||
|
expect(value).toEqual(new ArrayEntity([
|
||||||
|
new UnknownKeysEntity({
|
||||||
|
lookbehind: new StringEntity("A"),
|
||||||
|
first: new ArrayEntity([
|
||||||
|
new NumberEntity(9),
|
||||||
|
new NumberEntity(8),
|
||||||
|
new NumberEntity(7),
|
||||||
|
new NumberEntity(6),
|
||||||
|
new NumberEntity(5),
|
||||||
|
]),
|
||||||
|
second: new GuidEntity("00000000000000000000000000000000"),
|
||||||
|
}),
|
||||||
|
new UnknownKeysEntity({
|
||||||
|
lookbehind: new StringEntity("B"),
|
||||||
|
key: new StringEntity("hello"),
|
||||||
|
})
|
||||||
|
]))
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// Nested
|
||||||
|
let value = ArrayEntity.of(AlternativesEntity.accepting(
|
||||||
|
IntegerEntity,
|
||||||
|
ArrayEntity.of(IntegerEntity)
|
||||||
|
)).grammar.parse("((1, 2), (3, 4), 5)")
|
||||||
|
expect(value.toString()).toEqual("((1,2),(3,4),5)")
|
||||||
|
expect(value).toEqual(new ArrayEntity([
|
||||||
|
new ArrayEntity([new IntegerEntity(1), new IntegerEntity(2)]),
|
||||||
|
new ArrayEntity([new IntegerEntity(3), new IntegerEntity(4)]),
|
||||||
|
new IntegerEntity(5),
|
||||||
|
]))
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const grammar = ArrayEntity.grammar
|
||||||
|
let value = grammar.parse('(((1, "2"), (3, 4)), "5")')
|
||||||
|
expect(value.toString()).toEqual('(((1,"2"),(3,4)),"5")')
|
||||||
|
expect(value).toEqual(new ArrayEntity([
|
||||||
|
new ArrayEntity([
|
||||||
|
new ArrayEntity([new NumberEntity(1), new StringEntity("2")]),
|
||||||
|
new ArrayEntity([new NumberEntity(3), new NumberEntity(4)])
|
||||||
]),
|
]),
|
||||||
second: new GuidEntity("00000000000000000000000000000000"),
|
new StringEntity("5")
|
||||||
}),
|
]))
|
||||||
new UnknownKeysEntity({
|
}
|
||||||
lookbehind: new StringEntity("B"),
|
{
|
||||||
key: new StringEntity("hello"),
|
let value = ArrayEntity.grammar.parse(`(
|
||||||
})
|
One(a = (1,(2,(3,(4)))), b = ()),
|
||||||
]))
|
)`)
|
||||||
|
expect(value.toString()).toEqual("(One(a=(1,(2,(3,(4)))),b=()),)")
|
||||||
// Nested
|
expect(value).toEqual(new ArrayEntity([
|
||||||
expect(grammar.parse("((1, 2), (3, 4))")).toEqual(new ArrayEntity([
|
new UnknownKeysEntity({
|
||||||
new ArrayEntity([new NumberEntity(1), new NumberEntity(2)]),
|
lookbehind: "One",
|
||||||
new ArrayEntity([new NumberEntity(3), new NumberEntity(4)]),
|
a: new ArrayEntity([
|
||||||
]))
|
new NumberEntity(1),
|
||||||
expect(grammar.parse('(((1, 2), (3, 4)), 5)')).toEqual(new ArrayEntity([
|
|
||||||
new ArrayEntity([
|
|
||||||
new ArrayEntity([new NumberEntity(1), new NumberEntity(2)]),
|
|
||||||
new ArrayEntity([new NumberEntity(3), new NumberEntity(4)])
|
|
||||||
]),
|
|
||||||
new NumberEntity(5)
|
|
||||||
]))
|
|
||||||
expect(grammar.parse(`(
|
|
||||||
One(a = (1,(2,(3,(4)))), b = ()),
|
|
||||||
)`)).toEqual(new ArrayEntity([
|
|
||||||
new UnknownKeysEntity({
|
|
||||||
lookbehind: "One",
|
|
||||||
a: new ArrayEntity([
|
|
||||||
new NumberEntity(1),
|
|
||||||
new ArrayEntity([
|
|
||||||
new NumberEntity(2),
|
|
||||||
new ArrayEntity([
|
new ArrayEntity([
|
||||||
new NumberEntity(3),
|
new NumberEntity(2),
|
||||||
new ArrayEntity([
|
new ArrayEntity([
|
||||||
new NumberEntity(4),
|
new NumberEntity(3),
|
||||||
|
new ArrayEntity([
|
||||||
|
new NumberEntity(4),
|
||||||
|
])
|
||||||
])
|
])
|
||||||
])
|
])
|
||||||
])
|
]),
|
||||||
]),
|
b: new NullEntity(),
|
||||||
b: null,
|
}),
|
||||||
}),
|
]))
|
||||||
]))
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Boolean", () => {
|
test("Boolean", () => {
|
||||||
let grammar = BooleanEntity.grammar
|
let grammar = BooleanEntity.grammar
|
||||||
expect(grammar.parse("true")).toEqual(new BooleanEntity(true))
|
let value = grammar.parse("true")
|
||||||
expect(grammar.parse("True")).toEqual(new BooleanEntity(true))
|
expect(value).toEqual(new BooleanEntity(true))
|
||||||
expect(grammar.parse("false")).toEqual(new BooleanEntity(false))
|
expect(value).toBeInstanceOf(BooleanEntity)
|
||||||
expect(grammar.parse("False")).toEqual(new BooleanEntity(false))
|
expect(value.toString()).toEqual("True")
|
||||||
|
value = grammar.parse("True")
|
||||||
|
expect(value).toEqual(new BooleanEntity(true))
|
||||||
|
expect(value.toString()).toEqual("True")
|
||||||
|
value = grammar.parse("false")
|
||||||
|
expect(value).toEqual(new BooleanEntity(false))
|
||||||
|
expect(value.toString()).toEqual("False")
|
||||||
|
value = grammar.parse("False")
|
||||||
|
expect(value).toEqual(new BooleanEntity(false))
|
||||||
|
expect(value.toString()).toEqual("False")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("FormatTextEntity", () => {
|
test("FormatTextEntity", () => {
|
||||||
let grammar = FormatTextEntity.grammar
|
let grammar = FormatTextEntity.grammar
|
||||||
expect(
|
|
||||||
grammar.parse(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")`)
|
|
||||||
.print()
|
|
||||||
).toBe("Out Hit Blocking Hit")
|
|
||||||
expect(
|
|
||||||
grammar.parse(`LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Hit Bone Name")`)
|
|
||||||
.print()
|
|
||||||
).toBe("Out Hit Hit Bone Name")
|
|
||||||
expect(
|
|
||||||
grammar.parse(String.raw`LOCGEN_FORMAT_ORDERED(
|
|
||||||
NSLOCTEXT(
|
|
||||||
"PCGSettings",
|
|
||||||
"OverridableParamPinTooltip",
|
|
||||||
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
|
|
||||||
),
|
|
||||||
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
|
|
||||||
"float",
|
|
||||||
"InRangeMin"
|
|
||||||
)`)
|
|
||||||
.print()
|
|
||||||
|
|
||||||
).toBe(`If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\nAttribute type is "float" and its exact name is "InRangeMin"`)
|
let value = grammar.parse('LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")')
|
||||||
|
expect(value.print()).toEqual("Out Hit Blocking Hit")
|
||||||
|
expect(value.toString())
|
||||||
|
.toEqual('LOCGEN_FORMAT_NAMED(NSLOCTEXT("KismetSchema", "SplitPinFriendlyNameFormat", "{PinDisplayName} {ProtoPinDisplayName}"), "PinDisplayName", "Out Hit", "ProtoPinDisplayName", "Blocking Hit")')
|
||||||
|
|
||||||
|
value = grammar.parse(String.raw`LOCGEN_FORMAT_ORDERED(
|
||||||
|
NSLOCTEXT(
|
||||||
|
"PCGSettings",
|
||||||
|
"OverridableParamPinTooltip",
|
||||||
|
"{0}Attribute type is \"{1}\" and its exact name is \"{2}\""
|
||||||
|
),
|
||||||
|
"If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n",
|
||||||
|
"float",
|
||||||
|
"InRangeMin"
|
||||||
|
)`)
|
||||||
|
expect(value.print())
|
||||||
|
.toEqual(`If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\nAttribute type is "float" and its exact name is "InRangeMin"`)
|
||||||
|
expect(value.toString())
|
||||||
|
.toEqual(String.raw`LOCGEN_FORMAT_ORDERED(NSLOCTEXT("PCGSettings", "OverridableParamPinTooltip", "{0}Attribute type is \"{1}\" and its exact name is \"{2}\""), "If InRangeMin = InRangeMax, then that density value is mapped to the average of OutRangeMin and OutRangeMax\n", "float", "InRangeMin")`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("GuidEntity", () => {
|
test("GuidEntity", () => {
|
||||||
let serializer = SerializerFactory.getSerializer(GuidEntity)
|
let grammar = GuidEntity.grammar
|
||||||
|
|
||||||
let guid = serializer.read("0556a3ecabf648d0a5c07b2478e9dd32")
|
let value = grammar.parse("0556a3ecabf648d0a5c07b2478e9dd32")
|
||||||
expect(guid).toBeInstanceOf(GuidEntity)
|
expect(value).toBeInstanceOf(GuidEntity)
|
||||||
expect(guid.value).toBe("0556a3ecabf648d0a5c07b2478e9dd32")
|
expect(value).toEqual(new GuidEntity("0556a3ecabf648d0a5c07b2478e9dd32"))
|
||||||
|
expect(value.toString()).toEqual("0556a3ecabf648d0a5c07b2478e9dd32")
|
||||||
|
|
||||||
guid = serializer.read("64023BC344E0453DBB583FAC411489BC")
|
value = grammar.parse("64023BC344E0453DBB583FAC411489BC")
|
||||||
expect(guid).toBeInstanceOf(GuidEntity)
|
expect(value).toBeInstanceOf(GuidEntity)
|
||||||
expect(guid.value).toBe("64023BC344E0453DBB583FAC411489BC")
|
expect(value).toEqual(new GuidEntity("64023BC344E0453DBB583FAC411489BC"))
|
||||||
|
expect(value.toString()).toEqual("64023BC344E0453DBB583FAC411489BC")
|
||||||
|
|
||||||
guid = serializer.read("6edC4a425ca948da8bC78bA52DED6C6C")
|
value = grammar.parse("6edC4a425ca948da8bC78bA52DED6C6C")
|
||||||
expect(guid).toBeInstanceOf(GuidEntity)
|
expect(value).toBeInstanceOf(GuidEntity)
|
||||||
expect(guid.value).toBe("6edC4a425ca948da8bC78bA52DED6C6C")
|
expect(value).toEqual(new GuidEntity("6edC4a425ca948da8bC78bA52DED6C6C"))
|
||||||
|
expect(value.toString()).toEqual("6edC4a425ca948da8bC78bA52DED6C6C")
|
||||||
|
|
||||||
expect(() => serializer.read("172087193 9B04362973544B3564FDB2C")).toThrow()
|
expect(() => grammar.parse("172087193 9B04362973544B3564FDB2C")).toThrow()
|
||||||
expect(() => serializer.read("E25F14F8F3E9441AB07153E7DA2BA2B")).toThrow()
|
expect(() => grammar.parse("E25F14F8F3E9441AB07153E7DA2BA2B")).toThrow()
|
||||||
expect(() => serializer.read("A78988B0097E48418C8CB87EC5A67ABF7")).toThrow()
|
expect(() => grammar.parse("A78988B0097E48418C8CB87EC5A67ABF7")).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
test("IntegerEntity", () => {
|
test("IntegerEntity", () => {
|
||||||
let serializer = SerializerFactory.getSerializer(IntegerEntity)
|
let grammar = IntegerEntity.grammar
|
||||||
|
|
||||||
let integer = serializer.read("0")
|
let integer = grammar.parse("0")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(0))
|
||||||
expect(integer.value).toStrictEqual(0)
|
expect(integer).not.toEqual(new NaturalNumberEntity(0))
|
||||||
|
|
||||||
integer = serializer.read("+0")
|
integer = grammar.parse("+0")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(0))
|
||||||
expect(integer.value).toStrictEqual(0)
|
|
||||||
|
|
||||||
integer = serializer.read("-0")
|
integer = grammar.parse("-0")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(0))
|
||||||
expect(integer.value).toStrictEqual(0)
|
|
||||||
|
|
||||||
integer = serializer.read("99")
|
integer = grammar.parse("99")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(99))
|
||||||
expect(integer.value).toStrictEqual(99)
|
|
||||||
|
|
||||||
integer = serializer.read("-8685")
|
integer = grammar.parse("-8685")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(-8685))
|
||||||
expect(integer.value).toStrictEqual(-8685)
|
|
||||||
|
|
||||||
integer = serializer.read("+555")
|
integer = grammar.parse("+555")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(555))
|
||||||
expect(integer.value).toStrictEqual(555)
|
|
||||||
|
|
||||||
integer = serializer.read("1000000000")
|
integer = grammar.parse("1000000000")
|
||||||
expect(integer).toBeInstanceOf(IntegerEntity)
|
expect(integer).toEqual(new IntegerEntity(1000000000))
|
||||||
expect(integer.value).toStrictEqual(1000000000)
|
|
||||||
|
|
||||||
expect(() => serializer.read("1.2").value).toThrow()
|
expect(() => grammar.parse("1.2").value).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
test("KeyBindingEntity", () => {
|
test("KeyBindingEntity", () => {
|
||||||
Reference in New Issue
Block a user