path_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var Path = require('../../../../src/parse/path')
  2. function assertPath (str, expected) {
  3. var path = Path.parse(str)
  4. expect(pathMatch(path, expected)).toBe(true)
  5. }
  6. function assertInvalidPath (str) {
  7. var path = Path.parse(str)
  8. expect(path).toBeUndefined()
  9. }
  10. function pathMatch (a, b) {
  11. if (a.length !== b.length) {
  12. return false
  13. }
  14. for (var i = 0; i < a.length; i++) {
  15. if (a[i] !== b[i]) {
  16. return false
  17. }
  18. }
  19. return true
  20. }
  21. describe('Path Parser', function () {
  22. it('parse', function () {
  23. assertPath('', [])
  24. assertPath(' ', [])
  25. assertPath('a', ['a'])
  26. assertPath('a.b', ['a', 'b'])
  27. assertPath('a. b', ['a', 'b'])
  28. assertPath('a .b', ['a', 'b'])
  29. assertPath('a . b', ['a', 'b'])
  30. assertPath(' a . b ', ['a', 'b'])
  31. assertPath('a[0]', ['a', '0'])
  32. assertPath('a [0]', ['a', '0'])
  33. assertPath('a[0][1]', ['a', '0', '1'])
  34. assertPath('a [ 0 ] [ 1 ] ', ['a', '0', '1'])
  35. assertPath('[1234567890] ', ['1234567890'])
  36. assertPath(' [1234567890] ', ['1234567890'])
  37. assertPath('opt0', ['opt0'])
  38. assertPath('$foo.$bar._baz', ['$foo', '$bar', '_baz'])
  39. assertPath('foo["baz"]', ['foo', 'baz'])
  40. assertPath('foo["b\\"az"]', ['foo', 'b"az'])
  41. assertPath("foo['b\\'az']", ['foo', "b'az"])
  42. })
  43. it('handle invalid paths', function () {
  44. assertInvalidPath('.')
  45. assertInvalidPath(' . ')
  46. assertInvalidPath('..')
  47. assertInvalidPath('a[4')
  48. assertInvalidPath('a.b.')
  49. assertInvalidPath('a,b')
  50. assertInvalidPath('a["foo]')
  51. assertInvalidPath('[0x04]')
  52. assertInvalidPath('[0foo]')
  53. assertInvalidPath('[foo-bar]')
  54. assertInvalidPath('foo-bar')
  55. assertInvalidPath('42')
  56. assertInvalidPath('a[04]')
  57. assertInvalidPath(' a [ 04 ]')
  58. assertInvalidPath(' 42 ')
  59. assertInvalidPath('foo["bar]')
  60. assertInvalidPath("foo['bar]")
  61. })
  62. it('caching', function () {
  63. var path1 = Path.parse('a.b.c')
  64. var path2 = Path.parse('a.b.c')
  65. expect(path1).toBe(path2)
  66. })
  67. it('get', function () {
  68. var path = 'a[\'b"b"c\'][0]'
  69. var obj = {
  70. a: {
  71. 'b"b"c': [12345]
  72. }
  73. }
  74. expect(Path.get(obj, path)).toBe(12345)
  75. expect(Path.get(obj, 'a.c')).toBeUndefined()
  76. })
  77. it('set', function () {
  78. var path = 'a.b.c'
  79. var obj = {
  80. a: {
  81. b: {
  82. c: null
  83. }
  84. }
  85. }
  86. var res = Path.set(obj, path, 12345)
  87. expect(res).toBe(true)
  88. expect(obj.a.b.c).toBe(12345)
  89. })
  90. it('set non-existent', function () {
  91. var target = {}
  92. var res = Path.set(target, 'a.b.c', 123)
  93. expect(res).toBe(true)
  94. expect(target.a.b.c).toBe(123)
  95. })
  96. it('set on prototype chain', function () {
  97. var parent = { a: {} }
  98. var target = Object.create(parent)
  99. var res = Path.set(target, 'a.b.c', 123)
  100. expect(res).toBe(true)
  101. expect(target.hasOwnProperty('a')).toBe(false)
  102. expect(parent.a.b.c).toBe(123)
  103. })
  104. it('set invalid', function () {
  105. var res = Path.set({}, 'ab[c]d', 123)
  106. expect(res).toBe(false)
  107. })
  108. })