Skip to content

Commit 802269d

Browse files
committed
tune builder tests
1 parent 8e1edb7 commit 802269d

File tree

4 files changed

+133
-8
lines changed

4 files changed

+133
-8
lines changed
Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,119 @@
1-
namespace ConsoleHero.Test.Builders;
1+
using static ConsoleHero.TuneBuilder;
2+
3+
namespace ConsoleHero.Test.Builders;
24

35
[TestClass]
46
public class TuneBuilderTests
57
{
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+
}
6119
}

ConsoleHero/Helpers/BeepHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ internal class BeepHelper(IPlatformHelper plateformHelper) : IBeepHelper
66
{
77
private readonly IPlatformHelper _platformHelper = plateformHelper;
88

9-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Required for unit testing")]
109
void IBeepHelper.Beep() => Write("\a");
1110

1211
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Ensured by _platformHelper")]

ConsoleHero/Interfaces/PlatformHelper.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ namespace ConsoleHero.Interfaces;
44

55
internal class PlatformHelper : IPlatformHelper
66
{
7-
bool IPlatformHelper.IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
8-
bool IPlatformHelper.IsLinux => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
9-
bool IPlatformHelper.IsOSX => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
7+
internal PlatformHelper()
8+
{
9+
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
10+
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
11+
IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
12+
}
13+
14+
internal bool IsWindows { get; }
15+
internal bool IsLinux { get; }
16+
internal bool IsOSX { get; }
17+
18+
bool IPlatformHelper.IsWindows => IsWindows;
19+
20+
bool IPlatformHelper.IsLinux => IsLinux;
21+
22+
bool IPlatformHelper.IsOSX => IsOSX;
1023
}

ConsoleHero/Tune.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ internal Tune() { }
1919

2020
internal readonly struct Note(int frequency, int time)
2121
{
22-
public int NoteTone { get; } = frequency;
23-
public int NoteDuration { get; } = time;
22+
public int Tone { get; } = frequency;
23+
public int Duration { get; } = time;
2424
}
2525

2626
internal void Play()
@@ -30,7 +30,7 @@ internal void Play()
3030

3131
foreach (Note item in Notes)
3232
{
33-
_beepHelper.Beep(item.NoteTone, item.NoteDuration);
33+
_beepHelper.Beep(item.Tone, item.Duration);
3434
}
3535
}
3636

0 commit comments

Comments
 (0)