|
| 1 | +import { NodeFileSystem } from 'langium/node'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { createSafeDsServicesWithBuiltins } from '../../../../src/language/index.js'; |
| 4 | +import { |
| 5 | + BooleanConstant, |
| 6 | + FloatConstant, |
| 7 | + IntConstant, |
| 8 | + NullConstant, |
| 9 | + StringConstant, |
| 10 | +} from '../../../../src/language/partialEvaluation/model.js'; |
| 11 | +import { LiteralType, Type } from '../../../../src/language/typing/model.js'; |
| 12 | + |
| 13 | +const services = (await createSafeDsServicesWithBuiltins(NodeFileSystem)).SafeDs; |
| 14 | +const coreTypes = services.types.CoreTypes; |
| 15 | +const typeComputer = services.types.TypeComputer; |
| 16 | + |
| 17 | +const tests: ComputeClassTypeForLiteralTypeTest[] = [ |
| 18 | + // Base cases |
| 19 | + { |
| 20 | + literalType: new LiteralType(), |
| 21 | + expected: coreTypes.Nothing, |
| 22 | + }, |
| 23 | + { |
| 24 | + literalType: new LiteralType(new BooleanConstant(false)), |
| 25 | + expected: coreTypes.Boolean, |
| 26 | + }, |
| 27 | + { |
| 28 | + literalType: new LiteralType(new FloatConstant(1.5)), |
| 29 | + expected: coreTypes.Float, |
| 30 | + }, |
| 31 | + { |
| 32 | + literalType: new LiteralType(new IntConstant(1n)), |
| 33 | + expected: coreTypes.Int, |
| 34 | + }, |
| 35 | + { |
| 36 | + literalType: new LiteralType(NullConstant), |
| 37 | + expected: coreTypes.NothingOrNull, |
| 38 | + }, |
| 39 | + { |
| 40 | + literalType: new LiteralType(new StringConstant('')), |
| 41 | + expected: coreTypes.String, |
| 42 | + }, |
| 43 | + // Nullable types |
| 44 | + { |
| 45 | + literalType: new LiteralType(new BooleanConstant(false), NullConstant), |
| 46 | + expected: coreTypes.Boolean.updateNullability(true), |
| 47 | + }, |
| 48 | + { |
| 49 | + literalType: new LiteralType(new FloatConstant(1.5), NullConstant), |
| 50 | + expected: coreTypes.Float.updateNullability(true), |
| 51 | + }, |
| 52 | + { |
| 53 | + literalType: new LiteralType(new IntConstant(1n), NullConstant), |
| 54 | + expected: coreTypes.Int.updateNullability(true), |
| 55 | + }, |
| 56 | + { |
| 57 | + literalType: new LiteralType(new StringConstant(''), NullConstant), |
| 58 | + expected: coreTypes.String.updateNullability(true), |
| 59 | + }, |
| 60 | + // Other combinations |
| 61 | + { |
| 62 | + literalType: new LiteralType(new BooleanConstant(false), new FloatConstant(1.5)), |
| 63 | + expected: coreTypes.Any, |
| 64 | + }, |
| 65 | + { |
| 66 | + literalType: new LiteralType(new FloatConstant(1.5), new IntConstant(1n)), |
| 67 | + expected: coreTypes.Number, |
| 68 | + }, |
| 69 | + { |
| 70 | + literalType: new LiteralType(new IntConstant(1n), new StringConstant('')), |
| 71 | + expected: coreTypes.Any, |
| 72 | + }, |
| 73 | + { |
| 74 | + literalType: new LiteralType(new BooleanConstant(false), new FloatConstant(1.5), NullConstant), |
| 75 | + expected: coreTypes.AnyOrNull, |
| 76 | + }, |
| 77 | + { |
| 78 | + literalType: new LiteralType(new FloatConstant(1.5), new IntConstant(1n), NullConstant), |
| 79 | + expected: coreTypes.Number.updateNullability(true), |
| 80 | + }, |
| 81 | + { |
| 82 | + literalType: new LiteralType(new IntConstant(1n), new StringConstant(''), NullConstant), |
| 83 | + expected: coreTypes.AnyOrNull, |
| 84 | + }, |
| 85 | +]; |
| 86 | + |
| 87 | +describe.each(tests)('computeClassTypeForLiteralType', ({ literalType, expected }) => { |
| 88 | + it(`should return the class type for a literal type (${literalType})`, () => { |
| 89 | + expect(typeComputer.computeClassTypeForLiteralType(literalType)).toSatisfy((actual: Type) => |
| 90 | + actual.equals(expected), |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +/** |
| 96 | + * A test case for {@link computeClassTypeForLiteralType}. |
| 97 | + */ |
| 98 | +interface ComputeClassTypeForLiteralTypeTest { |
| 99 | + /** |
| 100 | + * The literal type to compute the class type for. |
| 101 | + */ |
| 102 | + literalType: LiteralType; |
| 103 | + |
| 104 | + /** |
| 105 | + * The expected type. |
| 106 | + */ |
| 107 | + expected: Type; |
| 108 | +} |
0 commit comments