path_spec.js 2.7 KB

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