import { describe, it, expect } from 'vitest'; import { stringUtils } from '@/utils/string'; describe('stringUtils', () => { describe('truncate', () => { it('应该截断长字符串并添加省略号', () => { const longString = 'This is a very long string that should be truncated'; const result = stringUtils.truncate(longString, 20); expect(result).toBe('This is a very lo...'); }); it('应该使用自定义后缀', () => { const longString = 'This is a very long string that should be truncated'; const result = stringUtils.truncate(longString, 21, ' [more]'); expect(result).toBe('This is a very [more]'); }); it('不应该截断短字符串', () => { const shortString = 'Short string'; const result = stringUtils.truncate(shortString, 20); expect(result).toBe(shortString); }); it('应该处理长度等于最大长度的字符串', () => { const exactString = 'Exact length'; const result = stringUtils.truncate(exactString, 12); expect(result).toBe(exactString); }); }); describe('capitalize', () => { it('应该将字符串首字母大写', () => { expect(stringUtils.capitalize('hello')).toBe('Hello'); expect(stringUtils.capitalize('world')).toBe('World'); }); it('应该处理空字符串', () => { expect(stringUtils.capitalize('')).toBe(''); }); it('应该保持已大写的首字母', () => { expect(stringUtils.capitalize('Hello')).toBe('Hello'); }); it('应该只改变首字母,保持其余部分不变', () => { expect(stringUtils.capitalize('hELLO')).toBe('HELLO'); }); }); describe('toCamelCase', () => { it('应该将短横线分隔的字符串转换为驼峰命名', () => { expect(stringUtils.toCamelCase('hello-world')).toBe('helloWorld'); expect(stringUtils.toCamelCase('my-variable-name')).toBe('myVariableName'); }); it('应该将下划线分隔的字符串转换为驼峰命名', () => { expect(stringUtils.toCamelCase('hello_world')).toBe('helloWorld'); expect(stringUtils.toCamelCase('my_variable_name')).toBe('myVariableName'); }); it('应该将空格分隔的字符串转换为驼峰命名', () => { expect(stringUtils.toCamelCase('hello world')).toBe('helloWorld'); expect(stringUtils.toCamelCase('my variable name')).toBe('myVariableName'); }); it('应该处理混合分隔符', () => { expect(stringUtils.toCamelCase('hello-world_test variable')).toBe('helloWorldTestVariable'); }); it('应该处理空字符串', () => { expect(stringUtils.toCamelCase('')).toBe(''); }); it('应该处理已经驼峰命名的字符串', () => { expect(stringUtils.toCamelCase('helloWorld')).toBe('helloWorld'); }); it('应该将首字母小写', () => { expect(stringUtils.toCamelCase('Hello-World')).toBe('helloWorld'); expect(stringUtils.toCamelCase('HELLO_WORLD')).toBe('helloWorld'); }); }); describe('toKebabCase', () => { it('应该将驼峰命名转换为短横线分隔', () => { expect(stringUtils.toKebabCase('helloWorld')).toBe('hello-world'); expect(stringUtils.toKebabCase('myVariableName')).toBe('my-variable-name'); }); it('应该处理已短横线分隔的字符串', () => { expect(stringUtils.toKebabCase('hello-world')).toBe('hello-world'); }); it('应该处理下划线分隔的字符串', () => { expect(stringUtils.toKebabCase('hello_world')).toBe('hello-world'); }); it('应该处理空字符串', () => { expect(stringUtils.toKebabCase('')).toBe(''); }); }); describe('toSnakeCase', () => { it('应该将驼峰命名转换为下划线分隔', () => { expect(stringUtils.toSnakeCase('helloWorld')).toBe('hello_world'); expect(stringUtils.toSnakeCase('myVariableName')).toBe('my_variable_name'); }); it('应该处理已下划线分隔的字符串', () => { expect(stringUtils.toSnakeCase('hello_world')).toBe('hello_world'); }); it('应该处理短横线分隔的字符串', () => { expect(stringUtils.toSnakeCase('hello-world')).toBe('hello_world'); }); it('应该处理空字符串', () => { expect(stringUtils.toSnakeCase('')).toBe(''); }); }); describe('escapeHtml', () => { it('应该转义HTML特殊字符', () => { expect(stringUtils.escapeHtml('
hello & world
')).toBe('<div>hello & world</div>'); expect(stringUtils.escapeHtml('a "quote" and \'apostrophe\'')).toBe('a "quote" and 'apostrophe''); }); it('应该处理不包含特殊字符的字符串', () => { expect(stringUtils.escapeHtml('plain text')).toBe('plain text'); }); it('应该处理空字符串', () => { expect(stringUtils.escapeHtml('')).toBe(''); }); it('应该只转义已知的特殊字符', () => { expect(stringUtils.escapeHtml('hello @ world #test')).toBe('hello @ world #test'); }); }); describe('randomString', () => { it('应该生成指定长度的随机字符串', () => { const length = 10; const result = stringUtils.randomString(length); expect(result).toHaveLength(length); }); it('应该生成不同的随机字符串', () => { const result1 = stringUtils.randomString(10); const result2 = stringUtils.randomString(10); expect(result1).not.toBe(result2); }); it('应该使用默认长度', () => { const result = stringUtils.randomString(); expect(result).toHaveLength(10); }); it('应该只包含字母和数字', () => { const result = stringUtils.randomString(100); const regex = /^[A-Za-z0-9]+$/; expect(regex.test(result)).toBe(true); }); }); describe('isEmpty', () => { it('应该识别空字符串', () => { expect(stringUtils.isEmpty('')).toBe(true); expect(stringUtils.isEmpty(' ')).toBe(true); expect(stringUtils.isEmpty('\t\n')).toBe(true); }); it('应该识别非空字符串', () => { expect(stringUtils.isEmpty('hello')).toBe(false); expect(stringUtils.isEmpty(' hello ')).toBe(false); }); it('应该处理null和undefined', () => { expect(stringUtils.isEmpty(null)).toBe(true); expect(stringUtils.isEmpty(undefined)).toBe(true); }); }); });