github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/parsers/path_spec.js (about)

     1  var Path = require('src/parsers/path')
     2  var _ = require('src/util')
     3  
     4  function assertPath (str, expected) {
     5    var path = Path.parsePath(str)
     6    var res = pathMatch(path, expected)
     7    expect(res).toBe(true)
     8    if (!res) {
     9      console.log('Path parse failed: ', str, path)
    10    }
    11  }
    12  
    13  function assertInvalidPath (str) {
    14    var path = Path.parsePath(str)
    15    expect(path).toBeUndefined()
    16  }
    17  
    18  function pathMatch (a, b) {
    19    if (a.length !== b.length) {
    20      return false
    21    }
    22    for (var i = 0; i < a.length; i++) {
    23      if (a[i] !== b[i]) {
    24        return false
    25      }
    26    }
    27    return true
    28  }
    29  
    30  describe('Path Parser', function () {
    31    it('parse simple paths', function () {
    32      assertPath('', [])
    33      assertPath(' ', [])
    34      assertPath('a', ['a'])
    35      assertPath('a.b', ['a', 'b'])
    36      assertPath('a. b', ['a', 'b'])
    37      assertPath('a .b', ['a', 'b'])
    38      assertPath('a . b', ['a', 'b'])
    39      assertPath(' a . b ', ['a', 'b'])
    40      assertPath('a[0]', ['a', '0'])
    41      assertPath('a [0]', ['a', '0'])
    42      assertPath('a[0][1]', ['a', '0', '1'])
    43      assertPath('a [ 0 ] [ 1 ] ', ['a', '0', '1'])
    44      assertPath('[1234567890] ', ['1234567890'])
    45      assertPath(' [1234567890] ', ['1234567890'])
    46      assertPath('opt0', ['opt0'])
    47      assertPath('$foo.$bar._baz', ['$foo', '$bar', '_baz'])
    48      assertPath('foo["baz"]', ['foo', 'baz'])
    49    })
    50  
    51    it('parse dynamic paths', function () {
    52      assertPath('foo["b\\"az"]', ['foo', '*"b\\"az"'])
    53      assertPath("foo['b\\'az']", ['foo', "*'b\\'az'"])
    54      assertPath('a[b][c]', ['a', '*b', '*c'])
    55      assertPath('a[ b ][ c ]', ['a', '*b', '*c'])
    56      assertPath('a[b.c]', ['a', '*b.c'])
    57      assertPath('a[b + "c"]', ['a', '*b + "c"'])
    58      assertPath('a[b[c]]', ['a', '*b[c]'])
    59      assertPath('a["c" + b]', ['a', '*"c" + b'])
    60    })
    61  
    62    it('handle invalid paths', function () {
    63      assertInvalidPath('.')
    64      assertInvalidPath(' . ')
    65      assertInvalidPath('..')
    66      assertInvalidPath('a[4')
    67      assertInvalidPath('a.b.')
    68      assertInvalidPath('a,b')
    69      assertInvalidPath('a["foo]')
    70      assertInvalidPath('[0foo]')
    71      assertInvalidPath('foo-bar')
    72      assertInvalidPath('42')
    73      assertInvalidPath('  42   ')
    74      assertInvalidPath('foo["bar]')
    75      assertInvalidPath("foo['bar]")
    76      assertInvalidPath('a]')
    77    })
    78  
    79    it('caching', function () {
    80      var path1 = Path.parsePath('a.b.c')
    81      var path2 = Path.parsePath('a.b.c')
    82      expect(path1).toBe(path2)
    83    })
    84  
    85    it('get', function () {
    86      var path = 'a[\'b"b"c\'][0]'
    87      var obj = {
    88        a: {
    89          'b"b"c': [12345]
    90        }
    91      }
    92      expect(Path.getPath(obj, path)).toBe(12345)
    93      expect(Path.getPath(obj, 'a.c')).toBeUndefined()
    94    })
    95  
    96    it('get dynamic', function () {
    97      var path = 'a[b]'
    98      var obj = {
    99        a: {
   100          key: 123
   101        },
   102        b: 'key'
   103      }
   104      expect(Path.getPath(obj, path)).toBe(123)
   105    })
   106  
   107    it('set', function () {
   108      var path = 'a.b.c'
   109      var obj = {
   110        a: {
   111          b: {
   112            c: null
   113          }
   114        }
   115      }
   116      var res = Path.setPath(obj, path, 12345)
   117      expect(res).toBe(true)
   118      expect(obj.a.b.c).toBe(12345)
   119    })
   120  
   121    it('set non-existent', function () {
   122      var target = {}
   123      var res = Path.setPath(target, 'a.b.c', 123)
   124      expect(res).toBe(true)
   125      expect(target.a.b.c).toBe(123)
   126    })
   127  
   128    it('set dynamic non-existent', function () {
   129      var target = {
   130        key: 'what',
   131        obj: {}
   132      }
   133      var res = Path.setPath(target, 'obj[key]', 123)
   134      expect(res).toBe(true)
   135      expect(target.obj.what).toBe(123)
   136      // sub expressions
   137      res = Path.setPath(target, 'obj["yo" + key]', 234)
   138      expect(res).toBe(true)
   139      expect(target.obj.yowhat).toBe(234)
   140    })
   141  
   142    it('set on prototype chain', function () {
   143      var parent = { a: {} }
   144      var target = Object.create(parent)
   145      var res = Path.setPath(target, 'a.b.c', 123)
   146      expect(res).toBe(true)
   147      expect(_.hasOwn(target, 'a')).toBe(false)
   148      expect(parent.a.b.c).toBe(123)
   149    })
   150  
   151    it('set array', function () {
   152      var target = {
   153        a: []
   154      }
   155      target.a.$set = jasmine.createSpy('Array.$set')
   156      var res = Path.setPath(target, 'a[1]', 123)
   157      expect(res).toBe(true)
   158      expect(target.a.$set).toHaveBeenCalledWith('1', 123)
   159    })
   160  
   161    it('set invalid', function () {
   162      var res = Path.setPath({}, 'ab[c]d', 123)
   163      expect(res).toBe(false)
   164    })
   165  })