forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.test.js
More file actions
73 lines (59 loc) · 2.62 KB
/
boolean.test.js
File metadata and controls
73 lines (59 loc) · 2.62 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import assert from 'assert'
import math from '../../../src/defaultInstance.js'
const bool = math.boolean
describe('boolean', function () {
it('should convert a boolean to a boolean', function () {
assert.strictEqual(bool(true), true)
assert.strictEqual(bool(false), false)
})
it('should convert null to a boolean', function () {
assert.strictEqual(bool(null), false)
})
it('should convert a number into a boolean', function () {
assert.strictEqual(bool(-2), true)
assert.strictEqual(bool(-1), true)
assert.strictEqual(bool(0), false)
assert.strictEqual(bool(1), true)
assert.strictEqual(bool(2), true)
})
it('should convert a bignumber into a boolean', function () {
assert.strictEqual(bool(math.bignumber(-2)), true)
assert.strictEqual(bool(math.bignumber(-1)), true)
assert.strictEqual(bool(math.bignumber(0)), false)
assert.strictEqual(bool(math.bignumber(1)), true)
assert.strictEqual(bool(math.bignumber(2)), true)
})
it('should convert the elements of a matrix or array to booleans', function () {
assert.deepStrictEqual(bool(math.matrix([1, 0, 1, 1])), math.matrix([true, false, true, true]))
assert.deepStrictEqual(bool([1, 0, 1, 1]), [true, false, true, true])
})
it('should convert a string into a boolean', function () {
assert.strictEqual(bool('true'), true)
assert.strictEqual(bool('false'), false)
assert.strictEqual(bool('True'), true)
assert.strictEqual(bool('False'), false)
assert.strictEqual(bool('1'), true)
assert.strictEqual(bool('0'), false)
assert.strictEqual(bool(' 0 '), false)
assert.strictEqual(bool('2'), true)
assert.strictEqual(bool(' 4e2 '), true)
assert.strictEqual(bool(' -4e2 '), true)
})
it('should throw an error if the string is not a valid number', function () {
assert.throws(function () { bool('') }, /Error: Cannot convert/)
assert.throws(function () { bool('23a') }, /Error: Cannot convert/)
})
it('should throw an error if there\'s a wrong number of arguments', function () {
assert.throws(function () { bool(1, 2) }, /TypeError: Too many arguments/)
})
it('should throw an error if used with a complex', function () {
assert.throws(function () { bool(math.complex(2, 3)) }, /TypeError: Unexpected type of argument/)
})
it('should throw an error if used with a unit', function () {
assert.throws(function () { bool(math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
})
it('should LaTeX boolean', function () {
const expression = math.parse('boolean(1)')
assert.strictEqual(expression.toTex(), '\\mathrm{boolean}\\left(1\\right)')
})
})