Skip to content

Commit

Permalink
bugfix: added Moq package to use it when testing serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos committed Dec 4, 2024
1 parent fed2968 commit 6e81595
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 425 deletions.
10 changes: 8 additions & 2 deletions GsaGH/Helpers/GH/Legend/ContourLegendConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ public class ContourLegendConfiguration : IContourLegendConfiguration {

public readonly int DefaultWidth = 15;
public readonly int DefaultHeight = 120;
private const string ScaleKey = "legendScale";
private const string VisibilityKey = "legend";
/// <summary>
/// Key used to de/serialise scale of the legend
/// </summary>
public static string ScaleKey => "legendScale";
/// <summary>
/// Key used to de/serialise visibility of the legend
/// </summary>
public static string VisibilityKey => "legend";

public ContourLegendConfiguration() {
ScaleBitmap();
Expand Down
1 change: 1 addition & 0 deletions GsaGHTests/GsaGHTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<None Remove="TestHelpers\Steel_Design_Complex.gwb.lok" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Rhino.Inside" Version="7.0.0" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
Expand Down
38 changes: 32 additions & 6 deletions GsaGHTests/Helpers/GH/Legend/ContourLegendConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;

using GH_IO.Serialization;

using GsaGH.Helpers.GH;

using Moq;

using Xunit;

namespace GsaGHTests.Helpers.GH.Legend {
[Collection("GrasshopperFixture collection")]
public class ContourLegendConfigurationTests {
private readonly ContourLegendConfiguration legendConfiguration;

Expand Down Expand Up @@ -172,19 +177,40 @@ public void DeserialiseWillThrowErrorWhenObjectIsNotSet() {

[Fact]
public void SerializeAndDeserializeLegendStateWorksCorrectly() {
var writer = new MockWriter();
var reader = new MockReader();
var mockWriter = new Mock<GH_IWriter>();
var mockReader = new Mock<GH_IReader>();

double scale = 2.0;
bool visibility = false;
string scaleKey = ContourLegendConfiguration.ScaleKey;
string visibilityKey = ContourLegendConfiguration.VisibilityKey;

legendConfiguration.SetLegendScale(2.0);
legendConfiguration.SetLegendScale(scale);
legendConfiguration.ToggleLegendVisibility();
legendConfiguration.SerializeLegendState(writer);
legendConfiguration.SerializeLegendState(mockWriter.Object);

// Verify that serialization methods were called with correct arguments
mockWriter.Verify(writer => writer.SetDouble(scaleKey, scale), Times.Once);
mockWriter.Verify(writer => writer.SetBoolean(visibilityKey, visibility), Times.Once);

// Mock the reader to return serialized values
mockReader.Setup(reader => reader.ItemExists(scaleKey)).Returns(true);
mockReader.Setup(reader => reader.ItemExists(visibilityKey)).Returns(true);
mockReader.Setup(reader => reader.GetDouble(scaleKey)).Returns(scale);
mockReader.Setup(reader => reader.GetBoolean(visibilityKey)).Returns(visibility);

reader.Data = writer.Data;
var deserializedConfig = new ContourLegendConfiguration();
deserializedConfig.DeserializeLegendState(reader);
deserializedConfig.DeserializeLegendState(mockReader.Object);

// Assert
Assert.Equal(legendConfiguration.Scale, deserializedConfig.Scale);
Assert.Equal(legendConfiguration.IsVisible, deserializedConfig.IsVisible);

// Verify that deserialization methods were called
mockReader.Verify(reader => reader.ItemExists(scaleKey), Times.Once);
mockReader.Verify(reader => reader.ItemExists(visibilityKey), Times.Once);
mockReader.Verify(reader => reader.GetDouble(scaleKey), Times.Once);
mockReader.Verify(reader => reader.GetBoolean(visibilityKey), Times.Once);
}
}

Expand Down
Loading

0 comments on commit 6e81595

Please sign in to comment.