28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { encodeQr } from './qr';
|
|
import { QR_ALL_MASKS, QR_FIXTURES } from './qr.fixtures';
|
|
|
|
const toRows = (grid: boolean[][]) => grid.map((r) => r.map((c) => (c ? '1' : '0')).join(''));
|
|
|
|
describe('encodeQr', () => {
|
|
// 参考矩阵来自 Python qrcode(纠错 M、字节模式)。逐模块一致,说明版本选择、
|
|
// Reed-Solomon、交错、功能图形、格式位与版本位(v7+)都正确
|
|
it.each(Object.entries(QR_FIXTURES))('与参考实现逐模块一致:%s', (_name, fx) => {
|
|
expect(toRows(encodeQr(fx.text, fx.mask))).toEqual(fx.rows);
|
|
});
|
|
|
|
it('自动选掩码:结果必须等于参考实现 8 种掩码之一(验证掩码应用与还原)', () => {
|
|
const got = toRows(encodeQr(QR_ALL_MASKS.text)).join('|');
|
|
expect(QR_ALL_MASKS.rows.map((r) => r.join('|'))).toContain(got);
|
|
});
|
|
|
|
it('自动选择掩码时,尺寸与版本一致', () => {
|
|
const fx = QR_FIXTURES.member;
|
|
expect(encodeQr(fx.text)).toHaveLength(fx.rows.length);
|
|
});
|
|
|
|
it('内容太长时抛错', () => {
|
|
expect(() => encodeQr('x'.repeat(300))).toThrow(RangeError);
|
|
});
|
|
});
|