-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: added unit tests for text wrapping
- Loading branch information
1 parent
7166b44
commit 01f16f6
Showing
3 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
GsaGHTests/Helpers/GH/Legend/ContourLegendMenagerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Windows.Forms; | ||
|
||
using Grasshopper.Kernel; | ||
|
||
using GsaGH.Components; | ||
using GsaGH.Helpers; | ||
using GsaGH.Helpers.GH; | ||
|
||
using Moq; | ||
|
||
using Xunit; | ||
|
||
namespace GsaGHTests.Helpers { | ||
[Collection("GrasshopperFixture collection")] | ||
public class ContourLegendMenagerTests { | ||
private static Mock<IContourLegend> _mockLegend; | ||
private static Mock<ContourLegendConfiguration> _mockConfiguration; | ||
private static Mock<LegendMenuBuilder> _mockMenuBuilder; | ||
private static ContourLegendManager _manager; | ||
private static Mock<IGH_PreviewArgs> _mockArgs; | ||
|
||
public ContourLegendMenagerTests() { | ||
_mockLegend = new Mock<IContourLegend>(); | ||
_mockConfiguration = new Mock<ContourLegendConfiguration>(); | ||
_mockMenuBuilder = new Mock<LegendMenuBuilder>(); | ||
_manager = new ContourLegendManager(_mockConfiguration.Object, _mockLegend.Object, _mockMenuBuilder.Object); | ||
|
||
_mockArgs = new Mock<IGH_PreviewArgs>(); | ||
} | ||
|
||
[Fact] | ||
public void DrawLegend_CallsDrawLegendRectangle() { | ||
string title = "Test Title"; | ||
string bottomText = "Test Bottom Text"; | ||
var gradients = new List<(int, int, Color)> { | ||
(0, 10, Color.Red), | ||
(10, 20, Color.Green), | ||
}; | ||
|
||
_manager.DrawLegend(_mockArgs.Object, title, bottomText, gradients); | ||
|
||
_mockLegend.Verify(l => l.DrawLegendRectangle(_mockArgs.Object, title, bottomText, gradients), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void CreateMenu_CallsCreateLegendToolStripMenuItem() { | ||
var mockUpdateUI = new Mock<Action>(); | ||
|
||
ToolStripMenuItem menuItem | ||
= _manager.CreateMenu(new Contour1dResults(), mockUpdateUI.Object); // don't care of the component type | ||
|
||
Assert.NotNull(menuItem); | ||
} | ||
|
||
[Fact] | ||
public void UpdateScale_CallsSetLegendScale() { | ||
double scale = 2.5; | ||
_manager.UpdateScale(scale); | ||
Assert.Equal(scale, _mockConfiguration.Object.Scale); | ||
} | ||
|
||
[Fact] | ||
public void ToggleVisibility_CallsToggleLegendVisibility() { | ||
bool result = _manager.ToggleVisibility(); | ||
Assert.False(_mockConfiguration.Object.IsVisible); | ||
} | ||
|
||
[Fact] | ||
public void GetBitmapSize_ReturnsCorrectSize() { | ||
double scale = 2; | ||
_manager.UpdateScale(scale); | ||
int defaultWidth = _mockConfiguration.Object.DefaultWidth; | ||
int defaultHeight = _mockConfiguration.Object.DefaultHeight; | ||
int expectedWidth = (int)(defaultWidth * scale); | ||
int expectedHeight = (int)(defaultHeight * scale); | ||
// Set up the mock configuration to return specific values for the Bitmap size | ||
(int width, int height) = _manager.GetBitmapSize(); | ||
|
||
Assert.Equal(expectedWidth, width); | ||
Assert.Equal(expectedHeight, height); | ||
} | ||
|
||
[Fact] | ||
public void SetTextValues_SetsTextValuesInConfiguration() { | ||
var textValues = new List<string> { | ||
"Value 1", | ||
"Value 2", | ||
"Value 3", | ||
}; | ||
_manager.SetTextValues(textValues); | ||
Assert.Equal(textValues, _mockConfiguration.Object.Values); | ||
} | ||
|
||
[Fact] | ||
public void SetPositionYValues_SetsPositionYValuesInConfiguration() { | ||
var yValues = new List<int> { | ||
10, | ||
20, | ||
30, | ||
}; | ||
_manager.SetPositionYValues(yValues); | ||
Assert.Equal(yValues, _mockConfiguration.Object.ValuePositionsY); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
using GsaGH.Helpers.GH; | ||
|
||
using Rhino; | ||
using Rhino.DocObjects; | ||
|
||
using Xunit; | ||
|
||
namespace GsaGHTests.Helpers { | ||
[Collection("GrasshopperFixture collection")] | ||
public class TextWrapperTests { | ||
|
||
[Fact] | ||
public void WrapText_ShouldWrapTextCorrectly() { | ||
string inputText = "This is a test sentence that should be wrapped correctly."; | ||
int maxWidth = 100; | ||
int fontSize = 10; | ||
|
||
string result = TextWrapper.WrapText(inputText, maxWidth, fontSize); | ||
|
||
Assert.NotNull(result); | ||
Assert.Contains("\n", result); | ||
Assert.Equal("This is a test\nsentence that\nshould be\nwrapped\ncorrectly.", result); | ||
} | ||
|
||
[Fact] | ||
public void GetCachedTextWidth_ShouldCacheTextWidthCorrectly() { | ||
string inputText = "This is a test sentence that should be wrapped correctly."; | ||
int fontSize = 10; | ||
int maxWidth = 100; | ||
|
||
string text1 = TextWrapper.WrapText(inputText, maxWidth, fontSize); | ||
string text2 = TextWrapper.WrapText(inputText, maxWidth, fontSize); | ||
|
||
Assert.Single(GetPrivateFieldValue()); | ||
} | ||
|
||
private Dictionary<(string, float), float> GetPrivateFieldValue() { | ||
FieldInfo fieldInfo | ||
= typeof(TextWrapper).GetField("textWidthCache", BindingFlags.NonPublic | BindingFlags.Static); | ||
return (Dictionary<(string, float), float>)fieldInfo.GetValue(null); | ||
} | ||
|
||
// Testowanie metody GetFontName | ||
[Fact] | ||
public void GetFontName_ShouldReturnFontNameFromRhino() { | ||
RhinoDoc rhinoDoc = CreateRhinoDocument("test"); | ||
string fontName = TextWrapper.GetFontName(rhinoDoc); | ||
CloseRhinoDoc(rhinoDoc); | ||
Assert.Equal("Arial", fontName); | ||
} | ||
|
||
[Fact] | ||
public void GetFontName_ShouldReturnDefaultWhenRhinoDocIsNull() { | ||
string fontName = TextWrapper.GetFontName(); | ||
Assert.Equal("Arial", fontName); | ||
} | ||
|
||
private static RhinoDoc CreateRhinoDocument(string modelTemplateFileName) { | ||
var rhinooDoc = RhinoDoc.Create(modelTemplateFileName); | ||
rhinooDoc.DimStyles.Current.Font = new Font("Test"); | ||
|
||
return rhinooDoc; | ||
} | ||
|
||
private static void CloseRhinoDoc(RhinoDoc rhinoDoc) { | ||
rhinoDoc.Dispose(); | ||
} | ||
} | ||
} |