camote-utils
Version:
A comprehensive TypeScript utility library featuring advanced string and number formatting, data structures, and algorithms
116 lines (115 loc) • 5.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
describe('Random Generation Functions', () => {
test('generateRandomInteger should return a number between min and max', () => {
const result = (0, index_1.generateRandomInteger)(1, 10);
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});
test('generateRandomIntegerArray should return an array of specified length', () => {
const result = (0, index_1.generateRandomIntegerArray)(5, 1, 10);
expect(result).toHaveLength(5);
result.forEach(num => {
expect(num).toBeGreaterThanOrEqual(1);
expect(num).toBeLessThanOrEqual(10);
});
});
test('generateRandomString should return a string of specified length', () => {
const result = (0, index_1.generateRandomString)(8);
expect(result).toHaveLength(8);
});
test('generateRandomPassword should return a string of at least 8 characters', () => {
const result = (0, index_1.generateRandomPassword)(10);
expect(result).toHaveLength(10);
});
test('generateRandomHexColor should return a valid hex color', () => {
const result = (0, index_1.generateRandomHexColor)();
expect(result).toMatch(/^#[0-9A-F]{6}$/i);
});
test('generateRandomRGB should return a valid RGB string', () => {
const result = (0, index_1.generateRandomRGB)();
expect(result).toMatch(/^rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)$/);
});
test('generateRandomHSL should return a valid HSL string', () => {
const result = (0, index_1.generateRandomHSL)();
expect(result).toMatch(/^hsl\(\d{1,3}, \d{1,3}%, \d{1,3}%\)$/);
});
test('generateColorPalette should return an array of specified length', () => {
const result = (0, index_1.generateColorPalette)(5);
expect(result).toHaveLength(5);
});
test('generateRandomColor should return a valid color string', () => {
const result = (0, index_1.generateRandomColor)();
expect(result).toMatch(/^(#([0-9A-F]{3}|[0-9A-F]{6})|rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)|hsl\(\d{1,3}, \d{1,3}%, \d{1,3}%\))$/);
});
test('generateUUID should return a valid UUID', () => {
const result = (0, index_1.generateUUID)();
expect(result).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
test('generateUUIDv4 should return a valid UUID v4', () => {
const result = (0, index_1.generateUUIDv4)();
expect(result).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
test('generateUUID should return a valid UUID', () => {
const result = (0, index_1.generateUUID)();
expect(result).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
test('generateUUIDv4 should return a valid UUID v4', () => {
const result = (0, index_1.generateUUIDv4)();
expect(result).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
test('generateRandom should return a number for integer type', () => {
const result = (0, index_1.generateRandom)({ type: 'integer', min: 1, max: 10 });
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});
});
describe('generateRandomIntegerInRange', () => {
test('returns a number within the range', () => {
const result = (0, index_1.generateRandomIntegerInRange)(5, 10);
expect(result).toBeGreaterThanOrEqual(5);
expect(result).toBeLessThanOrEqual(10);
});
test('returns the same number when min and max are equal', () => {
const result = (0, index_1.generateRandomIntegerInRange)(7, 7);
expect(result).toBe(7);
});
test('handles negative ranges', () => {
const result = (0, index_1.generateRandomIntegerInRange)(-10, -5);
expect(result).toBeGreaterThanOrEqual(-10);
expect(result).toBeLessThanOrEqual(-5);
});
test('throws an error if min is greater than max', () => {
expect(() => (0, index_1.generateRandomIntegerInRange)(10, 5)).toThrow('Min cannot be greater than max');
});
test('throws an error if min or max is not a finite number', () => {
expect(() => (0, index_1.generateRandomIntegerInRange)(NaN, 10)).toThrow('Both min and max must be finite numbers');
expect(() => (0, index_1.generateRandomIntegerInRange)(5, Infinity)).toThrow('Both min and max must be finite numbers');
});
describe('generateStrongPassword function', () => {
test('should generate a password of specified length', () => {
const password = (0, index_1.generateStrongPassword)(12);
expect(password).toHaveLength(12);
});
test('should throw an error if length is less than 8', () => {
expect(() => (0, index_1.generateStrongPassword)(7)).toThrow('Password length must be at least 8 characters');
});
test('should include at least one lowercase letter', () => {
const password = (0, index_1.generateStrongPassword)(12);
expect(password).toMatch(/[a-z]/);
});
test('should include at least one uppercase letter', () => {
const password = (0, index_1.generateStrongPassword)(12);
expect(password).toMatch(/[A-Z]/);
});
test('should include at least one number', () => {
const password = (0, index_1.generateStrongPassword)(12);
expect(password).toMatch(/[0-9]/);
});
test('should include at least one special character', () => {
const password = (0, index_1.generateStrongPassword)(12);
expect(password).toMatch(/[!@#$%^&*()_+[\]{}|;:,.<>?]/);
});
});
});