diff --git a/README.md b/README.md index 5b92ec28f7..a2f16a7394 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ -# Core -Point, Rectangle, Size Primitives for use across SixLabors libraries. +# SixLabers.Core + +**SixLabors.Core** provides classes for use across SixLabors libraries. + +[![Build status](https://ci.appveyor.com/api/projects/status/j1hvc99493b0jk3x/branch/develop?svg=true)](https://ci.appveyor.com/project/six-labors/core/branch/develop) +[![codecov](https://codecov.io/gh/SixLabors/Core/branch/develop/graph/badge.svg)](https://codecov.io/gh/SixLabors/Core) +[![GitHub license](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/SixLabors/Core/master/LICENSE) + +[![GitHub issues](https://img.shields.io/github/issues/SixLabors/Core.svg)](https://github.com/SixLabors/Core/issues) +[![GitHub stars](https://img.shields.io/github/stars/SixLabors/Core.svg)](https://github.com/SixLabors/Core/stargazers) +[![GitHub forks](https://img.shields.io/github/forks/SixLabors/Core.svg)](https://github.com/SixLabors/Core/network) \ No newline at end of file diff --git a/SixLabors.Core.sln b/SixLabors.Core.sln index 63fe397d4e..d10b177877 100644 --- a/SixLabors.Core.sln +++ b/SixLabors.Core.sln @@ -1,13 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.12 +VisualStudioVersion = 15.0.26430.14 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{C317F1B1-D75E-4C6D-83EB-80367343E0D7}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig appveyor.yml = appveyor.yml build.cmd = build.cmd + codecov.yml = codecov.yml + gitversion.yml = gitversion.yml README.md = README.md EndProjectSection EndProject @@ -15,16 +17,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{815C06 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{56801022-D71A-4FBE-BC5B-CBA08E2284EC}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{9E574A07-F879-4811-9C41-5CBDC6BAFDB7}" - ProjectSection(SolutionItems) = preProject - src\Shared\AssemblyInfo.Common.cs = src\Shared\AssemblyInfo.Common.cs - src\Shared\stylecop.json = src\Shared\stylecop.json - EndProjectSection -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SixLabors.Core", "src\SixLabors.Core\SixLabors.Core.csproj", "{09E744EC-4852-4FC7-BE78-C1B399F17967}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SixLabors.Core.Tests", "tests\SixLabors.Core.Tests\SixLabors.Core.Tests.csproj", "{F836E8E6-B4D9-4208-8346-140C74678B91}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CodeCoverage", "CodeCoverage", "{10A74B46-930F-49E3-A579-BC3A6A23321D}" + ProjectSection(SolutionItems) = preProject + tests\CodeCoverage\CodeCoverage.cmd = tests\CodeCoverage\CodeCoverage.cmd + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,9 +45,9 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {9E574A07-F879-4811-9C41-5CBDC6BAFDB7} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {09E744EC-4852-4FC7-BE78-C1B399F17967} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} {F836E8E6-B4D9-4208-8346-140C74678B91} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC} + {10A74B46-930F-49E3-A579-BC3A6A23321D} = {C317F1B1-D75E-4C6D-83EB-80367343E0D7} EndGlobalSection GlobalSection(Performance) = preSolution HasPerformanceSessions = true diff --git a/appveyor.yml b/appveyor.yml index edb297ed48..5a74728faf 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -24,6 +24,14 @@ deploy: artifact: /.*\.nupkg/ on: branch: master + - provider: NuGet + server: https://www.myget.org/F/sixlabors-unstable/api/v2/package + symbol_server: https://www.myget.org/F/sixlabors-unstable/symbols/api/v2/package + api_key: + secure: V/lEHP0UeMWIpWd0fiNlY2IgbCnJKQlGdRksECdJbOBdaE20Fl0RNL7WyqHe02o4 + artifact: /.*\.nupkg/ + on: + branch: develop # prevent the double build when a branch has an active PR skip_branch_with_pr: true diff --git a/build.cmd b/build.cmd index d1eaa59b01..eaa54e8616 100644 --- a/build.cmd +++ b/build.cmd @@ -2,14 +2,14 @@ if not "%GitVersion_NuGetVersion%" == "" ( dotnet restore /p:packageversion=%GitVersion_NuGetVersion% -)ELSE ( - dotnet restore +)ELSE ( + dotnet restore ) ECHO Building nuget packages if not "%GitVersion_NuGetVersion%" == "" ( dotnet build -c Release /p:packageversion=%GitVersion_NuGetVersion% -)ELSE ( +)ELSE ( dotnet build -c Release ) if not "%errorlevel%"=="0" goto failure @@ -19,7 +19,7 @@ dotnet test ./tests/SixLabors.Core.Tests/SixLabors.Primitives.Tests.csproj if not "%GitVersion_NuGetVersion%" == "" ( dotnet pack ./src/SixLabors.Core/ -c Release --output ../../artifacts --no-build /p:packageversion=%GitVersion_NuGetVersion% -)ELSE ( +)ELSE ( dotnet pack ./src/SixLabors.Core/ -c Release --output ../../artifacts --no-build ) diff --git a/codecov.yml b/codecov.yml index cadf5c5da6..ad0b0be56b 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,4 +1,5 @@ codecov: + branch: develop notify: require_ci_to_pass: true comment: off diff --git a/src/Shared/stylecop.json b/src/Shared/stylecop.json deleted file mode 100644 index df3c8c9d8c..0000000000 --- a/src/Shared/stylecop.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", - "settings": { - "documentationRules": { - "companyName": "Scott Williams", - "copyrightText": "Copyright (c) Scott Williams and contributors.\nLicensed under the Apache License, Version 2.0." - } - } -} \ No newline at end of file diff --git a/src/SixLabors.Core/Constants.cs b/src/SixLabors.Core/Constants.cs index 1d9dd24d9e..eda2f596f4 100644 --- a/src/SixLabors.Core/Constants.cs +++ b/src/SixLabors.Core/Constants.cs @@ -1,7 +1,5 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// namespace SixLabors { diff --git a/src/SixLabors.Core/HashHelpers.cs b/src/SixLabors.Core/HashHelpers.cs deleted file mode 100644 index e481e03dae..0000000000 --- a/src/SixLabors.Core/HashHelpers.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace SixLabors -{ - // lifted from coreFX repo - internal static class HashHelpers - { - public static readonly int RandomSeed = Guid.NewGuid().GetHashCode(); - - public static int Combine(int h1, int h2) - { - unchecked - { - // RyuJIT optimizes this to use the ROL instruction - // Related GitHub pull request: dotnet/coreclr#1830 - uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); - return ((int)rol5 + h1) ^ h2; - } - } - } -} diff --git a/src/SixLabors.Core/Helpers/DebugGuard.cs b/src/SixLabors.Core/Helpers/DebugGuard.cs new file mode 100644 index 0000000000..4a2ed9473b --- /dev/null +++ b/src/SixLabors.Core/Helpers/DebugGuard.cs @@ -0,0 +1,261 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Diagnostics; + +namespace SixLabors +{ + /// + /// Provides methods to protect against invalid parameters for a DEBUG build. + /// + [DebuggerStepThrough] + internal static class DebugGuard + { + /// + /// Verifies, that the method parameter with specified object value is not null + /// and throws an exception if it is found to be so. + /// + /// The target object, which cannot be null. + /// The name of the parameter that is to be checked. + /// is null + [Conditional("DEBUG")] + public static void NotNull(object target, string parameterName) + { + if (target == null) + { + throw new ArgumentNullException(parameterName); + } + } + + /// + /// Verifies that the specified value is less than a maximum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The maximum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is greater than the maximum value. + /// + [Conditional("DEBUG")] + public static void MustBeLessThan(TValue value, TValue max, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(max) >= 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}."); + } + } + + /// + /// Verifies that the specified value is less than or equal to a maximum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The maximum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is greater than the maximum value. + /// + [Conditional("DEBUG")] + public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(max) > 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}."); + } + } + + /// + /// Verifies that the specified value is greater than a minimum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The minimum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is less than the minimum value. + /// + [Conditional("DEBUG")] + public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(min) <= 0) + { + throw new ArgumentOutOfRangeException( + parameterName, + $"Value must be greater than {min}."); + } + } + + /// + /// Verifies that the specified value is greater than or equal to a minimum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The minimum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is less than the minimum value. + /// + [Conditional("DEBUG")] + public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(min) < 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}."); + } + } + + /// + /// Verifies, that the method parameter with specified target value is true + /// and throws an exception if it is found to be so. + /// + /// + /// The target value, which cannot be false. + /// + /// + /// The name of the parameter that is to be checked. + /// + /// + /// The error message, if any to add to the exception. + /// + /// + /// is false + /// + [Conditional("DEBUG")] + public static void IsTrue(bool target, string parameterName, string message) + { + if (!target) + { + throw new ArgumentException(message, parameterName); + } + } + + /// + /// Verifies, that the method parameter with specified target value is false + /// and throws an exception if it is found to be so. + /// + /// The target value, which cannot be true. + /// The name of the parameter that is to be checked. + /// The error message, if any to add to the exception. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void IsFalse(bool target, string parameterName, string message) + { + if (target) + { + throw new ArgumentException(message, parameterName); + } + } + + /// + /// Verifies, that the target span is of same size than the 'other' span. + /// + /// The element type of the spans + /// The target span. + /// The 'other' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void MustBeSameSized(ReadOnlySpan target, ReadOnlySpan other, string parameterName) + where T : struct + { + if (target.Length != other.Length) + { + throw new ArgumentException("Span-s must be the same size.", parameterName); + } + } + + /// + /// Verifies, that the target span is of same size than the 'other' span. + /// + /// The element type of the spans + /// The target span. + /// The 'other' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void MustBeSameSized(Span target, Span other, string parameterName) + where T : struct + { + if (target.Length != other.Length) + { + throw new ArgumentException("Span-s must be the same size.", parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// + /// The element type of the spans + /// The target span. + /// The 'minSpan' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void MustBeSizedAtLeast(ReadOnlySpan target, ReadOnlySpan minSpan, string parameterName) + where T : struct + { + if (target.Length < minSpan.Length) + { + throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}.", parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// + /// The element type of the spans + /// The target span. + /// The 'minSpan' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName) + where T : struct + { + if (target.Length < minSpan.Length) + { + throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}.", parameterName); + } + } + + /// + /// Verifies, that the `target` array has declared the length or longer. + /// + /// The element type of the spans + /// The target array. + /// The min length the array must have. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + [Conditional("DEBUG")] + public static void MustBeSizedAtLeast(T[] target, int minLength, string parameterName) + where T : struct + { + if (target.Length < minLength) + { + throw new ArgumentException($"The size must be at least {minLength}.", parameterName); + } + } + } +} \ No newline at end of file diff --git a/src/SixLabors.Core/Helpers/Guard.cs b/src/SixLabors.Core/Helpers/Guard.cs new file mode 100644 index 0000000000..ea92a85c02 --- /dev/null +++ b/src/SixLabors.Core/Helpers/Guard.cs @@ -0,0 +1,266 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace SixLabors +{ + /// + /// Provides methods to protect against invalid parameters. + /// + [DebuggerStepThrough] + internal static class Guard + { + /// + /// Verifies, that the method parameter with specified object value is not null + /// and throws an exception if it is found to be so. + /// + /// The target object, which cannot be null. + /// The name of the parameter that is to be checked. + /// The error message, if any to add to the exception. + /// is null + public static void NotNull(object target, string parameterName, string message = "") + { + if (target == null) + { + if (!string.IsNullOrWhiteSpace(message)) + { + throw new ArgumentNullException(parameterName, message); + } + + throw new ArgumentNullException(parameterName); + } + } + + /// + /// Verifies, that the string method parameter with specified object value and message + /// is not null, not empty and does not contain only blanks and throws an exception + /// if the object is null. + /// + /// The target string, which should be checked against being null or empty. + /// Name of the parameter. + /// The error message, if any to add to the exception. + /// is null. + /// is empty or contains only blanks. + public static void NotNullOrEmpty(string target, string parameterName, string message = "") + { + NotNull(target, parameterName, message); + + if (string.IsNullOrWhiteSpace(target)) + { + if (!string.IsNullOrWhiteSpace(message)) + { + throw new ArgumentException(message, parameterName); + } + + throw new ArgumentException("Value cannot be null, empty, or cannot contain only whitespace.", parameterName); + } + } + + /// + /// Verifies, that the enumeration is not null and not empty. + /// + /// The type of objects in the + /// The target enumeration, which should be checked against being null or empty. + /// Name of the parameter. + /// The error message, if any to add to the exception. + /// is null. + /// is empty. + public static void NotNullOrEmpty(IEnumerable target, string parameterName, string message = "") + { + NotNull(target, parameterName, message); + + if (!target.Any()) + { + if (!string.IsNullOrWhiteSpace(message)) + { + throw new ArgumentException(message, parameterName); + } + + throw new ArgumentException("Value cannot be empty.", parameterName); + } + } + + /// + /// Verifies that the specified value is less than a maximum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The maximum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is greater than the maximum value. + /// + public static void MustBeLessThan(TValue value, TValue max, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(max) >= 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}."); + } + } + + /// + /// Verifies that the specified value is less than or equal to a maximum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The maximum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is greater than the maximum value. + /// + public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(max) > 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}."); + } + } + + /// + /// Verifies that the specified value is greater than a minimum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The minimum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is less than the minimum value. + /// + public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(min) <= 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than {min}."); + } + } + + /// + /// Verifies that the specified value is greater than or equal to a minimum value + /// and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The minimum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is less than the minimum value. + /// + public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(min) < 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}."); + } + } + + /// + /// Verifies that the specified value is greater than or equal to a minimum value and less than + /// or equal to a maximum value and throws an exception if it is not. + /// + /// The target value, which should be validated. + /// The minimum value. + /// The maximum value. + /// The name of the parameter that is to be checked. + /// The type of the value. + /// + /// is less than the minimum value of greater than the maximum value. + /// + public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TValue max, string parameterName) + where TValue : IComparable + { + if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) + { + throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min} and less than or equal to {max}."); + } + } + + /// + /// Verifies, that the method parameter with specified target value is true + /// and throws an exception if it is found to be so. + /// + /// + /// The target value, which cannot be false. + /// + /// + /// The name of the parameter that is to be checked. + /// + /// + /// The error message, if any to add to the exception. + /// + /// + /// is false + /// + public static void IsTrue(bool value, string parameterName, string message) + { + if (!value) + { + throw new ArgumentException(message, parameterName); + } + } + + /// + /// Verifies, that the method parameter with specified target value is false + /// and throws an exception if it is found to be so. + /// + /// The target value, which cannot be true. + /// The name of the parameter that is to be checked. + /// The error message, if any to add to the exception. + /// + /// is true + /// + public static void IsFalse(bool value, string parameterName, string message) + { + if (value) + { + throw new ArgumentException(message, parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minLength', or longer. + /// + /// The element type of the spans + /// The target span. + /// The minimum length. + /// The name of the parameter that is to be checked. + /// + /// The length of is less than . + /// + public static void MustBeSizedAtLeast(T[] value, int minLength, string parameterName) + { + if (value.Length < minLength) + { + throw new ArgumentException($"The size must be at least {minLength}.", parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minLength', or longer. + /// + /// The element type of the spans + /// The target span. + /// The minimum length. + /// The name of the parameter that is to be checked. + /// + /// The length of is less than . + /// + public static void MustBeSizedAtLeast(ReadOnlySpan value, int minLength, string parameterName) + { + if (value.Length < minLength) + { + throw new ArgumentException($"The size must be at least {minLength}.", parameterName); + } + } + } +} diff --git a/src/SixLabors.Core/Helpers/HashHelpers.cs b/src/SixLabors.Core/Helpers/HashHelpers.cs new file mode 100644 index 0000000000..40a2b3f96a --- /dev/null +++ b/src/SixLabors.Core/Helpers/HashHelpers.cs @@ -0,0 +1,28 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors +{ + /// + /// Lifted from coreFX repo + /// + internal static class HashHelpers + { + /// + /// Combines the two specified hash codes. + /// + /// Hash code one + /// Hash code two + /// Returns a hash code for the two specified has codes. + public static int Combine(int h1, int h2) + { + unchecked + { + // RyuJIT optimizes this to use the ROL instruction + // Related GitHub pull request: dotnet/coreclr#1830 + uint rol5 = ((uint)h1 << 5) | ((uint)h1 >> 27); + return ((int)rol5 + h1) ^ h2; + } + } + } +} diff --git a/src/SixLabors.Core/MathF.cs b/src/SixLabors.Core/MathF.cs index b05914e74a..a4ad9ed666 100644 --- a/src/SixLabors.Core/MathF.cs +++ b/src/SixLabors.Core/MathF.cs @@ -1,13 +1,11 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Runtime.CompilerServices; namespace SixLabors { - using System; - using System.Runtime.CompilerServices; - /// /// Provides single-precision floating point constants and static methods for trigonometric, logarithmic, and other common mathematical functions. /// diff --git a/src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs b/src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs index 626be8ed74..80c4f46009 100644 --- a/src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs +++ b/src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs @@ -1,12 +1,10 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System.Numerics; namespace SixLabors.Primitives { - using System.Numerics; - /// /// Extension methods for the struct. /// diff --git a/src/SixLabors.Core/Primitives/Point.cs b/src/SixLabors.Core/Primitives/Point.cs index ca808c7ef5..1992d39226 100644 --- a/src/SixLabors.Core/Primitives/Point.cs +++ b/src/SixLabors.Core/Primitives/Point.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Represents an ordered pair of integer x- and y-coordinates that defines a point in /// a two-dimensional plane. @@ -182,7 +180,7 @@ namespace SixLabors.Primitives /// Translates a by the negative of a given value /// /// The point on the left hand of the operand. - /// The size on the right hand of the operand. + /// The value on the right hand of the operand. /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Point Multiply(Point point, int value) => new Point(unchecked(point.X * value), unchecked(point.Y * value)); @@ -212,6 +210,17 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Point Round(PointF point) => new Point(unchecked((int)MathF.Round(point.X)), unchecked((int)MathF.Round(point.Y))); + /// + /// Transforms a point by the given matrix. + /// + /// The source point. + /// The transformation matrix. + /// A transformed point. + public static PointF Transform(Point position, Matrix3x2 matrix) + { + return Vector2.Transform(position, matrix); + } + /// /// Converts a to a by performing a truncate operation on all the coordinates. /// @@ -235,7 +244,7 @@ namespace SixLabors.Primitives /// Rotation matrix used /// The rotated [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Point Rotate(Point point, System.Numerics.Matrix3x2 rotation) => Round(Vector2.Transform(new Vector2(point.X, point.Y), rotation)); + public static Point Rotate(Point point, Matrix3x2 rotation) => Round(Vector2.Transform(new Vector2(point.X, point.Y), rotation)); /// /// Skews a point using the given skew matrix. @@ -244,7 +253,7 @@ namespace SixLabors.Primitives /// Rotation matrix used /// The rotated [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Point Skew(Point point, System.Numerics.Matrix3x2 skew) => Round(Vector2.Transform(new Vector2(point.X, point.Y), skew)); + public static Point Skew(Point point, Matrix3x2 skew) => Round(Vector2.Transform(new Vector2(point.X, point.Y), skew)); /// /// Translates this by the specified amount. @@ -289,16 +298,5 @@ namespace SixLabors.Primitives private static short LowInt16(int n) => unchecked((short)(n & 0xffff)); private int GetHashCode(Point point) => HashHelpers.Combine(point.X.GetHashCode(), point.Y.GetHashCode()); - - /// - /// Transforms a point by the given matrix. - /// - /// The source point - /// The transformation matrix. - /// - public static PointF Transform(Point position, Matrix3x2 matrix) - { - return Vector2.Transform(position, matrix); - } } } \ No newline at end of file diff --git a/src/SixLabors.Core/Primitives/PointF.cs b/src/SixLabors.Core/Primitives/PointF.cs index e2bfa5e504..454e69e5cb 100644 --- a/src/SixLabors.Core/Primitives/PointF.cs +++ b/src/SixLabors.Core/Primitives/PointF.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Represents an ordered pair of single precision floating point x- and y-coordinates that defines a point in /// a two-dimensional plane. @@ -258,6 +256,17 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public static PointF Skew(PointF point, Matrix3x2 skew) => Vector2.Transform(new Vector2(point.X, point.Y), skew); + /// + /// Transforms a point by the given matrix. + /// + /// The source point. + /// The transformation matrix. + /// A transformed point. + public static PointF Transform(PointF position, Matrix3x2 matrix) + { + return Vector2.Transform(position, matrix); + } + /// /// Translates this by the specified amount. /// @@ -303,16 +312,5 @@ namespace SixLabors.Primitives /// A 32-bit signed integer that is the hash code for this instance. /// private int GetHashCode(PointF point) => HashHelpers.Combine(point.X.GetHashCode(), point.Y.GetHashCode()); - - /// - /// Transforms a point by the given matrix. - /// - /// The source point - /// The transformation matrix. - /// - public static PointF Transform(PointF position, Matrix3x2 matrix) - { - return Vector2.Transform(position, matrix); - } } } \ No newline at end of file diff --git a/src/SixLabors.Core/Primitives/Rectangle.cs b/src/SixLabors.Core/Primitives/Rectangle.cs index 981eee523e..27cba5d15e 100644 --- a/src/SixLabors.Core/Primitives/Rectangle.cs +++ b/src/SixLabors.Core/Primitives/Rectangle.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Stores a set of four integers that represent the location and size of a rectangle. /// @@ -276,6 +274,19 @@ namespace SixLabors.Primitives } } + /// + /// Transforms a rectangle by the given matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// A transformed rectangle. + public static RectangleF Transform(Rectangle rectangle, Matrix3x2 matrix) + { + PointF bottomRight = Point.Transform(new Point(rectangle.Right, rectangle.Bottom), matrix); + PointF topLeft = Point.Transform(rectangle.Location, matrix); + return new RectangleF(topLeft, new SizeF(bottomRight - topLeft)); + } + /// /// Converts a to a by performing a truncate operation on all the coordinates. /// @@ -455,18 +466,5 @@ namespace SixLabors.Primitives hashCode = HashHelpers.Combine(hashCode, rectangle.Height.GetHashCode()); return hashCode; } - - /// - /// Transforms a rectangle by the given matrix. - /// - /// The source rectangle - /// The transformation matrix. - /// - public static RectangleF Transform(Rectangle rectangle, Matrix3x2 matrix) - { - PointF bottomRight = Point.Transform(new Point(rectangle.Right, rectangle.Bottom), matrix); - PointF topLeft = Point.Transform(rectangle.Location, matrix); - return new RectangleF(topLeft, new SizeF(bottomRight - topLeft)); - } } } \ No newline at end of file diff --git a/src/SixLabors.Core/Primitives/RectangleF.cs b/src/SixLabors.Core/Primitives/RectangleF.cs index a126008bf3..09fa6f1ab4 100644 --- a/src/SixLabors.Core/Primitives/RectangleF.cs +++ b/src/SixLabors.Core/Primitives/RectangleF.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Stores a set of four single precision floating points that represent the location and size of a rectangle. /// @@ -251,6 +249,19 @@ namespace SixLabors.Primitives return r; } + /// + /// Transforms a rectangle by the given matrix. + /// + /// The source rectangle. + /// The transformation matrix. + /// A transformed rectangle. + public static RectangleF Transform(RectangleF rectangle, Matrix3x2 matrix) + { + PointF bottomRight = PointF.Transform(new PointF(rectangle.Right, rectangle.Bottom), matrix); + PointF topLeft = PointF.Transform(rectangle.Location, matrix); + return new RectangleF(topLeft, new SizeF(bottomRight - topLeft)); + } + /// /// Creates a rectangle that represents the union between and . /// @@ -388,18 +399,5 @@ namespace SixLabors.Primitives hashCode = HashHelpers.Combine(hashCode, rectangle.Height.GetHashCode()); return hashCode; } - - /// - /// Transforms a rectangle by the given matrix. - /// - /// The source rectangle - /// The transformation matrix. - /// - public static RectangleF Transform(RectangleF rectangle, Matrix3x2 matrix) - { - PointF bottomRight = PointF.Transform(new PointF(rectangle.Right, rectangle.Bottom), matrix); - PointF topLeft = PointF.Transform(rectangle.Location, matrix); - return new RectangleF(topLeft, new SizeF(bottomRight - topLeft)); - } } } \ No newline at end of file diff --git a/src/SixLabors.Core/Primitives/Size.cs b/src/SixLabors.Core/Primitives/Size.cs index 77aa037cf6..57884cf5dc 100644 --- a/src/SixLabors.Core/Primitives/Size.cs +++ b/src/SixLabors.Core/Primitives/Size.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Stores an ordered pair of integers, which specify a height and width. /// @@ -216,24 +214,6 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Size Subtract(Size left, Size right) => new Size(unchecked(left.Width - right.Width), unchecked(left.Height - right.Height)); - /// - /// Multiplies by an producing . - /// - /// Multiplicand of type . - /// Multiplier of type . - /// Product of type . - private static Size Multiply(Size size, int multiplier) => - new Size(unchecked(size.Width * multiplier), unchecked(size.Height * multiplier)); - - /// - /// Multiplies by a producing . - /// - /// Multiplicand of type . - /// Multiplier of type . - /// Product of type SizeF. - private static SizeF Multiply(Size size, float multiplier) => - new SizeF(size.Width * multiplier, size.Height * multiplier); - /// /// Converts a to a by performing a ceiling operation on all the dimensions. /// @@ -250,6 +230,19 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Size Round(SizeF size) => new Size(unchecked((int)MathF.Round(size.Width)), unchecked((int)MathF.Round(size.Height))); + /// + /// Transforms a size by the given matrix. + /// + /// The source size + /// The transformation matrix. + /// A transformed size. + public static SizeF Transform(Size size, Matrix3x2 matrix) + { + var v = Vector2.Transform(new Vector2(size.Width, size.Height), matrix); + + return new SizeF(v.X, v.Y); + } + /// /// Converts a to a by performing a round operation on all the dimensions. /// @@ -274,6 +267,24 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Size other) => this.Width == other.Width && this.Height == other.Height; + /// + /// Multiplies by an producing . + /// + /// Multiplicand of type . + /// Multiplier of type . + /// Product of type . + private static Size Multiply(Size size, int multiplier) => + new Size(unchecked(size.Width * multiplier), unchecked(size.Height * multiplier)); + + /// + /// Multiplies by a producing . + /// + /// Multiplicand of type . + /// Multiplier of type . + /// Product of type SizeF. + private static SizeF Multiply(Size size, float multiplier) => + new SizeF(size.Width * multiplier, size.Height * multiplier); + /// /// Returns the hash code for this instance. /// @@ -284,18 +295,5 @@ namespace SixLabors.Primitives /// A 32-bit signed integer that is the hash code for this instance. /// private int GetHashCode(Size size) => HashHelpers.Combine(size.Width.GetHashCode(), size.Height.GetHashCode()); - - /// - /// Transforms a size by the given matrix. - /// - /// The source size - /// The transformation matrix. - /// - public static SizeF Transform(Size size, Matrix3x2 matrix) - { - var v = Vector2.Transform(new Vector2(size.Width, size.Height), matrix); - - return new SizeF(v.X, v.Y); - } } } \ No newline at end of file diff --git a/src/SixLabors.Core/Primitives/SizeF.cs b/src/SixLabors.Core/Primitives/SizeF.cs index 9c44e23557..13c6552ac3 100644 --- a/src/SixLabors.Core/Primitives/SizeF.cs +++ b/src/SixLabors.Core/Primitives/SizeF.cs @@ -1,15 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.ComponentModel; +using System.Numerics; +using System.Runtime.CompilerServices; namespace SixLabors.Primitives { - using System; - using System.ComponentModel; - using System.Numerics; - using System.Runtime.CompilerServices; - /// /// Stores an ordered pair of single precision floating points, which specify a height and width. /// @@ -72,6 +70,16 @@ namespace SixLabors.Primitives [EditorBrowsable(EditorBrowsableState.Never)] public bool IsEmpty => this.Equals(Empty); + /// + /// Creates a with the coordinates of the specified . + /// + /// The point. + /// + /// The . + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Vector2(SizeF point) => new Vector2(point.Width, point.Height); + /// /// Creates a with the dimensions of the specified by truncating each of the dimensions. /// @@ -177,13 +185,17 @@ namespace SixLabors.Primitives public static SizeF Subtract(SizeF left, SizeF right) => new SizeF(left.Width - right.Width, left.Height - right.Height); /// - /// Multiplies by a producing . + /// Transforms a size by the given matrix. /// - /// Multiplicand of type . - /// Multiplier of type . - /// Product of type SizeF. - private static SizeF Multiply(SizeF size, float multiplier) => - new SizeF(size.Width * multiplier, size.Height * multiplier); + /// The source size. + /// The transformation matrix. + /// A transformed size. + public static SizeF Transform(SizeF size, Matrix3x2 matrix) + { + var v = Vector2.Transform(new Vector2(size.Width, size.Height), matrix); + + return new SizeF(v.X, v.Y); + } /// public override int GetHashCode() @@ -204,29 +216,15 @@ namespace SixLabors.Primitives [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(SizeF other) => this.Width.Equals(other.Width) && this.Height.Equals(other.Height); - private int GetHashCode(SizeF size) => HashHelpers.Combine(size.Width.GetHashCode(), size.Height.GetHashCode()); - /// - /// Creates a with the coordinates of the specified . - /// - /// The point. - /// - /// The . - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Vector2(SizeF point) => new Vector2(point.Width, point.Height); - - /// - /// Transforms a size by the given matrix. + /// Multiplies by a producing . /// - /// The source size - /// The transformation matrix. - /// - public static SizeF Transform(SizeF size, Matrix3x2 matrix) - { - var v = Vector2.Transform(new Vector2(size.Width, size.Height), matrix); + /// Multiplicand of type . + /// Multiplier of type . + /// Product of type SizeF. + private static SizeF Multiply(SizeF size, float multiplier) => + new SizeF(size.Width * multiplier, size.Height * multiplier); - return new SizeF(v.X, v.Y); - } + private int GetHashCode(SizeF size) => HashHelpers.Combine(size.Width.GetHashCode(), size.Height.GetHashCode()); } } \ No newline at end of file diff --git a/src/Shared/AssemblyInfo.Common.cs b/src/SixLabors.Core/Properties/AssemblyInfo.cs similarity index 66% rename from src/Shared/AssemblyInfo.Common.cs rename to src/SixLabors.Core/Properties/AssemblyInfo.cs index c9cc15b730..368bab2485 100644 --- a/src/Shared/AssemblyInfo.Common.cs +++ b/src/SixLabors.Core/Properties/AssemblyInfo.cs @@ -1,7 +1,5 @@ -// -// Copyright (c) Scott Williams and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// using System.Reflection; using System.Resources; @@ -10,6 +8,7 @@ using System.Runtime.CompilerServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. +[assembly: AssemblyTitle("SixLabors.Core")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Six Labors")] @@ -29,9 +28,16 @@ using System.Runtime.CompilerServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: AssemblyInformationalVersion("1.0.0.0")] +[assembly: AssemblyVersion("0.1.0")] +[assembly: AssemblyFileVersion("0.1.0")] +[assembly: AssemblyInformationalVersion("0.1.0-alpha02")] // Ensure the internals can be tested. [assembly: InternalsVisibleTo("SixLabors.Core.Tests")] + +// Ensure the internals are visible to the other projects. +[assembly: InternalsVisibleTo("SixLabors.Exif")] +[assembly: InternalsVisibleTo("SixLabors.Fonts")] +[assembly: InternalsVisibleTo("SixLabors.ImageSharp")] +[assembly: InternalsVisibleTo("SixLabors.ImageSharp.Drawing")] +[assembly: InternalsVisibleTo("SixLabors.Shapes")] diff --git a/src/SixLabors.Core/SixLabors.Core.csproj b/src/SixLabors.Core/SixLabors.Core.csproj index f7f25dfc18..37d4b90130 100644 --- a/src/SixLabors.Core/SixLabors.Core.csproj +++ b/src/SixLabors.Core/SixLabors.Core.csproj @@ -2,9 +2,8 @@ Low level primitives for use across Six Labors projects.. - SixLabors.Primitives $(packageversion) - 0.1.0-alpha1 + 0.1.0-alpha2 Six Labors netstandard1.1 true @@ -26,16 +25,26 @@ false false false + false full SixLabors + + ..\SixLabors.ruleset + + - + + + + All + + \ No newline at end of file diff --git a/src/SixLabors.Core/stylecop.json b/src/SixLabors.Core/stylecop.json deleted file mode 100644 index de88a8bfe6..0000000000 --- a/src/SixLabors.Core/stylecop.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", - "settings": { - "documentationRules": { - "companyName": "Six Labors", - "copyrightText": "Copyright (c) Six Labors and contributors.\nLicensed under the Apache License, Version 2.0." - } - } -} \ No newline at end of file diff --git a/SixLabors.Core.ruleset b/src/SixLabors.ruleset similarity index 100% rename from SixLabors.Core.ruleset rename to src/SixLabors.ruleset diff --git a/stylecop.json b/stylecop.json new file mode 100644 index 0000000000..c67c0db325 --- /dev/null +++ b/stylecop.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": + { + "orderingRules": + { + "usingDirectivesPlacement": "outsideNamespace" + }, + "documentationRules": + { + "xmlHeader": false, + "copyrightText": "Copyright (c) Six Labors and contributors.\nLicensed under the Apache License, Version 2.0." + } + } +} \ No newline at end of file diff --git a/tests/CodeCoverage/CodeCoverage.cmd b/tests/CodeCoverage/CodeCoverage.cmd index 7a89ab210e..d5318bf7a6 100644 --- a/tests/CodeCoverage/CodeCoverage.cmd +++ b/tests/CodeCoverage/CodeCoverage.cmd @@ -7,12 +7,10 @@ nuget restore packages.config -PackagesDirectory . cd .. cd .. -dotnet restore SixLabors.Core.sln -dotnet build SixLabors.Core.sln --no-incremental -c debug /p:codecov=true - -rem The -threshold options prevents this taking ages... -rem tests\CodeCoverage\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"dotnet.exe" -targetargs:"test tests\SixLabors.Shapes.Tests\SixLabors.Shapes.Tests.csproj --no-build -c Release /p:codecov=true" -threshold:10 -register:user -filter:"+[SixLabors.Shapes*]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -returntargetcode -output:.\SixLabors.Shapes.Coverage.xml -tests\CodeCoverage\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"dotnet.exe" -targetargs:"test tests\SixLabors.Core.Tests\SixLabors.Core.Tests.csproj --no-build -c debug" -searchdirs:"tests\SixLabors.Core.Tests\bin\Release\netcoreapp1.1" -register:user -output:.\SixLabors.Core.Coverage.xml -hideskipped:All -returntargetcode -oldStyle -filter:"+[SixLabors.*]*" +dotnet restore SixLabors.Core.sln +dotnet build SixLabors.Core.sln --no-incremental -c release /p:codecov=true + +tests\CodeCoverage\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"dotnet.exe" -targetargs:"test tests\SixLabors.Core.Tests\SixLabors.Core.Tests.csproj --no-build -c release" -searchdirs:"tests\SixLabors.Core.Tests\bin\Release\netcoreapp1.1" -register:user -output:.\SixLabors.Core.Coverage.xml -hideskipped:All -returntargetcode -oldStyle -filter:"+[SixLabors.*]*" if %errorlevel% neq 0 exit /b %errorlevel% diff --git a/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs b/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs new file mode 100644 index 0000000000..1e651f2674 --- /dev/null +++ b/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs @@ -0,0 +1,258 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +// tell this file to enable debug conditional method calls, i.e. all the debug guard calls +#define DEBUG + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using Xunit; + +namespace SixLabors.Helpers.Tests +{ + public class DebugGuardTests + { + [Fact] + public void AllStaticMethodsOnOnDebugGuardHaveDEBUGConditional() + { + var methods = typeof(DebugGuard).GetTypeInfo().GetMethods() + .Where(x => x.IsStatic); + + foreach (var m in methods) + { + var attribs = m.GetCustomAttributes(); + Assert.True(attribs.Select(x => x.ConditionString).Contains("DEBUG"), $"Method '{m.Name}' does not have [Conditional(\"DEBUG\")] set."); + } + } + + [Fact] + public void NotNull_TargetNotNull_ThrowsNoException() + { + DebugGuard.NotNull("test", "myParamName"); + } + + [Fact] + public void NotNull_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + DebugGuard.NotNull(null, "myParamName"); + }); + } + + [Fact] + public void MustBeLessThan_IsLess_ThrowsNoException() + { + DebugGuard.MustBeLessThan(0, 1, "myParamName"); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max) + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeLessThan(value, max, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than {max}.")); + } + + [Theory] + [InlineData(0, 1)] + [InlineData(1, 1)] + public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max) + { + DebugGuard.MustBeLessThanOrEqualTo(value, max, "myParamName"); + } + + [Fact] + public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeLessThanOrEqualTo(2, 1, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than or equal to 1.")); + } + + [Fact] + public void MustBeGreaterThan_IsGreater_ThrowsNoException() + { + DebugGuard.MustBeGreaterThan(2, 1, "myParamName"); + } + + [Theory] + [InlineData(1, 2)] + [InlineData(1, 1)] + public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min) + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeGreaterThan(value, min, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than {min}.")); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min) + { + DebugGuard.MustBeGreaterThanOrEqualTo(value, min, "myParamName"); + } + + [Fact] + public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2.")); + } + + [Fact] + public void IsTrue_IsTrue_ThrowsNoException() + { + DebugGuard.IsTrue(true, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsTrue_IsFalse_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.IsTrue(false, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void IsFalse_IsFalse_ThrowsNoException() + { + DebugGuard.IsFalse(false, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsFalse_IsTrue_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.IsFalse(true, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Theory] + [InlineData(new int[] { 1, 2 }, 1)] + [InlineData(new int[] { 1, 2 }, 2)] + public void MustBeSizedAtLeast_Array_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength) + { + DebugGuard.MustBeSizedAtLeast(value, minLength, "myParamName"); + } + + [Fact] + public void MustBeSizedAtLeast_Array_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeSizedAtLeast(new int[] { 1, 2 }, 3, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.Contains($"The size must be at least 3.", exception.Message); + } + + [Fact] + public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[3]), "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.Contains($"Span-s must be at least of length 3.", exception.Message); + } + + [Fact] + public void MustBeSizedAtLeast_Span_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeSizedAtLeast(new Span(new int[2]), new Span(new int[3]), "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.Contains($"Span-s must be at least of length 3.", exception.Message); + } + + [Fact] + public void MustBeSameSized_ReadOnlySpan_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeSameSized(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[3]), "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.Contains($"Span-s must be the same size.", exception.Message); + } + + [Fact] + public void MustBeSameSized_Span_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + DebugGuard.MustBeSameSized(new Span(new int[2]), new Span(new int[3]), "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.Contains($"Span-s must be the same size.", exception.Message); + } + + [Theory] + [InlineData(2, 2)] + [InlineData(4, 3)] + public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsEqualOrGreater_DoesNotThowException(int leftSize, int rightSize) + { + DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan(new int[leftSize]), new ReadOnlySpan(new int[rightSize]), "myParamName"); + } + + [Theory] + [InlineData(2, 2)] + [InlineData(4, 3)] + public void MustBeSizedAtLeast_Span_LengthIsEqualOrGreater_DoesNotThowException(int leftSize, int rightSize) + { + DebugGuard.MustBeSizedAtLeast(new Span(new int[leftSize]), new Span(new int[rightSize]), "myParamName"); + } + + [Fact] + public void MustBeSameSized_ReadOnlySpan_LengthIsEqual_DoesNotThrowException() + { + DebugGuard.MustBeSameSized(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[2]), "myParamName"); + } + + [Fact] + public void MustBeSameSized_Span_LengthIsEqual_DoesNotThrowException() + { + DebugGuard.MustBeSameSized(new Span(new int[2]), new Span(new int[2]), "myParamName"); + } + } +} diff --git a/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs b/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs new file mode 100644 index 0000000000..b7f859f358 --- /dev/null +++ b/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs @@ -0,0 +1,326 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace SixLabors.Helpers.Tests +{ + public class GuardTests + { + [Fact] + public void NotNull_TargetNotNull_ThrowsNoException() + { + Guard.NotNull("test", "myParamName"); + } + + [Fact] + public void NotNull_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNull(null, "myParamName"); + }); + } + + [Fact] + public void NotNull_TargetNullWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNull(null, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void NotNullOrEmpty_TargetNotNullOrEmpty_ThrowsNoException() + { + Guard.NotNullOrEmpty("test", "myParamName"); + } + + [Fact] + public void NotNullOrEmpty_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty(null, "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmpty_TargetWhitespace_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty("\n\n", "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmpty_TargetEmpty_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(string.Empty, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("Value cannot be null, empty, or cannot contain only whitespace.")); + } + + [Fact] + public void NotNullOrEmpty_TargetEmptyWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(string.Empty, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetNotNullOrEmpty_ThrowsNoException() + { + Guard.NotNullOrEmpty(new string[] { "test" }, "myParamName"); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty((IEnumerable)null, "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetEmpty_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(new string[] { }, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("Value cannot be empty.")); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetEmptyWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(new string[] { }, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void MustBeLessThan_IsLess_ThrowsNoException() + { + Guard.MustBeLessThan(0, 1, "myParamName"); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max) + { + var exception = Assert.Throws(() => + { + Guard.MustBeLessThan(value, max, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than {max}.")); + } + + [Theory] + [InlineData(0, 1)] + [InlineData(1, 1)] + public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max) + { + Guard.MustBeLessThanOrEqualTo(value, max, "myParamName"); + } + + [Fact] + public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeLessThanOrEqualTo(2, 1, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than or equal to 1.")); + } + + [Fact] + public void MustBeGreaterThan_IsGreater_ThrowsNoException() + { + Guard.MustBeGreaterThan(2, 1, "myParamName"); + } + + [Theory] + [InlineData(1, 2)] + [InlineData(1, 1)] + public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min) + { + var exception = Assert.Throws(() => + { + Guard.MustBeGreaterThan(value, min, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than {min}.")); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min) + { + Guard.MustBeGreaterThanOrEqualTo(value, min, "myParamName"); + } + + [Fact] + public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2.")); + } + + [Theory] + [InlineData(1, 1, 3)] + [InlineData(2, 1, 3)] + [InlineData(3, 1, 3)] + public void MustBeBetweenOrEqualTo_IsBetweenOrEqual_ThrowsNoException(int value, int min, int max) + { + Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName"); + } + + [Theory] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void MustBeBetweenOrEqualTo_IsLessOrGreater_ThrowsNoException(int value, int min, int max) + { + var exception = Assert.Throws(() => + { + Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}.")); + } + + [Fact] + public void IsTrue_IsTrue_ThrowsNoException() + { + Guard.IsTrue(true, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsTrue_IsFalse_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.IsTrue(false, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void IsFalse_IsFalse_ThrowsNoException() + { + Guard.IsFalse(false, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsFalse_IsTrue_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.IsFalse(true, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(2, 2)] + public void MustBeSizedAtLeast_Array_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) + { + Guard.MustBeSizedAtLeast(new int[valueLength], minLength, "myParamName"); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(2, 2)] + public void MustBeSizedAtLeast_Span_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) + { + Guard.MustBeSizedAtLeast(new Span(new int[valueLength]), minLength, "myParamName"); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(2, 2)] + public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsGreaterOrEqual_ThrowsNoException(int valueLength, int minLength) + { + Guard.MustBeSizedAtLeast(new ReadOnlySpan(new int[valueLength]), minLength, "myParamName"); + } + + [Fact] + public void MustBeSizedAtLeast_Array_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeSizedAtLeast(new int[] { 1, 2 }, 3, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"The size must be at least 3.")); + } + + [Fact] + public void MustBeSizedAtLeast_Span_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeSizedAtLeast(new Span(new int[2]), 3, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"The size must be at least 3.")); + } + + [Fact] + public void MustBeSizedAtLeast_ReadOnlySpan_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeSizedAtLeast(new ReadOnlySpan(new int[2]), 3, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"The size must be at least 3.")); + } + } +} diff --git a/tests/SixLabors.Core.Tests/Primitives/PointFTests.cs b/tests/SixLabors.Core.Tests/Primitives/PointFTests.cs index b6294cb1a5..5a29d1cff6 100644 --- a/tests/SixLabors.Core.Tests/Primitives/PointFTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/PointFTests.cs @@ -1,22 +1,20 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Globalization; +using System.Numerics; +using System.Reflection; +using Xunit; namespace SixLabors.Primitives.Tests { - using System; - using System.Globalization; - using System.Numerics; - using System.Reflection; - using Xunit; - public class PointFTests { [Fact] public void DefaultConstructorTest() { - Assert.Equal(PointF.Empty, new PointF()); + Assert.Equal(PointF.Empty, default(PointF)); } [Theory] @@ -37,7 +35,7 @@ namespace SixLabors.Primitives.Tests public void IsEmptyDefaultsTest() { Assert.True(PointF.Empty.IsEmpty); - Assert.True(new PointF().IsEmpty); + Assert.True(default(PointF).IsEmpty); Assert.True(new PointF(0, 0).IsEmpty); } @@ -151,7 +149,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTest_NotPointF() + public void EqualityTest_NotPointF() { var point = new PointF(0, 0); Assert.False(point.Equals(null)); @@ -167,7 +165,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var point = new PointF(10, 10); Assert.Equal(point.GetHashCode(), new PointF(10, 10).GetHashCode()); diff --git a/tests/SixLabors.Core.Tests/Primitives/PointTests.cs b/tests/SixLabors.Core.Tests/Primitives/PointTests.cs index 3f17aebfcb..a1880d0e1e 100644 --- a/tests/SixLabors.Core.Tests/Primitives/PointTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/PointTests.cs @@ -1,21 +1,18 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System.Globalization; +using System.Numerics; +using Xunit; namespace SixLabors.Primitives.Tests { - using System.Globalization; - using System.Numerics; - - using Xunit; - public class PointTests { [Fact] public void DefaultConstructorTest() { - Assert.Equal(Point.Empty, new Point()); + Assert.Equal(Point.Empty, default(Point)); } [Theory] @@ -47,7 +44,7 @@ namespace SixLabors.Primitives.Tests public void IsEmptyDefaultsTest() { Assert.True(Point.Empty.IsEmpty); - Assert.True(new Point().IsEmpty); + Assert.True(default(Point).IsEmpty); Assert.True(new Point(0, 0).IsEmpty); } @@ -186,7 +183,7 @@ namespace SixLabors.Primitives.Tests public void EqualityTest(int x, int y) { var p1 = new Point(x, y); - var p2 = new Point(x / 2 - 1, y / 2 - 1); + var p2 = new Point((x / 2) - 1, (y / 2) - 1); var p3 = new Point(x, y); Assert.True(p1 == p3); @@ -205,7 +202,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTest_NotPoint() + public void EqualityTest_NotPoint() { var point = new Point(0, 0); Assert.False(point.Equals(null)); @@ -214,7 +211,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var point = new Point(10, 10); Assert.Equal(point.GetHashCode(), new Point(10, 10).GetHashCode()); diff --git a/tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs b/tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs index 4dc7d9d35a..463509eac8 100644 --- a/tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/RectangleFTests.cs @@ -1,16 +1,13 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Globalization; +using System.Reflection; +using Xunit; namespace SixLabors.Primitives.Tests { - using System; - using System.Globalization; - using System.Reflection; - - using Xunit; - /// /// Tests the struct. /// @@ -19,7 +16,7 @@ namespace SixLabors.Primitives.Tests [Fact] public void DefaultConstructorTest() { - Assert.Equal(RectangleF.Empty, new RectangleF()); + Assert.Equal(RectangleF.Empty, default(RectangleF)); } [Theory] @@ -77,7 +74,7 @@ namespace SixLabors.Primitives.Tests public void IsEmptyTest() { Assert.True(RectangleF.Empty.IsEmpty); - Assert.True(new RectangleF().IsEmpty); + Assert.True(default(RectangleF).IsEmpty); Assert.True(new RectangleF(1, -2, -10, 10).IsEmpty); Assert.True(new RectangleF(1, -2, 10, -10).IsEmpty); Assert.True(new RectangleF(1, -2, 0, 0).IsEmpty); @@ -88,7 +85,7 @@ namespace SixLabors.Primitives.Tests [Theory] [InlineData(0, 0)] [InlineData(float.MaxValue, float.MinValue)] - public static void LocationSetTest(float x, float y) + public void LocationSetTest(float x, float y) { var point = new PointF(x, y); var rect = new RectangleF(10, 10, 10, 10) { Location = point }; @@ -100,7 +97,7 @@ namespace SixLabors.Primitives.Tests [Theory] [InlineData(0, 0)] [InlineData(float.MaxValue, float.MinValue)] - public static void SizeSetTest(float x, float y) + public void SizeSetTest(float x, float y) { var size = new SizeF(x, y); var rect = new RectangleF(10, 10, 10, 10) { Size = size }; @@ -125,7 +122,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTestNotRectangleF() + public void EqualityTestNotRectangleF() { var rectangle = new RectangleF(0, 0, 0, 0); Assert.False(rectangle.Equals(null)); @@ -141,7 +138,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var rect1 = new RectangleF(10, 10, 10, 10); var rect2 = new RectangleF(10, 10, 10, 10); @@ -158,12 +155,12 @@ namespace SixLabors.Primitives.Tests public void ContainsTest(float x, float y, float width, float height) { var rect = new RectangleF(x, y, width, height); - float X = (x + width) / 2; - float Y = (y + height) / 2; - var p = new PointF(X, Y); - var r = new RectangleF(X, Y, width / 2, height / 2); + float x1 = (x + width) / 2; + float y1 = (y + height) / 2; + var p = new PointF(x1, y1); + var r = new RectangleF(x1, y1, width / 2, height / 2); - Assert.False(rect.Contains(X, Y)); + Assert.False(rect.Contains(x1, y1)); Assert.False(rect.Contains(p)); Assert.False(rect.Contains(r)); } @@ -175,7 +172,7 @@ namespace SixLabors.Primitives.Tests public void InflateTest(float x, float y, float width, float height) { var rect = new RectangleF(x, y, width, height); - var inflatedRect = new RectangleF(x - width, y - height, width + 2 * width, height + 2 * height); + var inflatedRect = new RectangleF(x - width, y - height, width + (2 * width), height + (2 * height)); rect.Inflate(width, height); Assert.Equal(inflatedRect, rect); @@ -201,7 +198,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void IntersectIntersectingRectsTest() + public void IntersectIntersectingRectsTest() { var rect1 = new RectangleF(0, 0, 5, 5); var rect2 = new RectangleF(1, 1, 3, 3); @@ -255,13 +252,5 @@ namespace SixLabors.Primitives.Tests var r = new RectangleF(5, 5.1F, 1.3F, 1); Assert.Equal(string.Format(CultureInfo.CurrentCulture, "RectangleF [ X={0}, Y={1}, Width={2}, Height={3} ]", r.X, r.Y, r.Width, r.Height), r.ToString()); } - - [InlineData(0, 0, 0, 0)] - [InlineData(5, -5, 0.2, -1.3)] - public void ToStringTestEmpty(float x, float y, float width, float height) - { - var r = new RectangleF(x, y, width, height); - Assert.Equal("RectangleF [ Empty ]", r.ToString()); - } } } \ No newline at end of file diff --git a/tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs b/tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs index 640b27d678..90354bd72e 100644 --- a/tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/RectangleTests.cs @@ -1,15 +1,12 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Globalization; +using Xunit; namespace SixLabors.Primitives.Tests { - using System; - using System.Globalization; - - using Xunit; - /// /// Tests the struct. /// @@ -18,7 +15,7 @@ namespace SixLabors.Primitives.Tests [Fact] public void DefaultConstructorTest() { - Assert.Equal(Rectangle.Empty, new Rectangle()); + Assert.Equal(Rectangle.Empty, default(Rectangle)); } [Theory] @@ -51,8 +48,8 @@ namespace SixLabors.Primitives.Tests public void EmptyTest() { Assert.True(Rectangle.Empty.IsEmpty); + Assert.True(default(Rectangle).IsEmpty); Assert.True(new Rectangle(0, 0, 0, 0).IsEmpty); - Assert.True(new Rectangle().IsEmpty); } [Theory] @@ -107,7 +104,7 @@ namespace SixLabors.Primitives.Tests [Theory] [InlineData(0, 0)] [InlineData(int.MaxValue, int.MinValue)] - public static void LocationSetTest(int x, int y) + public void LocationSetTest(int x, int y) { var point = new Point(x, y); var rect = new Rectangle(10, 10, 10, 10) { Location = point }; @@ -119,7 +116,7 @@ namespace SixLabors.Primitives.Tests [Theory] [InlineData(0, 0)] [InlineData(int.MaxValue, int.MinValue)] - public static void SizeSetTest(int x, int y) + public void SizeSetTest(int x, int y) { var size = new Size(x, y); var rect = new Rectangle(10, 10, 10, 10) { Size = size }; @@ -145,7 +142,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTestNotRectangle() + public void EqualityTestNotRectangle() { var rectangle = new Rectangle(0, 0, 0, 0); Assert.False(rectangle.Equals(null)); @@ -154,7 +151,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var rect1 = new Rectangle(10, 10, 10, 10); var rect2 = new Rectangle(10, 10, 10, 10); @@ -193,7 +190,7 @@ namespace SixLabors.Primitives.Tests [InlineData(0, int.MinValue, int.MaxValue, 0)] public void ContainsTest(int x, int y, int width, int height) { - var rect = new Rectangle(unchecked(2 * x - width), unchecked(2 * y - height), width, height); + var rect = new Rectangle(unchecked((2 * x) - width), unchecked((2 * y) - height), width, height); var p = new Point(x, y); var r = new Rectangle(x, y, width / 2, height / 2); @@ -211,7 +208,7 @@ namespace SixLabors.Primitives.Tests Rectangle inflatedRect, rect = new Rectangle(x, y, width, height); unchecked { - inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height); + inflatedRect = new Rectangle(x - width, y - height, width + (2 * width), height + (2 * height)); } Assert.Equal(inflatedRect, Rectangle.Inflate(rect, width, height)); @@ -222,7 +219,7 @@ namespace SixLabors.Primitives.Tests var s = new Size(x, y); unchecked { - inflatedRect = new Rectangle(rect.X - x, rect.Y - y, rect.Width + 2 * x, rect.Height + 2 * y); + inflatedRect = new Rectangle(rect.X - x, rect.Y - y, rect.Width + (2 * x), rect.Height + (2 * y)); } rect.Inflate(s); @@ -243,7 +240,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void IntersectIntersectingRectsTest() + public void IntersectIntersectingRectsTest() { var rect1 = new Rectangle(0, 0, 5, 5); var rect2 = new Rectangle(1, 1, 3, 3); diff --git a/tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs b/tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs index 1cd3858c2f..04363fa943 100644 --- a/tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/SizeFTests.cs @@ -1,21 +1,19 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Globalization; +using System.Reflection; +using Xunit; namespace SixLabors.Primitives.Tests { - using System; - using System.Globalization; - using System.Reflection; - using Xunit; - public class SizeFTests { [Fact] public void DefaultConstructorTest() { - Assert.Equal(SizeF.Empty, new SizeF()); + Assert.Equal(SizeF.Empty, default(SizeF)); } [Theory] @@ -47,7 +45,7 @@ namespace SixLabors.Primitives.Tests public void IsEmptyDefaultsTest() { Assert.True(SizeF.Empty.IsEmpty); - Assert.True(new SizeF().IsEmpty); + Assert.True(default(SizeF).IsEmpty); Assert.True(new SizeF(0, 0).IsEmpty); } @@ -106,7 +104,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTest_NotSizeF() + public void EqualityTest_NotSizeF() { var size = new SizeF(0, 0); Assert.False(size.Equals(null)); @@ -122,7 +120,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var size = new SizeF(10, 10); Assert.Equal(size.GetHashCode(), new SizeF(10, 10).GetHashCode()); diff --git a/tests/SixLabors.Core.Tests/Primitives/SizeTests.cs b/tests/SixLabors.Core.Tests/Primitives/SizeTests.cs index 0117e5914f..0cfb62dd5c 100644 --- a/tests/SixLabors.Core.Tests/Primitives/SizeTests.cs +++ b/tests/SixLabors.Core.Tests/Primitives/SizeTests.cs @@ -1,14 +1,12 @@ -// -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. -// + +using System; +using System.Globalization; +using Xunit; namespace SixLabors.Primitives.Tests { - using System; - using System.Globalization; - using Xunit; - /// /// Tests the struct. /// @@ -17,7 +15,7 @@ namespace SixLabors.Primitives.Tests [Fact] public void DefaultConstructorTest() { - Assert.Equal(Size.Empty, new Size()); + Assert.Equal(Size.Empty, default(Size)); } [Theory] @@ -43,7 +41,7 @@ namespace SixLabors.Primitives.Tests public void IsEmptyDefaultsTest() { Assert.True(Size.Empty.IsEmpty); - Assert.True(new Size().IsEmpty); + Assert.True(default(Size).IsEmpty); Assert.True(new Size(0, 0).IsEmpty); } @@ -162,7 +160,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void EqualityTest_NotSize() + public void EqualityTest_NotSize() { var size = new Size(0, 0); Assert.False(size.Equals(null)); @@ -171,7 +169,7 @@ namespace SixLabors.Primitives.Tests } [Fact] - public static void GetHashCodeTest() + public void GetHashCodeTest() { var size = new Size(10, 10); Assert.Equal(size.GetHashCode(), new Size(10, 10).GetHashCode()); @@ -238,7 +236,6 @@ namespace SixLabors.Primitives.Tests Assert.Equal(mulExpected, multiplier * sz1); } - [Theory] [InlineData(1000, 0.0f)] [InlineData(1000, 1.0f)] @@ -285,7 +282,6 @@ namespace SixLabors.Primitives.Tests Assert.Equal(mulExpected, multiplier * sz1); } - [Fact] public void DivideByZeroChecks() { diff --git a/tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj b/tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj index cd34a8421a..a80bf5a60e 100644 --- a/tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj +++ b/tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj @@ -16,7 +16,17 @@ SixLabors.Tests + + ..\SixLabors.ruleset + + + + + + + + diff --git a/tests/SixLabors.ruleset b/tests/SixLabors.ruleset new file mode 100644 index 0000000000..9729157125 --- /dev/null +++ b/tests/SixLabors.ruleset @@ -0,0 +1,10 @@ + + + + + + + + + +