Skip to content

Commit

Permalink
Add more format based test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Jan 16, 2020
1 parent 43344a4 commit 59e547d
Showing 1 changed file with 73 additions and 34 deletions.
107 changes: 73 additions & 34 deletions src/functions/__tests__/formattedEnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
},
]);
},
);
});
});

0 comments on commit 59e547d

Please sign in to comment.