From 59e547d08b7f2bb950629cfce98758b45aac624c Mon Sep 17 00:00:00 2001 From: nulltoken Date: Fri, 17 Jan 2020 00:27:24 +0100 Subject: [PATCH] Add more format based test coverage --- src/functions/__tests__/formattedEnum.test.ts | 107 ++++++++++++------ 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/src/functions/__tests__/formattedEnum.test.ts b/src/functions/__tests__/formattedEnum.test.ts index debe2d15d..2afd3524e 100644 --- a/src/functions/__tests__/formattedEnum.test.ts +++ b/src/functions/__tests__/formattedEnum.test.ts @@ -51,48 +51,87 @@ describe('formattedEnum', () => { expect(runFormattedEnum(schema, defaultReportingThreshold)).toBeUndefined(); }); - test('should return undefined when all enum values respect the format', () => { - const schema = { - type: 'integer', - enum: [123, 456], - format: 'int64', - }; - - expect(runFormattedEnum(schema, defaultReportingThreshold)).toBeUndefined(); - }); - - test('should indentify enum values that do not respect the format', () => { - const schema = { - type: 'integer', - enum: [123, 'a string!', 456, 'and another one!'], - format: 'int64', - }; + describe('basic tests', () => { + test('should return undefined when all enum values respect the format', () => { + const schema = { + type: 'integer', + enum: [123, 456], + format: 'int64', + }; - expect(runFormattedEnum(schema, defaultReportingThreshold)).toEqual([ - { - message: 'Some enum values do not respect the specified format "int64": "a string!", "and another one!".', - }, - ]); - }); + expect(runFormattedEnum(schema, defaultReportingThreshold)).toBeUndefined(); + }); - test.each([ - [1, '"a string!"...'], - [2, '"a string!", "and another one!"...'], - [3, '"a string!", "and another one!", "seriously?"...'], - ])( - 'should add an ellipsis at the end of the error message when the number of invalid enum values is greater than the reporting threshold "%s"', - (threshold, errorMessageTrail) => { + test('should identify enum values that do not respect the format', () => { const schema = { type: 'integer', - enum: ['a string!', 'and another one!', 'seriously?', 'you must be kidding me'], + enum: [123, 'a string!', 456, 'and another one!'], format: 'int64', }; - expect(runFormattedEnum(schema, threshold)).toEqual([ + expect(runFormattedEnum(schema, defaultReportingThreshold)).toEqual([ { - message: `Some enum values do not respect the specified format "int64": ${errorMessageTrail}`, + message: 'Some enum values do not respect the specified format "int64": "a string!", "and another one!".', }, ]); - }, - ); + }); + }); + + describe('formats', () => { + const testCases: Array<[string, string, unknown[], unknown]> = [ + ['byte', 'string', ['SGVsbG8=', 'd29ybGQh'], 'Hello'], + ['int32', 'integer', [-2147483648, 17], 'Hello'], + ]; + + test.each(testCases)( + 'does not report anything when all the definitions are valid for format "%s" of type "%s"', + async (format: string, type: string, valids: unknown[], invalid: unknown) => { + const schema = { + type, + enum: valids, + format, + }; + + expect(runFormattedEnum(schema, defaultReportingThreshold)).toBeUndefined(); + }, + ); + + test.each(testCases)( + 'should identify enum values that do not respect the format "%s" of type "%s"', + async (format: string, type: string, valids: unknown[], invalid: unknown) => { + const schema = { + type, + enum: [valids[0], invalid], + format, + }; + + const results = runFormattedEnum(schema, defaultReportingThreshold); + + expect(results[0].message).toContain(`"${invalid}"`); + }, + ); + }); + + describe('reporting threshold', () => { + test.each([ + [1, '"a string!"...'], + [2, '"a string!", "and another one!"...'], + [3, '"a string!", "and another one!", "seriously?"...'], + ])( + 'should add an ellipsis at the end of the error message when the number of invalid enum values is greater than the reporting threshold "%s"', + (threshold, errorMessageTrail) => { + const schema = { + type: 'integer', + enum: ['a string!', 'and another one!', 'seriously?', 'you must be kidding me'], + format: 'int64', + }; + + expect(runFormattedEnum(schema, threshold)).toEqual([ + { + message: `Some enum values do not respect the specified format "int64": ${errorMessageTrail}`, + }, + ]); + }, + ); + }); });