|
| 1 | +import { BinaryData } from '../BinaryData.js'; |
| 2 | +import { BinaryReader } from '../BinaryReader.js'; |
| 3 | +import { BinaryWriter } from '../BinaryWriter.js'; |
| 4 | +import { Prefab } from '../Prefab.js'; |
| 5 | +import { ComponentHash } from '../types/ComponentHash.js'; |
| 6 | +import { LogicOperator } from '../types/LogicOperator.js'; |
| 7 | + |
| 8 | +import { LogicGateReceiverComponent } from './LogicGateReceiverComponent.js'; |
| 9 | + |
| 10 | +const componentHash = ComponentHash.LogicGateReceiver; |
| 11 | +const componentName = 'LogicGateReceiver'; |
| 12 | +const componentVersion = 2; |
| 13 | +const componentProps = { |
| 14 | + senders: [69, 420, 1337], |
| 15 | + operationType: LogicOperator.Xor, |
| 16 | + isInversedOutputSaved: true |
| 17 | +}; |
| 18 | + |
| 19 | +describe('new LogicGateReceiverComponent()', () => { |
| 20 | + describe('when given the required props', () => { |
| 21 | + it('returns an instance of the LogicGateReceiverComponent class', () => { |
| 22 | + const component = new LogicGateReceiverComponent({ version: componentVersion }); |
| 23 | + |
| 24 | + expect(component).toBeInstanceOf(LogicGateReceiverComponent); |
| 25 | + expect(component.hash).toStrictEqual(componentHash); |
| 26 | + expect(component.name).toStrictEqual(componentName); |
| 27 | + expect(component.version).toStrictEqual(componentVersion); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('when given additional props', () => { |
| 32 | + it('returns an instance of the LogicGateReceiverComponent class', () => { |
| 33 | + const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps }); |
| 34 | + |
| 35 | + expect(component).toBeInstanceOf(LogicGateReceiverComponent); |
| 36 | + expect(component.hash).toStrictEqual(componentHash); |
| 37 | + expect(component.name).toStrictEqual(componentName); |
| 38 | + expect(component.version).toStrictEqual(componentVersion); |
| 39 | + expect(component.senders).toStrictEqual(componentProps.senders); |
| 40 | + expect(component.operationType).toStrictEqual(componentProps.operationType); |
| 41 | + expect(component.isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved); |
| 42 | + }); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('LogicGateReceiverComponent.fromBinary()', () => { |
| 47 | + let fastForwardedReader: BinaryReader; |
| 48 | + |
| 49 | + const prefab = new Prefab('MRK_Small_Lever', { |
| 50 | + components: { |
| 51 | + LogicGateReceiver: new LogicGateReceiverComponent({ version: componentVersion, ...componentProps }) |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + const saveString = prefab.toSaveString(); |
| 57 | + const [dataString] = saveString.split('|'); |
| 58 | + |
| 59 | + if (typeof dataString === 'undefined') { |
| 60 | + throw new Error('Invalid test data.'); |
| 61 | + } |
| 62 | + |
| 63 | + const unsignedIntegers = dataString.split(',').map(Number); |
| 64 | + |
| 65 | + const data = BinaryData.fromUnsignedIntegerArray(unsignedIntegers); |
| 66 | + const reader = new BinaryReader(data.toBinaryString()); |
| 67 | + |
| 68 | + reader.readUnsignedInteger(); // Root prefab hash. |
| 69 | + reader.readUnsignedInteger(); // SaveString bytes. |
| 70 | + reader.readUnsignedInteger(); // Parent prefab hash. |
| 71 | + reader.readFloat(); // Parent position X. |
| 72 | + reader.readFloat(); // Parent position Y. |
| 73 | + reader.readFloat(); // Parent position Z. |
| 74 | + reader.readFloat(); // Parent rotation X. |
| 75 | + reader.readFloat(); // Parent rotation Y. |
| 76 | + reader.readFloat(); // Parent rotation Z. |
| 77 | + reader.readFloat(); // Parent rotation W. |
| 78 | + reader.readFloat(); // Parent scale. |
| 79 | + reader.readUnsignedInteger(); // LogicGateReceiverComponent hash. |
| 80 | + reader.readUnsignedInteger(); // LogicGateReceiverComponent data length. |
| 81 | + |
| 82 | + fastForwardedReader = reader; |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns an instance of the LogicGateReceiverComponent class', () => { |
| 86 | + const component = LogicGateReceiverComponent.fromBinary(fastForwardedReader, componentVersion); |
| 87 | + |
| 88 | + expect(component).toBeInstanceOf(LogicGateReceiverComponent); |
| 89 | + expect(component.hash).toStrictEqual(componentHash); |
| 90 | + expect(component.name).toStrictEqual(componentName); |
| 91 | + expect(component.version).toStrictEqual(componentVersion); |
| 92 | + expect(component.senders).toStrictEqual(componentProps.senders); |
| 93 | + expect(component.operationType).toStrictEqual(componentProps.operationType); |
| 94 | + expect(component.isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('LogicGateReceiverComponent.toBinary()', () => { |
| 99 | + it('returns a BinaryString representation of the component', () => { |
| 100 | + const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps }); |
| 101 | + |
| 102 | + const data = component.toBinary(); |
| 103 | + |
| 104 | + const expectedData = |
| 105 | + '00000000000000000000000000000011000000000000000000000000010001010000000000000000000000011010010000000000000000000000010100111001100000000000000000000000000000101'; |
| 106 | + |
| 107 | + expect(data).toStrictEqual(expectedData); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('LogicGateReceiverComponent.write()', () => { |
| 112 | + it('writes a BinaryString representation of the component to the given BinaryWriter, including component hash and data length', () => { |
| 113 | + const component = new LogicGateReceiverComponent({ version: componentVersion, ...componentProps }); |
| 114 | + |
| 115 | + const writer = new BinaryWriter(); |
| 116 | + component.write(writer); |
| 117 | + |
| 118 | + const data = writer.flush(); |
| 119 | + |
| 120 | + const expectedData = |
| 121 | + '010100101111001111111000000011100000000000000000000000001010000100000000000000000000000000000011000000000000000000000000010001010000000000000000000000011010010000000000000000000000010100111001100000000000000000000000000000101'; |
| 122 | + |
| 123 | + expect(data).toStrictEqual(expectedData); |
| 124 | + |
| 125 | + const reader = new BinaryReader(data); |
| 126 | + |
| 127 | + const hash = reader.readUnsignedInteger(); |
| 128 | + reader.readUnsignedInteger(); // LogicGateReceiverComponent data length. |
| 129 | + reader.readUnsignedInteger(); // Senders array length. |
| 130 | + const sender1 = reader.readUnsignedInteger(); |
| 131 | + const sender2 = reader.readUnsignedInteger(); |
| 132 | + const sender3 = reader.readUnsignedInteger(); |
| 133 | + const operationType = reader.readSignedInteger(); |
| 134 | + const isInversedOutputSaved = reader.readBoolean(); |
| 135 | + |
| 136 | + expect(hash).toStrictEqual(componentHash); |
| 137 | + expect(sender1).toStrictEqual(componentProps.senders[0]); |
| 138 | + expect(sender2).toStrictEqual(componentProps.senders[1]); |
| 139 | + expect(sender3).toStrictEqual(componentProps.senders[2]); |
| 140 | + expect(operationType).toStrictEqual(componentProps.operationType); |
| 141 | + expect(isInversedOutputSaved).toStrictEqual(componentProps.isInversedOutputSaved); |
| 142 | + }); |
| 143 | +}); |
0 commit comments