| 1 | 'use strict'; |
| 2 | |
| 3 | var assert = require('chai').assert; |
| 4 | var ObjectPath = require('../index.js'); |
| 5 | |
| 6 | describe('Parse', function(){ |
| 7 | it('parses simple paths in dot notation', function(){ |
| 8 | assert.deepEqual(ObjectPath.parse('a'), ['a'], 'incorrectly parsed single node'); |
| 9 | assert.deepEqual(ObjectPath.parse('a.b.c'), ['a','b','c'], 'incorrectly parsed multi-node'); |
| 10 | }); |
| 11 | |
| 12 | it('parses simple paths in bracket notation', function(){ |
| 13 | assert.deepEqual(ObjectPath.parse('["c"]'), ['c'], 'incorrectly parsed single headless node'); |
| 14 | assert.deepEqual(ObjectPath.parse('a["b"]["c"]'), ['a','b','c'], 'incorrectly parsed multi-node'); |
| 15 | assert.deepEqual(ObjectPath.parse('["a"]["b"]["c"]'), ['a','b','c'], 'incorrectly parsed headless multi-node'); |
| 16 | }); |
| 17 | |
| 18 | it('parses a numberic nodes in bracket notation', function(){ |
| 19 | assert.deepEqual(ObjectPath.parse('[5]'), ['5'], 'incorrectly parsed single headless numeric node'); |
| 20 | assert.deepEqual(ObjectPath.parse('[5]["a"][3]'), ['5','a','3'], 'incorrectly parsed headless numeric multi-node'); |
| 21 | }); |
| 22 | |
| 23 | it('parses a combination of dot and bracket notation', function(){ |
| 24 | assert.deepEqual(ObjectPath.parse('a[1].b.c.d["e"]["f"].g'), ['a','1','b','c','d','e','f','g']); |
| 25 | }); |
| 26 | |
| 27 | it('parses utf8 characters', function(){ |
| 28 | assert.deepEqual(ObjectPath.parse('∑´ƒ©∫∆.ø'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed utf8 characters from dot notation'); |
| 29 | assert.deepEqual(ObjectPath.parse('["∑´ƒ©∫∆"]["ø"]'), ['∑´ƒ©∫∆','ø'], 'incorrectly parsed utf8 characters from bracket notation'); |
| 30 | }); |
| 31 | |
| 32 | it('parses nodes with control characters', function(){ |
| 33 | assert.deepEqual(ObjectPath.parse('["a.b."]'), ['a.b.'], 'incorrectly parsed dots from inside brackets'); |
| 34 | assert.deepEqual(ObjectPath.parse('["\""][\'\\\'\']'), ['"','\\\''], 'incorrectly parsed escaped quotes'); |
| 35 | assert.deepEqual(ObjectPath.parse('["\'"][\'"\']'), ['\'','"'], 'incorrectly parsed unescaped quotes'); |
| 36 | assert.deepEqual(ObjectPath.parse('["\\""][\'\\\'\']'), ['\\"','\\\''], 'incorrectly parsed escape character'); |
| 37 | assert.deepEqual(ObjectPath.parse('["[\'a\']"][\'[\\"a\\"]\']'), ['[\'a\']','[\\"a\\"]'], 'incorrectly parsed escape character'); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe('Stringify', function(){ |
| 42 | it('stringifys simple paths with single quotes', function(){ |
| 43 | assert.deepEqual(ObjectPath.stringify(['a']), '[\'a\']', 'incorrectly stringified single node'); |
| 44 | assert.deepEqual(ObjectPath.stringify(['a','b','c']), '[\'a\'][\'b\'][\'c\']', 'incorrectly stringified multi-node'); |
| 45 | assert.deepEqual(ObjectPath.stringify(['a'], '\''), '[\'a\']', 'incorrectly stringified single node with excplicit single quote'); |
| 46 | assert.deepEqual(ObjectPath.stringify(['a','b','c'], '\''), '[\'a\'][\'b\'][\'c\']', 'incorrectly stringified multi-node with excplicit single quote'); |
| 47 | }); |
| 48 | it('stringifys simple paths with double quotes', function(){ |
| 49 | assert.deepEqual(ObjectPath.stringify(['a'],'"'), '["a"]', 'incorrectly stringified single node'); |
| 50 | assert.deepEqual(ObjectPath.stringify(['a','b','c'],'"'), '["a"]["b"]["c"]', 'incorrectly stringified multi-node'); |
| 51 | }); |
| 52 | |
| 53 | it('stringifys a numberic nodes in bracket notation with single quotes', function(){ |
| 54 | assert.deepEqual(ObjectPath.stringify(['5']), '[\'5\']', 'incorrectly stringified single headless numeric node'); |
| 55 | assert.deepEqual(ObjectPath.stringify(['5','a','3']), '[\'5\'][\'a\'][\'3\']', 'incorrectly stringified headless numeric multi-node'); |
| 56 | }); |
| 57 | |
| 58 | it('stringifys a numberic nodes in bracket notation with double quotes', function(){ |
| 59 | assert.deepEqual(ObjectPath.stringify(['5'],'"'), '["5"]', 'incorrectly stringified single headless numeric node'); |
| 60 | assert.deepEqual(ObjectPath.stringify(['5','a','3'],'"'), '["5"]["a"]["3"]', 'incorrectly stringified headless numeric multi-node'); |
| 61 | }); |
| 62 | |
| 63 | it('stringifys a combination of dot and bracket notation with single quotes', function(){ |
| 64 | assert.deepEqual(ObjectPath.stringify(['a','1','b','c','d','e','f','g']), '[\'a\'][\'1\'][\'b\'][\'c\'][\'d\'][\'e\'][\'f\'][\'g\']'); |
| 65 | }); |
| 66 | |
| 67 | it('stringifys a combination of dot and bracket notation with double quotes', function(){ |
| 68 | assert.deepEqual(ObjectPath.stringify(['a','1','b','c','d','e','f','g'],'"'), '["a"]["1"]["b"]["c"]["d"]["e"]["f"]["g"]'); |
| 69 | }); |
| 70 | |
| 71 | it('stringifys utf8 characters with single quotes', function(){ |
| 72 | assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆']), '[\'∑´ƒ©∫∆\']', 'incorrectly stringified single node path with utf8'); |
| 73 | assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆','ø']), '[\'∑´ƒ©∫∆\'][\'ø\']', 'incorrectly stringified multi-node path with utf8 characters'); |
| 74 | }); |
| 75 | |
| 76 | it('stringifys utf8 characters with double quotes', function(){ |
| 77 | assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆'],'"'), '["∑´ƒ©∫∆"]', 'incorrectly stringified single node path with utf8'); |
| 78 | assert.deepEqual(ObjectPath.stringify(['∑´ƒ©∫∆','ø'],'"'), '["∑´ƒ©∫∆"]["ø"]', 'incorrectly stringified multi-node path with utf8 characters'); |
| 79 | }); |
| 80 | |
| 81 | it("stringifys nodes with control characters and single quotes", function(){ |
| 82 | assert.deepEqual(ObjectPath.stringify(["a.b."],"'"), "['a.b.']", "incorrectly stringified dots from inside brackets"); |
| 83 | assert.deepEqual(ObjectPath.stringify(["'","\\\""],"'"), "['\\\'']['\\\"']", "incorrectly stringified escaped quotes"); |
| 84 | assert.deepEqual(ObjectPath.stringify(["\"","'"],"'"), "['\"']['\\'']", "incorrectly stringified unescaped quotes"); |
| 85 | assert.deepEqual(ObjectPath.stringify(["\\'","\\\""],"'"), "['\\\\'']['\\\"']", "incorrectly stringified escape character"); |
| 86 | assert.deepEqual(ObjectPath.stringify(["[\"a\"]","[\\'a\\']"],"'"), "['[\"a\"]']['[\\\\'a\\\\']']", "incorrectly stringified escape character"); |
| 87 | }); |
| 88 | |
| 89 | it('stringifys nodes with control characters and double quotes', function(){ |
| 90 | assert.deepEqual(ObjectPath.stringify(['a.b.'],'"'), '["a.b."]', 'incorrectly stringified dots from inside brackets'); |
| 91 | assert.deepEqual(ObjectPath.stringify(['"','\\\''],'"'), '["\\\""]["\\\'"]', 'incorrectly stringified escaped quotes'); |
| 92 | assert.deepEqual(ObjectPath.stringify(['\'','"'],'"'), '["\'"]["\\""]', 'incorrectly stringified unescaped quotes'); |
| 93 | assert.deepEqual(ObjectPath.stringify(['\\"','\\\''],'"'), '["\\\\""]["\\\'"]', 'incorrectly stringified escape character'); |
| 94 | assert.deepEqual(ObjectPath.stringify(['[\'a\']','[\\"a\\"]'],'"'), '["[\'a\']"]["[\\\\"a\\\\"]"]', 'incorrectly stringified escape character'); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | describe('Normalize', function(){ |
| 99 | it('normalizes a string', function(){ |
| 100 | assert.deepEqual(ObjectPath.normalize('a.b["c"]'), '[\'a\'][\'b\'][\'c\']', 'incorrectly normalized a string with single quotes'); |
| 101 | assert.deepEqual(ObjectPath.normalize('a.b["c"]','"'), '["a"]["b"]["c"]', 'incorrectly normalized a string with double quotes'); |
| 102 | }); |
| 103 | |
| 104 | it('normalizes an array', function(){ |
| 105 | assert.deepEqual(ObjectPath.normalize(['a','b','c']), '[\'a\'][\'b\'][\'c\']', 'incorrectly normalized an array with single quotes'); |
| 106 | assert.deepEqual(ObjectPath.normalize(['a','b','c'],'"'), '["a"]["b"]["c"]', 'incorrectly normalized an array with double quotes'); |
| 107 | }); |
| 108 | }); |