Skip to content

Commit

Permalink
#47 Adventure & Support test
Browse files Browse the repository at this point in the history
Some refactoring to make testing easier & remove hard-coded initialisations
Basic tests(Well test) for the adventure and support module
  • Loading branch information
David032 committed Mar 25, 2024
1 parent 9d436b1 commit dc51edd
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 13 deletions.
28 changes: 28 additions & 0 deletions ModuleTests/AdventureTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Moq;
using skullOS.Core;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;

namespace ModuleTests
{
public class AdventureTest
{
public AdventureTest()
{
FileManager.CreateSkullDirectory(true, true);
}

[Fact]
public void CanCreateAdventureModule()
{
//string now = DateTime.Now.ToString("M");
//var directory = FileManager.GetSkullDirectory() + @"/Timelapse - " + now + @"/";
Mock<ICameraService> cameraMock = new();
cameraMock.Setup(camera => camera.TakePictureAsync(It.IsAny<string>())).Returns(Task.FromResult("Pass"));

Adventure sut = new(cameraMock.Object);

Assert.NotNull(sut);
}
}
}
9 changes: 8 additions & 1 deletion ModuleTests/ModuleTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -22,4 +23,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\skullOS.Core\skullOS.Core.csproj" />
<ProjectReference Include="..\skullOS.HardwareServices\skullOS.HardwareServices.csproj" />
<ProjectReference Include="..\skullOS.Modules\skullOS.Modules.csproj" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions ModuleTests/SupportTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Moq;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;
using System.Device.Gpio;

namespace ModuleTests
{
public class SupportTest
{
[Fact]
public void CanCreateSupportModule()
{
Mock<ISpeakerService> speakerMock = new();
speakerMock.Setup(speaker => speaker.PlayAudio(It.IsAny<string>())).Returns(Task.CompletedTask);
Mock<GpioController> gpioMock = new();
gpioMock.Setup(gpio => gpio.OpenPin(It.IsAny<int>()));
gpioMock.Setup(gpio => gpio.RegisterCallbackForPinValueChangedEvent(It.IsAny<int>(), PinEventTypes.Rising, It.IsNotNull<PinChangeEventHandler>()));

var SupportModule = new Support(gpioMock.Object, speakerMock.Object, 4);

Assert.NotNull(SupportModule);
}
}
}
6 changes: 4 additions & 2 deletions ServoSkull.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.HardwareServices",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServoSkullDemo", "MiscFiles\ServoSkullDemo\ServoSkullDemo.csproj", "{0B0F89D7-32B6-4E20-B676-44F70E7B5352}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModuleTests", "ModuleTests\ModuleTests.csproj", "{356018AA-0F2A-48D2-B263-62D52906053F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModuleTests", "ModuleTests\ModuleTests.csproj", "{356018AA-0F2A-48D2-B263-62D52906053F}"
ProjectSection(ProjectDependencies) = postProject
{3CBE1B63-957B-43B6-939D-070EAC517C7C} = {3CBE1B63-957B-43B6-939D-070EAC517C7C}
{464B162B-62FC-49CC-A6AB-72DBE959DD8C} = {464B162B-62FC-49CC-A6AB-72DBE959DD8C}
{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56} = {DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreTests", "CoreTests\CoreTests.csproj", "{B5CE836D-7EEE-4D59-81AF-A29399CF6BC6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreTests", "CoreTests\CoreTests.csproj", "{B5CE836D-7EEE-4D59-81AF-A29399CF6BC6}"
ProjectSection(ProjectDependencies) = postProject
{464B162B-62FC-49CC-A6AB-72DBE959DD8C} = {464B162B-62FC-49CC-A6AB-72DBE959DD8C}
EndProjectSection
Expand Down
18 changes: 15 additions & 3 deletions skullOS.Modules/Adventure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ public class Adventure : Module, IAdventure
double interval = 30000;
ICameraService cameraService;

public Adventure()
public Adventure(ICameraService camService = null)

Check warning on line 17 in skullOS.Modules/Adventure.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
{
var now = DateTime.Now.ToString("M");
string now = DateTime.Now.ToString("M");
FileManager.CreateSubDirectory("Timelapse - " + now);
directory = FileManager.GetSkullDirectory() + @"/Timelapse - " + now + @"/";
cameraService = new CameraService();
if (camService == null)
{
cameraService = new CameraService();
}
else
{
cameraService = camService;
}


takePicture = new Timer(interval);
Expand All @@ -33,6 +40,11 @@ private void TakePicture_Elapsed(object? sender, System.Timers.ElapsedEventArgs
cameraService.TakePictureAsync(directory);
}

public static Timer GetTimelapseController()
{
return takePicture;

Check warning on line 45 in skullOS.Modules/Adventure.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Possible null reference return.
}

public override void OnAction(object? sender, EventArgs e)
{
throw new OnActionException("Adventure does not support OnAction");
Expand Down
11 changes: 9 additions & 2 deletions skullOS.Modules/Buzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ public class Buzzer : Module, IBuzzerModule
{
BuzzerService PwmBuzzer;
MelodyPlayer Player;
public Buzzer()
public Buzzer(BuzzerService buzzerService = null, int pwmPin = 13)

Check warning on line 15 in skullOS.Modules/Buzzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
{
PwmBuzzer = new BuzzerService(13);
if (buzzerService == null)
{
PwmBuzzer = new BuzzerService(pwmPin);
}
else
{
PwmBuzzer = buzzerService;
}
Player = new MelodyPlayer(PwmBuzzer.Buzzer);
}

Expand Down
26 changes: 21 additions & 5 deletions skullOS.Modules/Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ public class Support : Module, ISupport
GpioController controller;
ISpeakerService speakerService;
static Timer? LowBatteryAlert;
double interval = 60000;
int pin = 4;
readonly double interval = 60000;
int pin; //Defaults to Pimoroni's Lipo shim,

public Support()
public Support(GpioController gpioController = null, ISpeakerService speaker = null, int signalPin = 4)

Check warning on line 19 in skullOS.Modules/Support.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 19 in skullOS.Modules/Support.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
{
pin = signalPin;
LowBatteryAlert = new Timer(interval)
{
AutoReset = true,
};
LowBatteryAlert.Elapsed += LowBatteryAlert_Elapsed;

speakerService = new SpeakerService();
controller = new GpioController();
if (speaker == null)
{
speakerService = new SpeakerService();
}
else
{
speakerService = speaker;
}

if (gpioController == null)
{
controller = new GpioController();
}
else
{
controller = gpioController;
}
controller.OpenPin(pin);
controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Rising, OnLowBattery);
}
Expand Down

0 comments on commit dc51edd

Please sign in to comment.