Skip to content

Commit

Permalink
chore: use it.each for various other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 21, 2024
1 parent 887df73 commit d8c92b9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
44 changes: 20 additions & 24 deletions src/utils/__tests__/escapeName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@ import { describe, expect, it } from 'vitest';

import { escapeName, unescapeName } from '../escapeName';

const data = [
['', "''"],
['fooBar', 'fooBar'],
['Foo Bar', `'Foo Bar'`],
['foo bar', `'foo bar'`],
['foo-bar', `'foo-bar'`],
['foo.bar', `'foo.bar'`],
['foo_bar', 'foo_bar'],
['123foo.bar', `'123foo.bar'`],
['@foo.bar', `'@foo.bar'`],
['$foo.bar', `'$foo.bar'`],
['_foo.bar', `'_foo.bar'`],
['123foobar', `'123foobar'`],
['@foobar', `'@foobar'`],
['$foobar', '$foobar'],
['_foobar', '_foobar'],
const toCheck: { unescaped: string; escaped: string }[] = [
{ unescaped: '', escaped: "''" },
{ unescaped: 'fooBar', escaped: 'fooBar' },
{ unescaped: 'Foo Bar', escaped: `'Foo Bar'` },
{ unescaped: 'foo bar', escaped: `'foo bar'` },
{ unescaped: 'foo-bar', escaped: `'foo-bar'` },
{ unescaped: 'foo.bar', escaped: `'foo.bar'` },
{ unescaped: 'foo_bar', escaped: 'foo_bar' },
{ unescaped: '123foo.bar', escaped: `'123foo.bar'` },
{ unescaped: '@foo.bar', escaped: `'@foo.bar'` },
{ unescaped: '$foo.bar', escaped: `'$foo.bar'` },
{ unescaped: '_foo.bar', escaped: `'_foo.bar'` },
{ unescaped: '123foobar', escaped: `'123foobar'` },
{ unescaped: '@foobar', escaped: `'@foobar'` },
{ unescaped: '$foobar', escaped: '$foobar' },
{ unescaped: '_foobar', escaped: '_foobar' },
];

describe('escapeName', () => {
it('should escape', () => {
data.forEach(([unescaped, escaped]) => {
expect(escapeName(unescaped)).toBe(escaped);
});
it.each(toCheck)('should escape $unescaped to $escaped', ({ unescaped, escaped }) => {
expect(escapeName(unescaped)).toBe(escaped);
});
});

describe('unescapeName', () => {
it('should unescape', () => {
data.forEach(([unescaped, escaped]) => {
expect(unescapeName(escaped)).toBe(unescaped);
});
it.each(toCheck)('should unescape $escaped to $unescaped', ({ unescaped, escaped }) => {
expect(unescapeName(escaped)).toBe(unescaped);
});
});
22 changes: 12 additions & 10 deletions src/utils/__tests__/getPattern.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { describe, expect, it } from 'vitest';
import { getPattern } from '../getPattern';

describe('getPattern', () => {
it('should produce correct result', () => {
expect(getPattern()).toEqual(undefined);
expect(getPattern('')).toEqual('');
expect(getPattern('^[a-zA-Z]')).toEqual('^[a-zA-Z]');
expect(getPattern('^\\w+$')).toEqual('^\\\\w+$');
expect(getPattern('^\\d{3}-\\d{2}-\\d{4}$')).toEqual('^\\\\d{3}-\\\\d{2}-\\\\d{4}$');
expect(getPattern('\\')).toEqual('\\\\');
expect(getPattern('\\/')).toEqual('\\\\/');
expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/');
expect(getPattern("'")).toEqual("\\'");
it.each([
{ pattern: undefined, expected: undefined },
{ pattern: '', expected: '' },
{ pattern: '^[a-zA-Z]', expected: '^[a-zA-Z]' },
{ pattern: '^\\w+$', expected: '^\\\\w+$' },
{ pattern: '^\\d{3}-\\d{2}-\\d{4}$', expected: '^\\\\d{3}-\\\\d{2}-\\\\d{4}$' },
{ pattern: '\\', expected: '\\\\' },
{ pattern: '\\/', expected: '\\\\/' },
{ pattern: '\\/\\/', expected: '\\\\/\\\\/' },
{ pattern: "'", expected: "\\'" },
])('getPattern($pattern) -> $expected', ({ pattern, expected }) => {
expect(getPattern(pattern)).toEqual(expected);
});
});
46 changes: 24 additions & 22 deletions src/utils/__tests__/type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { describe, expect, it } from 'vitest';
import { getMappedType, getType } from '../type';

describe('getMappedType', () => {
it('should map types to the basics', () => {
expect(getMappedType('')).toEqual(undefined);
expect(getMappedType('any')).toEqual('unknown');
expect(getMappedType('array')).toEqual('unknown[]');
expect(getMappedType('boolean')).toEqual('boolean');
expect(getMappedType('byte')).toEqual('number');
expect(getMappedType('char')).toEqual('string');
expect(getMappedType('date-time')).toEqual('string');
expect(getMappedType('date')).toEqual('string');
expect(getMappedType('double')).toEqual('number');
expect(getMappedType('file')).toEqual('binary');
expect(getMappedType('float')).toEqual('number');
expect(getMappedType('int')).toEqual('number');
expect(getMappedType('integer')).toEqual('number');
expect(getMappedType('long')).toEqual('number');
expect(getMappedType('null')).toEqual('null');
expect(getMappedType('number')).toEqual('number');
expect(getMappedType('object')).toEqual('unknown');
expect(getMappedType('password')).toEqual('string');
expect(getMappedType('short')).toEqual('number');
expect(getMappedType('string')).toEqual('string');
expect(getMappedType('void')).toEqual('void');
it.each([
{ type: '', expected: undefined },
{ type: 'any', expected: 'unknown' },
{ type: 'array', expected: 'unknown[]' },
{ type: 'boolean', expected: 'boolean' },
{ type: 'byte', expected: 'number' },
{ type: 'char', expected: 'string' },
{ type: 'date-time', expected: 'string' },
{ type: 'date', expected: 'string' },
{ type: 'double', expected: 'number' },
{ type: 'file', expected: 'binary' },
{ type: 'float', expected: 'number' },
{ type: 'int', expected: 'number' },
{ type: 'integer', expected: 'number' },
{ type: 'long', expected: 'number' },
{ type: 'null', expected: 'null' },
{ type: 'number', expected: 'number' },
{ type: 'object', expected: 'unknown' },
{ type: 'password', expected: 'string' },
{ type: 'short', expected: 'number' },
{ type: 'string', expected: 'string' },
{ type: 'void', expected: 'void' },
])('should map type $type to $expected', ({ type, expected }) => {
expect(getMappedType(type)).toEqual(expected);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('unique', () => {
{ value: 'a', index: 2, arr: ['a', 'b', 'c'], result: false },
{ value: 'a', index: 1, arr: ['z', 'a', 'b'], result: true },
{ value: 'a', index: 2, arr: ['y', 'z', 'a'], result: true },
])('unique($value, $index, $arr) === $result', ({ value, index, arr, result }) => {
])('unique($value, $index, $arr) -> $result', ({ value, index, arr, result }) => {
expect(unique(value, index, arr)).toEqual(result);
});

Expand Down

0 comments on commit d8c92b9

Please sign in to comment.