Created
March 28, 2025 16:19
-
-
Save trikitrok/50d01545aa0f930b82af43e169f19d1a to your computer and use it in GitHub Desktop.
No mutants surviving (legacy version)
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
import {Alarm} from "../src/Alarm"; | |
describe("Alarm", () => { | |
const lowestSafePressure: number = 17; | |
const highestSafePressure: number = 21; | |
const tooLowPressure: number = lowestSafePressure - 1; | |
const tooHighPressure: number = highestSafePressure + 1; | |
let alarm: AlarmForTesting; | |
it("activates when sampled pressure is too low", () => { | |
alarm = anAlarmThatSamples(tooLowPressure); | |
alarm.check(); | |
expectThatDisplayedMessagesWere("Alarm activated!"); | |
}); | |
it.each([ | |
[lowestSafePressure], | |
[highestSafePressure] | |
])("does not activate when sampled pressure is safe (sampledPressure: %s)", (sampledPressure: number) => { | |
alarm = anAlarmThatSamples(sampledPressure); | |
alarm.check(); | |
expectThatThereWereNoDisplayedMessages(); | |
}); | |
it("activates when sampled pressure is too high", () => { | |
alarm = anAlarmThatSamples(tooHighPressure); | |
alarm.check(); | |
expectThatDisplayedMessagesWere("Alarm activated!"); | |
}); | |
it("deactivates after being activated when sampled pressure is safe", () => { | |
alarm = anAlarmThatSamples(tooHighPressure, lowestSafePressure); | |
alarm.check(); | |
alarm.check(); | |
expectThatDisplayedMessagesWere("Alarm activated!", "Alarm deactivated!"); | |
}); | |
it("activates after having been activated and deactivated when sampled pressure is unsafe again", () => { | |
alarm = anAlarmThatSamples(tooHighPressure, lowestSafePressure, tooLowPressure); | |
alarm.check(); | |
alarm.check(); | |
alarm.check(); | |
expectThatDisplayedMessagesWere("Alarm activated!", "Alarm deactivated!", "Alarm activated!"); | |
}); | |
it("does not activate when already activated", () => { | |
alarm = anAlarmThatSamples(tooHighPressure, tooLowPressure); | |
alarm.check(); | |
alarm.check(); | |
expectThatDisplayedMessagesWere("Alarm activated!"); | |
}); | |
function anAlarmThatSamples(...pressureValues: number[]): AlarmForTesting { | |
return new AlarmForTesting(...pressureValues); | |
} | |
function expectThatDisplayedMessagesWere(...messages: string[]): void { | |
expect(alarm.displayedMessages.length).toBe(messages.length); | |
for (let i = 0; i < messages.length; i++) { | |
expect(alarm.displayedMessages[i]).toBe(messages[i]); | |
} | |
} | |
function expectThatThereWereNoDisplayedMessages(): void { | |
expectThatDisplayedMessagesWere(); | |
} | |
}); | |
class AlarmForTesting extends Alarm { | |
private readonly sampledValues: number[]; | |
public readonly displayedMessages: string[]; | |
constructor(...pressureValues: number[]) { | |
super(); | |
this.sampledValues = [...pressureValues]; | |
this.displayedMessages = []; | |
} | |
protected override sampleValue(): number { | |
const value = this.sampledValues.shift(); | |
if(!value) { | |
throw new Error("Not enough sampled values for this test!"); | |
} | |
return value; | |
} | |
protected override display(message: string): void { | |
this.displayedMessages.push(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment