path.test.ts 3.2 KB

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