letter.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. capitalizeFirstLetter,
  4. toCamelCase,
  5. toLowerCaseFirstLetter,
  6. } from './letter';
  7. // 编写测试用例
  8. describe('capitalizeFirstLetter', () => {
  9. it('should capitalize the first letter of a string', () => {
  10. expect(capitalizeFirstLetter('hello')).toBe('Hello');
  11. expect(capitalizeFirstLetter('world')).toBe('World');
  12. });
  13. it('should handle empty strings', () => {
  14. expect(capitalizeFirstLetter('')).toBe('');
  15. });
  16. it('should handle single character strings', () => {
  17. expect(capitalizeFirstLetter('a')).toBe('A');
  18. expect(capitalizeFirstLetter('b')).toBe('B');
  19. });
  20. it('should not change the case of other characters', () => {
  21. expect(capitalizeFirstLetter('hElLo')).toBe('HElLo');
  22. });
  23. });
  24. describe('toLowerCaseFirstLetter', () => {
  25. it('should convert the first letter to lowercase', () => {
  26. expect(toLowerCaseFirstLetter('CommonAppName')).toBe('commonAppName');
  27. expect(toLowerCaseFirstLetter('AnotherKeyExample')).toBe(
  28. 'anotherKeyExample',
  29. );
  30. });
  31. it('should return the same string if the first letter is already lowercase', () => {
  32. expect(toLowerCaseFirstLetter('alreadyLowerCase')).toBe('alreadyLowerCase');
  33. });
  34. it('should handle empty strings', () => {
  35. expect(toLowerCaseFirstLetter('')).toBe('');
  36. });
  37. it('should handle single character strings', () => {
  38. expect(toLowerCaseFirstLetter('A')).toBe('a');
  39. expect(toLowerCaseFirstLetter('a')).toBe('a');
  40. });
  41. it('should handle strings with only one uppercase letter', () => {
  42. expect(toLowerCaseFirstLetter('A')).toBe('a');
  43. });
  44. it('should handle strings with special characters', () => {
  45. expect(toLowerCaseFirstLetter('!Special')).toBe('!Special');
  46. expect(toLowerCaseFirstLetter('123Number')).toBe('123Number');
  47. });
  48. });
  49. describe('toCamelCase', () => {
  50. it('should return the key if parentKey is empty', () => {
  51. expect(toCamelCase('child', '')).toBe('child');
  52. });
  53. it('should combine parentKey and key in camel case', () => {
  54. expect(toCamelCase('child', 'parent')).toBe('parentChild');
  55. });
  56. it('should handle empty key and parentKey', () => {
  57. expect(toCamelCase('', '')).toBe('');
  58. });
  59. it('should handle key with capital letters', () => {
  60. expect(toCamelCase('Child', 'parent')).toBe('parentChild');
  61. expect(toCamelCase('Child', 'Parent')).toBe('ParentChild');
  62. });
  63. });