diff --git a/PostScriptumMortarCalculator.Tests/Extensions/NumericExtensionsTests.cs b/PostScriptumMortarCalculator.Tests/Extensions/NumericExtensionsTests.cs index 42aab1b..69bef16 100644 --- a/PostScriptumMortarCalculator.Tests/Extensions/NumericExtensionsTests.cs +++ b/PostScriptumMortarCalculator.Tests/Extensions/NumericExtensionsTests.cs @@ -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 + { + 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 + { + 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)); + } } } \ No newline at end of file diff --git a/PostScriptumMortarCalculator.Tests/PostScriptumMortarCalculator.Tests.csproj b/PostScriptumMortarCalculator.Tests/PostScriptumMortarCalculator.Tests.csproj index 63adad8..655b5cf 100644 --- a/PostScriptumMortarCalculator.Tests/PostScriptumMortarCalculator.Tests.csproj +++ b/PostScriptumMortarCalculator.Tests/PostScriptumMortarCalculator.Tests.csproj @@ -50,9 +50,6 @@ - - - {849e4663-adc4-4328-8906-6d40976bfb22} diff --git a/PostScriptumMortarCalculator.Tests/Services/DataResourceServiceTest.cs b/PostScriptumMortarCalculator.Tests/Services/DataResourceServiceTest.cs index 9e05f38..27c2a4b 100644 --- a/PostScriptumMortarCalculator.Tests/Services/DataResourceServiceTest.cs +++ b/PostScriptumMortarCalculator.Tests/Services/DataResourceServiceTest.cs @@ -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); + } } } \ No newline at end of file diff --git a/PostScriptumMortarCalculator/Extensions/NumericExtensions.cs b/PostScriptumMortarCalculator/Extensions/NumericExtensions.cs index b5d5793..2041892 100644 --- a/PostScriptumMortarCalculator/Extensions/NumericExtensions.cs +++ b/PostScriptumMortarCalculator/Extensions/NumericExtensions.cs @@ -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) diff --git a/PostScriptumMortarCalculator/Utils/RoundedVector2.cs b/PostScriptumMortarCalculator/Utils/RoundedVector2.cs index 4c9971c..1fc068a 100644 --- a/PostScriptumMortarCalculator/Utils/RoundedVector2.cs +++ b/PostScriptumMortarCalculator/Utils/RoundedVector2.cs @@ -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) diff --git a/PostScriptumMortarCalculator/ViewModels/CalculatorViewModel.cs b/PostScriptumMortarCalculator/ViewModels/CalculatorViewModel.cs index 22e0ac8..389d2b2 100644 --- a/PostScriptumMortarCalculator/ViewModels/CalculatorViewModel.cs +++ b/PostScriptumMortarCalculator/ViewModels/CalculatorViewModel.cs @@ -12,8 +12,6 @@ namespace PostScriptumMortarCalculator.ViewModels { public class CalculatorViewModel : Screen, IHandle { - #region Properties - public RoundedVector2 MortarPositionMeters { get; private set; } public RoundedVector2 TargetPositionMeters { get; private set; } @@ -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 mortars, ConfigService configService, @@ -66,9 +56,6 @@ public CalculatorViewModel(MortarDataModel defaultMortar, AvailableMortars.AddRange(mortars); SelectedMortar = defaultMortar; } - #endregion - - #region PropertyChanged Handlers public void OnSelectedMortarChanged() { @@ -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 - - } } \ No newline at end of file diff --git a/PostScriptumMortarCalculator/ViewModels/MapViewModel.cs b/PostScriptumMortarCalculator/ViewModels/MapViewModel.cs index 8c14f62..2b7f1c8 100644 --- a/PostScriptumMortarCalculator/ViewModels/MapViewModel.cs +++ b/PostScriptumMortarCalculator/ViewModels/MapViewModel.cs @@ -15,10 +15,6 @@ namespace PostScriptumMortarCalculator.ViewModels { public class MapViewModel : Screen, IHandle { - private const string c_RESOURCE_PATH = "/PostScriptumMortarCalculator;component/Assets"; - - #region Properties - public RoundedVector2 TargetPositionPixels { get; set; } public RoundedVector2 MortarPositionPixels { get; set; } @@ -28,7 +24,6 @@ public class MapViewModel : Screen, IHandle public double MortarMinDistancePixels { get; private set; } - public double HalfMortarMinDistancePixels => MortarMinDistancePixels / 2d; @@ -65,11 +60,8 @@ public class MapViewModel : Screen, IHandle 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; @@ -79,10 +71,6 @@ public class MapViewModel : Screen, IHandle private readonly ConfigService m_configService; private readonly UserConfigModel m_configModel; - #endregion - - #region Initialisation - public MapViewModel(IReadOnlyList availableMaps, MortarDataModel defaultMortar, ConfigService configService, @@ -94,14 +82,11 @@ public MapViewModel(IReadOnlyList 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() { @@ -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; @@ -235,10 +216,6 @@ public void CanvasSizeChanged() OnSelectedMortarChanged(); } - #endregion - - #region Helpers - private bool IsMousePosInValidRange(double posX, double posY) { var tempTarget = new RoundedVector2(posX, posY); @@ -271,7 +248,5 @@ private void RecalculateTargetSplash() DistancePixels.ToScaledMeters(m_mapPixelsPerMeter))) .ToPixelScale(m_mapPixelsPerMeter) * 2; } - - #endregion } } \ No newline at end of file diff --git a/PostScriptumMortarCalculator/psmc_icon.ico b/PostScriptumMortarCalculator/psmc_icon.ico index e8262c4..73d7950 100644 Binary files a/PostScriptumMortarCalculator/psmc_icon.ico and b/PostScriptumMortarCalculator/psmc_icon.ico differ