From 721db0702b424cd8a8b83e6a0e865acc9bffc0b8 Mon Sep 17 00:00:00 2001 From: barsdeveloper Date: Wed, 5 Apr 2023 21:25:37 +0200 Subject: [PATCH] Serialization test --- cypress/e2e/entities.cy.js | 84 ++++++++++++++++++++++++++++++- cypress/fixtures/ComplexEntity.js | 9 ++-- cypress/fixtures/SimpleEntity.js | 3 -- cypress/fixtures/SimpleObject.js | 20 ++++++-- js/entity/IEntity.js | 1 + js/entity/PinCategoryEntity.js | 79 +++++++++++++++++++++++++++++ 6 files changed, 185 insertions(+), 11 deletions(-) create mode 100755 js/entity/PinCategoryEntity.js diff --git a/cypress/e2e/entities.cy.js b/cypress/e2e/entities.cy.js index 39b6199..dc4f503 100644 --- a/cypress/e2e/entities.cy.js +++ b/cypress/e2e/entities.cy.js @@ -1,6 +1,9 @@ /// import ComplexEntity from "../fixtures/ComplexEntity" +import GeneralSerializer from "../../js/serialization/GeneralSerializer.js" +import initializeSerializerFactory from "../../js/serialization/initializeSerializerFactory.js" +import SerializerFactory from "../../js/serialization/SerializerFactory.js" import SimpleEntity from "../fixtures/SimpleEntity" import SimpleObject from "../fixtures/SimpleObject" @@ -12,7 +15,22 @@ describe("Entity initialization", () => { context("SimpleEntity", () => { const entity = new SimpleEntity() - it("has 8 keys", () => expect(Object.keys(entity).length).to.equal(8)) + before(() => { + initializeSerializerFactory() + SerializerFactory.registerSerializer( + SimpleEntity, + new GeneralSerializer( + v => `{\n${v}\n}`, + SimpleEntity, + " ", + "\n", + false, + ": ", + undefined + ) + ) + }) + it("has 7 keys", () => expect(Object.keys(entity).length).to.equal(7)) it("has someNumber equal to 567", () => expect(entity) .to.have.property("someNumber") .which.is.a("number") @@ -78,6 +96,19 @@ describe("Entity initialization", () => { it("compares equal entities as equal", () => expect(other1.equals(other2)) .to.be.true ) + it("can serialize", () => { + expect(SerializerFactory.getSerializer(SimpleEntity).serialize(entity)) + .to.equal(`{ + someNumber: 567 + someString: "alpha" + someString2: "beta" + someBoolean: True + someBoolean2: False + someObjectString: "gamma" + someArray: (400,500,600,700,800,) +}` + ) + }) }) context("ComplexEntity", () => { @@ -100,7 +131,35 @@ describe("Entity initialization", () => { "oscar", "papa", "quebec", + "romeo", ] + before(() => { + initializeSerializerFactory() + SerializerFactory.registerSerializer( + ComplexEntity, + new GeneralSerializer( + v => `[[\n${v}\n]]`, + ComplexEntity, + " ", + "\n", + false, + ": ", + undefined + ) + ) + SerializerFactory.registerSerializer( + SimpleObject, + new GeneralSerializer( + v => `SimpleObject(${v})`, + SimpleObject, + "", + ", ", + false, + "=", + undefined + ) + ) + }) it(`has ${keys.length} keys`, () => expect(Object.keys(entity).length).to.equal(keys.length)) it("has specific keys names", () => expect(Object.keys(entity)).to.be.deep.equal(keys)) it("has alpha equal to 32", () => expect(entity) @@ -203,5 +262,28 @@ describe("Entity initialization", () => { entity.quebec = 6 expect(entity.quebec, "assigned 6").to.be.equal(6) }) + it("can serialize", () => { + expect(SerializerFactory.getSerializer(ComplexEntity).serialize(entity)) + .to.equal(`[[ + alpha: 32 + bravo: 78 + charlie: "Charlie" + delta: () + echo: "echo" + foxtrot: False + golf: () + hotel: () + india: () + juliett: ("a","b","c","d","e",) + kilo: (True,False,False,True,True,) + mike: "Bar" + november: 0 + oscar: SimpleObject(a=8, b=9) + papa: SimpleObject(a=12, b=13) + romeo.a: 8 + romeo.b: 9 +]]` + ) + }) }) }) diff --git a/cypress/fixtures/ComplexEntity.js b/cypress/fixtures/ComplexEntity.js index 0222036..f331d7c 100644 --- a/cypress/fixtures/ComplexEntity.js +++ b/cypress/fixtures/ComplexEntity.js @@ -1,7 +1,6 @@ import IEntity from "../../js/entity/IEntity" -import UnionType from "../../js/entity/UnionType" -import Utility from "../../js/Utility" import SimpleObject from "./SimpleObject" +import UnionType from "../../js/entity/UnionType" export default class ComplexEntity extends IEntity { @@ -61,12 +60,16 @@ export default class ComplexEntity extends IEntity { type: SimpleObject, }, papa: { - default: () => new SimpleObject(12, 13), + default: () => new SimpleObject({ a: 12, b: 13 }), }, quebec: { default: 0, // will assign undefined because it does not satisfy the predicate predicate: v => v >= 1 && v <= 10, }, + romeo: { + type: SimpleObject, + inline: true, + }, } static { diff --git a/cypress/fixtures/SimpleEntity.js b/cypress/fixtures/SimpleEntity.js index de2fbed..3ee5a5c 100644 --- a/cypress/fixtures/SimpleEntity.js +++ b/cypress/fixtures/SimpleEntity.js @@ -24,9 +24,6 @@ export default class SimpleEntity extends IEntity { someArray: { default: [400, 500, 600, 700, 800], }, - someSet: { - default: new Set([10, 20, 30, 40, 50, 60, 70]), - }, } static { diff --git a/cypress/fixtures/SimpleObject.js b/cypress/fixtures/SimpleObject.js index ff1e2d8..466ec36 100644 --- a/cypress/fixtures/SimpleObject.js +++ b/cypress/fixtures/SimpleObject.js @@ -1,7 +1,19 @@ -export default class SimpleObject { +import IEntity from "../../js/entity/IEntity.js" - constructor(a = 8, b = 9) { - this.a = a - this.b = b +export default class SimpleObject extends IEntity { + + static attributes = { + a: { + type: Number, + }, + b: { + type: Number, + }, + } + + constructor(values = {}) { + values.a ??= 8 + values.b ??= 9 + super(values) } } diff --git a/js/entity/IEntity.js b/js/entity/IEntity.js index f0b03b9..2d767eb 100644 --- a/js/entity/IEntity.js +++ b/js/entity/IEntity.js @@ -40,6 +40,7 @@ export default class IEntity { ignored: false, serialized: false, expected: false, + inline: false, } constructor(values = {}, suppressWarns = false) { diff --git a/js/entity/PinCategoryEntity.js b/js/entity/PinCategoryEntity.js new file mode 100755 index 0000000..9d424f8 --- /dev/null +++ b/js/entity/PinCategoryEntity.js @@ -0,0 +1,79 @@ +import FunctionReferenceEntity from "./FunctionReferenceEntity.js" +import IEntity from "./IEntity.js" +import ObjectReferenceEntity from "./ObjectReferenceEntity.js" +import PathSymbolEntity from "./PathSymbolEntity.js" + +export default class PinTypeEntity extends IEntity { + + static attributes = { + PinCategory: { + default: "", + }, + PinSubCategory: { + default: "", + }, + PinSubCategoryObject: { + type: ObjectReferenceEntity, + }, + PinSubCategoryMemberReference: { + type: FunctionReferenceEntity, + default: null, + }, + PinValueType: { + type: PinTypeEntity, + default: null, + }, + ContainerType: { + type: PathSymbolEntity, + }, + bIsReference: { + default: false, + }, + bIsConst: { + default: false, + }, + bIsWeakPointer: { + default: false, + }, + bIsUObjectWrapper: { + default: false, + }, + bSerializeAsSinglePrecisionFloat: { + default: false, + }, + } + + static { + this.cleanupAttributes(this.attributes) + } + + constructor(values = {}, suppressWarns = false) { + super(values, suppressWarns) + /** @type {String} */ this.PinCategory + /** @type {String} */ this.PinSubCategory + /** @type {ObjectReferenceEntity} */ this.PinSubCategoryObject + /** @type {FunctionReferenceEntity} */ this.PinSubCategoryMemberReference + /** @type {PinTypeEntity} */ this.PinValueType + /** @type {PathSymbolEntity} */ this.ContainerType + /** @type {Boolean} */ this.bIsReference + /** @type {Boolean} */ this.bIsConst + /** @type {Boolean} */ this.bIsWeakPointer + /** @type {Boolean} */ this.bIsUObjectWrapper + /** @type {Boolean} */ this.bIsUObjectWrapper + } + + /** @param {PinCategoryEntity} other */ + copyTypeFrom(other) { + this.PinCategory = other.PinCategory + this.PinSubCategory = other.PinSubCategory + this.PinSubCategoryObject = other.PinSubCategoryObject + this.PinSubCategoryMemberReference = other.PinSubCategoryMemberReference + this.PinValueType = other.PinValueType + this.ContainerType = other.ContainerType + this.bIsReference = other.bIsReference + this.bIsConst = other.bIsConst + this.bIsWeakPointer = other.bIsWeakPointer + this.bIsUObjectWrapper = other.bIsUObjectWrapper + this.bSerializeAsSinglePrecisionFloat = other.bSerializeAsSinglePrecisionFloat + } +}