Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tests to Music Blocks codebase. #4124 #4187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;