| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /**
- * 身份证解析工具函数单元测试
- */
- import { describe, it, expect } from 'vitest';
- import { parseIdCard, isValidIdCardFormat } from '../../src/utils/idCardParser';
- describe('idCardParser', () => {
- describe('parseIdCard - 18位身份证号', () => {
- it('应该正确解析男性身份证号', () => {
- const result = parseIdCard('110101199001011234');
- expect(result.gender).toBe('男');
- expect(result.birthDate).toBe('1990-01-01');
- expect(result.isValid).toBe(true);
- });
- it('应该正确解析女性身份证号', () => {
- // 17位是2,偶数=女
- const result = parseIdCard('110101199002022244');
- expect(result.gender).toBe('女');
- expect(result.birthDate).toBe('1990-02-02');
- expect(result.isValid).toBe(true);
- });
- it('应该处理带有X校验码的身份证号', () => {
- const result = parseIdCard('11010119900101123X');
- expect(result.isValid).toBe(true);
- expect(result.gender).toBe('男');
- });
- it('应该拒绝无效的出生日期', () => {
- const result = parseIdCard('110101199013011234'); // 月份13无效
- expect(result.birthDate).toBeNull();
- expect(result.isValid).toBe(false);
- });
- it('应该拒绝非数字字符(除X校验码外)', () => {
- const result = parseIdCard('110101A99001011234');
- expect(result.isValid).toBe(false);
- });
- });
- describe('parseIdCard - 15位身份证号', () => {
- it('应该正确解析15位男性身份证号', () => {
- const result = parseIdCard('110101900101123');
- expect(result.gender).toBe('男'); // 第15位是3,奇数
- expect(result.birthDate).toBe('1990-01-01');
- expect(result.isValid).toBe(true);
- });
- it('应该正确解析15位女性身份证号', () => {
- const result = parseIdCard('110101900101224');
- expect(result.gender).toBe('女'); // 第15位是4,偶数
- expect(result.birthDate).toBe('1990-01-01');
- expect(result.isValid).toBe(true);
- });
- });
- describe('parseIdCard - 边界情况', () => {
- it('应该处理空字符串', () => {
- const result = parseIdCard('');
- expect(result.gender).toBeNull();
- expect(result.birthDate).toBeNull();
- expect(result.isValid).toBe(false);
- });
- it('应该处理16位(无效长度)', () => {
- const result = parseIdCard('11010119900101123');
- expect(result.isValid).toBe(false);
- });
- it('应该处理19位(无效长度)', () => {
- const result = parseIdCard('1101011990010112345');
- expect(result.isValid).toBe(false);
- });
- it('应该处理带空格的身份证号', () => {
- const result = parseIdCard('110101 1990 0101 1234');
- expect(result.isValid).toBe(true);
- expect(result.gender).toBe('男');
- });
- it('应该处理小写x校验码', () => {
- const result = parseIdCard('11010119900101123x');
- expect(result.isValid).toBe(true);
- expect(result.gender).toBe('男');
- });
- });
- describe('isValidIdCardFormat', () => {
- it('应该验证有效的18位身份证号', () => {
- expect(isValidIdCardFormat('110101199001011234')).toBe(true);
- });
- it('应该验证有效的15位身份证号', () => {
- expect(isValidIdCardFormat('110101900101123')).toBe(true);
- });
- it('应该拒绝无效长度的身份证号', () => {
- expect(isValidIdCardFormat('11010119900101123')).toBe(false);
- expect(isValidIdCardFormat('1101011990010112345')).toBe(false);
- });
- it('应该拒绝包含非法字符的身份证号', () => {
- expect(isValidIdCardFormat('110101A99001011234')).toBe(false);
- });
- it('应该接受带X校验码的18位身份证号', () => {
- expect(isValidIdCardFormat('11010119900101123X')).toBe(true);
- });
- });
- describe('出生日期边界值测试', () => {
- it('应该拒绝年份小于1900的日期', () => {
- const result = parseIdCard('110101180001011234');
- expect(result.birthDate).toBeNull();
- expect(result.isValid).toBe(false);
- });
- it('应该接受当前年份的日期', () => {
- const currentYear = new Date().getFullYear();
- const idCard = `110101${currentYear}01011234`;
- const result = parseIdCard(idCard);
- expect(result.isValid).toBe(true);
- });
- it('应该拒绝大于当前年份的日期', () => {
- const nextYear = new Date().getFullYear() + 1;
- const idCard = `110101${nextYear}01011234`;
- const result = parseIdCard(idCard);
- expect(result.birthDate).toBeNull();
- expect(result.isValid).toBe(false);
- });
- });
- });
|