path.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { describe, expect, it } from 'vitest';
  2. import { Path } from '../src/core/path';
  3. describe('path', () => {
  4. it('toString', () => {
  5. expect(new Path('a.b.c').toString()).toEqual('a.b.c');
  6. expect(new Path('a.b[0].c').toString()).toEqual('a.b.0.c');
  7. expect(new Path(['a', 'b', 'c']).toString()).toEqual('a.b.c');
  8. });
  9. it('parent', () => {
  10. expect(new Path('a.b.c').parent!.toString()).toEqual('a.b');
  11. expect(new Path('a.b[0]').parent!.toString()).toEqual('a.b');
  12. expect(new Path('a').parent).toEqual(undefined);
  13. });
  14. it('isChild', () => {
  15. expect(new Path('a.b').isChild('a.b.c')).toEqual(true);
  16. expect(new Path('a.b').isChild('a.b[0]')).toEqual(true);
  17. });
  18. it('concat', () => {
  19. expect(new Path('a').concat('b').toString()).toEqual('a.b');
  20. expect(new Path('a').concat('b.c').toString()).toEqual('a.b.c');
  21. expect(new Path('a').concat(0).toString()).toEqual('a.0');
  22. expect(() => {
  23. new Path('a').concat({} as any);
  24. }).toThrowError(/invalid param type/);
  25. });
  26. it('compareArrayPath', () => {
  27. expect((Path.compareArrayPath(new Path('a.b.0'), new Path('a.b.1')) as number) < 0).toBe(true);
  28. expect((Path.compareArrayPath(new Path('a.b.0'), new Path('a.b.2')) as number) < 0).toBe(true);
  29. expect((Path.compareArrayPath(new Path('a.b.1'), new Path('a.b.0')) as number) < 0).toBe(false);
  30. expect((Path.compareArrayPath(new Path('a.b.0'), new Path('a.b.0')) as number) === 0).toBe(
  31. true
  32. );
  33. expect((Path.compareArrayPath(new Path('a.b.1'), new Path('a.b.0.x')) as number) < 0).toBe(
  34. false
  35. );
  36. expect((Path.compareArrayPath(new Path('a.b.1.y'), new Path('a.b.0.x')) as number) < 0).toBe(
  37. false
  38. );
  39. expect(() => Path.compareArrayPath(new Path('a.1'), new Path('a.b.0'))).toThrowError();
  40. expect(() => Path.compareArrayPath(new Path(''), new Path(''))).toThrowError();
  41. expect(() => Path.compareArrayPath(new Path('a.b.c'), new Path('a.b'))).toThrowError();
  42. });
  43. it('isChildOrGrandChild', () => {
  44. expect(new Path('a.b').isChildOrGrandChild('a.b.c')).toEqual(true);
  45. expect(new Path('a.b').isChildOrGrandChild('a.b[0]')).toEqual(true);
  46. expect(new Path('a.b').isChildOrGrandChild('a.b.1')).toEqual(true);
  47. expect(new Path('a.b').isChildOrGrandChild('a.b')).toEqual(false);
  48. expect(new Path('a.b').isChildOrGrandChild('a')).toEqual(false);
  49. expect(new Path('').isChildOrGrandChild('a')).toEqual(true);
  50. });
  51. it('replaceParent', () => {
  52. expect(new Path('a.b.c.d').replaceParent(new Path('a.b'), new Path('x.y')).toString()).toEqual(
  53. 'x.y.c.d'
  54. );
  55. expect(new Path('a.b.0.d').replaceParent(new Path('a.b'), new Path('x.y')).toString()).toEqual(
  56. 'x.y.0.d'
  57. );
  58. expect(new Path('0.d').replaceParent(new Path(''), new Path('x.y')).toString()).toEqual(
  59. 'x.y.0.d'
  60. );
  61. expect(
  62. new Path('a.b.c').replaceParent(new Path('a.b.c'), new Path('x.y.z')).toString()
  63. ).toEqual('x.y.z');
  64. expect(() =>
  65. new Path('a.b.0.d').replaceParent(new Path('a1.b'), new Path('x.y')).toString()
  66. ).toThrowError();
  67. expect(() =>
  68. new Path('a.0.d').replaceParent(new Path('a.0.d.e'), new Path('x.y')).toString()
  69. ).toThrowError();
  70. });
  71. });