mathjs/test/type/number.test.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

83 lines
2.9 KiB
JavaScript

const assert = require('assert')
const math = require('../../src/main')
const approx = require('../../tools/approx')
const number = math.number
describe('number', function () {
it('should be 0 if called with no argument', function () {
approx.equal(number(), 0)
})
it('should convert a boolean to a number', function () {
approx.equal(number(true), 1)
approx.equal(number(false), 0)
})
it('should convert null to a number', function () {
approx.equal(number(null), 0)
})
it('should convert a bignumber to a number', function () {
approx.equal(number(math.bignumber(0.1)), 0.1)
approx.equal(number(math.bignumber('1.3e500')), Infinity)
})
it('should convert a fraction to a number', function () {
approx.equal(number(math.fraction(2, 5)), 0.4)
})
it('should accept a number as argument', function () {
approx.equal(number(3), 3)
approx.equal(number(-3), -3)
})
it('should convert a unit to a number', function () {
approx.equal(number(math.unit('52cm'), 'm'), 0.52)
})
it('should parse the string if called with a valid string', function () {
approx.equal(number('2.1e3'), 2100)
approx.equal(number(' 2.1e-3 '), 0.0021)
approx.equal(number(''), 0)
approx.equal(number(' '), 0)
})
it('should throw an error if called with an invalid string', function () {
assert.throws(function () { number('2.3.4') }, SyntaxError)
assert.throws(function () { number('23a') }, SyntaxError)
})
it('should convert the elements of a matrix to numbers', function () {
assert.deepStrictEqual(number(math.matrix(['123', true])), math.matrix([123, 1]))
})
it('should convert the elements of an array to numbers', function () {
assert.deepStrictEqual(number(['123', true]), [123, 1])
})
it('should throw an error if called with a wrong number of arguments', function () {
assert.throws(function () { number(1, 2, 3) }, /TypeError: Too many arguments/)
})
it('should throw an error if called with a complex number', function () {
assert.throws(function () { number(math.complex(2, 3)) }, TypeError)
})
it('should throw an error with wrong type of arguments', function () {
assert.throws(function () { number(math.unit('5cm')) }, /Second argument with valueless unit expected/)
// assert.throws(function () {number(math.unit('5cm'), 2)}, TypeError); // FIXME: this should also throw an error
assert.throws(function () { number(math.unit('5cm'), new Date()) }, TypeError)
assert.throws(function () { number('23', 2) }, TypeError)
})
it('should LaTeX number', function () {
const expr1 = math.parse('number()')
const expr2 = math.parse('number(1)')
const expr3 = math.parse('number(1,cm)')
assert.strictEqual(expr1.toTex(), '0')
assert.strictEqual(expr2.toTex(), '\\left(1\\right)')
assert.strictEqual(expr3.toTex(), '\\left(\\left(1\\right)\\mathrm{cm}\\right)')
})
})