-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Made musicutils.test.js #4124 #4140
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c9cc194
Update 2
Commanderk3 82f7d6d
dk
Commanderk3 6ff89d2
musicutil final
Commanderk3 79e3e4c
Merge branch 'sugarlabs:master' into mb-utils
Commanderk3 2a4d003
Merge branch 'mb-utils' of https://github.com/Commanderk3/musicblocks…
Commanderk3 a007206
update
Commanderk3 cdd663d
final update
Commanderk3 ac25c6b
comment lines removed
Commanderk3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,376 @@ | ||
| global._ = jest.fn((str) => str); | ||
| global.window = { | ||
| btoa: jest.fn((str) => Buffer.from(str, "utf8").toString("base64")) | ||
| }; | ||
|
|
||
| const { | ||
| setOctaveRatio, | ||
| getOctaveRatio, | ||
| TEMPERAMENT, | ||
| TEMPERAMENTS, | ||
| INITIALTEMPERAMENTS, | ||
| PreDefinedTemperaments, | ||
| getTemperamentsList, | ||
| getTemperament, | ||
| getTemperamentKeys, | ||
| addTemperamentToList, | ||
| addTemperamentToDictionary, | ||
| updateTemperaments, | ||
| deleteTemperamentFromList, | ||
| DEFAULTINVERT, | ||
| DEFAULTMODE, | ||
| customMode, | ||
| getInvertMode, | ||
| getIntervalNumber, | ||
| getIntervalDirection, | ||
| getIntervalRatio, | ||
| getModeNumbers, | ||
| getDrumIndex, | ||
| getDrumName, | ||
| getDrumSymbol, | ||
| getFilterTypes | ||
| } = require("../musicutils"); | ||
|
|
||
|
|
||
| describe("musicutils", () => { | ||
| it("should set and get Octave Ratio", () => { | ||
| setOctaveRatio(4); | ||
| const octaveR = getOctaveRatio(); | ||
| expect(octaveR).toBe(4); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Temperament Functions", () => { | ||
| it("getTemperamentsList should return the list of temperaments", () => { | ||
| expect(getTemperamentsList()).toEqual([ | ||
| [_("Equal (12EDO)"), "equal", "equal"], | ||
| [_("Equal (5EDO)"), "equal5", "equal5"], | ||
| [_("Equal (7EDO)"), "equal7", "equal7"], | ||
| [_("Equal (19EDO)"), "equal19", "equal19"], | ||
| [_("Equal (31EDO)"), "equal31", "equal31"], | ||
| [_("5-limit Just Intonation"), "just intonation", "just intonation"], | ||
| [_("Pythagorean (3-limit JI)"), "Pythagorean", "Pythagorean"], | ||
| [_("Meantone") + " (1/3)", "1/3 comma meantone", "meantone (1/3)"], | ||
| [_("Meantone") + " (1/4)", "1/4 comma meantone", "meantone (1/4)"], | ||
| [_("Custom"), "custom", "custom"] | ||
| ]); | ||
| }); | ||
|
|
||
| describe("getTemperament", () => { | ||
| it("should return the correct temperament for a valid key", () => { | ||
| const equalTemperament = getTemperament("equal"); | ||
| expect(equalTemperament).toHaveProperty("perfect 1"); | ||
| expect(equalTemperament).toHaveProperty("minor 2"); | ||
| expect(equalTemperament).toHaveProperty("pitchNumber", 12); | ||
| }); | ||
|
|
||
| it('should return the correct temperament for equal5 key', () => { | ||
| const equal5Temperament = getTemperament("equal5"); | ||
| expect(equal5Temperament).toHaveProperty("perfect 1"); | ||
| expect(equal5Temperament).toHaveProperty("minor 2"); | ||
| expect(equal5Temperament).toHaveProperty("pitchNumber", 5); | ||
| }); | ||
|
|
||
| it("should return undefined for an invalid key", () => { | ||
| const invalidTemperament = getTemperament("invalid"); | ||
| expect(invalidTemperament).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getTemperamentKeys", () => { | ||
| it("should return an array with the correct length", () => { | ||
| const keys = getTemperamentKeys(); | ||
| expect(keys.length).toBe(10); | ||
| }); | ||
|
|
||
| it('should return an array containing all keys', () => { | ||
| const keys = getTemperamentKeys(); | ||
| expect(keys).toEqual( | ||
| expect.arrayContaining([ | ||
| "equal", | ||
| "equal5", | ||
| "equal7", | ||
| "equal19", | ||
| "equal31", | ||
| "just intonation", | ||
| "Pythagorean", | ||
| "1/3 comma meantone", | ||
| "1/4 comma meantone", | ||
| "custom" | ||
| ]) | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| /* | ||
| describe("Temperament Management", () => { | ||
| beforeEach(() => { | ||
| // Reset global variables before each test | ||
| global.TEMPERAMENTS = [...INITIALTEMPERAMENTS]; | ||
| global.TEMPERAMENT = { | ||
| equal: { | ||
| pitchNumber: 12, | ||
| interval: ["perfect 1", "minor 2", "major 2"] | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| it("addTemperamentToList should add a new temperament if not predefined", () => { | ||
| const newEntry = "customTemperament"; | ||
| addTemperamentToList(newEntry); | ||
| expect(TEMPERAMENTS).toContain(newEntry); | ||
| }); | ||
|
|
||
| it("addTemperamentToList should not add a predefined temperament", () => { | ||
| const predefinedEntry = "equal"; | ||
| addTemperamentToList(predefinedEntry); | ||
| expect(TEMPERAMENTS).not.toContain(predefinedEntry); | ||
| }); | ||
|
|
||
| it("deleteTemperamentFromList should remove a temperament from the dictionary", () => { | ||
| const oldEntry = "equal"; | ||
| deleteTemperamentFromList(oldEntry); | ||
| expect(TEMPERAMENT[oldEntry]).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("addTemperamentToDictionary should add a new temperament to the dictionary", () => { | ||
| const entryName = "newTemperament"; | ||
| const entryValue = { | ||
| pitchNumber: 7, | ||
| interval: ["perfect 1", "minor 3", "major 3"] | ||
| }; | ||
| addTemperamentToDictionary(entryName, entryValue); | ||
| it(TEMPERAMENT[entryName]).toEqual(entryValue); | ||
| }); | ||
|
|
||
| it("updateTemperaments should update TEMPERAMENTS with new entries", () => { | ||
| const newEntry = "customTemperament"; | ||
| TEMPERAMENT[newEntry] = { | ||
| pitchNumber: 8, | ||
| interval: ["perfect 1", "minor 3"] | ||
| }; | ||
| updateTemperaments(); | ||
| expect(TEMPERAMENTS.some(([_, name]) => name === newEntry)).toBe(true); | ||
| }); | ||
|
|
||
| it("updateTemperaments should not duplicate predefined temperaments", () => { | ||
| updateTemperaments(); | ||
| const predefinedEntries = TEMPERAMENTS.filter(([_, name]) => name in PreDefinedTemperaments); | ||
| expect(predefinedEntries.length).toBe(Object.keys(PreDefinedTemperaments).length); | ||
| }); | ||
| }); | ||
| }); | ||
| */ | ||
| describe("Constants", () => { | ||
| it("should have correct default values", () => { | ||
| expect(DEFAULTINVERT).toBe("even"); | ||
| expect(DEFAULTMODE).toBe("major"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("customMode", () => { | ||
| it("should return custom mode from MUSICALMODES", () => { | ||
| expect(customMode).toEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getInvertMode", () => { | ||
| it("should return the correct invert mode name", () => { | ||
| expect(getInvertMode("even")).toBe("even"); | ||
| expect(getInvertMode("scalar")).toBe("scalar"); | ||
| expect(getInvertMode("nonexistent")).toBe("nonexistent"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getIntervalNumber", () => { | ||
| it("should return the number of semi-tones for a given interval", () => { | ||
| expect(getIntervalNumber("perfect 5")).toBe(7); | ||
| expect(getIntervalNumber("major 3")).toBe(4); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getIntervalDirection", () => { | ||
| it("should return the direction of the interval", () => { | ||
| expect(getIntervalDirection("diminished 6")).toBe(-1); | ||
| expect(getIntervalDirection("minor 3")).toBe(-1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getIntervalRatio", () => { | ||
| it("should return the ratio for a given interval", () => { | ||
| expect(getIntervalRatio("perfect 5")).toBe(1.5); | ||
| expect(getIntervalRatio("major 3")).toBe(1.25); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getModeNumbers", () => { | ||
| it("should return the correct mode numbers for a valid mode", () => { | ||
| expect(getModeNumbers("chromatic")).toBe("0 1 2 3 4 5 6 7 8 9 10 11"); | ||
| expect(getModeNumbers("major")).toBe("0 2 4 5 7 9 11"); | ||
| expect(getModeNumbers("minor")).toBe("0 2 3 5 7 8 10"); | ||
| }); | ||
|
|
||
| it("should return an empty string for an invalid mode", () => { | ||
| expect(getModeNumbers("invalidMode")).toBe(""); | ||
| }); | ||
|
|
||
| it("should handle custom mode correctly", () => { | ||
| expect(getModeNumbers("custom")).toBe("0 1 2 3 4 5 6 7 8 9 10 11"); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| jest.mock('../musicutils', () => { | ||
| const actualModule = jest.requireActual('../musicutils'); | ||
| return { | ||
| ...actualModule, | ||
| getDrumIndex: jest.fn(), | ||
| getDrumName: jest.fn(), | ||
| getDrumSymbol: jest.fn() | ||
| }; | ||
| }); | ||
|
|
||
| describe('getDrum', () => { | ||
| let DRUMNAMES, DEFAULTDRUM; | ||
|
|
||
| beforeEach(() => { | ||
| DRUMNAMES = [ | ||
| ["snare drum", "snare drum", "images/snaredrum.svg", "sn", "drum"], | ||
| ["kick drum", "kick drum", "images/kick.svg", "hh", "drum"], | ||
| ["tom tom", "tom tom", "images/tom.svg", "tomml", "drum"], | ||
| ["floor tom", "floor tom", "images/floortom.svg", "tomfl", "drum"], | ||
| ["bass drum", "bass drum", "images/kick.svg", "tomfl", "drum"], | ||
| ["hi hat", "hi hat", "images/hihat.svg", "hh", "bell"] | ||
| ]; | ||
| DEFAULTDRUM = "kick drum"; | ||
|
|
||
| // Mock for getDrumIndex | ||
| require('../musicutils').getDrumIndex.mockImplementation((name) => { | ||
| if (name.slice(0, 4) === "http") return null; | ||
| if (name === "") return DRUMNAMES.findIndex(drum => drum[0] === DEFAULTDRUM); | ||
|
|
||
| const index = DRUMNAMES.findIndex( | ||
| (drum) => drum[0].toLowerCase() === name.toLowerCase() | ||
| ); | ||
| return index >= 0 ? index : -1; | ||
| }); | ||
|
|
||
| // Mock for getDrumName | ||
| require('../musicutils').getDrumName.mockImplementation((name) => { | ||
| if (name === "") name = DEFAULTDRUM; | ||
| if (name.slice(0, 4) === "http") return null; | ||
|
|
||
| for (let drum = 0; drum < DRUMNAMES.length; drum++) { | ||
| if (DRUMNAMES[drum][0].toLowerCase() === name.toLowerCase()) { | ||
| return DRUMNAMES[drum][0]; | ||
| } else if (DRUMNAMES[drum][1].toLowerCase() === name.toLowerCase()) { | ||
| return DRUMNAMES[drum][1]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| require('../musicutils').getDrumSymbol.mockImplementation((name) => { | ||
| if (name === "") return "hh"; | ||
|
|
||
| for (let drum = 0; drum < DRUMNAMES.length; drum++) { | ||
| if (DRUMNAMES[drum][0].toLowerCase() === name.toLowerCase()) { | ||
| return DRUMNAMES[drum][3]; | ||
| } else if (DRUMNAMES[drum][1].toLowerCase() === name.toLowerCase()) { | ||
| return "hh"; | ||
| } | ||
| } | ||
|
|
||
| return "hh"; | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDrumIndex', () => { | ||
| it('should return the index of a valid drum name', () => { | ||
| expect(getDrumIndex('snare drum')).toBe(0); | ||
| expect(getDrumIndex('kick drum')).toBe(1); | ||
| expect(getDrumIndex('floor tom')).toBe(3); | ||
| }); | ||
|
|
||
| it('should return -1 for an invalid drum name', () => { | ||
| expect(getDrumIndex('invalid drum')).toBe(-1); | ||
| }); | ||
|
|
||
| it('should return the index of the DEFAULTDRUM for empty input', () => { | ||
| expect(getDrumIndex('')).toBe(1); | ||
| }); | ||
|
|
||
| it('should ignore case sensitivity when matching drum names', () => { | ||
| expect(getDrumIndex('SNARE DRUM')).toBe(0); | ||
| }); | ||
|
|
||
| it('should return null for names starting with "http"', () => { | ||
| expect(getDrumIndex('http')).toBe(null); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDrumName', () => { | ||
| it('should return the name of a valid drum', () => { | ||
| expect(getDrumName('snare drum')).toBe('snare drum'); | ||
| expect(getDrumName('kick drum')).toBe('kick drum'); | ||
| }); | ||
|
|
||
| it('should return the DEFAULTDRUM name for empty input', () => { | ||
| expect(getDrumName('')).toBe('kick drum'); | ||
| }); | ||
|
|
||
| it('should return null for names starting with "http"', () => { | ||
| expect(getDrumName('http')).toBe(null); | ||
| }); | ||
|
|
||
| it('should ignore case sensitivity when matching drum names', () => { | ||
| expect(getDrumName('SNARE DRUM')).toBe('snare drum'); | ||
| expect(getDrumName('KICK DRUM')).toBe('kick drum'); | ||
| }); | ||
|
|
||
| it('should return null for an invalid drum name', () => { | ||
| expect(getDrumName('invalid drum')).toBe(null); | ||
| }); | ||
|
|
||
| it('should match the second element of DRUMNAMES if provided', () => { | ||
| expect(getDrumName('kick drum')).toBe('kick drum'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getDrumSymbol', () => { | ||
| it('should return the correct symbol for a valid drum name', () => { | ||
| expect(getDrumSymbol('snare drum')).toBe('sn'); | ||
| expect(getDrumSymbol('kick drum')).toBe('hh'); | ||
| expect(getDrumSymbol('floor tom')).toBe('tomfl'); | ||
| }); | ||
|
|
||
| it('should return "hh" for an empty name', () => { | ||
| expect(getDrumSymbol('')).toBe('hh'); | ||
| }); | ||
|
|
||
| it('should return "hh" for an invalid drum name', () => { | ||
| expect(getDrumSymbol('invalid drum')).toBe('hh'); | ||
| }); | ||
|
|
||
| it('should return "hh" for a name matching the second element of DRUMNAMES', () => { | ||
| expect(getDrumSymbol('snare drum')).toBe('sn'); | ||
| expect(getDrumSymbol('kick drum')).toBe('hh'); // As per logic | ||
| }); | ||
|
|
||
| it('should ignore case sensitivity when matching drum names', () => { | ||
| expect(getDrumSymbol('SNARE DRUM')).toBe('sn'); | ||
| expect(getDrumSymbol('KICK DRUM')).toBe('hh'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFilterTypes',() => { | ||
| it('should return default filter type', () => { | ||
| expect(getFilterTypes('')).toBe('highpass'); //DEFAULTFILTERTYPES | ||
| }); | ||
| it('should return correct filter types', () => { | ||
| expect(getFilterTypes('highpass')).toBe('highpass'); | ||
| expect(getFilterTypes('notch')).toBe('notch'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious why this commented code is included?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I made test functions for the "Temperament Management " block the results were not matched. Somehow the functions are not working.
I did not want to edit musicutil.js so I left this job to @Aman1919
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then please remove the commented code from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walterbender this shows error while running the test
