Skip to content

Commit a2c31c9

Browse files
authored
Adding tests for mathutils (sugarlabs#4130)
* adding test file for unit tests * Update mathutils.test.js * Update mathutils.test.js * Update mathutils.test.js * Update package.json * Update mathutils.js * Update mathutils.test.js
1 parent 38031f8 commit a2c31c9

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// A basic test file in jest framework for the mathutils.js
2+
const MathUtility = require('../mathutils');
3+
4+
// Mock SOLFEGENAMES for testing doRandom function
5+
const SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti'];
6+
global.SOLFEGENAMES = SOLFEGENAMES;
7+
8+
describe('MathUtility', () => {
9+
describe('doRandom', () => {
10+
test('returns random number between two numbers', () => {
11+
const result = MathUtility.doRandom(1, 10);
12+
expect(result).toBeGreaterThanOrEqual(1);
13+
expect(result).toBeLessThanOrEqual(10);
14+
});
15+
16+
test('returns random solfege array between two solfege names', () => {
17+
const result = MathUtility.doRandom('do', 'sol', 4);
18+
expect(result).toHaveLength(2);
19+
expect(SOLFEGENAMES).toContain(result[0]);
20+
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
21+
});
22+
23+
test('throws error for invalid inputs', () => {
24+
expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError');
25+
});
26+
});
27+
28+
describe('doOneOf', () => {
29+
test('returns either a or b', () => {
30+
const result = MathUtility.doOneOf('a', 'b');
31+
expect(['a', 'b']).toContain(result);
32+
});
33+
});
34+
35+
describe('doMod', () => {
36+
test('calculates modulus correctly', () => {
37+
expect(MathUtility.doMod(10, 3)).toBe(1);
38+
});
39+
40+
test('throws error for invalid inputs', () => {
41+
expect(() => MathUtility.doMod('a', 3)).toThrow('NanError');
42+
});
43+
});
44+
45+
describe('doSqrt', () => {
46+
test('calculates square root', () => {
47+
expect(MathUtility.doSqrt(9)).toBe(3);
48+
});
49+
50+
test('throws error for negative input', () => {
51+
expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError');
52+
});
53+
});
54+
55+
describe('doPlus', () => {
56+
test('adds two numbers', () => {
57+
expect(MathUtility.doPlus(2, 3)).toBe(5);
58+
});
59+
60+
test('concatenates strings', () => {
61+
expect(MathUtility.doPlus('a', 'b')).toBe('ab');
62+
});
63+
});
64+
65+
describe('doMinus', () => {
66+
test('subtracts numbers', () => {
67+
expect(MathUtility.doMinus(5, 3)).toBe(2);
68+
});
69+
70+
test('throws error for string inputs', () => {
71+
expect(() => MathUtility.doMinus('a', 3)).toThrow('NanError');
72+
});
73+
});
74+
75+
describe('doMultiply', () => {
76+
test('multiplies numbers', () => {
77+
expect(MathUtility.doMultiply(2, 3)).toBe(6);
78+
});
79+
80+
test('throws error for string inputs', () => {
81+
expect(() => MathUtility.doMultiply('a', 3)).toThrow('NanError');
82+
});
83+
});
84+
85+
describe('doDivide', () => {
86+
test('divides numbers', () => {
87+
expect(MathUtility.doDivide(6, 2)).toBe(3);
88+
});
89+
90+
test('throws error for division by zero', () => {
91+
expect(() => MathUtility.doDivide(6, 0)).toThrow('DivByZeroError');
92+
});
93+
});
94+
95+
describe('doCalculateDistance', () => {
96+
test('calculates Euclidean distance', () => {
97+
expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5);
98+
});
99+
});
100+
101+
describe('doPower', () => {
102+
test('calculates power', () => {
103+
expect(MathUtility.doPower(2, 3)).toBe(8);
104+
});
105+
});
106+
107+
describe('doAbs', () => {
108+
test('calculates absolute value', () => {
109+
expect(MathUtility.doAbs(-5)).toBe(5);
110+
});
111+
});
112+
113+
describe('doNegate', () => {
114+
test('negates number', () => {
115+
expect(MathUtility.doNegate(5)).toBe(-5);
116+
});
117+
118+
test('reverses string', () => {
119+
expect(MathUtility.doNegate('abc')).toBe('cba');
120+
});
121+
});
122+
123+
describe('doInt', () => {
124+
test('rounds to nearest integer', () => {
125+
expect(MathUtility.doInt(4.6)).toBe(5);
126+
});
127+
});
128+
});
129+

js/utils/mathutils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,5 @@ class MathUtility {
325325
}
326326
}
327327
}
328+
// Ensure mathutils.js exports the MathUtility class
329+
module.exports = MathUtility;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"scripts": {
2121
"lint": "eslint js/**/*.js",
2222
"serve": "python3 -c \"import os, sys; os.system('python3 -m SimpleHTTPServer 3000 --bind 127.0.0.1') if sys.version_info.major==2 else os.system('python3 -m http.server 3000 --bind 127.0.0.1');\"",
23-
"winserve": "py -c \"import os, sys; os.system('py -m SimpleHTTPServer 3000') if sys.version_info.major==2 else os.system('py -m http.server 3000 --bind 127.0.0.1');\""
23+
"winserve": "py -c \"import os, sys; os.system('py -m SimpleHTTPServer 3000') if sys.version_info.major==2 else os.system('py -m http.server 3000 --bind 127.0.0.1');\"",
24+
"test": "jest"
2425
},
2526
"devDependencies": {
2627
"@babel/core": "^7.11.1",

0 commit comments

Comments
 (0)