Skip to content

Commit

Permalink
Finished unit testing, updated icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirdoombox committed Mar 11, 2020
1 parent a6937d6 commit f08e6a3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,81 @@
using System.Collections.Generic;
using NUnit.Framework;
using PostScriptumMortarCalculator.Extensions;
using PostScriptumMortarCalculator.Models;
using PostScriptumMortarCalculator.Utils;

namespace PostScriptumMortarCalculator.Tests.Extensions
{
[TestFixture]
public class NumericExtensionsTests
{
// TODO: Test numeric extensions.
// TODO: More test cases.
private const double c_PRECISION = 0.01d;

[TestCase(10, 10, 2, 5, 5)]
[TestCase(100, 50, 5, 20, 10)]
public void RoundedVector2ToMetersScale_PassedValues_ReturnsCorrectResult(double vX, double vY,
double pixelsPerMeter, double vShouldBeX, double vShouldBeY)
{
var v = new RoundedVector2(vX, vY);
var vShouldBe = new RoundedVector2(vShouldBeX, vShouldBeY);
var scaledV = v.ToMetersScale(pixelsPerMeter);
Assert.That(scaledV.X, Is.EqualTo(vShouldBe.X).Within(c_PRECISION));
Assert.That(scaledV.Y, Is.EqualTo(vShouldBe.Y).Within(c_PRECISION));
}

[TestCase(10, 10, 2, 20, 20)]
[TestCase(100, 50, 5, 500, 250)]
public void RoundedVector2ToPixelScale_PassedValues_ReturnsCorrectResult(double vX, double vY,
double pixelsPerMeter, double vShouldBeX, double vShouldBeY)
{
var v = new RoundedVector2(vX, vY);
var vShouldBe = new RoundedVector2(vShouldBeX, vShouldBeY);
var scaledV = v.ToPixelScale(pixelsPerMeter);
Assert.That(scaledV.X, Is.EqualTo(vShouldBe.X).Within(c_PRECISION));
Assert.That(scaledV.Y, Is.EqualTo(vShouldBe.Y).Within(c_PRECISION));
}

[TestCase(0, 1000, 500, 0.5)]
public void PercentageBetweenMinAndMaxDistance_PassedValues_ReturnsCorrectResult(double minRange,
double maxRange, double distanceBetween, double shouldBePercentage)
{
var mortarData = new MortarDataModel(string.Empty, RoundedVector2.Zero, RoundedVector2.Zero,
new List<MortarDataModel.MortarRangeValue>
{
new MortarDataModel.MortarRangeValue(minRange, minRange),
new MortarDataModel.MortarRangeValue(maxRange, maxRange)
});
var percentageBetween = mortarData.PercentageBetweenMinAndMaxDistance(distanceBetween);
Assert.That(percentageBetween, Is.EqualTo(shouldBePercentage).Within(c_PRECISION));
}

[TestCase(0, 1000, 1001)]
public void PercentageBetweenMinAndMaxDistance_PassedIncorrectValues_ReturnsLessThanZero(double minRange,
double maxRange, double distanceBetween)
{
var mortarData = new MortarDataModel(string.Empty, RoundedVector2.Zero, RoundedVector2.Zero,
new List<MortarDataModel.MortarRangeValue>
{
new MortarDataModel.MortarRangeValue(minRange, minRange),
new MortarDataModel.MortarRangeValue(maxRange, maxRange)
});
var percentageBetween = mortarData.PercentageBetweenMinAndMaxDistance(distanceBetween);
Assert.That(percentageBetween, Is.LessThan(0));
}

[TestCase(0,100,50,0.5)]
public void PercentageBetween_PassedValues_ReturnsCorrectResult(double min, double max, double value,
double shouldBePercentage)
{
Assert.That(value.PercentageBetween(min,max), Is.EqualTo(shouldBePercentage).Within(c_PRECISION));
}

[TestCase(0,100,0.5,50)]
public void LerpBetween_PassedValues_ReturnsCorrectResult(double min, double max, double percentage,
double shouldBeValue)
{
Assert.That(percentage.LerpBetween(min,max), Is.EqualTo(shouldBeValue).Within(c_PRECISION));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
<Compile Include="Services\DataResourceServiceTest.cs" />
<Compile Include="Utils\RoundedVector2Tests.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="ViewModels" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PostScriptumMortarCalculator\PostScriptumMortarCalculator.csproj">
<Project>{849e4663-adc4-4328-8906-6d40976bfb22}</Project>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
using NUnit.Framework;
using PostScriptumMortarCalculator.Services;

namespace PostScriptumMortarCalculator.Tests.Services
{
[TestFixture]
public class DataResourceServiceTest
{
// TODO: Test data service.
[Test]
public void Maps_GetData_ReturnsNonEmptyCollection()
{
var service = new DataResourceService();
var mapData = service.GetMapData();
Assert.That(mapData, Is.Not.Null.Or.Empty);
}

[Test]
public void Mortars_GetData_ReturnsNonEmptyCollection()
{
var service = new DataResourceService();
var mortarData = service.GetMortarData();
Assert.That(mortarData, Is.Not.Null.Or.Empty);
}

[Test]
public void Credits_GetData_ReturnsNonEmptyCollections()
{
var service = new DataResourceService();
var creditsData = service.GetCreditsData();
Assert.That(creditsData.Contributors, Is.Not.Null.Or.Empty);
Assert.That(creditsData.ExternalTools, Is.Not.Null.Or.Empty);
}

[Test]
public void Help_GetData_ReturnsNonEmptyCollections()
{
var service = new DataResourceService();
var helpData = service.GetHelpData();
Assert.That(helpData.Hotkeys, Is.Not.Null.Or.Empty);
Assert.That(helpData.Hints, Is.Not.Null.Or.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static double PercentageBetweenMinAndMaxDistance(this MortarDataModel mor
var minDist = mortarData.MinRange.Distance;
var maxDist = mortarData.MaxRange.Distance;
if (distance > maxDist || distance < minDist) return -1;
return (distance - minDist) / (maxDist - minDist);
return PercentageBetween(distance, minDist, maxDist);
}

public static double PercentageBetween(this double value, double min, double max)
Expand Down
2 changes: 2 additions & 0 deletions PostScriptumMortarCalculator/Utils/RoundedVector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public struct RoundedVector2

public double X { get; }
public double Y { get; }

public static RoundedVector2 Zero => new RoundedVector2(0,0);


public RoundedVector2(double x, double y)
Expand Down
25 changes: 2 additions & 23 deletions PostScriptumMortarCalculator/ViewModels/CalculatorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace PostScriptumMortarCalculator.ViewModels
{
public class CalculatorViewModel : Screen, IHandle<PositionChangedEvent>
{
#region Properties

public RoundedVector2 MortarPositionMeters { get; private set; }

public RoundedVector2 TargetPositionMeters { get; private set; }
Expand Down Expand Up @@ -43,18 +41,10 @@ public double Milliradians
return 0;
}
}

#endregion

#region Private Members


private readonly ConfigService m_configService;
private readonly IEventAggregator m_eventAggregator;

#endregion

#region Initialisation


public CalculatorViewModel(MortarDataModel defaultMortar,
IReadOnlyList<MortarDataModel> mortars,
ConfigService configService,
Expand All @@ -66,9 +56,6 @@ public CalculatorViewModel(MortarDataModel defaultMortar,
AvailableMortars.AddRange(mortars);
SelectedMortar = defaultMortar;
}
#endregion

#region PropertyChanged Handlers

public void OnSelectedMortarChanged()
{
Expand All @@ -85,17 +72,9 @@ public void Handle(PositionChangedEvent message)
Angle = message.Angle;
}

#endregion

#region UI Event Handlers

public void CopyToClipboardClicked()
{
Clipboard.SetData(DataFormats.Text, $"Angle: {Angle} | Mills: {Milliradians}");
}

#endregion


}
}
31 changes: 3 additions & 28 deletions PostScriptumMortarCalculator/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ namespace PostScriptumMortarCalculator.ViewModels
{
public class MapViewModel : Screen, IHandle<MortarChangedEvent>
{
private const string c_RESOURCE_PATH = "/PostScriptumMortarCalculator;component/Assets";

#region Properties

public RoundedVector2 TargetPositionPixels { get; set; }

public RoundedVector2 MortarPositionPixels { get; set; }
Expand All @@ -28,7 +24,6 @@ public class MapViewModel : Screen, IHandle<MortarChangedEvent>

public double MortarMinDistancePixels { get; private set; }


public double HalfMortarMinDistancePixels =>
MortarMinDistancePixels / 2d;

Expand Down Expand Up @@ -65,11 +60,8 @@ public class MapViewModel : Screen, IHandle<MortarChangedEvent>
public double Opacity { get; set; }
public string MapImageSource => c_RESOURCE_PATH + SelectedMap.MapImagePath;

#endregion
private const string c_RESOURCE_PATH = "/PostScriptumMortarCalculator;component/Assets";

#region Private Fields
private const double c_LINE_WIDTH = 5;

private ZoomBorder m_zoomBorder;
private Canvas m_canvas;
private bool m_isMouseCaptured;
Expand All @@ -79,10 +71,6 @@ public class MapViewModel : Screen, IHandle<MortarChangedEvent>
private readonly ConfigService m_configService;
private readonly UserConfigModel m_configModel;

#endregion

#region Initialisation

public MapViewModel(IReadOnlyList<MapDataModel> availableMaps,
MortarDataModel defaultMortar,
ConfigService configService,
Expand All @@ -94,14 +82,11 @@ public MapViewModel(IReadOnlyList<MapDataModel> availableMaps,
m_configService = configService;
m_configModel = configService.ActiveConfig;
SelectedMortar = defaultMortar;
SelectedMap = string.IsNullOrWhiteSpace(m_configModel.LastMapName)
? AvailableMaps.First()
SelectedMap = string.IsNullOrWhiteSpace(m_configModel.LastMapName)
? AvailableMaps.First()
: AvailableMaps.First(x => x.Name == m_configModel.LastMapName);
Opacity = m_configModel.Opacity;
}
#endregion

#region PropertyChanged Handlers

public void OnSelectedMapChanged()
{
Expand Down Expand Up @@ -143,10 +128,6 @@ public void Handle(MortarChangedEvent message)
SelectedMortar = message.ActiveMortar;
}

#endregion

#region UI Event Handlers

public void CanvasLoaded(object sender, RoutedEventArgs _)
{
m_canvas = (Canvas) sender;
Expand Down Expand Up @@ -235,10 +216,6 @@ public void CanvasSizeChanged()
OnSelectedMortarChanged();
}

#endregion

#region Helpers

private bool IsMousePosInValidRange(double posX, double posY)
{
var tempTarget = new RoundedVector2(posX, posY);
Expand Down Expand Up @@ -271,7 +248,5 @@ private void RecalculateTargetSplash()
DistancePixels.ToScaledMeters(m_mapPixelsPerMeter)))
.ToPixelScale(m_mapPixelsPerMeter) * 2;
}

#endregion
}
}
Binary file modified PostScriptumMortarCalculator/psmc_icon.ico
Binary file not shown.

0 comments on commit f08e6a3

Please sign in to comment.