-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test for the Turtleactions (#4227)
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -264,3 +264,4 @@ function setupDictActions(activity) { | |
} | ||
}; | ||
} | ||
module.exports = setupDictActions; |
This file contains 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 |
---|---|---|
|
@@ -129,3 +129,4 @@ function setupOrnamentActions(activity) { | |
} | ||
}; | ||
} | ||
module.exports = setupOrnamentActions; |
This file contains 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,88 @@ | ||
const setupDictActions = require('../DictActions'); | ||
describe('setupDictActions', () => { | ||
let activity; | ||
let turtle; | ||
let targetTurtle; | ||
|
||
beforeAll(() => { | ||
global.Turtle = { | ||
DictActions: {} | ||
}; | ||
|
||
global._ = jest.fn((key) => key); | ||
}); | ||
|
||
beforeEach(() => { | ||
activity = { | ||
turtles: { | ||
ithTurtle: jest.fn(), | ||
screenX2turtleX: jest.fn(), | ||
screenY2turtleY: jest.fn(), | ||
}, | ||
logo: { | ||
turtleDicts: {} | ||
}, | ||
textMsg: jest.fn() | ||
}; | ||
|
||
turtle = 0; | ||
targetTurtle = { | ||
painter: { | ||
color: 'red', | ||
value: 10, | ||
chroma: 0.5, | ||
pensize: 2, | ||
font: 'Arial', | ||
orientation: 90, | ||
doSetColor: jest.fn(), | ||
doSetValue: jest.fn(), | ||
doSetChroma: jest.fn(), | ||
doSetPensize: jest.fn(), | ||
doSetFont: jest.fn(), | ||
doSetHeading: jest.fn(), | ||
doSetXY: jest.fn() | ||
}, | ||
container: { | ||
x: 100, | ||
y: 200 | ||
}, | ||
singer: { | ||
notesPlayed: [1, 2], | ||
lastNotePlayed: ['C4'], | ||
notePitches: ['C'], | ||
noteOctaves: [4], | ||
keySignature: 'C', | ||
movable: true, | ||
pitchNumberOffset: 0 | ||
} | ||
}; | ||
activity.turtles.ithTurtle.mockReturnValue(targetTurtle); | ||
activity.turtles.screenX2turtleX.mockReturnValue(100); | ||
activity.turtles.screenY2turtleY.mockReturnValue(200); | ||
setupDictActions(activity); | ||
}); | ||
|
||
it('should set the turtle color correctly', () => { | ||
Turtle.DictActions.SetDictValue(0, turtle, 'color', 'blue'); | ||
expect(targetTurtle.painter.doSetColor).toHaveBeenCalledWith('blue'); | ||
}); | ||
|
||
it('should get the turtle color correctly', () => { | ||
const color = Turtle.DictActions._GetDict(0, turtle, 'color'); | ||
expect(color).toBe('red'); | ||
}); | ||
|
||
it('should return error message if dictionary does not exist', () => { | ||
activity.logo.turtleDicts[turtle] = {}; | ||
const result = Turtle.DictActions.getValue('nonexistentDict', 'color', turtle); | ||
expect(result).toBe('Dictionary with this name does not exist'); | ||
}); | ||
|
||
it('should set and get values in the turtle dictionary', () => { | ||
activity.logo.turtleDicts[turtle] = {}; | ||
Turtle.DictActions.setValue('customDict', 'color', 'green', turtle); | ||
expect(activity.logo.turtleDicts[turtle].customDict.color).toBe('green'); | ||
const value = Turtle.DictActions.getValue('customDict', 'color', turtle); | ||
expect(value).toBe('green'); | ||
}); | ||
}); |
This file contains 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,82 @@ | ||
const setupOrnamentActions = require('../OrnamentActions'); | ||
describe('OrnamentActions', () => { | ||
let activity, turtleMock; | ||
|
||
beforeEach(() => { | ||
global.Singer = { | ||
OrnamentActions: null, | ||
}; | ||
|
||
turtleMock = { | ||
singer: { | ||
staccato: [], | ||
justCounting: [], | ||
inNeighbor: [], | ||
neighborStepPitch: [], | ||
neighborNoteValue: [], | ||
}, | ||
}; | ||
|
||
activity = { | ||
turtles: { | ||
ithTurtle: jest.fn(() => turtleMock), | ||
}, | ||
blocks: { | ||
blockList: { 1: {} }, | ||
}, | ||
logo: { | ||
setDispatchBlock: jest.fn(), | ||
setTurtleListener: jest.fn(), | ||
notation: { | ||
notationBeginSlur: jest.fn(), | ||
notationEndSlur: jest.fn(), | ||
}, | ||
}, | ||
}; | ||
|
||
global.MusicBlocks = { isRun: true }; | ||
global.Mouse = { | ||
getMouseFromTurtle: jest.fn(() => ({ | ||
MB: { listeners: [] }, | ||
})), | ||
}; | ||
setupOrnamentActions(activity); | ||
}); | ||
|
||
test('setStaccato sets up staccato properly', () => { | ||
Singer.OrnamentActions.setStaccato(2, 0, 1); | ||
expect(turtleMock.singer.staccato).toContain(1 / 2); | ||
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_staccato_0'); | ||
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith( | ||
0, | ||
'_staccato_0', | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
test('setSlur sets up slur properly', () => { | ||
Singer.OrnamentActions.setSlur(2, 0, 1); | ||
expect(turtleMock.singer.staccato).toContain(-1 / 2); | ||
expect(activity.logo.notation.notationBeginSlur).toHaveBeenCalledWith(0); | ||
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_staccato_0'); | ||
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith( | ||
0, | ||
'_staccato_0', | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
test('doNeighbor sets up neighbor action properly', () => { | ||
Singer.OrnamentActions.doNeighbor(3, 4, 0, 1); | ||
expect(turtleMock.singer.inNeighbor).toContain(1); | ||
expect(turtleMock.singer.neighborStepPitch).toContain(3); | ||
expect(turtleMock.singer.neighborNoteValue).toContain(4); | ||
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_neighbor_0_1'); | ||
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith( | ||
0, | ||
'_neighbor_0_1', | ||
expect.any(Function) | ||
); | ||
}); | ||
}); | ||
|