Module
x/ts_serialize/serialize_property_options_map_test.ts
A zero dependency library for serializing data
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216// Copyright 2018-2022 Gamebridge.ai authors. All rights reserved. MIT license.
import { assertEquals, assertStrictEquals, fail, test } from "./test_deps.ts";import { SerializePropertyOptionsMap,} from "./serialize_property_options_map.ts";import { SerializePropertyOptions } from "./serialize_property.ts";import { ERROR_DUPLICATE_PROPERTY_KEY, ERROR_DUPLICATE_SERIALIZE_KEY,} from "./error_messages.ts";
test({ name: "SerializePropertyOptionsMap correctly initializes", fn() { new SerializePropertyOptionsMap(); },});
test({ name: "SerializePropertyOptionsMap setting a property correctly sets both keys", fn() { const testObj = new SerializePropertyOptionsMap(); const spOptions = new SerializePropertyOptions("a", "b"); testObj.set(spOptions);
assertEquals(testObj.hasPropertyKey("a"), true); assertEquals(testObj.hasSerializedKey("b"), true); assertStrictEquals(testObj.getByPropertyKey("a"), spOptions); assertStrictEquals(testObj.getBySerializedKey("b"), spOptions);
const allPropertyOptions = Array.from(testObj.propertyOptions()); assertEquals(allPropertyOptions.length, 1); assertEquals(allPropertyOptions[0], spOptions); },});
test({ name: "SerializePropertyOptionsMap parent properties are correctly retained", fn() { const testParent = new SerializePropertyOptionsMap(); const spOptions = new SerializePropertyOptions("a", "b"); testParent.set(spOptions);
const testObj = new SerializePropertyOptionsMap(testParent);
assertEquals(testObj.hasPropertyKey("a"), true); assertEquals(testObj.hasSerializedKey("b"), true); assertStrictEquals(testObj.getByPropertyKey("a"), spOptions); assertStrictEquals(testObj.getBySerializedKey("b"), spOptions);
const allPropertyOptions = Array.from(testObj.propertyOptions()); assertEquals(allPropertyOptions.length, 1); assertEquals(allPropertyOptions[0], spOptions); },});
test({ name: "SerializePropertyOptionsMap can properly override a parent property", fn() { const testParent = new SerializePropertyOptionsMap(); const parentSPOptions = new SerializePropertyOptions("a", "b"); testParent.set(parentSPOptions);
const testObj = new SerializePropertyOptionsMap(testParent); const childSPOptions = new SerializePropertyOptions("a", "b"); testObj.set(childSPOptions);
assertEquals(testObj.hasPropertyKey("a"), true); assertEquals(testObj.hasSerializedKey("b"), true); assertStrictEquals(testObj.getByPropertyKey("a"), childSPOptions); assertStrictEquals(testObj.getBySerializedKey("b"), childSPOptions);
const allPropertyOptions = Array.from(testObj.propertyOptions()); assertEquals(allPropertyOptions.length, 1); assertEquals(allPropertyOptions[0], childSPOptions); },});
test({ name: "SerializePropertyOptionsMap error when trying to add a duplicate property key", fn() { const testObj = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("a", "b"); const childSPOptions2 = new SerializePropertyOptions("a", "c"); testObj.set(childSPOptions); try { testObj.set(childSPOptions2); fail("Shouldn't be able to set duplicate property keys"); } catch (e) { assertEquals(e.message, `${ERROR_DUPLICATE_PROPERTY_KEY}: a`); } },});
test({ name: "SerializePropertyOptionsMap error when trying to add a duplicate serialize key", fn() { const testObj = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("a", "a"); const childSPOptions2 = new SerializePropertyOptions("b", "a"); testObj.set(childSPOptions); try { testObj.set(childSPOptions2); fail("Shouldn't be able to set duplicate property keys"); } catch (e) { assertEquals(e.message, `${ERROR_DUPLICATE_SERIALIZE_KEY}: a`); } },});
test({ name: "SerializePropertyOptionsMap parent property key is ignored if overridden by a new child property", fn() { const testParent = new SerializePropertyOptionsMap(); const parentSPOptions = new SerializePropertyOptions("a", "a"); testParent.set(parentSPOptions);
const testChild = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("b", "a"); testChild.set(childSPOptions);
assertStrictEquals(testChild.getByPropertyKey("b"), childSPOptions); assertStrictEquals(testChild.getByPropertyKey("a"), undefined);
assertStrictEquals(testChild.hasPropertyKey("b"), true); assertStrictEquals(testChild.hasPropertyKey("a"), false);
const childEntries = Array.from(testChild.propertyOptions()); assertEquals(childEntries.length, 1); assertEquals(childEntries[0], childSPOptions); },});
test({ name: "SerializePropertyOptionsMap parent serialize key is ignored if overridden by a new child property", fn() { const testParent = new SerializePropertyOptionsMap(); const parentSPOptions = new SerializePropertyOptions("a", "a"); testParent.set(parentSPOptions);
const testChild = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("a", "b"); testChild.set(childSPOptions);
assertStrictEquals(testChild.getBySerializedKey("b"), childSPOptions); assertStrictEquals(testChild.getBySerializedKey("a"), undefined);
assertStrictEquals(testChild.hasSerializedKey("b"), true); assertStrictEquals(testChild.hasSerializedKey("a"), false);
const childEntries = Array.from(testChild.propertyOptions()); assertEquals(childEntries.length, 1); assertEquals(childEntries[0], childSPOptions); },});
test({ name: "SerializePropertyOptionsMap can access child object property if replacing an ignored parent property key", fn() { const testParent = new SerializePropertyOptionsMap(); const parentSPOptions = new SerializePropertyOptions("a", "a"); testParent.set(parentSPOptions);
const testChild = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("b", "a"); testChild.set(childSPOptions); const childSPOptions2 = new SerializePropertyOptions("a", "c"); testChild.set(childSPOptions2);
assertStrictEquals(testChild.getByPropertyKey("b"), childSPOptions); assertStrictEquals(testChild.getByPropertyKey("a"), childSPOptions2);
assertStrictEquals(testChild.hasPropertyKey("b"), true); assertStrictEquals(testChild.hasPropertyKey("a"), true);
const childEntries = Array.from(testChild.propertyOptions()); assertEquals(childEntries.length, 2); assertEquals(childEntries[0], childSPOptions); assertEquals(childEntries[1], childSPOptions2); },});
test({ name: "SerializePropertyOptionsMap an access child object property if replacing an ignored parent serialized key", fn() { const testParent = new SerializePropertyOptionsMap(); const parentSPOptions = new SerializePropertyOptions("a", "a"); testParent.set(parentSPOptions);
const testChild = new SerializePropertyOptionsMap(); const childSPOptions = new SerializePropertyOptions("a", "b"); testChild.set(childSPOptions); const childSPOptions2 = new SerializePropertyOptions("c", "a"); testChild.set(childSPOptions2);
assertStrictEquals(testChild.getBySerializedKey("b"), childSPOptions); assertStrictEquals(testChild.getBySerializedKey("a"), childSPOptions2);
assertStrictEquals(testChild.hasSerializedKey("b"), true); assertStrictEquals(testChild.hasSerializedKey("a"), true);
const childEntries = Array.from(testChild.propertyOptions()); assertEquals(childEntries.length, 2); assertEquals(childEntries[0], childSPOptions); assertEquals(childEntries[1], childSPOptions2); },});