diff --git a/HISTORY.md b/HISTORY.md index c60e7eceb..a99b26379 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -17,6 +17,8 @@ https://github.com/josdejong/mathjs - Changed configuration option `decimals` to `precision` (applies to BigNumbers only). - Fixed support for element-wise comparisons between a string and a matrix. +- Fixed: expression parser now trows IndexErrors with one-based indices instead + of zero-based. - Minor bug fixes. diff --git a/test/expression/parse.test.js b/test/expression/parse.test.js index 489c443db..84a8bb5da 100644 --- a/test/expression/parse.test.js +++ b/test/expression/parse.test.js @@ -906,6 +906,18 @@ describe('parse', function() { }); + describe('errors', function () { + + it('should return IndexErrors with one based indices', function () { + // functions throw a zero-based error + assert.throws(function () {math.subset([1,2,3], math.index(4))}, /Index out of range \(4 > 2\)/); + assert.throws(function () {math.subset([1,2,3], math.index(-2))}, /Index out of range \(-2 < 0\)/); + + // evaluation via parser throws one-based error + assert.throws(function () {math.eval('[1,2,3][4]')}, /Index out of range \(4 > 3\)/); + assert.throws(function () {math.eval('[1,2,3][-2]')}, /Index out of range \(-2 < 1\)/); + }) + }); describe('node tree', function () {