Skip to content

Commit

Permalink
fix: typescript runtime test and tuples (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored May 6, 2024
1 parent a217537 commit f5bf72d
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 177 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@
"test:runtime:python": "cross-env CI=true jest ./test/runtime/runtime-python.spec.ts",
"generate:runtime:python": "cross-env CI=true ts-node ./test/runtime/runtime-python.ts",
"generate:runtime:java": "cross-env CI=true ts-node ./test/runtime/runtime-java.ts",
"test:runtime:go": "cross-env CI=true jest ./test/runtime/runtime-go.spec.ts",
"generate:runtime:go": "cross-env CI=true ts-node ./test/runtime/runtime-go.ts",
"test:runtime:cplusplus": "cross-env CI=true jest ./test/runtime/runtime-cplusplus.spec.ts",
"generate:runtime:cplusplus": "cross-env CI=true ts-node ./test/runtime/runtime-cplusplus.ts",
"test:runtime:kotlin": "cross-env CI=true jest ./test/runtime/runtime-kotlin.spec.ts",
"generate:runtime:kotlin": "cross-env CI=true ts-node ./test/runtime/runtime-kotlin.ts",
"test:runtime:rust": "cross-env CI=true jest ./test/runtime/runtime-rust.spec.ts",
"generate:runtime:rust": "cross-env CI=true ts-node ./test/runtime/runtime-rust.ts",
"test:runtime:php": "cross-env CI=true jest ./test/runtime/runtime-php.spec.ts",
"generate:runtime:php": "cross-env CI=true ts-node ./test/runtime/runtime-php.ts",
"test:runtime:typescript": "cd ./test/runtime/runtime-typescript && npm i && npm run test",
"generate:runtime:typescript": "cross-env CI=true ts-node ./test/runtime/runtime-typescript.ts",
"test:runtime:csharp": "cross-env CI=true jest ./test/runtime/runtime-csharp.spec.ts",
"generate:runtime:csharp": "cross-env CI=true ts-node ./test/runtime/runtime-csharp.ts",
"test:runtime:scala": "cross-env CI=true jest ./test/runtime/runtime-scala.spec.ts",
"generate:runtime:scala": "cross-env CI=true ts-node ./test/runtime/runtime-scala.ts",
"test:runtime:go": "cross-env CI=true jest ./test/runtime/runtime-go.spec.ts",
"test:runtime:cplusplus": "cross-env CI=true jest ./test/runtime/runtime-cplusplus.spec.ts",
"test:runtime:kotlin": "cross-env CI=true jest ./test/runtime/runtime-kotlin.spec.ts",
"test:runtime:rust": "cross-env CI=true jest ./test/runtime/runtime-rust.spec.ts",
"test:runtime:php": "cross-env CI=true jest ./test/runtime/runtime-php.spec.ts",
"test:runtime:typescript": "cd ./test/runtime/runtime-typescript && npm i && npm run test",
"test:runtime:csharp": "cross-env CI=true jest ./test/runtime/runtime-csharp.spec.ts",
"test:runtime:scala": "cross-env CI=true jest ./test/runtime/runtime-scala.spec.ts",
"test:watch": "jest --watch",
"docs": "npm run docs:api",
"docs:api": "typedoc --sourceLinkExternal src/index.ts --out ./modelina-website/public/apidocs/generated --name Modelina",
Expand Down
5 changes: 3 additions & 2 deletions src/generators/typescript/presets/utils/MarshalFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function renderMarshalProperty(

return realizePropertyFactory(modelInstanceVariable);
}

/**
* Render marshalling logic for tuples
*/
Expand All @@ -47,12 +48,12 @@ function renderTupleSerialization(
tupleEntry.value
);
return `if(${modelInstanceVariable}[${tupleEntry.index}]) {
serializedTuple[${tupleEntry.index}] = ${temp}
serializedTuple[${tupleEntry.index}] = \`${temp}\`
} else {
serializedTuple[${tupleEntry.index}] = null;
}`;
});
return `const serializedTuple = [];
return `const serializedTuple: any[] = [];
${t.join('\n')}
json += \`"${unconstrainedProperty}": [\${serializedTuple.join(',')}],\`;`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
json += \`\\"arrayTest\\": [\${arrayTestJsonValues.join(',')}],\`;
}
if(this.tupleTest !== undefined) {
const serializedTuple = [];
const serializedTuple: any[] = [];
if(this.tupleTest[0]) {
serializedTuple[0] = \${this.tupleTest[0].marshal()}
serializedTuple[0] = \`\${this.tupleTest[0].marshal()}\`
} else {
serializedTuple[0] = null;
}
if(this.tupleTest[1]) {
serializedTuple[1] = \${typeof this.tupleTest[1] === 'number' || typeof this.tupleTest[1] === 'boolean' ? this.tupleTest[1] : JSON.stringify(this.tupleTest[1])}
serializedTuple[1] = \`\${typeof this.tupleTest[1] === 'number' || typeof this.tupleTest[1] === 'boolean' ? this.tupleTest[1] : JSON.stringify(this.tupleTest[1])}\`
} else {
serializedTuple[1] = null;
}
Expand Down
78 changes: 78 additions & 0 deletions test/runtime/generic-input-all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"$id": "TestObject",
"description": "This object contains all types of MetaModel generations",
"type": "object",
"properties": {
"string_type": {
"type": "string"
},
"number_type": {
"type": "number"
},
"boolean_type": {
"type": "boolean",
"description": "Status if marriage live in given house"
},
"union_type": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
}
]
},
"array_type": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
}
]
}
},
"tuple_type": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "number"
}
],
"additionalItems": false
},
"object_type": {
"type": "object",
"properties": {
"test": {
"type": "string"
}
}
},
"dictionary_type": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"enum_type": {
"enum": ["test", 1, true, {"test": 2}]
}
},
"patternProperties": {
"^S(.?*)test&": {
"type": "string"
}
},
"required": ["string_type", "boolean_type"]
}
49 changes: 49 additions & 0 deletions test/runtime/generic-input-simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$id": "TestObject",
"description": "This object contains simple types of MetaModel generations. Object, enum, number, boolean array, tuple.",
"type": "object",
"properties": {
"string_type": {
"type": "string"
},
"number_type": {
"type": "number"
},
"boolean_type": {
"type": "boolean",
"description": "Status if marriage live in given house"
},
"array_type": {
"type": "array",
"items": {
"type": "string"
}
},
"tuple_type": {
"type": "array",
"items": [
{
"type": "string"
},
{
"type": "number"
}
],
"additionalItems": false
},
"object_type": {
"type": "object",
"properties": {
"test": {
"type": "string"
}
},
"additionalProperties": false
},
"enum_type": {
"enum": ["test", 1, true, {"test": 2}]
}
},
"additionalProperties": false,
"required": ["string_type", "boolean_type"]
}
46 changes: 12 additions & 34 deletions test/runtime/runtime-typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, TypeScriptFileGenerator } from '../../';
import path from 'path';
import input from './generic-input.json';
import input from './generic-input-all.json';
import simpleInput from './generic-input-simple.json';

async function generateNamedExport() {
const generator = new TypeScriptFileGenerator({});
Expand Down Expand Up @@ -52,6 +53,7 @@ async function generateMarshalling() {
{ exportType: 'named' }
);
}

async function generateJsonBinPack() {
const generator = new TypeScriptFileGenerator({
presets: [
Expand All @@ -64,29 +66,8 @@ async function generateJsonBinPack() {
TS_JSONBINPACK_PRESET
]
});
const simplifiedInput = {
"$id": "Address",
"description": "This object contains all types of MetaModel generations",
"type": "object",
"properties": {
"street_name": {
"type": "string"
},
"house_number": {
"type": "number"
},
"marriage": {
"type": "boolean",
"description": "Status if marriage live in given house"
},
},
"required": [
"street_name",
"house_number",
]
}
await generator.generateToFiles(
simplifiedInput,
simpleInput,
path.resolve(
// eslint-disable-next-line no-undef
__dirname,
Expand All @@ -96,14 +77,11 @@ async function generateJsonBinPack() {
);
}

async function generateEverything() {
await Promise.all(
[
generateNamedExport(),
generateDefaultExport(),
generateMarshalling(),
generateJsonBinPack()
]
)
}
generateEverything();
Promise.all(
[
generateNamedExport(),
generateDefaultExport(),
generateMarshalling(),
generateJsonBinPack()
]
)
41 changes: 25 additions & 16 deletions test/runtime/runtime-typescript/test/DefaultAddressTest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import Address from '../src/defaultExport/Address';
import NestedObject from '../src/defaultExport/NestedObject';
import TestObject from '../src/defaultExport/TestObject';
import ObjectType from '../src/defaultExport/ObjectType';
import EnumType from '../src/defaultExport/EnumType';

describe('Address', () => {
describe('Default exports', () => {
test('should be able to instantiate default export model', () => {
const nestedObj = new NestedObject({
const objectType = new ObjectType({
test: 'test'
});
const address = new Address({
streetName: 'test',
houseNumber: 1,
marriage: true,
members: 2,
const testObject = new TestObject({
stringType: 'test',
numberType: 1,
booleanType: true,
arrayType: [1, 'test'],
nestedObject: nestedObj
objectType: objectType,
dictionaryType: new Map(Object.entries({"test": "test"})),
additionalProperties: new Map(Object.entries({"test": "test"})),
enumType: EnumType.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_2_CURLYRIGHT,
tupleType: ['test', 1],
unionType: 'test'
});
expect(address.streetName).toEqual('test');
expect(address.houseNumber).toEqual(1);
expect(address.marriage).toEqual(true);
expect(address.members).toEqual(2);
expect(address.arrayType).toEqual([1, 'test']);
expect(address.nestedObject?.test).toEqual('test');
expect(testObject.stringType).toEqual('test');
expect(testObject.numberType).toEqual(1);
expect(testObject.booleanType).toEqual(true);
expect(testObject.arrayType).toEqual([1, 'test']);
expect(testObject.objectType).toEqual(objectType);
expect(Object.fromEntries(testObject.dictionaryType!)).toEqual({"test": "test"});
expect(Object.fromEntries(testObject.additionalProperties!)).toEqual({"test": "test"});
expect(testObject.enumType).toEqual(EnumType.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_2_CURLYRIGHT);
expect(testObject.tupleType).toEqual(['test', 1]);
expect(testObject.unionType).toEqual('test');
});
});
29 changes: 19 additions & 10 deletions test/runtime/runtime-typescript/test/JsonBinPackPreset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Address } from '../src/jsonbinpack/Address';
import { ObjectType } from '../src/jsonbinpack/ObjectType';
import { TestObject } from '../src/jsonbinpack/TestObject';
import { EnumType } from '../src/jsonbinpack/EnumType';

describe('Address', () => {
const address = new Address({
streetName: 'test',
houseNumber: 1,
marriage: true
describe('JSON binpack', () => {
const objectType = new ObjectType({
test: 'test'
});
test('be able to serialize model and turning it back to a model with the same values', async () => {
const serialized = await address.jsonbinSerialize();
const newAddress = await Address.jsonbinDeserialize(serialized);
expect(address.marshal()).toEqual(newAddress.marshal());
const testObject = new TestObject({
stringType: 'test',
numberType: 1,
booleanType: true,
arrayType: ['test'],
objectType: objectType,
enumType: EnumType.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_2_CURLYRIGHT,
tupleType: ['test', 1]
});
test.skip('should be able to serialize model and turning it back to a model with the same values', async () => {
const serialized = await testObject.jsonbinSerialize();
const newTestObject = await TestObject.jsonbinDeserialize(serialized);
expect(testObject.marshal()).toEqual(newTestObject.marshal());
});
});
Loading

0 comments on commit f5bf72d

Please sign in to comment.