mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-16 02:10:38 +08:00
Entities tests fixed
This commit is contained in:
67
dist/ueblueprint.js
vendored
67
dist/ueblueprint.js
vendored
@@ -2528,7 +2528,7 @@ class IEntity {
|
||||
* @template {typeof IEntity} T
|
||||
* @this {T}
|
||||
*/
|
||||
static withDefault(value = /** @type {(type: T) => InstanceType<T>} */(type => new type())) {
|
||||
static withDefault(value = /** @type {(type: T) => (InstanceType<T> | NullEntity)} */(type => new type())) {
|
||||
const result = this.asUniqueClass();
|
||||
result.default = value;
|
||||
return result
|
||||
@@ -2636,12 +2636,20 @@ class IEntity {
|
||||
}
|
||||
const thisKeys = Object.keys(this);
|
||||
const otherKeys = Object.keys(other);
|
||||
if (thisKeys.length != otherKeys.length || !(this instanceof other.constructor) && !(other instanceof this.constructor)) {
|
||||
if (
|
||||
thisKeys.length !== otherKeys.length
|
||||
|| this.lookbehind != other.lookbehind
|
||||
|| !(this instanceof other.constructor) && !(other instanceof this.constructor)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
for (let i = 0; i < thisKeys.length; ++i) {
|
||||
const a = this[thisKeys[i]];
|
||||
const b = other[otherKeys[i]];
|
||||
const k = thisKeys[i];
|
||||
if (!otherKeys.includes(k)) {
|
||||
return false
|
||||
}
|
||||
const a = this[k];
|
||||
const b = other[k];
|
||||
if (a instanceof IEntity) {
|
||||
if (!a.equals(b)) {
|
||||
return false
|
||||
@@ -2655,18 +2663,22 @@ class IEntity {
|
||||
return true
|
||||
}
|
||||
|
||||
/** @this {IEntity | Array} */
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
Self = this.Self(),
|
||||
printKey = Self.printKey,
|
||||
wrap = Self.wrap,
|
||||
) {
|
||||
const Self = this.Self();
|
||||
let result = "";
|
||||
let first = true;
|
||||
for (const key of this.keys) {
|
||||
const keys = this instanceof IEntity ? this.keys : Object.keys(this);
|
||||
for (const key of keys) {
|
||||
/** @type {IEntity} */
|
||||
const value = this[key];
|
||||
if (value === undefined || !this.showProperty(key)) {
|
||||
let keyValue = this instanceof Array ? `(${key})` : key;
|
||||
if (value === undefined || this instanceof IEntity && !this.showProperty(key)) {
|
||||
continue
|
||||
}
|
||||
if (first) {
|
||||
@@ -2674,27 +2686,30 @@ class IEntity {
|
||||
} else {
|
||||
result += Self.attributeSeparator;
|
||||
}
|
||||
if (Self.inlined) {
|
||||
result += value.toString(insideString, indentation, k => printKey(`${key}.${k}`));
|
||||
if (value.Self?.().inlined) {
|
||||
const inlinedPrintKey = value.Self().className() === "ArrayEntity"
|
||||
? k => printKey(`${keyValue}${k}`)
|
||||
: k => printKey(`${keyValue}.${k}`);
|
||||
result += value.toString(insideString, indentation, Self, inlinedPrintKey, Self.notWrapped);
|
||||
continue
|
||||
}
|
||||
let keyValue = printKey(key);
|
||||
keyValue = printKey(keyValue);
|
||||
if (keyValue.length) {
|
||||
if (Self.quoted) {
|
||||
keyValue = `"${keyValue}"`;
|
||||
}
|
||||
result += (Self.attributeSeparator.includes("\n") ? indentation : "") + keyValue + Self.keySeparator;
|
||||
}
|
||||
let serialization = value?.toString(insideString, indentation, printKey);
|
||||
let serialization = value?.toString(insideString, indentation);
|
||||
if (Self.serialized) {
|
||||
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`;
|
||||
}
|
||||
result += serialization;
|
||||
}
|
||||
if (Self.trailing && result.length) {
|
||||
if (this instanceof IEntity && this.trailing && result.length) {
|
||||
result += Self.attributeSeparator;
|
||||
}
|
||||
return Self.wrap(this, result)
|
||||
return wrap(this, result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4598,9 +4613,14 @@ class ArrayEntity extends IEntity {
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
Self = this.Self(),
|
||||
printKey = Self.printKey,
|
||||
wrap = Self.wrap,
|
||||
) {
|
||||
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator);
|
||||
if (this.Self().inlined) {
|
||||
return super.toString.bind(this.values, insideString, indentation, Self, printKey, wrap)()
|
||||
}
|
||||
let result = this.values.map(v => v?.toString(insideString)).join(this.Self().attributeSeparator);
|
||||
if (this.trailing) {
|
||||
result += this.Self().attributeSeparator;
|
||||
}
|
||||
@@ -5135,6 +5155,7 @@ class FunctionReferenceEntity extends IEntity {
|
||||
MemberName: StringEntity,
|
||||
MemberGuid: GuidEntity,
|
||||
}
|
||||
/** @type {P<FunctionReferenceEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this)
|
||||
|
||||
constructor(values) {
|
||||
@@ -5152,7 +5173,7 @@ class PinTypeEntity extends IEntity {
|
||||
PinCategory: StringEntity.withDefault(),
|
||||
PinSubCategory: StringEntity.withDefault(),
|
||||
PinSubCategoryObject: ObjectReferenceEntity.withDefault(),
|
||||
PinSubCategoryMemberReference: FunctionReferenceEntity.withDefault(type => null),
|
||||
PinSubCategoryMemberReference: FunctionReferenceEntity.withDefault(),
|
||||
PinValueType: PinTypeEntity.withDefault(),
|
||||
ContainerType: SymbolEntity,
|
||||
bIsReference: BooleanEntity.withDefault(),
|
||||
@@ -5196,6 +5217,7 @@ class Vector2DEntity extends IEntity {
|
||||
X: NumberEntity.withDefault(),
|
||||
Y: NumberEntity.withDefault(),
|
||||
}
|
||||
/** @type {P<Vector2DEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
|
||||
|
||||
constructor(values) {
|
||||
@@ -5379,6 +5401,7 @@ class VectorEntity extends IEntity {
|
||||
Y: NumberEntity.withDefault(),
|
||||
Z: NumberEntity.withDefault(),
|
||||
}
|
||||
/** @type {P<VectorEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
|
||||
|
||||
constructor(values) {
|
||||
@@ -5433,7 +5456,7 @@ class SimpleSerializationVectorEntity extends VectorEntity {
|
||||
/** @template {IEntity} T */
|
||||
class PinEntity extends IEntity {
|
||||
|
||||
static lookbehind = "INVTEXT"
|
||||
static lookbehind = "Pin"
|
||||
static #typeEntityMap = {
|
||||
[Configuration.paths.linearColor]: LinearColorEntity,
|
||||
[Configuration.paths.rotator]: RotatorEntity,
|
||||
@@ -5490,6 +5513,7 @@ class PinEntity extends IEntity {
|
||||
bAdvancedView: BooleanEntity.withDefault(),
|
||||
bOrphanedPin: BooleanEntity.withDefault(),
|
||||
}
|
||||
/** @type {P<PinEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this)
|
||||
|
||||
#recomputesNodeTitleOnChange = false
|
||||
@@ -13327,6 +13351,7 @@ class TerminalTypeEntity extends IEntity {
|
||||
|
||||
class UnknownKeysEntity extends IEntity {
|
||||
|
||||
/** @type {P<UnknownKeysEntity>} */
|
||||
static grammar = Parsernostrum.seq(
|
||||
// Lookbehind
|
||||
Parsernostrum.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1),
|
||||
@@ -13433,16 +13458,16 @@ function initializeSerializerFactory() {
|
||||
InvariantTextEntity.grammar,
|
||||
FormatTextEntity.grammar,
|
||||
PinReferenceEntity.grammar,
|
||||
Vector2DEntity.grammar,
|
||||
VectorEntity.grammar,
|
||||
Vector4DEntity.grammar,
|
||||
VectorEntity.grammar,
|
||||
Vector2DEntity.grammar,
|
||||
RotatorEntity.grammar,
|
||||
LinearColorEntity.grammar,
|
||||
UnknownKeysEntity.grammar,
|
||||
SymbolEntity.grammar,
|
||||
ArrayEntity.of(PinReferenceEntity).grammar,
|
||||
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
|
||||
Parsernostrum.lazy(() => ArrayEntity.createGrammar(Grammar.unknownValue)),
|
||||
Parsernostrum.lazy(() => ArrayEntity.createGrammar(IEntity.unknownEntityGrammar)),
|
||||
);
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
|
||||
6
dist/ueblueprint.min.js
vendored
6
dist/ueblueprint.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -64,9 +64,14 @@ export default class ArrayEntity extends IEntity {
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
Self = this.Self(),
|
||||
printKey = Self.printKey,
|
||||
wrap = Self.wrap,
|
||||
) {
|
||||
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator)
|
||||
if (this.Self().inlined) {
|
||||
return super.toString.bind(this.values, insideString, indentation, Self, printKey, wrap)()
|
||||
}
|
||||
let result = this.values.map(v => v?.toString(insideString)).join(this.Self().attributeSeparator)
|
||||
if (this.trailing) {
|
||||
result += this.Self().attributeSeparator
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import P from "parsernostrum"
|
||||
import Grammar from "../serialization/Grammar.js"
|
||||
import GuidEntity from "./GuidEntity.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
@@ -12,6 +13,7 @@ export default class FunctionReferenceEntity extends IEntity {
|
||||
MemberName: StringEntity,
|
||||
MemberGuid: GuidEntity,
|
||||
}
|
||||
/** @type {P<FunctionReferenceEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this)
|
||||
|
||||
constructor(values) {
|
||||
|
||||
@@ -135,7 +135,7 @@ export default class IEntity {
|
||||
* @template {typeof IEntity} T
|
||||
* @this {T}
|
||||
*/
|
||||
static withDefault(value = /** @type {(type: T) => InstanceType<T>} */(type => new type())) {
|
||||
static withDefault(value = /** @type {(type: T) => (InstanceType<T> | NullEntity)} */(type => new type())) {
|
||||
const result = this.asUniqueClass()
|
||||
result.default = value
|
||||
return result
|
||||
@@ -270,18 +270,22 @@ export default class IEntity {
|
||||
return true
|
||||
}
|
||||
|
||||
/** @this {IEntity | Array} */
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
Self = this.Self(),
|
||||
printKey = Self.printKey,
|
||||
wrap = Self.wrap,
|
||||
) {
|
||||
const Self = this.Self()
|
||||
let result = ""
|
||||
let first = true
|
||||
for (const key of this.keys) {
|
||||
const keys = this instanceof IEntity ? this.keys : Object.keys(this)
|
||||
for (const key of keys) {
|
||||
/** @type {IEntity} */
|
||||
const value = this[key]
|
||||
if (value === undefined || !this.showProperty(key)) {
|
||||
let keyValue = this instanceof Array ? `(${key})` : key
|
||||
if (value === undefined || this instanceof IEntity && !this.showProperty(key)) {
|
||||
continue
|
||||
}
|
||||
if (first) {
|
||||
@@ -289,26 +293,29 @@ export default class IEntity {
|
||||
} else {
|
||||
result += Self.attributeSeparator
|
||||
}
|
||||
if (Self.inlined) {
|
||||
result += value.toString(insideString, indentation, k => printKey(`${key}.${k}`))
|
||||
if (value.Self?.().inlined) {
|
||||
const inlinedPrintKey = value.Self().className() === "ArrayEntity"
|
||||
? k => printKey(`${keyValue}${k}`)
|
||||
: k => printKey(`${keyValue}.${k}`)
|
||||
result += value.toString(insideString, indentation, Self, inlinedPrintKey, Self.notWrapped)
|
||||
continue
|
||||
}
|
||||
let keyValue = printKey(key)
|
||||
keyValue = printKey(keyValue)
|
||||
if (keyValue.length) {
|
||||
if (Self.quoted) {
|
||||
keyValue = `"${keyValue}"`
|
||||
}
|
||||
result += (Self.attributeSeparator.includes("\n") ? indentation : "") + keyValue + Self.keySeparator
|
||||
}
|
||||
let serialization = value?.toString(insideString, indentation, printKey)
|
||||
let serialization = value?.toString(insideString, indentation)
|
||||
if (Self.serialized) {
|
||||
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`
|
||||
}
|
||||
result += serialization
|
||||
}
|
||||
if (this.trailing && result.length) {
|
||||
if (this instanceof IEntity && this.trailing && result.length) {
|
||||
result += Self.attributeSeparator
|
||||
}
|
||||
return Self.wrap(this, result)
|
||||
return wrap(this, result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,12 @@ import StringEntity from "./StringEntity.js"
|
||||
import Vector2DEntity from "./Vector2DEntity.js"
|
||||
import Vector4DEntity from "./Vector4DEntity.js"
|
||||
import VectorEntity from "./VectorEntity.js"
|
||||
import P from "parsernostrum"
|
||||
|
||||
/** @template {IEntity} T */
|
||||
export default class PinEntity extends IEntity {
|
||||
|
||||
static lookbehind = "INVTEXT"
|
||||
static lookbehind = "Pin"
|
||||
static #typeEntityMap = {
|
||||
[Configuration.paths.linearColor]: LinearColorEntity,
|
||||
[Configuration.paths.rotator]: RotatorEntity,
|
||||
@@ -92,6 +93,7 @@ export default class PinEntity extends IEntity {
|
||||
bAdvancedView: BooleanEntity.withDefault(),
|
||||
bOrphanedPin: BooleanEntity.withDefault(),
|
||||
}
|
||||
/** @type {P<PinEntity>} */
|
||||
static grammar = Grammar.createEntityGrammar(this)
|
||||
|
||||
#recomputesNodeTitleOnChange = false
|
||||
|
||||
@@ -13,7 +13,7 @@ export default class PinTypeEntity extends IEntity {
|
||||
PinCategory: StringEntity.withDefault(),
|
||||
PinSubCategory: StringEntity.withDefault(),
|
||||
PinSubCategoryObject: ObjectReferenceEntity.withDefault(),
|
||||
PinSubCategoryMemberReference: FunctionReferenceEntity.withDefault(type => null),
|
||||
PinSubCategoryMemberReference: FunctionReferenceEntity.withDefault(),
|
||||
PinValueType: PinTypeEntity.withDefault(),
|
||||
ContainerType: SymbolEntity,
|
||||
bIsReference: BooleanEntity.withDefault(),
|
||||
|
||||
69
tests/customEntities.spec.js
Normal file
69
tests/customEntities.spec.js
Normal file
@@ -0,0 +1,69 @@
|
||||
// @ts-nocheck
|
||||
|
||||
import StringEntity from "../js/entity/StringEntity.js"
|
||||
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
||||
import { expect, test } from "./fixtures/test.js"
|
||||
import Entity1 from "./resources/Entity1.js"
|
||||
import Entity2 from "./resources/Entity2.js"
|
||||
import Entity3 from "./resources/Entity3.js"
|
||||
import Entity4 from "./resources/Entity4.js"
|
||||
import entity2Value from "./resources/serializedEntity2.js"
|
||||
import entity2Value1 from "./resources/serializedEntity2-1.js"
|
||||
import entity3Value from "./resources/serializedEntity3.js"
|
||||
import entity4Value from "./resources/serializedEntity4.js"
|
||||
|
||||
test.beforeAll(() => initializeSerializerFactory())
|
||||
|
||||
test.describe.configure({ mode: "parallel" })
|
||||
|
||||
test("Entity2", () => {
|
||||
const value = new Entity2()
|
||||
expect(Object.keys(value)).toHaveLength(9)
|
||||
expect(value.toString()).toEqual(entity2Value)
|
||||
const other = new Entity2({ someString2: new StringEntity("gamma") })
|
||||
expect(value.equals(other)).toBeFalsy()
|
||||
other.someString2 = new StringEntity("beta")
|
||||
expect(value.equals(other)).toBeTruthy()
|
||||
})
|
||||
|
||||
test("Entity2-1", () => {
|
||||
Entity2.attributes.someEntity = Entity2.attributes.someEntity.flagInlined()
|
||||
const value = new Entity2()
|
||||
expect(value.toString()).toEqual(entity2Value1)
|
||||
})
|
||||
|
||||
test("Entity3", () => {
|
||||
let value = new Entity3()
|
||||
const keys = [
|
||||
"alpha",
|
||||
"bravo",
|
||||
"charlie",
|
||||
"delta",
|
||||
"echo",
|
||||
"foxtrot",
|
||||
"golf",
|
||||
"hotel",
|
||||
"india",
|
||||
"juliett",
|
||||
"kilo",
|
||||
// "lima", // Not defined by default
|
||||
"mike",
|
||||
"november",
|
||||
"oscar",
|
||||
"papa",
|
||||
// "quebec", // Not defined by default
|
||||
"romeo",
|
||||
"sierra",
|
||||
]
|
||||
expect(Object.keys(value)).toStrictEqual(keys)
|
||||
expect(value.toString()).toEqual(entity3Value)
|
||||
})
|
||||
|
||||
test("Entity4", () => {
|
||||
Entity1.attributeSeparator = " - "
|
||||
Entity1.keySeparator = ":"
|
||||
Entity1.printKey = k => k.toUpperCase()
|
||||
Entity1.wrap = (entity, v) => `E1[${v}]`
|
||||
const entity = new Entity4()
|
||||
expect(entity.toString()).toEqual(entity4Value)
|
||||
})
|
||||
@@ -1,169 +0,0 @@
|
||||
// @ts-nocheck
|
||||
|
||||
import NumberEntity from "../js/entity/NumberEntity.js"
|
||||
import UnknownKeysEntity from "../js/entity/UnknownKeysEntity.js"
|
||||
import Grammar from "../js/serialization/Grammar.js"
|
||||
import ObjectSerializer from "../js/serialization/ObjectSerializer.js"
|
||||
import Serializer from "../js/serialization/Serializer.js"
|
||||
import SerializerFactory from "../js/serialization/SerializerFactory.js"
|
||||
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
||||
import { expect, test } from "./fixtures/test.js"
|
||||
import Entity1 from "./resources/Entity1.js"
|
||||
import Entity2 from "./resources/Entity2.js"
|
||||
import Entity3 from "./resources/Entity3.js"
|
||||
import Entity4 from "./resources/Entity4.js"
|
||||
import Entity5 from "./resources/Entity5.js"
|
||||
import EntityF from "./resources/EntityF.js"
|
||||
import entity2Value from "./resources/serializedEntity2.js"
|
||||
import entity3Value from "./resources/serializedEntity3.js"
|
||||
import entity4Value from "./resources/serializedEntity4.js"
|
||||
import entity5Value1 from "./resources/serializedEntity5-1.js"
|
||||
|
||||
test.describe.configure({ mode: "parallel" })
|
||||
|
||||
test("Entity2", () => {
|
||||
const entity = new Entity2()
|
||||
initializeSerializerFactory()
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity2,
|
||||
new Serializer(Entity2, (entity, v) => `{\n${v}\n}`, "\n", false, ": ", k => ` ${k}`)
|
||||
)
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity1,
|
||||
new Serializer(Entity1, (entity, v) => `Entity1(${v})`, ", ", false, "=",)
|
||||
)
|
||||
expect(Object.keys(entity)).toHaveLength(9)
|
||||
expect(entity.someNumber).toBe(567)
|
||||
expect(entity.someString).toBe("alpha")
|
||||
expect(entity.someString2).toBe("beta")
|
||||
expect(entity.someBoolean).toBe(true)
|
||||
expect(entity.someBoolean2).toBe(false)
|
||||
expect(entity.someObjectString).toBe("gamma")
|
||||
expect(entity.someArray).toStrictEqual([400, 500, 600, 700, 800])
|
||||
expect(entity.someArray2).toStrictEqual([400, 500, 600, 700, 800])
|
||||
|
||||
expect(entity.equals(new Entity2())).toBeTruthy()
|
||||
|
||||
const other = new Entity2({
|
||||
someString2: "gamma"
|
||||
})
|
||||
expect(entity.equals(other)).toBe(false)
|
||||
const other1 = new Entity2({
|
||||
someNumber: 123,
|
||||
someString: "a",
|
||||
someString2: "b",
|
||||
someBoolean: false,
|
||||
someBoolean2: false,
|
||||
someObjectString: new String("delta"),
|
||||
someArray: [-1, -2, -3],
|
||||
})
|
||||
const other2 = new Entity2({
|
||||
someNumber: 123,
|
||||
someString: "a",
|
||||
someString2: "b",
|
||||
someBoolean: false,
|
||||
someBoolean2: false,
|
||||
someObjectString: "delta",
|
||||
someArray: [-1, -2, -3],
|
||||
})
|
||||
expect(other1.equals(other2)).toBeTruthy()
|
||||
expect(SerializerFactory.getSerializer(Entity2).write(entity)).toBe(entity2Value)
|
||||
expect(Grammar.getAttribute(Entity2, ["someEntity", "a"]).type).toBe(Number)
|
||||
})
|
||||
|
||||
test("Entity3", () => {
|
||||
let entity = new Entity3()
|
||||
const keys = [
|
||||
"alpha",
|
||||
"bravo",
|
||||
"charlie",
|
||||
"delta",
|
||||
"echo",
|
||||
"foxtrot",
|
||||
"golf",
|
||||
"hotel",
|
||||
"india",
|
||||
"juliett",
|
||||
"kilo",
|
||||
// "lima", // Not defined by default
|
||||
"mike",
|
||||
"november",
|
||||
"oscar",
|
||||
"papa",
|
||||
"quebec",
|
||||
"romeo",
|
||||
"sierra",
|
||||
]
|
||||
initializeSerializerFactory()
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity3,
|
||||
new Serializer(Entity3, (entity, v) => `[[\n${v}\n]]`, "\n", false, ": ", k => ` ${k}`)
|
||||
)
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity1,
|
||||
new Serializer(Entity1, (entity, v) => `Entity1(${v})`, ", ", false, "=",)
|
||||
)
|
||||
expect(Object.keys(entity)).toHaveLength(keys.length)
|
||||
expect(Object.keys(entity)).toStrictEqual(keys)
|
||||
expect(entity.alpha).toEqual(new NumberEntity(32))
|
||||
expect(entity.bravo).toBe(new NumberEntity(78))
|
||||
expect(entity.charlie).toBe("Charlie")
|
||||
expect(entity.delta).toBeNull()
|
||||
expect(entity.echo).toBe("echo")
|
||||
expect(entity.foxtrot).toBe(false)
|
||||
expect(entity.golf).toStrictEqual([])
|
||||
expect(entity.hotel).toBeNull()
|
||||
expect(entity.india).toStrictEqual([])
|
||||
expect(entity.juliett).toStrictEqual(["a", "b", "c", "d", "e"])
|
||||
expect(entity.kilo).toStrictEqual([true, false, false, true, true])
|
||||
expect(entity.mike).toBe("Bar")
|
||||
expect(entity.november).toBe(new NumberEntity())
|
||||
expect(entity.oscar).toStrictEqual(new Entity1({ a: 8, b: 9 }))
|
||||
expect(entity.papa).toStrictEqual(new Entity1({ a: 12, b: 13 }))
|
||||
|
||||
expect(SerializerFactory.getSerializer(Entity3).write(entity)).toBe(entity3Value)
|
||||
|
||||
expect(Grammar.getAttribute(Entity3, ["romeo", "b"]).type).toBe(Number)
|
||||
expect(Grammar.getAttribute(Entity3, ["sierra", "someString2"]).type).toBe(String)
|
||||
expect(Grammar.getAttribute(Entity3, ["sierra", "someObjectString"]).type).toBe(String)
|
||||
expect(Grammar.getAttribute(Entity3, ["sierra", "someObjectString"]).type).toBe(String)
|
||||
expect(Grammar.getAttribute(Entity3, ["sierra", "someEntity", "b"]).type).toBe(Number)
|
||||
})
|
||||
|
||||
test("Entity4", () => {
|
||||
const entity = new Entity4()
|
||||
initializeSerializerFactory()
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity1,
|
||||
new Serializer(Entity1, (entity, v) => `E1[${v}]`, " - ", false, ":", k => k.toUpperCase())
|
||||
)
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity4,
|
||||
new Serializer(Entity4, (entity, v) => `Begin\n${v}\nEnd`, "\n", false, " => ", k => ` \${${k}}`)
|
||||
)
|
||||
expect(Entity4.attributes.second.type).toStrictEqual([Entity1])
|
||||
expect(SerializerFactory.getSerializer(Entity4).write(entity)).toBe(entity4Value)
|
||||
})
|
||||
|
||||
test("Entity5", () => {
|
||||
let entity = new Entity5()
|
||||
initializeSerializerFactory()
|
||||
SerializerFactory.registerSerializer(
|
||||
Entity5,
|
||||
new ObjectSerializer(Entity5)
|
||||
)
|
||||
SerializerFactory.registerSerializer(
|
||||
EntityF,
|
||||
new Serializer(UnknownKeysEntity, (entity, string) => `${entity.lookbehind ?? ""}(${string})`)
|
||||
)
|
||||
expect(entity = SerializerFactory.getSerializer(Entity5).read(entity5Value1)).toEqual(new Entity5({
|
||||
key1: "Value 1",
|
||||
key2: new EntityF({
|
||||
lookbehind: "Foo",
|
||||
arg1: 55,
|
||||
arg2: "Argument 2",
|
||||
}),
|
||||
}))
|
||||
expect(entity.key2).toBeInstanceOf(EntityF)
|
||||
expect(SerializerFactory.getSerializer(Entity5).write(entity)).toBe(entity5Value1)
|
||||
})
|
||||
@@ -3,12 +3,11 @@ import NumberEntity from "../../js/entity/NumberEntity.js"
|
||||
|
||||
export default class Entity1 extends IEntity {
|
||||
|
||||
static attributeSeparator = ", "
|
||||
static wrap = (entity, v) => `Entity1(${v})`
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
a: NumberEntity.withDefault(type => new type(8)),
|
||||
b: NumberEntity.withDefault(type => new type(9)),
|
||||
}
|
||||
|
||||
constructor(values = {}) {
|
||||
super(values)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,12 @@ import Entity1 from "./Entity1.js"
|
||||
|
||||
export default class Entity2 extends IEntity {
|
||||
|
||||
static attributeSeparator = "\n"
|
||||
static keySeparator = ": "
|
||||
static printKey = k => ` ${k}`
|
||||
static wrap = (entity, v) => `{\n${v}\n}`
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
someNumber: NumberEntity.withDefault(type => new type(567)),
|
||||
someString: StringEntity.withDefault(type => new type("alpha")),
|
||||
someString2: StringEntity.withDefault(type => new type("beta")),
|
||||
@@ -22,12 +27,12 @@ export default class Entity2 extends IEntity {
|
||||
new NumberEntity(800),
|
||||
])),
|
||||
someArray2: ArrayEntity.of(NumberEntity).withDefault(type => new type([
|
||||
new NumberEntity(400),
|
||||
new NumberEntity(500),
|
||||
new NumberEntity(600),
|
||||
new NumberEntity(700),
|
||||
new NumberEntity(800),
|
||||
])).flagInlined(),
|
||||
new NumberEntity(-400),
|
||||
new NumberEntity(-500),
|
||||
new NumberEntity(-600),
|
||||
new NumberEntity(-700),
|
||||
new NumberEntity(-800),
|
||||
])),
|
||||
someEntity: Entity1.withDefault(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import AlternativesEntity from "../../js/entity/AlternativesEntity.js"
|
||||
import ArrayEntity from "../../js/entity/ArrayEntity.js"
|
||||
import BooleanEntity from "../../js/entity/BooleanEntity.js"
|
||||
import IEntity from "../../js/entity/IEntity.js"
|
||||
import NullEntity from "../../js/entity/NullEntity.js"
|
||||
import NumberEntity from "../../js/entity/NumberEntity.js"
|
||||
import StringEntity from "../../js/entity/StringEntity.js"
|
||||
import Entity1 from "./Entity1.js"
|
||||
@@ -9,15 +10,20 @@ import Entity2 from "./Entity2.js"
|
||||
|
||||
export default class Entity3 extends IEntity {
|
||||
|
||||
static attributeSeparator = "\n"
|
||||
static keySeparator = ": "
|
||||
static printKey = k => ` ${k}`
|
||||
static wrap = (entity, v) => `[[\n${v}\n]]`
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
alpha: NumberEntity.withDefault(type => new type(32)),
|
||||
bravo: NumberEntity.withDefault(type => new type(78)),
|
||||
charlie: StringEntity.withDefault(type => new type("Charlie")),
|
||||
delta: StringEntity.withDefault(type => null),
|
||||
delta: StringEntity.withDefault(type => new NullEntity()),
|
||||
echo: StringEntity.withDefault(type => new type("echo")),
|
||||
foxtrot: BooleanEntity.withDefault(),
|
||||
golf: ArrayEntity.of(StringEntity).withDefault(),
|
||||
hotel: ArrayEntity.of(NumberEntity).withDefault(() => null),
|
||||
hotel: ArrayEntity.of(NumberEntity).withDefault(() => new NullEntity()),
|
||||
india: ArrayEntity.of(NumberEntity).withDefault(),
|
||||
juliett: ArrayEntity.of(StringEntity).withDefault(type => new type([
|
||||
new StringEntity("a"),
|
||||
@@ -34,11 +40,18 @@ export default class Entity3 extends IEntity {
|
||||
new BooleanEntity(true),
|
||||
])),
|
||||
lima: StringEntity,
|
||||
mike: AlternativesEntity.accepting(NumberEntity, StringEntity, ArrayEntity).withDefault(type => new StringEntity("Bar")),
|
||||
november: AlternativesEntity.accepting(NumberEntity, StringEntity, ArrayEntity).withDefault(type => new NumberEntity(0)),
|
||||
oscar: Entity1.withDefault(type => new type()),
|
||||
papa: Entity1.withDefault(type => new type({ a: 12, b: 13 })),
|
||||
quebec: NumberEntity.withDefault(), // will assign undefined because it does not satisfy the predicate,
|
||||
mike: AlternativesEntity
|
||||
.accepting(NumberEntity, StringEntity, ArrayEntity)
|
||||
.withDefault(type => new StringEntity("Bar")),
|
||||
november: AlternativesEntity
|
||||
.accepting(NumberEntity, StringEntity, ArrayEntity)
|
||||
.withDefault(type => new NumberEntity(0)),
|
||||
oscar: Entity1.withDefault(),
|
||||
papa: Entity1.withDefault(type => new type({
|
||||
a: new NumberEntity(12),
|
||||
b: new NumberEntity(13),
|
||||
})),
|
||||
quebec: NumberEntity,
|
||||
romeo: Entity1.withDefault().flagInlined(),
|
||||
sierra: Entity2.withDefault().flagInlined(),
|
||||
}
|
||||
|
||||
@@ -1,33 +1,23 @@
|
||||
import AttributeInfo from "../../js/entity/AttributeInfo.js"
|
||||
import ArrayEntity from "../../js/entity/ArrayEntity.js"
|
||||
import IEntity from "../../js/entity/IEntity.js"
|
||||
import NullEntity from "../../js/entity/NullEntity.js"
|
||||
import NumberEntity from "../../js/entity/NumberEntity.js"
|
||||
import Entity1 from "./Entity1.js"
|
||||
import Entity3 from "./Entity3.js"
|
||||
|
||||
export default class Entity4 extends IEntity {
|
||||
|
||||
static attributeSeparator = "\n"
|
||||
static keySeparator = " => "
|
||||
static printKey = k => ` \${${k}}`
|
||||
static wrap = (entity, v) => `Begin\n${v}\nEnd`
|
||||
static attributes = {
|
||||
first: new AttributeInfo({
|
||||
type: Entity3,
|
||||
default: new Entity3(),
|
||||
inlined: true,
|
||||
}),
|
||||
second: new AttributeInfo({
|
||||
default: [new Entity1({ a: 1, b: 2 }), new Entity1({ a: 11, b: 22 })],
|
||||
inlined: true,
|
||||
}),
|
||||
third: new AttributeInfo({
|
||||
type: Array,
|
||||
default: null,
|
||||
})
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
/** @type {Entity1} */ this.second
|
||||
IEntity.defineAttributes(this.second, {
|
||||
0: {
|
||||
inlined: true,
|
||||
},
|
||||
})
|
||||
...super.attributes,
|
||||
first: Entity3.withDefault().flagInlined(),
|
||||
second: ArrayEntity.of(Entity1).withDefault(type => new type([
|
||||
new (Entity1.flagInlined())({ a: new NumberEntity(1), b: new NumberEntity(2) }),
|
||||
new Entity1({ a: new NumberEntity(11), b: new NumberEntity(22) }),
|
||||
])).flagInlined(),
|
||||
third: ArrayEntity.withDefault(() => new NullEntity()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import AttributeInfo from "../../js/entity/AttributeInfo.js"
|
||||
import IntegerEntity from "../../js/entity/IntegerEntity.js"
|
||||
import ObjectEntity from "../../js/entity/ObjectEntity.js"
|
||||
import EntityF from "./EntityF.js"
|
||||
|
||||
// @ts-expect-error
|
||||
export default class Entity5 extends ObjectEntity {
|
||||
|
||||
static attributes = {
|
||||
key1: AttributeInfo.createType(String),
|
||||
key2: AttributeInfo.createType(EntityF),
|
||||
key3: new AttributeInfo({
|
||||
type: IntegerEntity,
|
||||
default: new IntegerEntity(5),
|
||||
silent: true,
|
||||
}),
|
||||
}
|
||||
static grammar = this.createGrammar()
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import IEntity from "../../js/entity/IEntity.js"
|
||||
import NumberEntity from "../../js/entity/NumberEntity.js"
|
||||
import StringEntity from "../../js/entity/StringEntity.js"
|
||||
import Grammar from "../../js/serialization/Grammar.js"
|
||||
|
||||
export default class EntityF extends IEntity {
|
||||
|
||||
static lookbehind = ["Foo", "Bar"]
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
arg1: NumberEntity,
|
||||
arg2: StringEntity,
|
||||
}
|
||||
static grammar = Grammar.createEntityGrammar(this)
|
||||
|
||||
constructor(values = {}) {
|
||||
super(values)
|
||||
}
|
||||
}
|
||||
12
tests/resources/serializedEntity2-1.js
Normal file
12
tests/resources/serializedEntity2-1.js
Normal file
@@ -0,0 +1,12 @@
|
||||
export default `{
|
||||
someNumber: 567
|
||||
someString: "alpha"
|
||||
someString2: "beta"
|
||||
someBoolean: True
|
||||
someBoolean2: False
|
||||
someObjectString: "gamma"
|
||||
someArray: (400,500,600,700,800)
|
||||
someArray2: (-400,-500,-600,-700,-800)
|
||||
someEntity.a: 8
|
||||
someEntity.b: 9
|
||||
}`
|
||||
@@ -6,6 +6,6 @@ export default `{
|
||||
someBoolean2: False
|
||||
someObjectString: "gamma"
|
||||
someArray: (400,500,600,700,800)
|
||||
someArray2: (400,500,600,700,800)
|
||||
someArray2: (-400,-500,-600,-700,-800)
|
||||
someEntity: Entity1(a=8, b=9)
|
||||
}`
|
||||
|
||||
@@ -14,7 +14,6 @@ export default `[[
|
||||
november: 0
|
||||
oscar: Entity1(a=8, b=9)
|
||||
papa: Entity1(a=12, b=13)
|
||||
quebec: 6
|
||||
romeo.a: 8
|
||||
romeo.b: 9
|
||||
sierra.someNumber: 567
|
||||
@@ -24,6 +23,6 @@ export default `[[
|
||||
sierra.someBoolean2: False
|
||||
sierra.someObjectString: "gamma"
|
||||
sierra.someArray: (400,500,600,700,800)
|
||||
sierra.someArray2: (400,500,600,700,800)
|
||||
sierra.someArray2: (-400,-500,-600,-700,-800)
|
||||
sierra.someEntity: Entity1(a=8, b=9)
|
||||
]]`
|
||||
|
||||
@@ -23,7 +23,7 @@ export default `Begin
|
||||
\${first.sierra.someBoolean2} => False
|
||||
\${first.sierra.someObjectString} => "gamma"
|
||||
\${first.sierra.someArray} => (400,500,600,700,800)
|
||||
\${first.sierra.someArray2} => (400,500,600,700,800)
|
||||
\${first.sierra.someArray2} => (-400,-500,-600,-700,-800)
|
||||
\${first.sierra.someEntity} => E1[A:8 - B:9]
|
||||
\${second(0).a} => 1
|
||||
\${second(0).b} => 2
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export default `Begin Object
|
||||
key1="Value 1"
|
||||
key2=Foo(arg1=55,arg2="Argument 2")
|
||||
End Object
|
||||
`
|
||||
@@ -3,6 +3,7 @@ import Utility from "../js/Utility.js"
|
||||
import AlternativesEntity from "../js/entity/AlternativesEntity.js"
|
||||
import ArrayEntity from "../js/entity/ArrayEntity.js"
|
||||
import BooleanEntity from "../js/entity/BooleanEntity.js"
|
||||
import ByteEntity from "../js/entity/ByteEntity.js"
|
||||
import ColorChannelEntity from "../js/entity/ColorChannelEntity.js"
|
||||
import FormatTextEntity from "../js/entity/FormatTextEntity.js"
|
||||
import GuidEntity from "../js/entity/GuidEntity.js"
|
||||
@@ -23,12 +24,9 @@ import StringEntity from "../js/entity/StringEntity.js"
|
||||
import SymbolEntity from "../js/entity/SymbolEntity.js"
|
||||
import UnknownKeysEntity from "../js/entity/UnknownKeysEntity.js"
|
||||
import Vector2DEntity from "../js/entity/Vector2DEntity.js"
|
||||
import VectorEntity from "../js/entity/VectorEntity.js"
|
||||
import SerializerFactory from "../js/serialization/SerializerFactory.js"
|
||||
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
||||
import { Exception } from "sass"
|
||||
import ByteEntity from "../js/entity/ByteEntity.js"
|
||||
import Vector4DEntity from "../js/entity/Vector4DEntity.js"
|
||||
import VectorEntity from "../js/entity/VectorEntity.js"
|
||||
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
|
||||
|
||||
test.beforeAll(() => initializeSerializerFactory())
|
||||
|
||||
@@ -652,14 +650,6 @@ test("ObjectReferenceEntity", () => {
|
||||
expect(value).toEqual(new ObjectReferenceEntity("/Game/_YukiritoLib/Textures/T_紫色渐变01.T_紫色渐变01"))
|
||||
})
|
||||
|
||||
test("PinEntity", () => {
|
||||
const serializer = SerializerFactory.getSerializer(PinEntity)
|
||||
|
||||
expect(serializer.read("Pin (PinType.PinSubCategoryMemberReference=())")).toMatchObject({
|
||||
"PinType": { "PinSubCategoryMemberReference": null }
|
||||
})
|
||||
})
|
||||
|
||||
test("SimpleSerializationRotatorEntity", () => {
|
||||
const grammar = SimpleSerializationRotatorEntity.grammar
|
||||
{
|
||||
3
types.js
3
types.js
@@ -145,7 +145,6 @@
|
||||
* @typedef {import("./js/entity/FormatTextEntity.js").default} FormatTextEntity
|
||||
* @typedef {import("./js/entity/FunctionReferenceEntity.js").default} FunctionReferenceEntity
|
||||
* @typedef {import("./js/entity/GuidEntity.js").default} GuidEntity
|
||||
* @typedef {import("./js/entity/IdentifierEntity.js").default} IdentifierEntity
|
||||
* @typedef {import("./js/entity/IEntity.js").default} IEntity
|
||||
* @typedef {import("./js/entity/Integer64Entity.js").default} Integer64Entity
|
||||
* @typedef {import("./js/entity/IntegerEntity.js").default} IntegerEntity
|
||||
@@ -155,10 +154,10 @@
|
||||
* @typedef {import("./js/entity/LocalizedTextEntity.js").default} LocalizedTextEntity
|
||||
* @typedef {import("./js/entity/MacroGraphReferenceEntity.js").default} MacroGraphReferenceEntity
|
||||
* @typedef {import("./js/entity/NaturalNumberEntity.js").default} NaturalNumberEntity
|
||||
* @typedef {import("./js/entity/NullEntity.js").default} NullEntity
|
||||
* @typedef {import("./js/entity/ObjectEntity.js").default} ObjectEntity
|
||||
* @typedef {import("./js/entity/ObjectReferenceEntity.js").default} ObjectReferenceEntity
|
||||
* @typedef {import("./js/entity/objects/KnotEntity.js").default} KnotEntity
|
||||
* @typedef {import("./js/entity/PathSymbolEntity.js").default} PathSymbolEntity
|
||||
* @typedef {import("./js/entity/PinEntity.js").default} PinEntity
|
||||
* @typedef {import("./js/entity/PinReferenceEntity.js").default} PinReferenceEntity
|
||||
* @typedef {import("./js/entity/PinTypeEntity.js").default} PinTypeEntity
|
||||
|
||||
Reference in New Issue
Block a user