Skip to content

Commit 0b2f710

Browse files
committed
Abstracted beeps for testing.
1 parent c46f40c commit 0b2f710

File tree

8 files changed

+125
-8
lines changed

8 files changed

+125
-8
lines changed

ConsoleHero.Test/BeepHelperTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using ConsoleHero.Helpers;
2+
using ConsoleHero.Interfaces;
3+
using Moq;
4+
5+
namespace ConsoleHero.Test;
6+
7+
[TestClass]
8+
public class BeepHelperTests
9+
{
10+
#pragma warning disable CS8618 //Allow nullable. TestInitialize ensures.
11+
private Mock<IPlatformHelper> _platformHelperMock;
12+
private BeepHelper _beepHelper;
13+
#pragma warning restore CS8618
14+
15+
[TestInitialize]
16+
public void TestInitialize()
17+
{
18+
_platformHelperMock = new Mock<IPlatformHelper>();
19+
_beepHelper = new BeepHelper(_platformHelperMock.Object);
20+
}
21+
22+
[TestMethod]
23+
public void Beep_NoParameters_WritesBellCharacter()
24+
{
25+
using ConsoleOutput consoleOutput = new();
26+
27+
BeepHelper.Beep();
28+
29+
// Assert
30+
Assert.AreEqual("\a", consoleOutput.GetOutput());
31+
}
32+
33+
[TestMethod]
34+
public void Beep_LinuxPlatform_StartsProcess()
35+
{
36+
_platformHelperMock.Setup(p => p.IsWindows).Returns(false);
37+
_platformHelperMock.Setup(p => p.IsLinux).Returns(true);
38+
39+
// Act
40+
_beepHelper.Beep(1000, 500);
41+
42+
// Assert
43+
// Add appropriate assertions for process calls if applicable.
44+
}
45+
46+
[TestMethod]
47+
public void Beep_MacOSPlatform_StartsProcess()
48+
{
49+
_platformHelperMock.Setup(p => p.IsWindows).Returns(false);
50+
_platformHelperMock.Setup(p => p.IsOSX).Returns(true);
51+
52+
// Act
53+
_beepHelper.Beep(1000, 500);
54+
55+
// Assert
56+
// Add appropriate assertions for process calls if applicable.
57+
}
58+
59+
[TestMethod]
60+
public void Beep_NonWindowsPlatformWithException_WritesBellCharacter()
61+
{
62+
_platformHelperMock.Setup(p => p.IsWindows).Returns(false);
63+
_platformHelperMock.Setup(p => p.IsLinux).Returns(true);
64+
using ConsoleOutput consoleOutput = new();
65+
66+
// Act
67+
_beepHelper.Beep(1000, 500);
68+
69+
// Assert
70+
Assert.AreEqual("\a", consoleOutput.GetOutput());
71+
}
72+
}

ConsoleHero.Test/HelperTests.cs renamed to ConsoleHero.Test/ColorHelperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ConsoleHero.Test;
44

55
[TestClass]
6-
public class HelperTests
6+
public class ColorHelperTests
77
{
88
private readonly StringWriter _consoleOutput = new();
99

ConsoleHero.Test/ConsoleOutput.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace ConsoleHero.Test;
2+
public class ConsoleOutput : IDisposable
3+
{
4+
private readonly StringWriter _stringWriter;
5+
private readonly TextWriter _originalOutput;
6+
7+
public ConsoleOutput()
8+
{
9+
_stringWriter = new StringWriter();
10+
_originalOutput = Console.Out;
11+
Console.SetOut(_stringWriter);
12+
}
13+
14+
public string GetOutput() => _stringWriter.ToString();
15+
16+
public void Dispose()
17+
{
18+
GC.SuppressFinalize(this);
19+
Console.SetOut(_originalOutput);
20+
_stringWriter.Dispose();
21+
}
22+
}

ConsoleHero/AssemblyInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
using System.Runtime.CompilerServices;
22

3-
[assembly: InternalsVisibleTo("ConsoleHero.Test")]
3+
[assembly: InternalsVisibleTo("ConsoleHero.Test")]
4+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

ConsoleHero/Helpers/BeepHelper.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
using System.Runtime.InteropServices;
1+
using ConsoleHero.Interfaces;
22

33
namespace ConsoleHero.Helpers;
44

5-
internal static class BeepHelper
5+
internal class BeepHelper(IPlatformHelper plateformHelper)
66
{
7+
private readonly IPlatformHelper _platformHelper = plateformHelper;
8+
79
internal static void Beep() => Write("\a");
810

9-
internal static void Beep(int frequency, int duration)
11+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Ensured by _platformHelper")]
12+
internal void Beep(int frequency, int duration)
1013
{
11-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
14+
if (_platformHelper.IsWindows)
1215
{
1316
Console.Beep(frequency, duration);
1417
}
15-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
18+
else if (_platformHelper.IsLinux || _platformHelper.IsOSX)
1619
{
1720
try
1821
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ConsoleHero.Interfaces;
2+
internal interface IPlatformHelper
3+
{
4+
bool IsWindows { get; }
5+
bool IsLinux { get; }
6+
bool IsOSX { get; }
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace ConsoleHero.Interfaces;
4+
5+
internal class PlatformHelper : IPlatformHelper
6+
{
7+
public bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
8+
public bool IsLinux => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
9+
public bool IsOSX => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
10+
}

ConsoleHero/Tune.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ConsoleHero.Helpers;
2+
using ConsoleHero.Interfaces;
23

34
namespace ConsoleHero;
45

@@ -9,6 +10,7 @@ namespace ConsoleHero;
910
/// </summary>
1011
public record Tune : INode
1112
{
13+
internal BeepHelper _beepHelper = new(new PlatformHelper());
1214
internal Tune() { }
1315
internal List<Note> Notes { get; set; } = [];
1416
internal bool Wait { get; set; } = true;
@@ -28,7 +30,7 @@ internal void Play()
2830

2931
foreach (Note item in Notes)
3032
{
31-
BeepHelper.Beep(item.NoteTone, item.NoteDuration);
33+
_beepHelper.Beep(item.NoteTone, item.NoteDuration);
3234
}
3335
}
3436

0 commit comments

Comments
 (0)