Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/unit tests #6

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pomodoro.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pomodoro.Core", "src\Pomodo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pomodoro.CLI", "src\Pomodoro.CLI\Pomodoro.CLI.csproj", "{DC908E78-1F9F-4BFB-A109-0841414AA515}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{22E54916-32AA-4656-A80A-C21F35F7AD92}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pomodoro.UnitTests", "test\Pomodoro.UnitTests\Pomodoro.UnitTests.csproj", "{F291F25F-C03F-4BEA-853C-177BAB505932}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,9 +30,14 @@ Global
{DC908E78-1F9F-4BFB-A109-0841414AA515}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC908E78-1F9F-4BFB-A109-0841414AA515}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC908E78-1F9F-4BFB-A109-0841414AA515}.Release|Any CPU.Build.0 = Release|Any CPU
{F291F25F-C03F-4BEA-853C-177BAB505932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F291F25F-C03F-4BEA-853C-177BAB505932}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F291F25F-C03F-4BEA-853C-177BAB505932}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F291F25F-C03F-4BEA-853C-177BAB505932}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0455894E-E897-455B-976B-EFF4F2C105D8} = {CCDCD0F8-510F-413D-989C-037908089053}
{DC908E78-1F9F-4BFB-A109-0841414AA515} = {CCDCD0F8-510F-413D-989C-037908089053}
{F291F25F-C03F-4BEA-853C-177BAB505932} = {22E54916-32AA-4656-A80A-C21F35F7AD92}
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions src/Pomodoro.Core/Entities/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public TimeSpan TaskTotalElapsedTime() =>
public void AddClock(Clock newClock)
{
_clocks.Add(newClock);

AmountOfTimersToBeExecuted++;
CreatedTimersCount++;
}
}
28 changes: 15 additions & 13 deletions src/Pomodoro.Core/ValueObjects/ClockTimer/ClockTimerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
using System.Timers;
using Pomodoro.Core.Entities;

public abstract class ClockTimerState(Timer timer)
public abstract class ClockTimerState(Timer timer, Clock clock)
{
protected Timer _timer = timer;

protected Clock _clock = clock;

public event EventHandler<ElapsedEventArgs> OnElapsed = (
object? sender,
ElapsedEventArgs eventArgs
) => { };
) =>
{ };

public abstract void Start();

Expand All @@ -20,20 +23,19 @@ ElapsedEventArgs eventArgs

public abstract TimeSpan RemainingTime();

protected virtual void OnTimerElapsed(
protected void OnTimerElapsed(
object? sender,
ElapsedEventArgs eventArgs
) => OnElapsed.Invoke(sender, eventArgs);
}

