From bea9a9f9f9ba6d23cb41e7136edbd76f2cce67be Mon Sep 17 00:00:00 2001 From: Christian Capeans Date: Fri, 15 Sep 2023 12:26:38 +0200 Subject: [PATCH] Finish expression tests --- .../definitions/utils/expression.d.ts | 91 +++++++++++++++++++ .../types/__tests__/utils/expression.test.js | 33 +++++++ 2 files changed, 124 insertions(+) diff --git a/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts b/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts index 90daa2a9f5..1b679d1b8b 100644 --- a/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts +++ b/packages/core/strapi/lib/types/__tests__/definitions/utils/expression.d.ts @@ -1,4 +1,5 @@ import type { Utils } from '@strapi/strapi'; +import { MatchAllIntersect } from '../../../utils/expression'; // IsNever type IsNeverGivenNever = Utils.Expression.IsNever; @@ -128,6 +129,72 @@ type IfWithNeverReturnType = Utils.Expression.If; type IfWithUnknownReturnType = Utils.Expression.If; type IfWithAnyReturnType = Utils.Expression.If; +// MatchFirst +type MatchFirstReturnsTestSuccessful = Utils.Expression.MatchFirst< + [[Utils.Expression.IsTrue, 'test-successful']], + 'fail' +>; +type MatchFirstReturnsDefault = Utils.Expression.MatchFirst< + [[Utils.Expression.IsTrue, 'test-successful']], + 'default' +>; +type MatchFirstReturnsNeverWithEmptyTests = Utils.Expression.MatchFirst<[], 'default'>; +type MatchFirstReturnsNeverWithNeverDefault = Utils.Expression.MatchFirst< + [[Utils.Expression.IsTrue, 'test-successful']], + never +>; + +// MatchAllIntersect +type MatchAllIntersectWithOneTrueCondition = Utils.Expression.MatchAllIntersect< + [ + [Utils.Expression.IsTrue, { test: 1 }], + [Utils.Expression.IsTrue, { test: 2 }] + ] +>; +type MatchAllIntersectWithAllFalseConditions = Utils.Expression.MatchAllIntersect< + [ + [Utils.Expression.IsTrue, { test: 1 }], + [Utils.Expression.IsTrue, { test: 2 }] + ] +>; + +type MatchAllIntersectWithIntersection = Utils.Expression.MatchAllIntersect< + [ + [Utils.Expression.Extends<'test', string>, 'test'], + [Utils.Expression.Extends<'test', string>, 'test'] + ] +>; + +// Test +type TestPasses = Utils.Expression.Test, 'test'>; +type TestFails = Utils.Expression.Test, 'test'>; + +// And +type AndTrue = Utils.Expression.And< + Utils.Expression.IsTrue, + Utils.Expression.IsTrue +>; +type AndFalse = Utils.Expression.And< + Utils.Expression.IsTrue, + Utils.Expression.IsTrue +>; + +// Or +type OrTrue = Utils.Expression.Or< + Utils.Expression.IsTrue, + Utils.Expression.IsTrue +>; + +type OrFalse = Utils.Expression.Or< + Utils.Expression.IsTrue, + Utils.Expression.IsTrue +>; + +type OrTrueFalse = Utils.Expression.Or< + Utils.Expression.IsTrue, + Utils.Expression.IsTrue +>; + export { // IsNever IsNeverGivenNever, @@ -248,4 +315,28 @@ export { IfWithNeverReturnType, IfWithUnknownReturnType, IfWithAnyReturnType, + + // MAtchFirst + MatchFirstReturnsTestSuccessful, + MatchFirstReturnsDefault, + MatchFirstReturnsNeverWithEmptyTests, + MatchFirstReturnsNeverWithNeverDefault, + + // MatchAllIntersect + MatchAllIntersectWithOneTrueCondition, + MatchAllIntersectWithAllFalseConditions, + MatchAllIntersectWithIntersection, + + // Test + TestPasses, + TestFails, + + // And + AndTrue, + AndFalse, + + // Or + OrTrue, + OrFalse, + OrTrueFalse, }; diff --git a/packages/core/strapi/lib/types/__tests__/utils/expression.test.js b/packages/core/strapi/lib/types/__tests__/utils/expression.test.js index 0f52c94b80..0a4e4223d3 100644 --- a/packages/core/strapi/lib/types/__tests__/utils/expression.test.js +++ b/packages/core/strapi/lib/types/__tests__/utils/expression.test.js @@ -161,4 +161,37 @@ describe('Utils.Expression', () => { type('IfWithUnknownReturnType').isUnknown(); type('IfWithAnyReturnType').isAny(); }); + + test('MatchFirst', () => { + type('MatchFirstReturnsTestSuccessful').isStringLiteral('test-successful'); + type('MatchFirstReturnsDefault').isStringLiteral('default'); + type('MatchFirstReturnsNeverWithEmptyTests').isNever(); + type('MatchFirstReturnsNeverWithNeverDefault').isNever(); + }); + + test('MatchAllIntersect', () => { + type('MatchAllIntersectWithOneTrueCondition').isAnonymousObject({ + properties: { + test: t.numberLiteral(1), + }, + }); + type('MatchAllIntersectWithAllFalseConditions').isUnknown(); + type('MatchAllIntersectWithIntersection').isStringLiteral('test'); + }); + + test('Test', () => { + type('TestPasses').isTuple([t.booleanLiteral(true), t.stringLiteral('test')]); + type('TestFails').isTuple([t.booleanLiteral(false), t.stringLiteral('test')]); + }); + + test('And', () => { + type('AndTrue').isBooleanLiteral(true); + type('AndFalse').isBooleanLiteral(false); + }); + + test('Or', () => { + type('OrTrue').isBooleanLiteral(true); + type('OrFalse').isBooleanLiteral(false); + type('OrTrueFalse').isBooleanLiteral(true); + }); });