UNPKG

3.04 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 Red Hat, Inc. and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const chai_1 = require("chai");
19const rpc_message_encoder_1 = require("./rpc-message-encoder");
20const uint8_array_message_buffer_1 = require("./uint8-array-message-buffer");
21describe('PPC Message Encoder & Decoder', () => {
22 describe('MsgPack Encoder & Decoder', () => {
23 it('should encode object into binary message and decode the message back into the original object', () => {
24 const buffer = new Uint8Array(1024);
25 const writer = new uint8_array_message_buffer_1.Uint8ArrayWriteBuffer(buffer);
26 const testObject = {
27 string: 'string',
28 boolean: true,
29 integer: 5,
30 float: 14.5,
31 array: ['1', 2, { three: 'three' }],
32 set: new Set([1, 2, 3]),
33 map: new Map([[1, 1], [2, 2], [3, 3]]),
34 buffer: new TextEncoder().encode('ThisIsAUint8Array'),
35 object: { foo: 'bar', baz: true },
36 undefined: undefined,
37 // eslint-disable-next-line no-null/no-null
38 null: null
39 };
40 const encoder = new rpc_message_encoder_1.MsgPackMessageEncoder();
41 encoder.encode(writer, testObject);
42 const written = writer.getCurrentContents();
43 const reader = new uint8_array_message_buffer_1.Uint8ArrayReadBuffer(written);
44 const decoder = new rpc_message_encoder_1.MsgPackMessageDecoder();
45 const decoded = decoder.decode(reader);
46 (0, chai_1.expect)(decoded).deep.equal(testObject);
47 });
48 it('should fail with an EncodingError when trying to encode the object ', () => {
49 const x = new Set();
50 const y = new Set();
51 x.add(y);
52 y.add(x);
53 const writer = new uint8_array_message_buffer_1.Uint8ArrayWriteBuffer();
54 const encoder = new rpc_message_encoder_1.MsgPackMessageEncoder();
55 (0, chai_1.expect)(() => encoder.encode(writer, x)).to.throw(rpc_message_encoder_1.EncodingError);
56 });
57 });
58});
59//# sourceMappingURL=rpc-message-encoder.spec.js.map
\No newline at end of file