|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright 2025 TypeFox GmbH |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the MIT License, which is available in the project root. |
| 5 | + ******************************************************************************/ |
| 6 | + |
| 7 | +import { beforeEach, describe, expect, test } from 'vitest'; |
| 8 | +import { CustomKind } from '../../../src/kinds/custom/custom-kind.js'; |
| 9 | +import { TestingSpecifics, createTypirServicesForTesting } from '../../../src/test/predefined-language-nodes.js'; |
| 10 | +import { TypirServices } from '../../../src/typir.js'; |
| 11 | + |
| 12 | +// These test cases test custom types with optional properties |
| 13 | + |
| 14 | +describe('Optional custom properties', () => { |
| 15 | + type MyCustomType = { |
| 16 | + myNumber?: number; |
| 17 | + myString?: string; |
| 18 | + }; |
| 19 | + |
| 20 | + let typir: TypirServices<TestingSpecifics>; |
| 21 | + let customKind: CustomKind<MyCustomType, TestingSpecifics>; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + typir = createTypirServicesForTesting(); |
| 25 | + |
| 26 | + customKind = new CustomKind<MyCustomType, TestingSpecifics>(typir, { |
| 27 | + name: 'MyCustom1', |
| 28 | + calculateTypeName: properties => `Custom1-${properties.myNumber}-${properties.myString}`, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + test('Specified non-undefined values', () => { |
| 33 | + const properties = customKind.create({ properties: { myNumber: 123, myString: 'hello' } }).finish().getTypeFinal()!.properties; |
| 34 | + expect(properties.myNumber).toBe(123); |
| 35 | + expect(properties.myString).toBe('hello'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('Skipped all values (implicit undefined)', () => { |
| 39 | + const properties = customKind.create({ properties: { /* empty */ } }).finish().getTypeFinal()!.properties; |
| 40 | + expect(properties.myNumber).toBe(undefined); |
| 41 | + expect(properties.myString).toBe(undefined); |
| 42 | + }); |
| 43 | + |
| 44 | + test('Used "undefined" as values (explicit undefined)', () => { |
| 45 | + const properties = customKind.create({ properties: { myNumber: undefined, myString: undefined } }).finish().getTypeFinal()!.properties; |
| 46 | + expect(properties.myNumber).toBe(undefined); |
| 47 | + expect(properties.myString).toBe(undefined); |
| 48 | + }); |
| 49 | + |
| 50 | +}); |
| 51 | + |
| 52 | +describe('Custom properties with "undefined" as type', () => { |
| 53 | + type MyCustomType = { |
| 54 | + myNumber: number | undefined; |
| 55 | + myString: string | undefined; |
| 56 | + }; |
| 57 | + |
| 58 | + let typir: TypirServices<TestingSpecifics>; |
| 59 | + let customKind: CustomKind<MyCustomType, TestingSpecifics>; |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + typir = createTypirServicesForTesting(); |
| 63 | + |
| 64 | + customKind = new CustomKind<MyCustomType, TestingSpecifics>(typir, { |
| 65 | + name: 'MyCustom1', |
| 66 | + calculateTypeName: properties => `Custom1-${properties.myNumber}-${properties.myString}`, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + test('Specified non-undefined values', () => { |
| 71 | + const properties = customKind.create({ properties: { myNumber: 123, myString: 'hello' } }).finish().getTypeFinal()!.properties; |
| 72 | + expect(properties.myNumber).toBe(123); |
| 73 | + expect(properties.myString).toBe('hello'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Used "undefined" as values (explicit undefined)', () => { |
| 77 | + const properties = customKind.create({ properties: { myNumber: undefined, myString: undefined } }).finish().getTypeFinal()!.properties; |
| 78 | + expect(properties.myNumber).toBe(undefined); |
| 79 | + expect(properties.myString).toBe(undefined); |
| 80 | + }); |
| 81 | + |
| 82 | +}); |
0 commit comments