Skip to content

Commit

Permalink
Merge pull request #56 from David032/47-module-tests
Browse files Browse the repository at this point in the history
#47 Module Tests added
  • Loading branch information
David032 authored Apr 3, 2024
2 parents 7ac5adf + 1b932f3 commit c1f494e
Show file tree
Hide file tree
Showing 42 changed files with 812 additions and 165 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ on:
jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -25,4 +26,14 @@
<ProjectReference Include="..\skullOS.Core\skullOS.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="TestData\" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\TestSettings.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions CoreTests/FileManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CoreTests
{
public class FileManagerTests
{

}
}
File renamed without changes.
12 changes: 12 additions & 0 deletions CoreTests/LoggerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreTests
{
internal class LoggerTests
{
}
}
17 changes: 17 additions & 0 deletions CoreTests/SettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using skullOS.Core;

namespace CoreTests
{
public class SettingsTests
{
[Fact]
public void TestSettingsLoader()
{
var result = SettingsLoader.LoadConfig(@"TestData/TestSettings.txt");
Assert.NotNull(result);
Assert.Single(result);
result.TryGetValue("Worked", out string response);
Assert.Equal("True", response);
}
}
}
1 change: 1 addition & 0 deletions CoreTests/TestData/TestSettings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Worked=True
51 changes: 51 additions & 0 deletions ModuleTests/AdventureTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Moq;
using skullOS.Core;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;
using skullOS.Modules.Exceptions;

namespace ModuleTests
{
public class AdventureTest
{
Mock<ICameraService> cameraMock;
Adventure sut;
public AdventureTest()
{
FileManager.CreateSkullDirectory(false, true);

cameraMock = new();
cameraMock.Setup(camera => camera.TakePictureAsync(It.IsAny<string>())).Returns(Task.FromResult("Pass"));
sut = new(cameraMock.Object);
}

[Fact]
public void CanCreateAdventureModule()
{
Assert.NotNull(sut);
}
[Fact]
public void OnEnableThrowsException()
{
Assert.Throws<OnEnableException>(() => sut.OnEnable(It.IsAny<string[]>()));
}
[Fact]
public void OnActionThrowsException()
{
Assert.Throws<OnActionException>(() => sut.OnAction(It.IsAny<object>(), It.IsAny<EventArgs>()));
}
[Fact]
public void NameReturnsCorrect()
{
Assert.Equal("Adventure", sut.ToString());
}

//Timelapse settings should probably be moved to read from file?
[Fact]
public void TimerHasCorrectDuration()
{
var timer = sut.GetTimelapseController();
Assert.Equal(30000, timer.Interval);
}
}
}
54 changes: 54 additions & 0 deletions ModuleTests/BuzzerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Moq;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;
using skullOS.Modules.Exceptions;
using skullOS.Modules.Interfaces;

namespace ModuleTests
{
public class BuzzerTest
{
Buzzer sut;
Mock<IMelodyPlayer> buzzerPlayer;

public BuzzerTest()
{
Mock<IBuzzerService> buzzerHardware = new();
buzzerPlayer = new();
buzzerPlayer.Setup(x => x.Play(It.IsAny<IList<MelodyElement>>(), It.IsAny<int>())).Verifiable();

sut = new Buzzer(buzzerHardware.Object, 13, buzzerPlayer.Object);
}

[Fact]
public void CanCreateBuzzer()
{
Assert.NotNull(sut);
}

[Fact]
public void CanPlayTune()
{
sut.PlayTune(BuzzerLibrary.Tunes.AlphabetSong);

Mock.Verify([buzzerPlayer]);
}

[Fact]
public void NameReturnsCorrect()
{
Assert.Equal("Buzzer", sut.ToString());
}

[Fact]
public void OnEnableThrowsException()
{
Assert.Throws<OnEnableException>(() => sut.OnEnable(It.IsAny<string[]>()));
}
[Fact]
public void OnActionThrowsException()
{
Assert.Throws<OnActionException>(() => sut.OnAction(It.IsAny<object>(), It.IsAny<EventArgs>()));
}
}
}
44 changes: 44 additions & 0 deletions ModuleTests/CameraTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Moq;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;

