Skip to content

Commit

Permalink
test(functions): include error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 14, 2022
1 parent fcac182 commit 2da8258
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/functions/src/__tests__/alphabetical.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Core Functions / Alphabetical', () => {
[{ keyedBy: 2 }, '"alphabetical" function and its "keyedBy" option accepts only the following types: string'],
])('given invalid %p options, should throw', async (opts, error) => {
await expect(runAlphabetical([], opts)).rejects.toThrowAggregateError(
new AggregateError([new RulesetValidationError(error, [])]),
new AggregateError([new RulesetValidationError('invalid-function-options', error, [])]),
);
});
});
Expand Down
29 changes: 25 additions & 4 deletions packages/functions/src/__tests__/casing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,34 +379,53 @@ describe('Core Functions / Casing', () => {
{ type: 'foo' },
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function and its "type" option accept the following values: flat, camel, pascal, kebab, cobol, snake, macro',
[],
),
],
],
[
{ type: 'macro', foo: true },
[new RulesetValidationError('"casing" function does not support "foo" option', [])],
[new RulesetValidationError('invalid-function-options', '"casing" function does not support "foo" option', [])],
],
[
{
type: 'pascal',
disallowDigits: false,
separator: {},
},
[new RulesetValidationError('"casing" function is missing "separator.char" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function is missing "separator.char" option',
[],
),
],
],
[
{
type: 'pascal',
disallowDigits: false,
separator: { allowLeading: true },
},
[new RulesetValidationError('"casing" function is missing "separator.char" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function is missing "separator.char" option',
[],
),
],
],
[
{ type: 'snake', separator: { char: 'a', foo: true } },
[new RulesetValidationError('"casing" function does not support "separator.foo" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function does not support "separator.foo" option',
[],
),
],
],
[
{
Expand All @@ -417,6 +436,7 @@ describe('Core Functions / Casing', () => {
},
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function and its "separator.char" option accepts only char, i.e. "I" or "/"',
[],
),
Expand All @@ -431,6 +451,7 @@ describe('Core Functions / Casing', () => {
},
[
new RulesetValidationError(
'invalid-function-options',
'"casing" function and its "separator.char" option accepts only char, i.e. "I" or "/"',
[],
),
Expand Down
4 changes: 3 additions & 1 deletion packages/functions/src/__tests__/defined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('Core Functions / Defined', () => {
describe('validation', () => {
it.each([{}, 2])('given invalid %p options, should throw', async opts => {
await expect(runDefined([], opts)).rejects.toThrowAggregateError(
new AggregateError([new RulesetValidationError('"defined" function does not accept any options', [])]),
new AggregateError([
new RulesetValidationError('invalid-function-options', '"defined" function does not accept any options', []),
]),
);
});
});
Expand Down
11 changes: 10 additions & 1 deletion packages/functions/src/__tests__/enumeration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ describe('Core Functions / Enumeration', () => {
values: ['foo', 2],
foo: true,
},
[new RulesetValidationError('"enumeration" function does not support "foo" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"enumeration" function does not support "foo" option',
[],
),
],
],
[
{
values: [{}],
},
[
new RulesetValidationError(
'invalid-function-options',
'"enumeration" and its "values" option support only arrays of primitive values, i.e. ["Berlin", "London", "Paris"]',
[],
),
Expand All @@ -57,6 +64,7 @@ describe('Core Functions / Enumeration', () => {
null,
[
new RulesetValidationError(
'invalid-function-options',
'"enumeration" function has invalid options specified. Example valid options: { "values": ["Berlin", "London", "Paris"] }, { "values": [2, 3, 5, 8, 13, 21] }',
[],
),
Expand All @@ -66,6 +74,7 @@ describe('Core Functions / Enumeration', () => {
2,
[
new RulesetValidationError(
'invalid-function-options',
'"enumeration" function has invalid options specified. Example valid options: { "values": ["Berlin", "London", "Paris"] }, { "values": [2, 3, 5, 8, 13, 21] }',
[],
),
Expand Down
4 changes: 3 additions & 1 deletion packages/functions/src/__tests__/falsy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('Core Functions / Falsy', () => {
describe('validation', () => {
it.each([{}, 2])('given invalid %p options, should throw', async opts => {
await expect(runFalsy([], opts)).rejects.toThrowAggregateError(
new AggregateError([new RulesetValidationError('"falsy" function does not accept any options', [])]),
new AggregateError([
new RulesetValidationError('invalid-function-options', '"falsy" function does not accept any options', []),
]),
);
});
});
Expand Down
8 changes: 7 additions & 1 deletion packages/functions/src/__tests__/length.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('Core Functions / Length', () => {
null,
[
new RulesetValidationError(
'invalid-function-options',
'"length" function has invalid options specified. Example valid options: { "min": 2 }, { "max": 5 }, { "min": 0, "max": 10 }',
[],
),
Expand All @@ -68,6 +69,7 @@ describe('Core Functions / Length', () => {
2,
[
new RulesetValidationError(
'invalid-function-options',
'"length" function has invalid options specified. Example valid options: { "min": 2 }, { "max": 5 }, { "min": 0, "max": 10 }',
[],
),
Expand All @@ -78,12 +80,13 @@ describe('Core Functions / Length', () => {
min: 2,
foo: true,
},
[new RulesetValidationError('"length" function does not support "foo" option', [])],
[new RulesetValidationError('invalid-function-options', '"length" function does not support "foo" option', [])],
],
[
{ min: '2' },
[
new RulesetValidationError(
'invalid-function-options',
'"length" function and its "min" option accepts only the following types: number',
[],
),
Expand All @@ -94,6 +97,7 @@ describe('Core Functions / Length', () => {
{ max: '2' },
[
new RulesetValidationError(
'invalid-function-options',
`"length" function and its "max" option accepts only the following types: number`,
[],
),
Expand All @@ -103,10 +107,12 @@ describe('Core Functions / Length', () => {
{ min: '4', max: '2' },
[
new RulesetValidationError(
'invalid-function-options',
`"length" function and its "min" option accepts only the following types: number`,
[],
),
new RulesetValidationError(
'invalid-function-options',
`"length" function and its "max" option accepts only the following types: number`,
[],
),
Expand Down
29 changes: 26 additions & 3 deletions packages/functions/src/__tests__/pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Core Functions / Pattern', () => {
null,
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function has invalid options specified. Example valid options: { "match": "^Stoplight" }, { "notMatch": "Swagger" }, { "match": "Stoplight", "notMatch": "Swagger" }',
[],
),
Expand All @@ -66,20 +67,37 @@ describe('Core Functions / Pattern', () => {
{},
[
new RulesetValidationError(
'invalid-function-options',
`"pattern" function has invalid options specified. Example valid options: { "match": "^Stoplight" }, { "notMatch": "Swagger" }, { "match": "Stoplight", "notMatch": "Swagger" }`,
[],
),
],
],
[{ foo: true }, [new RulesetValidationError('"pattern" function does not support "foo" option', [])]],
[
{ foo: true },
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function does not support "foo" option',
[],
),
],
],
[
{ match: 2 },
[new RulesetValidationError('"pattern" function and its "match" option must be string or RegExp instance', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function and its "match" option must be string or RegExp instance',
[],
),
],
],
[
{ notMatch: null },
[
new RulesetValidationError(
'invalid-function-options',
'"pattern" function and its "notMatch" option must be string or RegExp instance',
[],
),
Expand All @@ -88,8 +106,13 @@ describe('Core Functions / Pattern', () => {
[
{ match: 4, notMatch: 10 },
[
new RulesetValidationError(`"pattern" function and its "match" option must be string or RegExp instance`, []),
new RulesetValidationError(
'invalid-function-options',
`"pattern" function and its "match" option must be string or RegExp instance`,
[],
),
new RulesetValidationError(
'invalid-function-options',
`"pattern" function and its "notMatch" option must be string or RegExp instance`,
[],
),
Expand Down
15 changes: 13 additions & 2 deletions packages/functions/src/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,31 @@ describe('Core Functions / Schema', () => {
2,
[
new RulesetValidationError(
'invalid-function-options',
'"schema" function has invalid options specified. Example valid options: { "schema": { /* any JSON Schema can be defined here */ } , { "schema": { "type": "object" }, "dialect": "auto" }',
[],
),
],
],
[
{ schema: { type: 'object' }, foo: true },
[new RulesetValidationError('"schema" function does not support "foo" option', [])],
[new RulesetValidationError('invalid-function-options', '"schema" function does not support "foo" option', [])],
],
[
{ schema: { type: 'object' }, oasVersion: 1 },
[new RulesetValidationError('"schema" function does not support "oasVersion" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"schema" function does not support "oasVersion" option',
[],
),
],
],
[
{ schema: { type: 'object' }, dialect: 'foo' },
[
new RulesetValidationError(
'invalid-function-options',
'"schema" function and its "dialect" option accepts only the following values: "auto", "draft4", "draft6", "draft7", "draft2019-09", "draft2020-12"',
[],
),
Expand All @@ -509,6 +517,7 @@ describe('Core Functions / Schema', () => {
{ schema: { type: 'object' }, allErrors: null },
[
new RulesetValidationError(
'invalid-function-options',
'"schema" function and its "allErrors" option accepts only the following types: boolean',
[],
),
Expand All @@ -518,10 +527,12 @@ describe('Core Functions / Schema', () => {
{ schema: null, allErrors: null },
[
new RulesetValidationError(
'invalid-function-options',
`"schema" function and its "schema" option accepts only the following types: object`,
[],
),
new RulesetValidationError(
'invalid-function-options',
`"schema" function and its "allErrors" option accepts only the following types: boolean`,
[],
),
Expand Down
4 changes: 3 additions & 1 deletion packages/functions/src/__tests__/truthy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('Core Functions / Truthy', () => {
describe('validation', () => {
it.each([{}, 2])('given invalid %p options, should throw', async opts => {
await expect(runTruthy([], opts)).rejects.toThrowAggregateError(
new AggregateError([new RulesetValidationError('"truthy" function does not accept any options', [])]),
new AggregateError([
new RulesetValidationError('invalid-function-options', '"truthy" function does not accept any options', []),
]),
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('Core Functions / UnreferencedReusableObject', () => {
null,
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" function has invalid options specified. Example valid options: { "reusableObjectsLocation": "#/components/schemas" }, { "reusableObjectsLocation": "#/$defs" }',
[],
),
Expand All @@ -41,6 +42,7 @@ describe('Core Functions / UnreferencedReusableObject', () => {
2,
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" function has invalid options specified. Example valid options: { "reusableObjectsLocation": "#/components/schemas" }, { "reusableObjectsLocation": "#/$defs" }',
[],
),
Expand All @@ -50,6 +52,7 @@ describe('Core Functions / UnreferencedReusableObject', () => {
{},
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" function is missing "reusableObjectsLocation" option. Example valid options: { "reusableObjectsLocation": "#/components/schemas" }, { "reusableObjectsLocation": "#/$defs" }',
[],
),
Expand All @@ -60,14 +63,21 @@ describe('Core Functions / UnreferencedReusableObject', () => {
reusableObjectsLocation: '#',
foo: true,
},
[new RulesetValidationError('"unreferencedReusableObject" function does not support "foo" option', [])],
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" function does not support "foo" option',
[],
),
],
],
[
{
reusableObjectsLocation: 2,
},
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" and its "reusableObjectsLocation" option support only valid JSON Pointer fragments, i.e. "#", "#/foo", "#/paths/~1user"',
[],
),
Expand All @@ -79,6 +89,7 @@ describe('Core Functions / UnreferencedReusableObject', () => {
},
[
new RulesetValidationError(
'invalid-function-options',
'"unreferencedReusableObject" and its "reusableObjectsLocation" option support only valid JSON Pointer fragments, i.e. "#", "#/foo", "#/paths/~1user"',
[],
),
Expand Down
Loading

0 comments on commit 2da8258

Please sign in to comment.