UNPKG

2.38 kBJavaScriptView Raw
1var assert = require('assert'),
2 approx = require('../tools/approx'),
3 math = require('../index');
4
5describe('factory', function() {
6
7 it('should get a default instance of mathjs', function() {
8 assert.strictEqual(typeof math, 'object');
9 assert.deepEqual(math.config(), {
10 matrix: 'Matrix',
11 number: 'number',
12 precision: 64,
13 predictable: false,
14 epsilon: 1e-12,
15 randomSeed: null
16 });
17 });
18
19 it('should create an instance of math.js with custom configuration', function() {
20 var math1 = math.create({
21 matrix: 'Array',
22 number: 'BigNumber'
23 });
24
25 assert.strictEqual(typeof math1, 'object');
26 assert.deepEqual(math1.config(), {
27 matrix: 'Array',
28 number: 'BigNumber',
29 precision: 64,
30 predictable: false,
31 epsilon: 1e-12,
32 randomSeed: null
33 });
34 });
35
36 it('two instances of math.js should be isolated from each other', function() {
37 var math1 = math.create();
38 var math2 = math.create({
39 matrix: 'Array'
40 });
41
42 assert.notStrictEqual(math, math1);
43 assert.notStrictEqual(math, math2);
44 assert.notStrictEqual(math1, math2);
45 assert.notDeepEqual(math1.config(), math2.config());
46 assert.notDeepEqual(math.config(), math2.config());
47
48 // changing config should not affect the other
49 math1.config({number: 'BigNumber'});
50 assert.strictEqual(math.config().number, 'number');
51 assert.strictEqual(math1.config().number, 'BigNumber');
52 assert.strictEqual(math2.config().number, 'number');
53 });
54
55 it('should apply configuration using the config function', function() {
56 var math1 = math.create();
57
58 var config = math1.config();
59 assert.deepEqual(config, {
60 matrix: 'Matrix',
61 number: 'number',
62 precision: 64,
63 predictable: false,
64 epsilon: 1e-12,
65 randomSeed: null
66 });
67
68 // restore the original config
69 math1.config(config);
70 });
71
72 // TODO: test whether the namespace is correct: has functions like sin, constants like pi, objects like type and error.
73
74 it('should throw an error when ES5 is not supported', function() {
75 var create = Object.create;
76 Object.create = undefined; // fake missing Object.create function
77
78 assert.throws(function () {
79 var math1 = math.create();
80 }, /ES5 not supported/);
81
82 // restore Object.create
83 Object.create = create;
84 });
85
86});