Simple entities serialization fixed

This commit is contained in:
barsdeveloper
2024-06-04 14:40:47 +02:00
parent c05e6d3cc9
commit e16822760f
11 changed files with 266 additions and 128 deletions

29
dist/ueblueprint.js vendored
View File

@@ -3073,7 +3073,7 @@ class Grammar {
).chain(([attributeName, _1]) => { ).chain(([attributeName, _1]) => {
const attributeKey = attributeName.split(Configuration.keysSeparator); const attributeKey = attributeName.split(Configuration.keysSeparator);
const attributeValue = this.getAttribute(entityType, attributeKey); const attributeValue = this.getAttribute(entityType, attributeKey);
return attributeValue.grammar.map(attributeValue => return (attributeValue?.grammar ?? IEntity.unknownEntityGrammar).map(attributeValue =>
values => { values => {
handleObjectSet(values, attributeKey, attributeValue); handleObjectSet(values, attributeKey, attributeValue);
Utility.objectSet(values, attributeKey, attributeValue); Utility.objectSet(values, attributeKey, attributeValue);
@@ -3083,11 +3083,11 @@ class Grammar {
} }
/** /**
* @template {IEntity} T * @template {typeof IEntity} T
* @param {new (...args: any) => T} entityType * @param {T} entityType
* @return {Parsernostrum<T>} * @return {Parsernostrum<InstanceType<T>>}
*/ */
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation) { static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation, complete = false) {
const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind; const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind;
return Parsernostrum.seq( return Parsernostrum.seq(
Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1), Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1),
@@ -3108,7 +3108,12 @@ class Grammar {
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) { if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind); entityType = entityType.withLookbehind(lookbehind);
} }
return Parsernostrum.success().map(() => new entityType(values)) const keys = Object.keys(values);
return complete
? Parsernostrum.success()
.assert(v => Object.keys(entityType.attributes).every(k => keys.includes(k)))
.map(() => new entityType(values))
: Parsernostrum.success().map(() => new entityType(values))
}) })
} }
@@ -5191,7 +5196,7 @@ class Vector2DEntity extends IEntity {
X: NumberEntity.withDefault(), X: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("Vector2DEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
constructor(values) { constructor(values) {
super(values); super(values);
@@ -5228,7 +5233,7 @@ class RotatorEntity extends IEntity {
P: NumberEntity.withDefault(), P: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("RotatorEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
constructor(values) { constructor(values) {
super(values); super(values);
@@ -5324,7 +5329,7 @@ class Vector4DEntity extends IEntity {
Z: NumberEntity.withDefault(), Z: NumberEntity.withDefault(),
W: NumberEntity.withDefault(), W: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("Vector4DEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
constructor(values) { constructor(values) {
super(values); super(values);
@@ -5374,7 +5379,7 @@ class VectorEntity extends IEntity {
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
Z: NumberEntity.withDefault(), Z: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("VectorEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
constructor(values) { constructor(values) {
super(values); super(values);
@@ -13428,11 +13433,11 @@ function initializeSerializerFactory() {
InvariantTextEntity.grammar, InvariantTextEntity.grammar,
FormatTextEntity.grammar, FormatTextEntity.grammar,
PinReferenceEntity.grammar, PinReferenceEntity.grammar,
Vector4DEntity.grammar, Vector2DEntity.grammar,
VectorEntity.grammar, VectorEntity.grammar,
Vector4DEntity.grammar,
RotatorEntity.grammar, RotatorEntity.grammar,
LinearColorEntity.grammar, LinearColorEntity.grammar,
Vector2DEntity.grammar,
UnknownKeysEntity.grammar, UnknownKeysEntity.grammar,
SymbolEntity.grammar, SymbolEntity.grammar,
ArrayEntity.of(PinReferenceEntity).grammar, ArrayEntity.of(PinReferenceEntity).grammar,

File diff suppressed because one or more lines are too long

View File

@@ -243,12 +243,20 @@ export default class IEntity {
} }
const thisKeys = Object.keys(this) const thisKeys = Object.keys(this)
const otherKeys = Object.keys(other) 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 return false
} }
for (let i = 0; i < thisKeys.length; ++i) { for (let i = 0; i < thisKeys.length; ++i) {
const a = this[thisKeys[i]] const k = thisKeys[i]
const b = other[otherKeys[i]] if (!otherKeys.includes(k)) {
return false
}
const a = this[k]
const b = other[k]
if (a instanceof IEntity) { if (a instanceof IEntity) {
if (!a.equals(b)) { if (!a.equals(b)) {
return false return false
@@ -298,7 +306,7 @@ export default class IEntity {
} }
result += serialization result += serialization
} }
if (Self.trailing && result.length) { if (this.trailing && result.length) {
result += Self.attributeSeparator result += Self.attributeSeparator
} }
return Self.wrap(this, result) return Self.wrap(this, result)

View File

@@ -10,7 +10,7 @@ export default class RotatorEntity extends IEntity {
P: NumberEntity.withDefault(), P: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("RotatorEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("RotatorEntity")
constructor(values) { constructor(values) {
super(values) super(values)

View File

@@ -4,6 +4,7 @@ import IEntity from "./IEntity.js"
export default class UnknownKeysEntity extends IEntity { export default class UnknownKeysEntity extends IEntity {
/** @type {P<UnknownKeysEntity>} */
static grammar = P.seq( static grammar = P.seq(
// Lookbehind // Lookbehind
P.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1), P.reg(new RegExp(`(${Grammar.Regex.Path.source}|${Grammar.Regex.Symbol.source}\\s*)?\\(\\s*`), 1),

View File

@@ -1,3 +1,4 @@
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"
import NumberEntity from "./NumberEntity.js" import NumberEntity from "./NumberEntity.js"
@@ -9,7 +10,8 @@ export default class Vector2DEntity extends IEntity {
X: NumberEntity.withDefault(), X: NumberEntity.withDefault(),
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("Vector2DEntity") /** @type {P<Vector2DEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector2DEntity")
constructor(values) { constructor(values) {
super(values) super(values)

View File

@@ -11,7 +11,7 @@ export default class Vector4DEntity extends IEntity {
Z: NumberEntity.withDefault(), Z: NumberEntity.withDefault(),
W: NumberEntity.withDefault(), W: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("Vector4DEntity") static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("Vector4DEntity")
constructor(values) { constructor(values) {
super(values) super(values)

View File

@@ -1,3 +1,4 @@
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"
import NumberEntity from "./NumberEntity.js" import NumberEntity from "./NumberEntity.js"
@@ -10,7 +11,8 @@ export default class VectorEntity extends IEntity {
Y: NumberEntity.withDefault(), Y: NumberEntity.withDefault(),
Z: NumberEntity.withDefault(), Z: NumberEntity.withDefault(),
} }
static grammar = Grammar.createEntityGrammar(this).label("VectorEntity") /** @type {P<VectorEntity>} */
static grammar = Grammar.createEntityGrammar(this, Grammar.commaSeparation, true).label("VectorEntity")
constructor(values) { constructor(values) {
super(values) super(values)

View File

@@ -182,7 +182,7 @@ export default class Grammar {
).chain(([attributeName, _1]) => { ).chain(([attributeName, _1]) => {
const attributeKey = attributeName.split(Configuration.keysSeparator) const attributeKey = attributeName.split(Configuration.keysSeparator)
const attributeValue = this.getAttribute(entityType, attributeKey) const attributeValue = this.getAttribute(entityType, attributeKey)
return attributeValue.grammar.map(attributeValue => return (attributeValue?.grammar ?? IEntity.unknownEntityGrammar).map(attributeValue =>
values => { values => {
handleObjectSet(values, attributeKey, attributeValue) handleObjectSet(values, attributeKey, attributeValue)
Utility.objectSet(values, attributeKey, attributeValue) Utility.objectSet(values, attributeKey, attributeValue)
@@ -192,11 +192,11 @@ export default class Grammar {
} }
/** /**
* @template {IEntity} T * @template {typeof IEntity} T
* @param {new (...args: any) => T} entityType * @param {T} entityType
* @return {Parsernostrum<T>} * @return {Parsernostrum<InstanceType<T>>}
*/ */
static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation) { static createEntityGrammar(entityType, entriesSeparator = this.commaSeparation, complete = false) {
const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind const lookbehind = entityType.lookbehind instanceof Array ? entityType.lookbehind.join("|") : entityType.lookbehind
return Parsernostrum.seq( return Parsernostrum.seq(
Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1), Parsernostrum.reg(new RegExp(String.raw`(${lookbehind})\s*\(\s*`), 1),
@@ -217,7 +217,12 @@ export default class Grammar {
if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) { if (entityType.lookbehind instanceof Array || entityType.lookbehind !== lookbehind) {
entityType = entityType.withLookbehind(lookbehind) entityType = entityType.withLookbehind(lookbehind)
} }
return Parsernostrum.success().map(() => new entityType(values)) const keys = Object.keys(values)
return complete
? Parsernostrum.success()
.assert(v => Object.keys(entityType.attributes).every(k => keys.includes(k)))
.map(() => new entityType(values))
: Parsernostrum.success().map(() => new entityType(values))
}) })
} }

View File

@@ -67,14 +67,14 @@ export default function initializeSerializerFactory() {
PinReferenceEntity.grammar, PinReferenceEntity.grammar,
Vector4DEntity.grammar, Vector4DEntity.grammar,
VectorEntity.grammar, VectorEntity.grammar,
Vector2DEntity.grammar,
RotatorEntity.grammar, RotatorEntity.grammar,
LinearColorEntity.grammar, LinearColorEntity.grammar,
Vector2DEntity.grammar,
UnknownKeysEntity.grammar, UnknownKeysEntity.grammar,
SymbolEntity.grammar, SymbolEntity.grammar,
ArrayEntity.of(PinReferenceEntity).grammar, ArrayEntity.of(PinReferenceEntity).grammar,
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar, ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
Parsernostrum.lazy(() => ArrayEntity.createGrammar(Grammar.unknownValue)), Parsernostrum.lazy(() => ArrayEntity.createGrammar(IEntity.unknownEntityGrammar)),
) )
SerializerFactory.registerSerializer( SerializerFactory.registerSerializer(

View File

@@ -28,6 +28,7 @@ import SerializerFactory from "../js/serialization/SerializerFactory.js"
import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js" import initializeSerializerFactory from "../js/serialization/initializeSerializerFactory.js"
import { Exception } from "sass" import { Exception } from "sass"
import ByteEntity from "../js/entity/ByteEntity.js" import ByteEntity from "../js/entity/ByteEntity.js"
import Vector4DEntity from "../js/entity/Vector4DEntity.js"
test.beforeAll(() => initializeSerializerFactory()) test.beforeAll(() => initializeSerializerFactory())
@@ -817,7 +818,7 @@ test("SimpleSerializationVectorEntity", () => {
} }
}) })
test("String", () => { test("StringEntity", () => {
const grammar = StringEntity.grammar const grammar = StringEntity.grammar
{ {
let value = grammar.parse('""') let value = grammar.parse('""')
@@ -833,6 +834,7 @@ test("String", () => {
let value = grammar.parse('"hello"') let value = grammar.parse('"hello"')
expect(value).toEqual(new StringEntity("hello")) expect(value).toEqual(new StringEntity("hello"))
expect(value.equals(new StringEntity("hello"))).toBeTruthy() expect(value.equals(new StringEntity("hello"))).toBeTruthy()
expect(value.equals(new SymbolEntity("hello"))).toBeFalsy()
expect(value.equals(new NumberEntity())).toBeFalsy() expect(value.equals(new NumberEntity())).toBeFalsy()
expect(value.print()).toEqual("hello") expect(value.print()).toEqual("hello")
expect(value.toString()).toEqual(`"hello"`) expect(value.toString()).toEqual(`"hello"`)
@@ -861,122 +863,235 @@ test("String", () => {
test("UnknownKeysValue", () => { test("UnknownKeysValue", () => {
const parser = IEntity.unknownEntityGrammar const parser = IEntity.unknownEntityGrammar
expect(parser.parse('"Hello"').constructor).toStrictEqual(String) expect(parser.parse('"Hello"')).toBeInstanceOf(StringEntity)
expect(parser.parse("()")).toBeNull() expect(parser.parse("()")).toBeInstanceOf(NullEntity)
expect(parser.parse("8345").constructor).toStrictEqual(Number) expect(parser.parse("8345")).toBeInstanceOf(NumberEntity)
expect(parser.parse("True").constructor).toStrictEqual(Boolean) expect(parser.parse("True")).toBeInstanceOf(BooleanEntity)
expect(parser.parse("False").constructor).toStrictEqual(Boolean) expect(parser.parse("False")).toBeInstanceOf(BooleanEntity)
expect(parser.parse("F0223D3742E67C0D9FEFB2A64946B7F0").constructor).toStrictEqual(GuidEntity) expect(parser.parse("F0223D3742E67C0D9FEFB2A64946B7F0")).toBeInstanceOf(GuidEntity)
expect(parser.parse("SYMBOL1").constructor).toStrictEqual(SymbolEntity) expect(parser.parse("SYMBOL1")).toBeInstanceOf(SymbolEntity)
expect(parser.parse("Symbol_2_3_4").constructor).toStrictEqual(SymbolEntity) expect(parser.parse("Symbol_2_3_4")).toBeInstanceOf(SymbolEntity)
expect(parser.parse("(X=-0.495, Y=0, )").constructor).toStrictEqual(Vector2DEntity) expect(parser.parse("(X=-0.495,Y=+765.0,Z=7,W=56)")).toBeInstanceOf(Vector4DEntity)
expect(parser.parse("(X=-0.495,Y=+765.0,Z=7)").constructor).toStrictEqual(VectorEntity) expect(parser.parse("(X=-0.495, Y=0, )")).toBeInstanceOf(Vector2DEntity)
expect(parser.parse("(R=1.000000,P=7.6,Y=+88.99)").constructor).toStrictEqual(RotatorEntity) expect(parser.parse("(X=-0.495,Y=+765.0,Z=7)")).toBeInstanceOf(VectorEntity)
expect(parser.parse("(R=0.000000,G=0.660000,B=1.000000,A=1.000000)").constructor) expect(parser.parse("(R=1.000000,P=7.6,Y=+88.99)")).toBeInstanceOf(RotatorEntity)
.toStrictEqual(LinearColorEntity) expect(parser.parse("(R=0.000000,G=0.660000,B=1.000000,A=1.000000)")).toBeInstanceOf(LinearColorEntity)
expect(parser.parse(`Class'"/Script/Engine.KismetSystemLibrary"'`).constructor) expect(parser.parse(`Class'"/Script/Engine.KismetSystemLibrary"'`)).toBeInstanceOf(ObjectReferenceEntity)
.toStrictEqual(ObjectReferenceEntity) expect(parser.parse("(1,2,3,4,5,6,7,8,9)")).toBeInstanceOf(ArrayEntity)
expect(parser.parse("(1,2,3,4,5,6,7,8,9)")).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]) expect(parser.parse(`("Hello", "World",)`)).toBeInstanceOf(ArrayEntity)
expect(parser.parse(`("Hello", "World",)`)).toStrictEqual(["Hello", "World"]) expect(parser.parse(`("Alpha", 123, Beta, "Gamma", "Delta", 99)`)).toBeInstanceOf(ArrayEntity)
expect(parser.parse(`("Alpha", 123, Beta, "Gamma", "Delta", 99)`))
.toStrictEqual(["Alpha", 123, new SymbolEntity("Beta"), "Gamma", "Delta", 99])
}) })
test("UnknownKeysEntity", () => { test("UnknownKeysEntity", () => {
const serializer = SerializerFactory.getSerializer(UnknownKeysEntity) const grammar = UnknownKeysEntity.grammar
{
let unknown = serializer.read('LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey="Hello")') let value = grammar.parse('LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey="Hello")')
expect(unknown).toBeInstanceOf(UnknownKeysEntity) expect(value).toBeInstanceOf(UnknownKeysEntity)
expect(unknown).toMatchObject({ expect(value).toEqual(new UnknownKeysEntity({
lookbehind: "LookbehindValue", lookbehind: "LookbehindValue",
FirstKey: 1, FirstKey: new NumberEntity(1),
SecondKey: new SymbolEntity("SOME_SYMBOL2"), SecondKey: new SymbolEntity("SOME_SYMBOL2"),
ThirdKey: "Hello", ThirdKey: new StringEntity("Hello"),
}) }))
expect(value.equals(new UnknownKeysEntity({
unknown = serializer.read('(A = (-1,-2,-3), B = SomeFunction(B1 = "b1", B2 = (X=101,Y=102,Z=103)))') lookbehind: "LookbehindValue",
expect(unknown).toBeInstanceOf(UnknownKeysEntity) FirstKey: new NumberEntity(1),
expect(unknown).toMatchObject({ SecondKey: new SymbolEntity("SOME_SYMBOL2"),
A: [-1, -2, -3], ThirdKey: new StringEntity("Hello"),
B: new UnknownKeysEntity({ }))).toBeTruthy()
lookbehind: "SomeFunction", expect(value.equals(new UnknownKeysEntity({
B1: "b1", lookbehind: "LookbehindValue modified",
B2: new VectorEntity({ X: 101, Y: 102, Z: 103 }), FirstKey: new NumberEntity(1),
}), SecondKey: new SymbolEntity("SOME_SYMBOL2"),
}) ThirdKey: new StringEntity("Hello"),
}))).toBeFalsy()
expect(value.equals(new UnknownKeysEntity({
lookbehind: "LookbehindValue",
FirstKey: new NumberEntity(1),
SecondKey: new StringEntity("SOME_SYMBOL2"),
ThirdKey: new StringEntity("Hello"),
}))).toBeFalsy
expect(value.toString()).toEqual('LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey="Hello")')
expect(value.toString(true)).toEqual(
String.raw`LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey=\"Hello\")`
)
}
{
let value = grammar.parse('(A = (-1,-2,-3), B = SomeFunction(B1 = "b1", B2 = (X=101,Y=102,Z=103)))')
expect(value).toBeInstanceOf(UnknownKeysEntity)
expect(value).toEqual(new UnknownKeysEntity({
A: new ArrayEntity([new NumberEntity(-1), new NumberEntity(-2), new NumberEntity(-3)]),
B: new UnknownKeysEntity({
lookbehind: "SomeFunction",
B1: new StringEntity("b1"),
B2: new VectorEntity({ X: new NumberEntity(101), Y: new NumberEntity(102), Z: new NumberEntity(103) }),
}),
}))
expect(value.equals(new UnknownKeysEntity({
A: new ArrayEntity([new NumberEntity(-1), new NumberEntity(-2), new NumberEntity(-3)]),
B: new UnknownKeysEntity({
lookbehind: "SomeFunction",
B1: new StringEntity("b1"),
B2: new VectorEntity({ X: new NumberEntity(101), Y: new NumberEntity(102), Z: new NumberEntity(103) }),
}),
}))).toBeTruthy()
expect(value.equals(new UnknownKeysEntity({
A: new ArrayEntity([new NumberEntity(-1), new NumberEntity(-2), new NumberEntity(-3)]),
B: new UnknownKeysEntity({
lookbehind: "SomeFunction",
B1: new StringEntity("b1"),
B2: new VectorEntity({ X: new IntegerEntity(101), Y: new NumberEntity(102), Z: new ByteEntity(103) }),
}),
}))).toBeTruthy()
expect(value.equals(new UnknownKeysEntity({
A: new ArrayEntity([new NumberEntity(-1), new NumberEntity(-2), new NumberEntity(-3)]),
B: new UnknownKeysEntity({
lookbehind: "SomeFunction",
B1: new StringEntity("b2"),
B2: new VectorEntity({ X: new NumberEntity(101), Y: new NumberEntity(102), Z: new NumberEntity(103) }),
}),
}))).toBeFalsy()
expect(value.equals(new UnknownKeysEntity({
A: new ArrayEntity([new NumberEntity(-1), new NumberEntity(-2), new NumberEntity(-3)]),
B: new UnknownKeysEntity({
lookbehind: "SomeFunction",
B1: new StringEntity("b1"),
B2: new VectorEntity({ X: new NumberEntity(101), Y: new NumberEntity(-102), Z: new NumberEntity(103) }),
}),
}))).toBeFalsy()
expect(value.toString()).toEqual('(A=(-1,-2,-3),B=SomeFunction(B1="b1",B2=(X=101,Y=102,Z=103)))')
expect(value.toString(true)).toEqual(String.raw`(A=(-1,-2,-3),B=SomeFunction(B1=\"b1\",B2=(X=101,Y=102,Z=103)))`)
}
expect(() => grammar.parse('LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey="Hello)')).toThrow("Could not parse")
expect(() => grammar.parse('LookbehindValue(FirstKey=1,SecondKey=SOME_SYMBOL2,ThirdKey="Hello"')).toThrow("Could not parse")
}) })
test("VectorEntity", () => { test("VectorEntity", () => {
const serializer = SerializerFactory.getSerializer(VectorEntity) const grammar = VectorEntity.grammar
{
let vector = serializer.read("(X=1,Y=2,Z=3.5)") let value = grammar.parse("(X=1,Y=2,Z=3.500)")
expect(vector).toBeInstanceOf(VectorEntity) expect(value).toBeInstanceOf(VectorEntity)
expect(vector).toStrictEqual(new VectorEntity({ expect(value).toEqual(new VectorEntity({
X: 1, X: new NumberEntity(1),
Y: 2, Y: new NumberEntity(2),
Z: 3.5, Z: new NumberEntity(3.5),
})) }))
expect(value.toArray()).toStrictEqual([1, 2, 3.5])
vector = serializer.read("(X=10,Y=+20.88,Z=-30.54,)") expect(value.equals(new VectorEntity({
expect(vector).toBeInstanceOf(VectorEntity) X: new NumberEntity(1),
expect(vector).toStrictEqual(new VectorEntity({ Y: new NumberEntity(2),
X: 10, Z: new NumberEntity(3.5),
Y: 20.88, }))).toBeTruthy()
Z: -30.54, expect(value.equals(new VectorEntity({
})) X: new NumberEntity(1),
Y: new NumberEntity(2),
vector = serializer.read(`( Z: new NumberEntity(3.5),
w: new NumberEntity(7),
}))).toBeFalsy()
expect(value.toString()).toEqual("(X=1,Y=2,Z=3.500)")
expect(value.toString(true)).toEqual("(X=1,Y=2,Z=3.500)")
}
{
let value = grammar.parse("(X=10,Y=+20.880,Z=-30.54,)")
expect(value).toBeInstanceOf(VectorEntity)
expect(value).toEqual(new VectorEntity({
X: new NumberEntity(10),
Y: new NumberEntity(20.88),
Z: new NumberEntity(-30.54),
}))
expect(value.equals(new VectorEntity({
X: new NumberEntity(10),
Y: new NumberEntity(20.88),
Z: new NumberEntity(-30.54),
}))).toBeTruthy()
expect(value.toString()).toEqual("(X=10,Y=20.880,Z=-30.54,)")
}
{
let value = grammar.parse(`(
Z = -3.66, Z = -3.66,
X X
= -1, Y = = -0, Y =
-2 -2
, ,
)`) )`)
expect(vector).toBeInstanceOf(VectorEntity) expect(value).toBeInstanceOf(VectorEntity)
expect(vector).toStrictEqual(new VectorEntity({ expect(value).toEqual(new VectorEntity({
X: -1, X: new NumberEntity(0),
Y: -2, Y: new NumberEntity(-2),
Z: -3.66, Z: new NumberEntity(-3.66),
})) }))
expect(value.toArray()).toStrictEqual([0, -2, -3.66])
expect(() => serializer.read("(X=1,Y=\"2\",Z=3)")).toThrow() expect(value.equals(new VectorEntity({
expect(() => serializer.read("(X=1,Z=3)")).toThrow() X: new NumberEntity(0),
expect(() => serializer.read("(X=1,Y=2,Unexpected=6,Z=3.5)")).toThrow() Y: new NumberEntity(-2),
Z: new NumberEntity(-3.66),
}))).toBeTruthy()
expect(value.equals(new VectorEntity({
X: new NumberEntity(-0),
Y: new NumberEntity(-2.01),
Z: new NumberEntity(-3.66),
}))).toBeFalsy()
expect(value.equals(new VectorEntity({
X: new NumberEntity(-0),
Y: new NumberEntity(-2),
Z: new NumberEntity(-3.65),
}))).toBeFalsy()
expect(value.toString()).toEqual("(Z=-3.66,X=0,Y=-2,)")
}
expect(() => grammar.parse("(X=1,Y=\"2\",Z=3)")).toThrow("Could not parse")
expect(() => grammar.parse("(X=1,Z=3)")).toThrow("Could not parse")
}) })
test("Vector2DEntity", () => { test("Vector2DEntity", () => {
let serializer = SerializerFactory.getSerializer(Vector2DEntity) const grammar = Vector2DEntity.grammar
{
let vector = serializer.read("(X=78,Y=56.3)") const value = grammar.parse("(X=78,Y=56.3)")
expect(vector).toBeInstanceOf(Vector2DEntity) expect(value).toBeInstanceOf(Vector2DEntity)
expect(vector).toStrictEqual(new Vector2DEntity({ expect(value).toEqual(new Vector2DEntity({
X: 78, X: new NumberEntity(78),
Y: 56.3, Y: new NumberEntity(56.3),
})) }))
expect(value.toArray()).toStrictEqual([78, 56.3])
vector = serializer.read("(X=+4.5,Y=-8.88,)") expect(value.equals(new Vector2DEntity({
expect(vector).toBeInstanceOf(Vector2DEntity) X: new NumberEntity(78),
expect(vector).toStrictEqual(new Vector2DEntity({ Y: new NumberEntity(56.3),
X: 4.5, }))).toBeTruthy()
Y: -8.88, expect(value.toString(true)).toEqual("(X=78,Y=56.3)")
})) }
{
vector = serializer.read(`( const value = grammar.parse("(X=+4.5,Y=-8.88,)")
Y = +93.004, expect(value).toBeInstanceOf(Vector2DEntity)
expect(value).toEqual(new Vector2DEntity({
X: new NumberEntity(4.5),
Y: new NumberEntity(-8.88),
}))
expect(value.equals(new Vector2DEntity({
X: new IntegerEntity(4.5),
Y: new NumberEntity(-8.88),
}))).toBeFalsy()
}
{
const value = grammar.parse(`(
Y = +93.004000,
X X
= 0, = 0,
)`) )`)
expect(vector).toBeInstanceOf(Vector2DEntity) expect(value).toBeInstanceOf(Vector2DEntity)
expect(vector).toStrictEqual(new Vector2DEntity({ expect(value).toEqual(new Vector2DEntity({
X: 0, X: new NumberEntity(0),
Y: 93.004, Y: new NumberEntity(93.004),
})) }))
expect(value.equals(new Vector2DEntity({
expect(() => serializer.read("(X=1,Y=\"2\")")).toThrow() X: new NumberEntity(0),
expect(() => serializer.read("(X=1)")).toThrow() Y: new NumberEntity(93.004),
expect(() => serializer.read("(X=777, Y=555, Unexpected=6, HH=2)")).toThrow() }))).toBeTruthy()
expect(value.toString()).toEqual("(Y=93.004000,X=0,)")
}
expect(() => grammar.parse("(X=1,Y=2")).toThrow("Could not parse")
expect(() => grammar.parse("(X=1,Y=\"2\")")).toThrow("Could not parse")
expect(() => grammar.parse("(X=1)")).toThrow("Could not parse")
}) })