|
| 1 | +require("../turtle"); |
| 2 | +const Turtle = global.Turtle; |
| 3 | +// Mock all external dependencies |
| 4 | +global.importMembers = jest.fn(); |
| 5 | +global.Singer = jest.fn().mockImplementation(() => ({ |
| 6 | + attack: [], |
| 7 | + decay: [], |
| 8 | + sustain: [], |
| 9 | + release: [], |
| 10 | + scalarTransposition: 0, |
| 11 | + scalarTranspositionValues: [], |
| 12 | + transposition: 0, |
| 13 | + transpositionValues: [], |
| 14 | + register: 0, |
| 15 | + beatFactor: 1, |
| 16 | + dotCount: 0, |
| 17 | + noteBeat: {}, |
| 18 | + noteValue: {}, |
| 19 | + oscList: {}, |
| 20 | + noteDrums: {}, |
| 21 | + notePitches: {}, |
| 22 | + noteOctaves: {}, |
| 23 | + noteCents: {}, |
| 24 | + noteHertz: {}, |
| 25 | + noteBeatValues: {}, |
| 26 | + embeddedGraphics: {}, |
| 27 | + lastNotePlayed: null, |
| 28 | + previousNotePlayed: null, |
| 29 | + noteStatus: null, |
| 30 | + noteDirection: 0, |
| 31 | + pitchNumberOffset: 39, |
| 32 | + currentOctave: 4, |
| 33 | + inHarmonic: [], |
| 34 | + partials: [], |
| 35 | + inNeighbor: [], |
| 36 | + neighborStepPitch: [], |
| 37 | + neighborNoteValue: [], |
| 38 | + inDefineMode: false, |
| 39 | + defineMode: [], |
| 40 | + notesPlayed: [0, 1], |
| 41 | + whichNoteToCount: 1, |
| 42 | + movable: false, |
| 43 | + bpm: [], |
| 44 | + previousTurtleTime: 0, |
| 45 | + turtleTime: 0, |
| 46 | + pushedNote: false, |
| 47 | + duplicateFactor: 1, |
| 48 | + inDuplicate: false, |
| 49 | + skipFactor: 1, |
| 50 | + skipIndex: 0, |
| 51 | + instrumentNames: [], |
| 52 | + inCrescendo: [], |
| 53 | + crescendoDelta: [], |
| 54 | + crescendoInitialVolume: {}, |
| 55 | + intervals: [], |
| 56 | + semitoneIntervals: [], |
| 57 | + staccato: [], |
| 58 | + glide: [], |
| 59 | + glideOverride: 0, |
| 60 | + swing: [], |
| 61 | + swingTarget: [], |
| 62 | + swingCarryOver: 0, |
| 63 | + tie: false, |
| 64 | + tieNotePitches: [], |
| 65 | + tieNoteExtras: [], |
| 66 | + tieCarryOver: 0, |
| 67 | + tieFirstDrums: [], |
| 68 | + drift: 0, |
| 69 | + maxLagCorrectionRatio: 0.25, |
| 70 | + drumStyle: [], |
| 71 | + voices: [], |
| 72 | + backward: [], |
| 73 | + vibratoIntensity: [], |
| 74 | + vibratoRate: [], |
| 75 | + distortionAmount: [], |
| 76 | + tremoloFrequency: [], |
| 77 | + tremoloDepth: [], |
| 78 | + rate: [], |
| 79 | + octaves: [], |
| 80 | + baseFrequency: [], |
| 81 | + chorusRate: [], |
| 82 | + delayTime: [], |
| 83 | + chorusDepth: [], |
| 84 | + neighborArgNote1: [], |
| 85 | + neighborArgNote2: [], |
| 86 | + neighborArgBeat: [], |
| 87 | + neighborArgCurrentBeat: [], |
| 88 | + inNoteBlock: [], |
| 89 | + multipleVoices: false, |
| 90 | + invertList: [], |
| 91 | + beatList: [], |
| 92 | + factorList: [], |
| 93 | + keySignature: "", |
| 94 | + pitchDrumTable: {}, |
| 95 | + defaultStrongBeats: false, |
| 96 | + pickup: 0, |
| 97 | + beatsPerMeasure: 4, |
| 98 | + noteValuePerBeat: 4, |
| 99 | + currentBeat: 0, |
| 100 | + currentMeasure: 0, |
| 101 | + justCounting: [], |
| 102 | + justMeasuring: [], |
| 103 | + firstPitch: [], |
| 104 | + lastPitch: [], |
| 105 | + suppressOutput: false, |
| 106 | + dispatchFactor: 1, |
| 107 | + runningFromEvent: false |
| 108 | +})); |
| 109 | +global.Painter = jest.fn().mockImplementation(() => ({ |
| 110 | + cp1x: 0, |
| 111 | + cp1y: 100, |
| 112 | + cp2x: 100, |
| 113 | + cp2y: 100 |
| 114 | +})); |
| 115 | +global.delayExecution = jest.fn(); |
| 116 | +global.DEFAULTVOICE = "electronic synth"; |
| 117 | +global.DEFAULTVOLUME = 50; |
| 118 | + |
| 119 | +describe("Turtle", () => { |
| 120 | + let turtle; |
| 121 | + let mockActivity; |
| 122 | + |
| 123 | + beforeEach(() => { |
| 124 | + mockActivity = { refreshCanvas: jest.fn() }; |
| 125 | + turtle = new Turtle(mockActivity, 0, "turtle1", {}, null); |
| 126 | + }); |
| 127 | + |
| 128 | + describe("blinking()", () => { |
| 129 | + it("should return false when _blinkFinished is true", () => { |
| 130 | + turtle._blinkFinished = true; |
| 131 | + expect(turtle.blinking()).toBe(false); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should return true when _blinkFinished is false", () => { |
| 135 | + turtle._blinkFinished = false; |
| 136 | + expect(turtle.blinking()).toBe(true); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe("doWait()", () => { |
| 141 | + it("should set _waitTime in milliseconds", () => { |
| 142 | + turtle.doWait(2); |
| 143 | + expect(turtle._waitTime).toBe(2000); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should handle decimal seconds", () => { |
| 147 | + turtle.doWait(0.5); |
| 148 | + expect(turtle._waitTime).toBe(500); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should handle string input by converting to number", () => { |
| 152 | + turtle.doWait("3"); |
| 153 | + expect(turtle._waitTime).toBe(3000); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should set _waitTime to 0 when called with 0", () => { |
| 157 | + turtle.doWait(0); |
| 158 | + expect(turtle._waitTime).toBe(0); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe("initTurtle()", () => { |
| 163 | + it("should reset _waitTime to 0", () => { |
| 164 | + turtle.doWait(5); |
| 165 | + turtle.initTurtle(false); |
| 166 | + expect(turtle._waitTime).toBe(0); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should set embeddedGraphicsFinished to true", () => { |
| 170 | + turtle.embeddedGraphicsFinished = false; |
| 171 | + turtle.initTurtle(false); |
| 172 | + expect(turtle.embeddedGraphicsFinished).toBe(true); |
| 173 | + }); |
| 174 | + |
| 175 | + it("should set inSetTimbre to false", () => { |
| 176 | + turtle.inSetTimbre = true; |
| 177 | + turtle.initTurtle(false); |
| 178 | + expect(turtle.inSetTimbre).toBe(false); |
| 179 | + }); |
| 180 | + |
| 181 | + it("should reset singer.scalarTransposition to 0", () => { |
| 182 | + turtle.initTurtle(false); |
| 183 | + expect(turtle.singer.scalarTransposition).toBe(0); |
| 184 | + }); |
| 185 | + |
| 186 | + it("should reset singer.register to 0", () => { |
| 187 | + turtle.initTurtle(false); |
| 188 | + expect(turtle.singer.register).toBe(0); |
| 189 | + }); |
| 190 | + |
| 191 | + it("should reset singer.beatFactor to 1", () => { |
| 192 | + turtle.initTurtle(false); |
| 193 | + expect(turtle.singer.beatFactor).toBe(1); |
| 194 | + }); |
| 195 | + |
| 196 | + it("should set singer.keySignature to C major", () => { |
| 197 | + turtle.initTurtle(false); |
| 198 | + expect(turtle.singer.keySignature).toBe("C major"); |
| 199 | + }); |
| 200 | + |
| 201 | + it("should set singer.beatsPerMeasure to 4", () => { |
| 202 | + turtle.initTurtle(false); |
| 203 | + expect(turtle.singer.beatsPerMeasure).toBe(4); |
| 204 | + }); |
| 205 | + |
| 206 | + it("should set singer.noteValuePerBeat to 4", () => { |
| 207 | + turtle.initTurtle(false); |
| 208 | + expect(turtle.singer.noteValuePerBeat).toBe(4); |
| 209 | + }); |
| 210 | + |
| 211 | + it("should set singer.currentOctave to 4", () => { |
| 212 | + turtle.initTurtle(false); |
| 213 | + expect(turtle.singer.currentOctave).toBe(4); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should set singer.suppressOutput to the passed argument", () => { |
| 217 | + turtle.initTurtle(true); |
| 218 | + expect(turtle.singer.suppressOutput).toBe(true); |
| 219 | + }); |
| 220 | + |
| 221 | + it("should set singer.suppressOutput to false when passed false", () => { |
| 222 | + turtle.initTurtle(false); |
| 223 | + expect(turtle.singer.suppressOutput).toBe(false); |
| 224 | + }); |
| 225 | + |
| 226 | + it("should initialize singer.notesPlayed to [0, 1]", () => { |
| 227 | + turtle.initTurtle(false); |
| 228 | + expect(turtle.singer.notesPlayed).toEqual([0, 1]); |
| 229 | + }); |
| 230 | + |
| 231 | + it("should reset singer.tie to false", () => { |
| 232 | + turtle.singer.tie = true; |
| 233 | + turtle.initTurtle(false); |
| 234 | + expect(turtle.singer.tie).toBe(false); |
| 235 | + }); |
| 236 | + |
| 237 | + it("should reset singer.transposition to 0", () => { |
| 238 | + turtle.initTurtle(false); |
| 239 | + expect(turtle.singer.transposition).toBe(0); |
| 240 | + }); |
| 241 | + |
| 242 | + it("should initialize endOfClampSignals as empty object", () => { |
| 243 | + turtle.initTurtle(false); |
| 244 | + expect(turtle.endOfClampSignals).toEqual({}); |
| 245 | + }); |
| 246 | + |
| 247 | + it("should initialize butNotThese as empty object", () => { |
| 248 | + turtle.initTurtle(false); |
| 249 | + expect(turtle.butNotThese).toEqual({}); |
| 250 | + }); |
| 251 | + }); |
| 252 | +}); |
0 commit comments