From da25a7f51f22add0cbfc2545df89a93732bb634a Mon Sep 17 00:00:00 2001 From: James South Date: Tue, 5 Aug 2014 14:43:07 +0100 Subject: [PATCH] Moar tests plus known colours! Former-commit-id: f829fb7fa90a3ce36088ff67c48ea8841ec57f0c --- .../RegularExpressionUnitTests.cs | 82 ++++++++++++++----- .../Helpers/CommonParameterParserUtility.cs | 42 +++++++++- .../NET45/Processors/Format.cs | 1 - 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs index 86c346e38..db81f4533 100644 --- a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs +++ b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs @@ -29,18 +29,25 @@ namespace ImageProcessor.Web.UnitTests /// /// The alpha regex unit test. /// + /// + /// The input string. + /// + /// + /// The expected result. + /// [Test] - public void TestAlphaRegex() + [TestCase("alpha=66", 66)] + [TestCase("alpha=-66", 66)] + [TestCase("alpha=101", 1)] + [TestCase("alpha=-101", 1)] + [TestCase("alpha=000053", 53)] + public void TestAlphaRegex(string input, int expected) { - const string Querystring = "alpha=56"; - const int Expected = 56; - Processors.Alpha alpha = new Processors.Alpha(); - alpha.MatchRegexIndex(Querystring); - - int actual = alpha.Processor.DynamicParameter; + alpha.MatchRegexIndex(input); + int result = alpha.Processor.DynamicParameter; - Assert.AreEqual(Expected, actual); + Assert.AreEqual(expected, result); } /// @@ -95,6 +102,32 @@ namespace ImageProcessor.Web.UnitTests Assert.AreEqual(expected, result); } + /// + /// The saturation regex unit test. + /// + /// + /// The input string. + /// + /// + /// The expected result. + /// + [Test] + [TestCase("saturation=56", 56)] + [TestCase("saturation=84", 84)] + [TestCase("saturation=66", 66)] + [TestCase("saturation=101", 1)] + [TestCase("saturation=00001", 1)] + [TestCase("saturation=-50", -50)] + [TestCase("saturation=0", 0)] + public void TestSaturationRegex(string input, int expected) + { + Processors.Saturation saturation = new Processors.Saturation(); + saturation.MatchRegexIndex(input); + int result = saturation.Processor.DynamicParameter; + + Assert.AreEqual(expected, result); + } + /// /// The rotate regex unit test. /// @@ -288,20 +321,29 @@ namespace ImageProcessor.Web.UnitTests [Test] public void TestTintRegex() { - const string HexQuerystring = "tint=6aa6cc"; - const string RgbaQuerystring = "tint=106,166,204,255"; - Color expectedHex = ColorTranslator.FromHtml("#" + "6aa6cc"); - Color expectedRgba = Color.FromArgb(255, 106, 166, 204); + Dictionary data = new Dictionary + { + { + "tint=6aa6cc", ColorTranslator.FromHtml("#" + "6aa6cc") + }, + { + "tint=106,166,204,255", Color.FromArgb(255, 106, 166, 204) + }, + { + "tint=fff", Color.FromArgb(255, 255, 255, 255) + }, + { + "tint=white", Color.White + } + }; Processors.Tint tint = new Processors.Tint(); - tint.MatchRegexIndex(HexQuerystring); - Color actualHex = tint.Processor.DynamicParameter; - Assert.AreEqual(expectedHex, actualHex); - - tint = new Processors.Tint(); - tint.MatchRegexIndex(RgbaQuerystring); - Color actualRgba = tint.Processor.DynamicParameter; - Assert.AreEqual(expectedRgba, actualRgba); + foreach (KeyValuePair item in data) + { + tint.MatchRegexIndex(item.Key); + Color result = tint.Processor.DynamicParameter; + Assert.AreEqual(item.Value, result); + } } } } \ No newline at end of file diff --git a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs index 22c266a85..3e8362d08 100644 --- a/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs +++ b/src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs @@ -11,8 +11,10 @@ namespace ImageProcessor.Web.Helpers { using System; + using System.Collections.Generic; using System.Drawing; using System.Globalization; + using System.Text; using System.Text.RegularExpressions; using ImageProcessor.Common.Extensions; @@ -24,10 +26,15 @@ namespace ImageProcessor.Web.Helpers /// public static class CommonParameterParserUtility { + /// + /// The collection of known colors. + /// + private static readonly Dictionary KnownColors = new Dictionary(); + /// /// The regular expression to search strings for colors. /// - private static readonly Regex ColorRegex = new Regex(@"(bgcolor|color|tint|vignette)(=|-)(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2})", RegexOptions.Compiled); + private static readonly Regex ColorRegex = BuildColorRegex(); /// /// The regular expression to search strings for angles. @@ -93,6 +100,11 @@ namespace ImageProcessor.Web.Helpers { string value = match.Value.Split(new[] { '=', '-' })[1]; + if (KnownColors.ContainsKey(value)) + { + return Color.FromKnownColor(KnownColors[value]); + } + if (value.Contains(",")) { int[] split = value.ToPositiveIntegerArray(); @@ -224,5 +236,33 @@ namespace ImageProcessor.Web.Helpers return 0; } + + /// + /// Builds a regular expression for the three main colour types. + /// + /// + /// The to match colors. + /// + private static Regex BuildColorRegex() + { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append(@"(bgcolor|color|tint|vignette)(=|-)(\d+,\d+,\d+,\d+|([0-9a-fA-F]{3}){1,2}|("); + + KnownColor[] knownColors = (KnownColor[])Enum.GetValues(typeof(KnownColor)); + + for (int i = 0; i < knownColors.Length; i++) + { + KnownColor knownColor = knownColors[i]; + string name = knownColor.ToString().ToLowerInvariant(); + + KnownColors.Add(name, knownColor); + + stringBuilder.Append(i > 0 ? "|" + name : name); + } + + stringBuilder.Append("))"); + + return new Regex(stringBuilder.ToString(), RegexOptions.IgnoreCase); + } } } diff --git a/src/ImageProcessor.Web/NET45/Processors/Format.cs b/src/ImageProcessor.Web/NET45/Processors/Format.cs index b419f211f..7b57d7ddd 100644 --- a/src/ImageProcessor.Web/NET45/Processors/Format.cs +++ b/src/ImageProcessor.Web/NET45/Processors/Format.cs @@ -15,7 +15,6 @@ namespace ImageProcessor.Web.Processors using System.Linq; using System.Text; using System.Text.RegularExpressions; - using System.Web; using ImageProcessor.Configuration; using ImageProcessor.Imaging.Formats;