public class WorkState : ClockTimerState
{
private readonly Clock _clock;

public WorkState(Clock clock) : base(new Timer(clock.WorkInterval.Duration))
public WorkState(Clock clock) : base(
new Timer(clock.WorkInterval.Duration),
clock
)
{
_clock = clock;
_timer = new Timer(_clock.WorkInterval.Duration);
_timer.Elapsed += OnWorkPeriodEnded;
}

Expand Down Expand Up @@ -76,12 +78,12 @@ private void OnWorkPeriodEnded(object? source, ElapsedEventArgs eventArgs)

public class BreakState : ClockTimerState
{
private readonly Clock _clock;

public BreakState(Clock clock) : base(new Timer(clock.WorkInterval.Duration))
public BreakState(Clock clock) :
base(
new Timer(clock.WorkInterval.Duration),
clock
)
{
_clock = clock;
_timer = new Timer(_clock.BreakInterval.Duration);
_timer.Elapsed += OnBreakPeriodEnded;
}

Expand Down
1 change: 1 addition & 0 deletions test/Pomodoro.UnitTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
29 changes: 29 additions & 0 deletions test/Pomodoro.UnitTests/Pomodoro.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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="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="..\..\src\Pomodoro.Core\Pomodoro.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using Pomodoro.Core.Entities;
using Pomodoro.Core.ValueObjects.ClockTimer;
using Task = System.Threading.Tasks.Task;

namespace Pomodoro.UnitTests.ValueObjects.ClockTimer;

public class ClockTimerStateTests
{
public class Setup
{
public readonly Clock Clock;

public readonly Settings Settings;

public Setup()
{
Settings = new Settings();
Clock = new Clock(Settings.DefaultWorkTime, Settings.DefaultBreakTime, Settings);
}
}

public class WorkStateTests : IClassFixture<Setup>
{
private readonly Setup Setup;

private readonly WorkState WorkState;

public WorkStateTests(Setup setup)
{
Setup = setup;
WorkState = new WorkState(Setup.Clock);
}

[Fact]
public void Start_WorkState()
{
var clock = Setup.Clock;
var clockTimerWorkState = WorkState;

clockTimerWorkState.Start();

Assert.True(clock.WorkInterval.IsActive);
Assert.False(clock.WorkInterval.IsCompleted);
Assert.False(clock.BreakInterval.IsActive);
Assert.False(clock.BreakInterval.IsCompleted);
}

[Fact]
public void Stop_WorkState()
{
var clock = Setup.Clock;
var clockTimerWorkState = WorkState;

clockTimerWorkState.Start();

Assert.True(clock.WorkInterval.IsActive);
Assert.False(clock.WorkInterval.IsCompleted);
Assert.False(clock.BreakInterval.IsActive);
Assert.False(clock.BreakInterval.IsCompleted);

clockTimerWorkState.Stop();

Assert.False(clock.WorkInterval.IsActive);
Assert.False(clock.WorkInterval.IsCompleted);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
public async void Elapsed_Time_Should_Always_Be_Lesser_Than_Work_Duration(int seconds)
{
var clock = new Clock(
Settings.DefaultWorkTime,
Settings.DefaultBreakTime,
Setup.Settings
);
var clockTimerWorkState = new WorkState(clock);

clockTimerWorkState.Start();

await Task.Delay(TimeSpan.FromSeconds(seconds));

var elapsedTime = clockTimerWorkState.ElapsedTime();

Assert.True(elapsedTime <= clock.WorkInterval.Duration);
}

[Theory]
[InlineData(6)]
public async void Elapsed_Time_Should_Be_Set_To_Zero_When_It_is_Greater_Than_Work_Duration(int seconds)
{
var clockTimerWorkState = WorkState;

clockTimerWorkState.Start();

await Task.Delay(TimeSpan.FromSeconds(seconds));

var elapsedTime = clockTimerWorkState.ElapsedTime();

Assert.Equal(TimeSpan.Zero, elapsedTime);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
public async void Remaining_Time_Should_Be_Always_Less_Than_Work_Duration(int seconds)
{
var clockTimerWorkState = WorkState;

clockTimerWorkState.Start();

await Task.Delay(TimeSpan.FromSeconds(seconds));

var remainingTime = clockTimerWorkState.RemainingTime();

Assert.True(remainingTime <= Setup.Clock.WorkInterval.Duration);
}
}

public class BreakStateTests : IClassFixture<Setup>
{
private readonly Setup Setup;

private readonly BreakState BreakState;

public BreakStateTests(Setup setup)
{
Setup = setup;
BreakState = new BreakState(Setup.Clock);
}

[Fact]
public void Start_BreakState()
{
var clock = Setup.Clock;
var clockTimerBreakState = BreakState;

clockTimerBreakState.Start();

Assert.True(clock.BreakInterval.IsActive);
Assert.False(clock.BreakInterval.IsCompleted);
Assert.False(clock.WorkInterval.IsCompleted);
Assert.False(clock.WorkInterval.IsActive);
}

[Fact]
public void Stop_BreakState()
{
var clock = Setup.Clock;
var clockTimerBreakState = BreakState;

clockTimerBreakState.Start();

Assert.True(clock.BreakInterval.IsActive);
Assert.False(clock.BreakInterval.IsCompleted);
Assert.False(clock.WorkInterval.IsActive);
Assert.False(clock.WorkInterval.IsCompleted);

clockTimerBreakState.Stop();

Assert.False(clock.BreakInterval.IsActive);
Assert.False(clock.BreakInterval.IsCompleted);
}
}
}