@eclipse-emfcloud/modelserver-client
Version:
Typescript rest client to interact with an EMF.cloud modelserver
97 lines • 4.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/********************************************************************************
* Copyright (c) 2022 STMicroelectronics and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the MIT License which is
* available at https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: EPL-2.0 OR MIT
*******************************************************************************/
const chai_1 = require("chai");
const type_util_1 = require("./type-util");
describe('tests for type-util', () => {
describe('encode', () => {
const emptyArray = [];
const tags = ['high-prio', 'red'];
// eslint-disable-next-line no-null/no-null
const nada = null; // JSON uses null
const emptyObj = {};
it('JSON v1 as JSON v1', () => {
const object = { eClass: 'Foo', name: 'test', nested: [{ eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }] };
const encoded = (0, type_util_1.encode)('json')(object);
(0, chai_1.expect)(encoded).to.be.equal(encoded);
});
it('JSON v1 as JSON v2', () => {
const object = { eClass: 'Foo', name: 'test', nested: [{ eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }] };
const expected = { $type: 'Foo', name: 'test', nested: [{ $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }] };
const encoded = (0, type_util_1.encode)('json-v2')(object);
(0, chai_1.expect)(encoded).to.be.eql(expected);
});
it('JSON v2 as JSON v1', () => {
const object = { $type: 'Foo', name: 'test', nested: [{ $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }] };
const expected = {
eClass: 'Foo',
name: 'test',
nested: [{ eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }]
};
const encoded = (0, type_util_1.encode)('json')(object);
(0, chai_1.expect)(encoded).to.be.eql(expected);
});
it('JSON v2 as JSON v2', () => {
const object = { $type: 'Foo', name: 'test', nested: [{ $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }] };
const encoded = (0, type_util_1.encode)('json-v2')(object);
(0, chai_1.expect)(encoded).to.be.equal(encoded);
});
});
describe('encode string', () => {
const emptyArray = [];
const tags = ['high-prio', 'red'];
// eslint-disable-next-line no-null/no-null
const nada = null; // JSON uses null
const emptyObj = {};
it('JSON v1 as JSON v1', () => {
const object = JSON.stringify({
eClass: 'Foo',
name: 'test',
nested: { eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }
});
const encoded = (0, type_util_1.encode)('json')(object);
(0, chai_1.expect)(encoded).to.be.equal(encoded);
});
it('JSON v1 as JSON v2', () => {
const object = JSON.stringify({
eClass: 'Foo',
name: 'test',
nested: { eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }
});
const expected = { $type: 'Foo', name: 'test', nested: { $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj } };
const encoded = (0, type_util_1.encode)('json-v2')(object);
(0, chai_1.expect)(encoded).to.be.a('string');
(0, chai_1.expect)(JSON.parse(encoded)).to.be.eql(expected);
});
it('JSON v2 as JSON v1', () => {
const object = JSON.stringify({
$type: 'Foo',
name: 'test',
nested: { $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }
});
const expected = { eClass: 'Foo', name: 'test', nested: { eClass: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj } };
const encoded = (0, type_util_1.encode)('json')(object);
(0, chai_1.expect)(encoded).to.be.a('string');
(0, chai_1.expect)(JSON.parse(encoded)).to.be.eql(expected);
});
it('JSON v2 as JSON v2', () => {
const object = JSON.stringify({
$type: 'Foo',
name: 'test',
nested: { $type: 'Nested', name: 'inner', tags, emptyArray, nada, emptyObj }
});
const encoded = (0, type_util_1.encode)('json-v2')(object);
(0, chai_1.expect)(encoded).to.be.equal(encoded);
});
});
});
//# sourceMappingURL=type-util.spec.js.map