|
1 |
| -namespace ConsoleHero.Test.Builders; |
| 1 | +using static ConsoleHero.TuneBuilder; |
| 2 | + |
| 3 | +namespace ConsoleHero.Test.Builders; |
2 | 4 |
|
3 | 5 | [TestClass]
|
4 | 6 | public class TuneBuilderTests
|
5 | 7 | {
|
| 8 | +#pragma warning disable CS8618 //Allow nullable. TestInitialize ensures. |
| 9 | + private Tune _tune; |
| 10 | +#pragma warning restore CS8618 |
| 11 | + |
| 12 | + [TestInitialize] |
| 13 | + public void SetUp() => _tune = Beep(); |
| 14 | + |
| 15 | + private (int tone, int duration) GetFirstNote() |
| 16 | + { |
| 17 | + Assert.IsTrue(_tune.Notes.Count > 0, "No notes were added to the tune."); |
| 18 | + return (_tune.Notes[0].Tone, _tune.Notes[0].Duration); |
| 19 | + } |
| 20 | + |
| 21 | + [TestMethod] |
| 22 | + public void Note_WithToneAndDuration_AddsCorrectNote() |
| 23 | + { |
| 24 | + _tune = Note(Tone.A, Duration.QUARTER).Beep(); |
| 25 | + |
| 26 | + (int tone, int duration) = GetFirstNote(); |
| 27 | + Assert.AreEqual(220, tone); |
| 28 | + Assert.AreEqual(400, duration); |
| 29 | + } |
| 30 | + |
| 31 | + [TestMethod] |
| 32 | + public void Note_WithCustomToneAndDuration_AddsCorrectNote() |
| 33 | + { |
| 34 | + _tune = Note(440, Duration.WHOLE).Beep(); |
| 35 | + |
| 36 | + (int tone, int duration) = GetFirstNote(); |
| 37 | + Assert.AreEqual(440, tone); |
| 38 | + Assert.AreEqual(1600, duration); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void Note_WithToneAndCustomDuration_AddsCorrectNote() |
| 43 | + { |
| 44 | + _tune = Note(Tone.B, 500).Beep(); |
| 45 | + |
| 46 | + (int tone, int duration) = GetFirstNote(); |
| 47 | + Assert.AreEqual(247, tone); |
| 48 | + Assert.AreEqual(500, duration); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void Quarter_WithTone_AddsNoteWithQuarterDuration() |
| 53 | + { |
| 54 | + _tune = Quarter(Tone.G).Beep(); |
| 55 | + |
| 56 | + (int tone, int duration) = GetFirstNote(); |
| 57 | + Assert.AreEqual(392, tone); |
| 58 | + Assert.AreEqual(400, duration); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void Whole_WithTone_AddsNoteWithWholeDuration() |
| 63 | + { |
| 64 | + _tune = Whole(Tone.C).Beep(); |
| 65 | + |
| 66 | + (int tone, int duration) = GetFirstNote(); |
| 67 | + Assert.AreEqual(262, tone); |
| 68 | + Assert.AreEqual(1600, duration); |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void GoTo_WithAction_SetsEffectOnTune() |
| 73 | + { |
| 74 | + bool effectCalled = false; |
| 75 | + Action action = () => effectCalled = true; |
| 76 | + |
| 77 | + Tune tune = Note(Tone.A, Duration.QUARTER).GoTo(action).WaitToPlay(); |
| 78 | + Assert.IsNotNull(tune.Effect); |
| 79 | + |
| 80 | + tune.Effect?.Invoke(); |
| 81 | + Assert.IsTrue(effectCalled, "Action was not called as expected."); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void WaitToPlay_Default_ReturnsTuneWithWaitEnabled() |
| 86 | + { |
| 87 | + _tune = Quarter(Tone.E).WaitToPlay(); |
| 88 | + Assert.IsTrue(_tune.Wait, "Wait should be enabled by default."); |
| 89 | + } |
| 90 | + |
| 91 | + [TestMethod] |
| 92 | + public void ContinueWhilePlaying_SetsWaitToFalse() |
| 93 | + { |
| 94 | + _tune = Quarter(Tone.E).ContinueWhilePlaying(); |
| 95 | + Assert.IsFalse(_tune.Wait, "Wait should be disabled."); |
| 96 | + } |
| 97 | + |
| 98 | + [TestMethod] |
| 99 | + public void MultipleNotes_AddedInSequence() |
| 100 | + { |
| 101 | + _tune = Note(Tone.A, Duration.HALF) |
| 102 | + .Quarter(Tone.C) |
| 103 | + .Eighth(Tone.G) |
| 104 | + .Beep(); |
| 105 | + |
| 106 | + Assert.AreEqual(3, _tune.Notes.Count, "Tune should contain three notes."); |
| 107 | + |
| 108 | + Tune.Note firstNote = _tune.Notes[0]; |
| 109 | + Tune.Note secondNote = _tune.Notes[1]; |
| 110 | + Tune.Note thirdNote = _tune.Notes[2]; |
| 111 | + |
| 112 | + Assert.AreEqual(220, firstNote.Tone); |
| 113 | + Assert.AreEqual(800, firstNote.Duration); |
| 114 | + Assert.AreEqual(262, secondNote.Tone); |
| 115 | + Assert.AreEqual(400, secondNote.Duration); |
| 116 | + Assert.AreEqual(392, thirdNote.Tone); |
| 117 | + Assert.AreEqual(200, thirdNote.Duration); |
| 118 | + } |
6 | 119 | }
|
0 commit comments