path_parser_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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('get from Array', function () {
  79. var path = ['a','b','0']
  80. var obj = {
  81. a: {
  82. b: [123]
  83. }
  84. }
  85. expect(Path.getFromArray(obj, path)).toBe(123)
  86. expect(Path.getFromArray(obj, ['a','c','d'])).toBeUndefined()
  87. })
  88. it('get from observer delimited path', function () {
  89. var delim = Observer.pathDelimiter
  90. var path = ['a','b','0'].join(delim)
  91. var obj = {
  92. a: {
  93. b: [123]
  94. }
  95. }
  96. expect(Path.getFromObserver(obj, path)).toBe(123)
  97. expect(Path.getFromObserver(obj, ['a','c'].join(delim))).toBeUndefined()
  98. })
  99. it('set success', function () {
  100. var path = 'a.b.c'
  101. var obj = {
  102. a: {
  103. b: {
  104. c: null
  105. }
  106. }
  107. }
  108. var res = Path.set(obj, path, 12345)
  109. expect(res).toBe(true)
  110. expect(obj.a.b.c).toBe(12345)
  111. })
  112. it('set fail', function () {
  113. var obj = {
  114. a: null
  115. }
  116. var res = Path.set(obj, 'a.b.c', 12345)
  117. expect(res).toBe(false)
  118. res = Path.set(obj, 'a.b', 12345)
  119. expect(res).toBe(false)
  120. })
  121. it('set invalid', function () {
  122. var res = Path.set({}, 'ab[c]d', 123)
  123. expect(res).toBe(false)
  124. })
  125. })