Skip to content

Commit 577148c

Browse files
committed
Minor code improvements
1 parent 80ef43a commit 577148c

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

src/Html2OpenXml/IO/ImageHeader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ public static Size KeepAspectRatio(Size actualSize, Size preferredSize)
114114
}
115115

116116
/// <summary>
117-
/// Examines the a file's first bytes and estimates the file's type.
117+
/// Examines the first bytes of the file and estimates its image type if possible.
118118
/// </summary>
119+
/// <returns>Returns <see cref="FileType.Unrecognized"/> if not recognized.</returns>
119120
private static FileType DetectFileType (SequentialBinaryReader reader)
120121
{
121122
byte[] magicBytes = new byte[MaxMagicBytesLength];

src/Html2OpenXml/Primitives/Size.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace HtmlToOpenXml;
1515
/// <summary>
1616
/// Represents a dimension in 2D coordinate space.
1717
/// </summary>
18-
public struct Size
18+
public struct Size : System.IEquatable<Size>
1919
{
2020
/// <summary>
2121
/// Initializes a new instance of the <see cref='HtmlToOpenXml.Size'/> class.
@@ -46,4 +46,18 @@ public Size(int width, int height)
4646
/// Represents the vertical component of this size.
4747
/// </summary>
4848
public int Height { get; set; }
49+
50+
/// <inheritdoc/>
51+
public bool Equals(Size other)
52+
{
53+
return this.Width == other.Width && this.Height == other.Height;
54+
}
55+
56+
/// <summary>
57+
/// Gets a representation of this size.
58+
/// </summary>
59+
public override string ToString()
60+
{
61+
return string.Format("({0},{1})", Width, Height);
62+
}
4963
}

test/HtmlToOpenXml.Tests/ImageFormats/ImageHeaderTests.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,25 @@ namespace HtmlToOpenXml.Tests.ImageFormats
99
[TestFixture]
1010
public class ImageHeaderTests
1111
{
12-
[TestCase("Resources.html2openxml.bmp")]
13-
[TestCase("Resources.html2openxml.gif")]
14-
[TestCase("Resources.html2openxml.jpg")]
15-
[TestCase("Resources.html2openxml.png")]
16-
[TestCase("Resources.html2openxml.emf")]
17-
public void GuessFormat_ReturnsImageSize(string resourceName)
12+
[TestCaseSource(nameof(GuessImageSizeTestCases))]
13+
public void GuessFormat_ReturnsImageSize((string resourceName, Size expectedSize) td)
1814
{
19-
using (var imageStream = ResourceHelper.GetStream(resourceName))
15+
using (var imageStream = ResourceHelper.GetStream(td.resourceName))
2016
{
2117
Size size = ImageHeader.GetDimensions(imageStream);
22-
Assert.Multiple(() =>
23-
{
24-
Assert.That(size.Width, Is.EqualTo(100));
25-
Assert.That(size.Height, Is.EqualTo(100));
26-
});
18+
Assert.That(size, Is.EqualTo(td.expectedSize));
2719
}
2820
}
2921

30-
[Test]
31-
public void AnimatedGif_ReturnsImageSize()
22+
private static IEnumerable<(string, Size)> GuessImageSizeTestCases()
3223
{
33-
using (var imageStream = ResourceHelper.GetStream("Resources.stan.gif"))
34-
{
35-
Size size = ImageHeader.GetDimensions(imageStream);
36-
Assert.Multiple(() =>
37-
{
38-
Assert.That(size.Width, Is.EqualTo(252));
39-
Assert.That(size.Height, Is.EqualTo(318));
40-
});
41-
}
24+
yield return ("Resources.html2openxml.bmp", new Size(100, 100));
25+
yield return ("Resources.html2openxml.gif", new Size(100, 100));
26+
yield return ("Resources.html2openxml.jpg", new Size(100, 100));
27+
yield return ("Resources.html2openxml.png", new Size(100, 100));
28+
yield return ("Resources.html2openxml.emf", new Size(100, 100));
29+
// animated gif:
30+
yield return ("Resources.stan.gif", new Size(252, 318));
4231
}
4332

4433
/// <summary>

test/HtmlToOpenXml.Tests/Primitives/MarginTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,24 @@ public void ParseWithFloat_ShouldSucceed ()
5151
Assert.That(Math.Round(margin.Left.ValueInPoint * 2).ToString(), Is.EqualTo("0"));
5252
});
5353
}
54+
55+
[Test]
56+
public void ParseWithAuto_ShouldSucceed ()
57+
{
58+
var margin = Margin.Parse("0 auto");
59+
60+
Assert.Multiple(() => {
61+
Assert.That(margin.IsValid, Is.EqualTo(true));
62+
63+
Assert.That(margin.Top.Value, Is.EqualTo(0));
64+
Assert.That(margin.Top.Type, Is.EqualTo(UnitMetric.Pixel));
65+
66+
Assert.That(margin.Bottom.Value, Is.EqualTo(0));
67+
Assert.That(margin.Bottom.Type, Is.EqualTo(UnitMetric.Pixel));
68+
69+
Assert.That(margin.Left.Type, Is.EqualTo(UnitMetric.Auto));
70+
Assert.That(margin.Right.Type, Is.EqualTo(UnitMetric.Auto));
71+
});
72+
}
5473
}
5574
}

0 commit comments

Comments
 (0)