Skip to content

Commit

Permalink
Added a dummy unit test to detect the game category
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Dec 4, 2022
1 parent 67fb6b3 commit 7f85190
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
12 changes: 9 additions & 3 deletions FacepunchCommitsMonitor.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31410.357
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacepunchCommitsMonitor", "src/FacepunchCommitsMonitor.csproj", "{D4E06CA7-DF8D-4833-BD8A-072C0771D8C6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FacepunchCommitsMonitor", "src\FacepunchCommitsMonitor.csproj", "{D4E06CA7-DF8D-4833-BD8A-072C0771D8C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacepunchCommitsMonitorTests", "tests\FacepunchCommitsMonitorTests.csproj", "{A71B1881-7607-400C-A0F7-EB3C0D31DB63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{D4E06CA7-DF8D-4833-BD8A-072C0771D8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4E06CA7-DF8D-4833-BD8A-072C0771D8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4E06CA7-DF8D-4833-BD8A-072C0771D8C6}.Release|Any CPU.Build.0 = Release|Any CPU
{A71B1881-7607-400C-A0F7-EB3C0D31DB63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A71B1881-7607-400C-A0F7-EB3C0D31DB63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A71B1881-7607-400C-A0F7-EB3C0D31DB63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A71B1881-7607-400C-A0F7-EB3C0D31DB63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 10 additions & 4 deletions src/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public partial class Form : System.Windows.Forms.Form
private const string directory = "images";
private const string fallbackAvatar = "https://files.facepunch.com/garry/f549bfc2-2a49-4eb8-a701-3efd7ae046ac.png";

/// <summary>
/// A regex which matches the numbers "0" to "9" atomically at least once.
/// </summary>
[GeneratedRegex("[0-9]+")]
private static partial Regex FindNumbers();

/// <summary>
/// Initialize the form and all its components.
/// </summary>
Expand Down Expand Up @@ -122,11 +128,11 @@ private void SafeInvoke(Control element, Action callback)
private void ActionTimer_Tick(object sender, EventArgs e)
{
// Remaining time text
var remainingTime = Math.Round((TimeSpan.FromMilliseconds(IntervalTime) - (DateTime.Now - Program.StartTime)).TotalSeconds);
var remainingTime = Math.Round((TimeSpan.FromMilliseconds(IntervalTime) - (DateTime.Now - Monitor.StartTime)).TotalSeconds);

SafeInvoke(label6, new Action(() =>
{
label6.Text = Regex.Replace(label6.Text, "[0-9]+", Math.Max(remainingTime, 0).ToString());
label6.Text = FindNumbers().Replace(label6.Text, Math.Max(remainingTime, 0).ToString());
}));

// Remaining time progress bar
Expand Down Expand Up @@ -234,8 +240,8 @@ private void NumericUpDown1_ValueChanged(object sender, EventArgs e)
{
IntervalTime = (double)numericUpDown1.Value * 1000;

Program.StartTime = DateTime.Now;
Program.CheckTimer.Interval = IntervalTime;
Monitor.StartTime = DateTime.Now;
Monitor.CheckTimer.Interval = IntervalTime;
}
}
}
6 changes: 3 additions & 3 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Commit(string identifier, string category, string repository, string bran
}
}

internal class Program
public class Monitor
{
public static DateTime StartTime { get; set; }
public static Timer CheckTimer { get; set; } = new();
Expand Down Expand Up @@ -72,7 +72,7 @@ private static async Task Main()
/// Gives the name of the category associated with the commit repository.
/// This also serves as a check to see if the game needs to be monitored.
/// </summary>
private static string SelectGameCategory(string repository)
public static string SelectGameCategory(string repository)
{
if (repository.Contains("Garrys") && Form.Repositories["Garry's Mod"])
return "Garry's Mod";
Expand All @@ -81,7 +81,7 @@ private static string SelectGameCategory(string repository)
return "Rust";

if (repository.Contains("sbox") && Form.Repositories["Sandbox"])
return "Sandbox";
return "S&Box";

return "N/A";
}
Expand Down
20 changes: 20 additions & 0 deletions tests/FacepunchCommitsMonitorTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework> <!-- Windows 11 -->
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion> <!-- Windows 10 version 2004 (May 2020 Update) -->
<PlatformTarget>x64</PlatformTarget>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\FacepunchCommitsMonitor.csproj" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions tests/MonitorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FacepunchCommitsMonitor.Tests;

[TestClass]
public class MonitorTests
{
[TestMethod]
public void SelectGameCategoryTest()
{
var gmod = Monitor.SelectGameCategory("GarrysMod2");
var rust = Monitor.SelectGameCategory("Rustify");
var sbox = Monitor.SelectGameCategory("SboxUnreal");

Assert.AreEqual("Garry's Mod", gmod);
Assert.AreEqual("Rust", rust);
Assert.AreEqual("S&Box", sbox);
}
}

0 comments on commit 7f85190

Please sign in to comment.