namespace ModuleTests
{
public class CameraTest
{
Mock<ICameraService> camMock = new();
Mock<IMicrophoneService> micMock = new();
Mock<IBuzzerService> buzzerMock = new();
Mock<ISpeakerService> speakerMock = new();
Mock<ILedService> LedMock = new();

[Fact]
public void CanCreate()
{
Camera sut = new Camera(camMock.Object, micMock.Object, speakerMock.Object, LedMock.Object, buzzerMock.Object, @"TestData/CameraSettings.txt");
Assert.NotNull(sut);
}

[Fact]
public async void CanTakePicture()
{
camMock.Setup(camera => camera.TakePictureAsync(It.IsAny<string>())).Returns(Task.FromResult("Pass")).Verifiable();

Camera sut = new Camera(camMock.Object, micMock.Object, speakerMock.Object, LedMock.Object, buzzerMock.Object, @"TestData/CameraSettings.txt");

await sut.TakePicture();
Mock.Verify(camMock);
}
[Fact]
public async void CanRecordVideo()
{
camMock.Setup(camera =>
camera.RecordShortVideoAsync(It.IsAny<string>(), It.IsAny<bool>())).Returns(Task.FromResult("Pass")).Verifiable();

Camera sut = new Camera(camMock.Object, micMock.Object, speakerMock.Object, LedMock.Object, buzzerMock.Object, @"TestData/CameraSettings.txt");

await sut.RecordShortVideo();
Mock.Verify(camMock);
}
}
}
1 change: 1 addition & 0 deletions ModuleTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
43 changes: 43 additions & 0 deletions ModuleTests/ModuleTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<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>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

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

<ItemGroup>
<Folder Include="TestData\" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\PropSettings.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions ModuleTests/PropTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Moq;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;
using skullOS.Modules.Exceptions;

namespace ModuleTests
{
public class PropTest
{
Prop sut;
string pathToTestData = @"TestData/PropSettings.txt";

public PropTest()
{
Mock<ISpeakerService> speakerMock = new();
speakerMock.Setup(speaker => speaker.PlayAudio(It.IsAny<string>())).Returns(Task.CompletedTask);
Mock<ILedService> ledMock = new();

sut = new Prop(speakerMock.Object, ledMock.Object, pathToTestData);
}

[Fact]
public void CanCreateProp()
{
Assert.NotNull(sut);
}

[Fact]
public void NameReturnsCorrect()
{
Assert.Equal("Prop", sut.ToString());
}

[Fact]
public void OnEnableThrowsException()
{
Assert.Throws<OnEnableException>(() => sut.OnEnable(It.IsAny<string[]>()));
}
[Fact]
public void OnActionThrowsException()
{
Assert.Throws<OnActionException>(() => sut.OnAction(It.IsAny<object>(), It.IsAny<EventArgs>()));
}
}
}
44 changes: 44 additions & 0 deletions ModuleTests/SupportTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Moq;
using skullOS.HardwareServices.Interfaces;
using skullOS.Modules;
using skullOS.Modules.Exceptions;
using skullOS.Tests;
using System.Device.Gpio;

namespace ModuleTests
{
public class SupportTest
{
Support sut;
public SupportTest()
{
Mock<ISpeakerService> speakerMock = new();
speakerMock.Setup(speaker => speaker.PlayAudio(It.IsAny<string>())).Returns(Task.CompletedTask);
Mock<MockableGpioDriver> gpioMock = new();
var ctrlr = new GpioController(PinNumberingScheme.Logical, gpioMock.Object);

sut = new Support(ctrlr, speakerMock.Object, 4);
}

[Fact]
public void CanCreateSupportModule()
{
Assert.NotNull(sut);
}
[Fact]
public void SupportOnEnableThrowsException()
{
Assert.Throws<OnEnableException>(() => sut.OnEnable(It.IsAny<string[]>()));
}
[Fact]
public void SupportOnActionThrowsException()
{
Assert.Throws<OnActionException>(() => sut.OnAction(It.IsAny<object>(), It.IsAny<EventArgs>()));
}
[Fact]
public void SupportReturnsCorrectName()
{
Assert.Equal("Support", sut.ToString());
}
}
}
Loading

0 comments on commit c1f494e

Please sign in to comment.