Skip to content

Commit

Permalink
Implement tests to Music Blocks codebase. sugarlabs#4124
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamatha1718 committed Dec 26, 2024
1 parent a99c894 commit 9dd3af0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 11 additions & 5 deletions js/utils/__tests__/mathutils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ describe('MathUtility', () => {
});

test('throws error for invalid inputs', () => {
expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError');
expect(() => MathUtility.doRandom('invalid', 10)).toThrow('NanError'); //Invalid solfage name
expect(() => MathUtility.doRandom(1, 'invalid')).toThrow('NanError'); //Invalid input
});

test('returns valid random solfege for octave', () => {
const result = MathUtility.doRandom('do', 'ti', 4);
expect(SOLFEGENAMES).toContain(result[0]); //Check if the result contains a valid solfage name
expect(Number(result[1])).toBeGreaterThanOrEqual(4); //Ensure octave is valid
});
});

describe('doOneOf', () => {
describe('pickRandom', () => {
test('returns either a or b', () => {
const result = MathUtility.doOneOf('a', 'b');
const result = MathUtility.pickRandom('a', 'b');
expect(['a', 'b']).toContain(result);
});
});
Expand Down Expand Up @@ -125,5 +132,4 @@ describe('MathUtility', () => {
expect(MathUtility.doInt(4.6)).toBe(5);
});
});
});

});
14 changes: 7 additions & 7 deletions js/utils/mathutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* NumberBlocks.js
*/
class MathUtility {
/**
/**
* Returns a random integer in a range.
*
* @static
Expand Down Expand Up @@ -74,7 +74,7 @@ class MathUtility {
octave = octave === undefined ? 4 : octave;

const broadScale = [];
for (const i of [octave, octave + 1]) {
for (const i of[octave, octave + 1]) {
for (let j = 0; j < SOLFEGENAMES.length; j++) {
broadScale.push(SOLFEGENAMES[j] + " " + i);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ class MathUtility {
* @param {*} b
* @returns {*} - Either a or b.
*/
static doOneOf(a, b) {
static pickRandom(a, b) {
return Math.random() < 0.5 ? a : b;
}

Expand Down Expand Up @@ -229,7 +229,7 @@ class MathUtility {
}
}

/**
/**
* Calculates Euclidean distance between (cursor x, cursor y) and (mouse 'x' and mouse 'y').
*
* @static
Expand Down Expand Up @@ -257,7 +257,7 @@ class MathUtility {
}
}

/**
/**
* Returns a to the power of b.
*
* @static
Expand Down Expand Up @@ -290,7 +290,7 @@ class MathUtility {
}
}

/**
/**
* Returns negative value (if number) or string in reverse (if string).
*
* @static
Expand Down Expand Up @@ -326,4 +326,4 @@ class MathUtility {
}
}
// Ensure mathutils.js exports the MathUtility class
module.exports = MathUtility;
module.exports = MathUtility;

0 comments on commit 9dd3af0

Please sign in to comment.