|
1 | 1 | // A basic test file in jest framework for the mathutils.js |
| 2 | +const MathUtility = require('../mathutils'); |
2 | 3 |
|
3 | | -const MathUtility = require("../mathutils.js"); |
| 4 | +// Mock SOLFEGENAMES for testing doRandom function |
| 5 | +const SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti']; |
| 6 | +global.SOLFEGENAMES = SOLFEGENAMES; |
4 | 7 |
|
5 | 8 | describe('MathUtility', () => { |
6 | | - beforeAll(() => { |
7 | | - global.SOLFEGENAMES = ['do', 're', 'mi', 'fa', 'sol', 'la', 'ti']; |
8 | | - }); |
9 | | - |
10 | | - describe('doRandom', () => { |
11 | | - it('should return a random number within the given range', () => { |
12 | | - const min = 1, max = 10; |
13 | | - const result = MathUtility.doRandom(min, max); |
14 | | - expect(result).toBeGreaterThanOrEqual(min); |
15 | | - expect(result).toBeLessThanOrEqual(max); |
16 | | - }); |
17 | | - |
18 | | - it('should throw an error for invalid inputs', () => { |
19 | | - expect(() => MathUtility.doRandom(null, undefined)).toThrow('NanError'); |
20 | | - }); |
21 | | - |
22 | | - it('should return a valid solfege array when inputs are solfege strings', () => { |
23 | | - const result = MathUtility.doRandom('do', 'mi', 4); |
24 | | - expect(result).toHaveLength(2); |
25 | | - expect(global.SOLFEGENAMES).toContain(result[0]); |
26 | | - expect(Number(result[1])).toBeGreaterThanOrEqual(4); |
27 | | - }); |
28 | | - }); |
29 | | - |
30 | | - describe('doOneOf', () => { |
31 | | - it('should return either of the inputs randomly', () => { |
32 | | - const a = 'apple', b = 'banana'; |
33 | | - const result = MathUtility.doOneOf(a, b); |
34 | | - expect([a, b]).toContain(result); |
35 | | - }); |
36 | | - }); |
37 | | - |
38 | | - describe('doMod', () => { |
39 | | - it('should return the modulus of two numbers', () => { |
40 | | - expect(MathUtility.doMod(10, 3)).toBe(1); |
41 | | - }); |
42 | | - |
43 | | - it('should throw an error for invalid inputs', () => { |
44 | | - expect(() => MathUtility.doMod('a', 3)).toThrow('NanError'); |
45 | | - }); |
46 | | - }); |
| 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 | + }); |
47 | 15 |
|
48 | | - describe('doSqrt', () => { |
49 | | - it('should return the square root of a number', () => { |
50 | | - expect(MathUtility.doSqrt(9)).toBe(3); |
51 | | - }); |
| 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 | + }); |
52 | 22 |
|
53 | | - it('should throw an error for negative numbers', () => { |
54 | | - expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError'); |
| 23 | + test('throws error for invalid inputs', () => { |
| 24 | + expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError'); |
| 25 | + }); |
55 | 26 | }); |
56 | | - }); |
57 | 27 |
|
58 | | - describe('doPlus', () => { |
59 | | - it('should add two numbers', () => { |
60 | | - expect(MathUtility.doPlus(3, 4)).toBe(7); |
| 28 | + describe('doOneOf', () => { |
| 29 | + test('returns either a or b', () => { |
| 30 | + const result = MathUtility.doOneOf('a', 'b'); |
| 31 | + expect(['a', 'b']).toContain(result); |
| 32 | + }); |
61 | 33 | }); |
62 | 34 |
|
63 | | - it('should concatenate strings', () => { |
64 | | - expect(MathUtility.doPlus('3', 4)).toBe('34'); |
65 | | - }); |
66 | | - }); |
| 35 | + describe('doMod', () => { |
| 36 | + test('calculates modulus correctly', () => { |
| 37 | + expect(MathUtility.doMod(10, 3)).toBe(1); |
| 38 | + }); |
67 | 39 |
|
68 | | - describe('doMinus', () => { |
69 | | - it('should subtract two numbers', () => { |
70 | | - expect(MathUtility.doMinus(10, 4)).toBe(6); |
| 40 | + test('throws error for invalid inputs', () => { |
| 41 | + expect(() => MathUtility.doMod('a', 3)).toThrow('NanError'); |
| 42 | + }); |
71 | 43 | }); |
72 | 44 |
|
73 | | - it('should throw an error for strings', () => { |
74 | | - expect(() => MathUtility.doMinus('10', 4)).toThrow('NanError'); |
75 | | - }); |
76 | | - }); |
| 45 | + describe('doSqrt', () => { |
| 46 | + test('calculates square root', () => { |
| 47 | + expect(MathUtility.doSqrt(9)).toBe(3); |
| 48 | + }); |
77 | 49 |
|
78 | | - describe('doMultiply', () => { |
79 | | - it('should multiply two numbers', () => { |
80 | | - expect(MathUtility.doMultiply(2, 5)).toBe(10); |
| 50 | + test('throws error for negative input', () => { |
| 51 | + expect(() => MathUtility.doSqrt(-1)).toThrow('NoSqrtError'); |
| 52 | + }); |
81 | 53 | }); |
82 | 54 |
|
83 | | - it('should throw an error for strings', () => { |
84 | | - expect(() => MathUtility.doMultiply('2', 5)).toThrow('NanError'); |
85 | | - }); |
86 | | - }); |
| 55 | + describe('doPlus', () => { |
| 56 | + test('adds two numbers', () => { |
| 57 | + expect(MathUtility.doPlus(2, 3)).toBe(5); |
| 58 | + }); |
87 | 59 |
|
88 | | - describe('doDivide', () => { |
89 | | - it('should divide two numbers', () => { |
90 | | - expect(MathUtility.doDivide(10, 2)).toBe(5); |
| 60 | + test('concatenates strings', () => { |
| 61 | + expect(MathUtility.doPlus('a', 'b')).toBe('ab'); |
| 62 | + }); |
91 | 63 | }); |
92 | 64 |
|
93 | | - it('should throw an error for division by zero', () => { |
94 | | - expect(() => MathUtility.doDivide(10, 0)).toThrow('DivByZeroError'); |
95 | | - }); |
96 | | - }); |
| 65 | + describe('doMinus', () => { |
| 66 | + test('subtracts numbers', () => { |
| 67 | + expect(MathUtility.doMinus(5, 3)).toBe(2); |
| 68 | + }); |
97 | 69 |
|
98 | | - describe('doCalculateDistance', () => { |
99 | | - it('should calculate Euclidean distance', () => { |
100 | | - expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5); |
| 70 | + test('throws error for string inputs', () => { |
| 71 | + expect(() => MathUtility.doMinus('a', 3)).toThrow('NanError'); |
| 72 | + }); |
101 | 73 | }); |
102 | 74 |
|
103 | | - it('should throw an error for invalid inputs', () => { |
104 | | - expect(() => MathUtility.doCalculateDistance('a', 0, 3, 4)).toThrow('NanError'); |
105 | | - }); |
106 | | - }); |
| 75 | + describe('doMultiply', () => { |
| 76 | + test('multiplies numbers', () => { |
| 77 | + expect(MathUtility.doMultiply(2, 3)).toBe(6); |
| 78 | + }); |
107 | 79 |
|
108 | | - describe('doPower', () => { |
109 | | - it('should calculate the power of a number', () => { |
110 | | - expect(MathUtility.doPower(2, 3)).toBe(8); |
| 80 | + test('throws error for string inputs', () => { |
| 81 | + expect(() => MathUtility.doMultiply('a', 3)).toThrow('NanError'); |
| 82 | + }); |
111 | 83 | }); |
112 | 84 |
|
113 | | - it('should throw an error for invalid inputs', () => { |
114 | | - expect(() => MathUtility.doPower('2', 3)).toThrow('NanError'); |
115 | | - }); |
116 | | - }); |
| 85 | + describe('doDivide', () => { |
| 86 | + test('divides numbers', () => { |
| 87 | + expect(MathUtility.doDivide(6, 2)).toBe(3); |
| 88 | + }); |
117 | 89 |
|
118 | | - describe('doAbs', () => { |
119 | | - it('should return the absolute value of a number', () => { |
120 | | - expect(MathUtility.doAbs(-5)).toBe(5); |
| 90 | + test('throws error for division by zero', () => { |
| 91 | + expect(() => MathUtility.doDivide(6, 0)).toThrow('DivByZeroError'); |
| 92 | + }); |
121 | 93 | }); |
122 | 94 |
|
123 | | - it('should throw an error for invalid inputs', () => { |
124 | | - expect(() => MathUtility.doAbs('a')).toThrow('NanError'); |
| 95 | + describe('doCalculateDistance', () => { |
| 96 | + test('calculates Euclidean distance', () => { |
| 97 | + expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5); |
| 98 | + }); |
125 | 99 | }); |
126 | | - }); |
127 | 100 |
|
128 | | - describe('doNegate', () => { |
129 | | - it('should negate a number', () => { |
130 | | - expect(MathUtility.doNegate(5)).toBe(-5); |
| 101 | + describe('doPower', () => { |
| 102 | + test('calculates power', () => { |
| 103 | + expect(MathUtility.doPower(2, 3)).toBe(8); |
| 104 | + }); |
131 | 105 | }); |
132 | 106 |
|
133 | | - it('should reverse a string', () => { |
134 | | - expect(MathUtility.doNegate('hello')).toBe('olleh'); |
| 107 | + describe('doAbs', () => { |
| 108 | + test('calculates absolute value', () => { |
| 109 | + expect(MathUtility.doAbs(-5)).toBe(5); |
| 110 | + }); |
135 | 111 | }); |
136 | 112 |
|
137 | | - it('should throw an error for invalid inputs', () => { |
138 | | - expect(() => MathUtility.doNegate(null)).toThrow('NoNegError'); |
139 | | - }); |
140 | | - }); |
| 113 | + describe('doNegate', () => { |
| 114 | + test('negates number', () => { |
| 115 | + expect(MathUtility.doNegate(5)).toBe(-5); |
| 116 | + }); |
141 | 117 |
|
142 | | - describe('doInt', () => { |
143 | | - it('should round a number to the nearest integer', () => { |
144 | | - expect(MathUtility.doInt(5.5)).toBe(6); |
| 118 | + test('reverses string', () => { |
| 119 | + expect(MathUtility.doNegate('abc')).toBe('cba'); |
| 120 | + }); |
145 | 121 | }); |
146 | 122 |
|
147 | | - it('should throw an error for invalid inputs', () => { |
148 | | - expect(() => MathUtility.doInt('hello')).toThrow('NanError'); |
| 123 | + describe('doInt', () => { |
| 124 | + test('rounds to nearest integer', () => { |
| 125 | + expect(MathUtility.doInt(4.6)).toBe(5); |
| 126 | + }); |
149 | 127 | }); |
150 | | - }); |
151 | 128 | }); |
| 129 | + |
0 commit comments