summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/read-package-json/node_modules/json-parse-helpfulerror/node_modules/jju/test/test_errors.js
blob: 8b2cdb7dcbdf67c1bf0a4650bbc00ac62ff04d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var assert = require('assert')
var parse = require('../').parse

function addTest(arg, row, col, errRegExp) {
  var fn = function() {
    try {
      parse(arg)
    } catch(err) {
      if (row !== undefined) assert.equal(err.row, row, 'wrong row: ' + err.row)
      if (col !== undefined) assert.equal(err.column, col, 'wrong column: ' + err.column)
      if (errRegExp) assert(errRegExp.exec(err.message))
      return
    }
    throw Error("no error")
  }

  if (typeof(describe) === 'function') {
    it('test_errors: ' + JSON.stringify(arg), fn)
  } else {
    fn()
  }
}

// semicolon will be unexpected, so it indicates an error position
addTest(';', 1, 1)
addTest('\n\n\n;', 4, 1)
addTest('\r\n;', 2, 1)
addTest('\n\r;', 3, 1)
addTest('\n\u2028;', 3, 1)
addTest('\n\u2029;', 3, 1)
addTest('[\n1\n,\n;', 4, 1)
addTest('{\n;', 2, 1)
addTest('{\n1\n:\n;', 4, 1)
addTest('.e3', 1, 3, /"\.e3"/)

// line continuations
addTest('["\\\n",\n;', 3, 1)
addTest('["\\\r\n",\n;', 3, 1)
addTest('["\\\u2028",\n;', 3, 1)
addTest('["\\\u2029",\n;', 3, 1)

// bareword rewind
addTest('nulz', 1, 1)

// no data
addTest('  ', 1, 3, /No data.*whitespace/)
addTest('blah', 1, 1, /Unexpected token 'b'/)
addTest('', 1, 1, /No data.*empty input/)

try {
  parse('{{{{{{{{{')
} catch(err) {
  var x = err.stack.match(/parseObject/g)
  assert(!x || x.length < 2, "shouldn't blow up the stack with internal calls")
}