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('