glob.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // test src/glob.ts
  2. import { describe, expect, it } from 'vitest';
  3. import { Glob } from '../src/glob';
  4. describe('glob', () => {
  5. it('return original path array if no *', () => {
  6. const obj = { a: { b: { c: 1 } } };
  7. expect(Glob.findMatchPaths(obj, 'a.b.c')).toEqual(['a.b.c']);
  8. });
  9. it('object: when * is in middle of the path', () => {
  10. const obj = {
  11. a: { b: { c: 1 } },
  12. x: { y: { z: 2 } },
  13. };
  14. expect(Glob.findMatchPaths(obj, 'a.*.c')).toEqual(['a.b.c']);
  15. });
  16. it('object:when * is at the end of the path', () => {
  17. const obj = {
  18. a: { b: { c: 1 } },
  19. x: { y: { z: 2 } },
  20. };
  21. expect(Glob.findMatchPaths(obj, 'a.*')).toEqual(['a.b']);
  22. });
  23. it('object:when * is at the start of the path', () => {
  24. const obj = {
  25. a: { b: { c: 1 } },
  26. x: { y: { z: 2 } },
  27. };
  28. expect(Glob.findMatchPaths(obj, '*.y')).toEqual(['x.y']);
  29. });
  30. it('array: when * is at the end of the path', () => {
  31. const obj = {
  32. other: 100,
  33. arr: [
  34. {
  35. x: 1,
  36. y: { a: 1, b: 2 },
  37. },
  38. {
  39. x: 10,
  40. y: {
  41. a: 10,
  42. b: 20,
  43. },
  44. },
  45. ],
  46. };
  47. expect(Glob.findMatchPaths(obj, 'arr.*')).toEqual(['arr.0', 'arr.1']);
  48. });
  49. it('array: when * is at the start of the path', () => {
  50. const arr = [
  51. {
  52. x: 1,
  53. y: { a: 1, b: 2 },
  54. },
  55. {
  56. x: 10,
  57. y: {
  58. a: 10,
  59. b: 20,
  60. },
  61. },
  62. ];
  63. expect(Glob.findMatchPaths(arr, '*')).toEqual(['0', '1']);
  64. });
  65. it('array: when * is in the middle of the path', () => {
  66. const obj = {
  67. other: 100,
  68. arr: [
  69. {
  70. x: 1,
  71. y: { a: 1, b: 2 },
  72. },
  73. {
  74. x: 10,
  75. y: {
  76. a: 10,
  77. b: 20,
  78. },
  79. },
  80. ],
  81. };
  82. expect(Glob.findMatchPaths(obj, 'arr.*.y')).toEqual(['arr.0.y', 'arr.1.y']);
  83. });
  84. });