Skip to content

Commit

Permalink
bugfix: added unit tests for contour legend configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos committed Dec 4, 2024
1 parent 8811ab3 commit fed2968
Show file tree
Hide file tree
Showing 3 changed files with 611 additions and 3 deletions.
6 changes: 3 additions & 3 deletions GsaGH/Helpers/GH/Legend/ContourLegendConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class ContourLegendConfiguration : IContourLegendConfiguration {
public double Scale { get; private set; } = 1.0;
public bool IsVisible { get; private set; } = true;

private const int DefaultWidth = 15;
private const int DefaultHeight = 120;
public readonly int DefaultWidth = 15;
public readonly int DefaultHeight = 120;
private const string ScaleKey = "legendScale";
private const string VisibilityKey = "legend";

Expand Down Expand Up @@ -56,7 +56,7 @@ private void ScaleBitmap() {
}

public bool IsLegendDisplayable() {
return Values.Any() && IsVisible;
return Values.Any() && ValuePositionsY.Any() && IsVisible;
}

/// <summary>
Expand Down
191 changes: 191 additions & 0 deletions GsaGHTests/Helpers/GH/Legend/ContourLegendConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;

using GsaGH.Helpers.GH;

using Xunit;

namespace GsaGHTests.Helpers.GH.Legend {
public class ContourLegendConfigurationTests {
private readonly ContourLegendConfiguration legendConfiguration;

public ContourLegendConfigurationTests() {
legendConfiguration = new ContourLegendConfiguration();
}

[Fact]
public void WhenInitialisedShouldContainEmptyValues() {
Assert.NotNull(legendConfiguration.Values);
Assert.Empty(legendConfiguration.Values);
}

[Fact]
public void WhenInitialisedShouldContainEmptyPositionYValues() {
Assert.NotNull(legendConfiguration.ValuePositionsY);
Assert.Empty(legendConfiguration.ValuePositionsY);
}

[Fact]
public void WhenInitialisedIsVisibleShouldBeTrue() {
Assert.True(legendConfiguration.IsVisible);
}

[Fact]
public void WhenInitialisedScaleShouldBeSet() {
double defaultScale = 1.0d;
Assert.Equal(defaultScale, legendConfiguration.Scale);
}

[Fact]
public void WhenInitialisedBitmapShouldBeSet() {
Assert.NotNull(legendConfiguration.Bitmap);
Assert.Equal(legendConfiguration.DefaultWidth, legendConfiguration.Bitmap.Width);
Assert.Equal(legendConfiguration.DefaultHeight, legendConfiguration.Bitmap.Height);
}

[Fact]
public void WhenInitialisedIsLegendDisplayableShouldReturnFalse() {
Assert.False(legendConfiguration.IsLegendDisplayable());
}

[Fact]
public void SetTextValuesShouldThrowErrorWhenValuesAreNull() {
Assert.Throws<ArgumentNullException>(() => legendConfiguration.SetTextValues(null));
}

[Fact]
public void SetTextValuesShouldSetValuesCorrectly() {
var expectedValues = new List<string>() {
"1",
"2",
};
legendConfiguration.SetTextValues(expectedValues);
Assert.Equal(expectedValues.Count, legendConfiguration.Values.Count);
Assert.Same(expectedValues, legendConfiguration.Values);
}

[Fact]
public void SetTextValuesShouldSetEmptyList() {
legendConfiguration.SetTextValues(new List<string>());
Assert.NotNull(legendConfiguration.Values);
Assert.Empty(legendConfiguration.Values);
}

[Fact]
public void SetValuePositionsYShouldThrowErrorWhenValuesAreNull() {
Assert.Throws<ArgumentNullException>(() => legendConfiguration.SetValuePositionsY(null));
}

[Fact]
public void SetValuePositionsYShouldSetValuesCorrectly() {
var expectedValues = new List<int>() {
1,
2,
};
legendConfiguration.SetValuePositionsY(expectedValues);
Assert.Equal(expectedValues.Count, legendConfiguration.ValuePositionsY.Count);
Assert.Same(expectedValues, legendConfiguration.ValuePositionsY);
}

[Fact]
public void SetValuePositionsYShouldSetEmptyList() {
legendConfiguration.SetValuePositionsY(new List<int>());
Assert.NotNull(legendConfiguration.ValuePositionsY);
Assert.Empty(legendConfiguration.ValuePositionsY);
}

[Fact]
public void SetScaleShouldThrowErrorWhenScaleIsLessThan0() {
Assert.Throws<ArgumentOutOfRangeException>(() => legendConfiguration.SetLegendScale(-1));
}

[Fact]
public void SetScaleShouldThrowErrorWhenScaleIsEqual0() {
Assert.Throws<ArgumentOutOfRangeException>(() => legendConfiguration.SetLegendScale(0));
}

[Fact]
public void SetScaleShouldSetScaleCorrectly() {
const double expectedScale = 1.5;
legendConfiguration.SetLegendScale(expectedScale);
Assert.Equal(expectedScale, legendConfiguration.Scale);
}

[Fact]
public void SetScaleShouldResizeBitmapCorrectly() {
const double expectedScale = 1.5;
int expectedWidth = (int)(expectedScale * legendConfiguration.DefaultWidth);
int expectedHeight = (int)(expectedScale * legendConfiguration.DefaultHeight);
legendConfiguration.SetLegendScale(expectedScale);
Assert.Equal(expectedHeight, legendConfiguration.Bitmap.Height);
Assert.Equal(expectedWidth, legendConfiguration.Bitmap.Width);
}

[Fact]
public void ToggleLegendVisibilityWorksCorrectly() {
Assert.False(legendConfiguration.ToggleLegendVisibility());
Assert.True(legendConfiguration.ToggleLegendVisibility());
}

[Fact]
public void IsLegendDisplayableWillReturnTrueIfLegendIsVisibleAndHasValues() {
Assert.False(legendConfiguration.IsLegendDisplayable());
legendConfiguration.SetTextValues(new List<string>() {
"1",
});
legendConfiguration.SetValuePositionsY(new List<int>() {
1,
});
Assert.True(legendConfiguration.IsLegendDisplayable());
}

[Fact]
public void IsLegendDisplayableWillReturnFalseIfLegendIsNotVisibleButHasValues() {
Assert.False(legendConfiguration.IsLegendDisplayable());
legendConfiguration.ToggleLegendVisibility();
legendConfiguration.SetTextValues(new List<string>() {
"1",
});
legendConfiguration.SetValuePositionsY(new List<int>() {
1,
});
Assert.False(legendConfiguration.IsLegendDisplayable());
}

[Fact]
public void IsLegendDisplayableWillReturnFalseIfLegendIsVisibleButHasNotValues() {
legendConfiguration.SetValuePositionsY(new List<int>() {
1,
});
Assert.False(legendConfiguration.IsLegendDisplayable());
}

[Fact]
public void SerialiseWillThrowErrorWhenObjectIsNotSet() {
Assert.Throws<ArgumentNullException>(() => legendConfiguration.SerializeLegendState(null));
}

[Fact]
public void DeserialiseWillThrowErrorWhenObjectIsNotSet() {
Assert.Throws<ArgumentNullException>(() => legendConfiguration.DeserializeLegendState(null));
}

[Fact]
public void SerializeAndDeserializeLegendStateWorksCorrectly() {
var writer = new MockWriter();
var reader = new MockReader();

legendConfiguration.SetLegendScale(2.0);
legendConfiguration.ToggleLegendVisibility();
legendConfiguration.SerializeLegendState(writer);

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

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

}
Loading

0 comments on commit fed2968

Please sign in to comment.