forked from mtusi2026/mtusi2026lab4
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { validate } from 'class-validator';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { CodePointLength } from './code-point-length.js';
|
|
|
|
class Sample {
|
|
@CodePointLength(3, 5)
|
|
value!: string;
|
|
}
|
|
|
|
async function fails(value: unknown): Promise<boolean> {
|
|
const sample = new Sample();
|
|
(sample as { value: unknown }).value = value;
|
|
const errors = await validate(sample);
|
|
return errors.length > 0;
|
|
}
|
|
|
|
describe('CodePointLength', () => {
|
|
it('counts Unicode code points instead of UTF-16 units', async () => {
|
|
// '🔐' is one code point but two UTF-16 units.
|
|
expect(await fails('🔐🔐')).toBe(true);
|
|
expect(await fails('🔐🔐🔐')).toBe(false);
|
|
expect(await fails('🔐🔐🔐🔐🔐')).toBe(false);
|
|
expect(await fails('🔐🔐🔐🔐🔐🔐')).toBe(true);
|
|
// Combining marks are separate code points, not grapheme clusters:
|
|
// three 'é' sequences are six code points and exceed the maximum.
|
|
expect(await fails('e\u0301e\u0301')).toBe(false);
|
|
expect(await fails('e\u0301e\u0301e\u0301')).toBe(true);
|
|
});
|
|
|
|
it('applies to BMP text boundaries', async () => {
|
|
expect(await fails('ab')).toBe(true);
|
|
expect(await fails('abc')).toBe(false);
|
|
expect(await fails('abcde')).toBe(false);
|
|
expect(await fails('abcdef')).toBe(true);
|
|
});
|
|
|
|
it('rejects empty and non-string values', async () => {
|
|
expect(await fails('')).toBe(true);
|
|
expect(await fails(undefined)).toBe(true);
|
|
expect(await fails(12345)).toBe(true);
|
|
expect(await fails(['a', 'b', 'c'])).toBe(true);
|
|
});
|
|
});
|