Browse Source

Moar tests plus known colours!

Former-commit-id: f829fb7fa90a3ce36088ff67c48ea8841ec57f0c
pull/17/head
James South 12 years ago
parent
commit
da25a7f51f
  1. 82
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  2. 42
      src/ImageProcessor.Web/NET45/Helpers/CommonParameterParserUtility.cs
  3. 1
      src/ImageProcessor.Web/NET45/Processors/Format.cs

82
src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs

@ -29,18 +29,25 @@ namespace ImageProcessor.Web.UnitTests
/// <summary>
/// The alpha regex unit test.
/// </summary>
/// <param name="input">
/// The input string.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
[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);
}
/// <summary>
@ -95,6 +102,32 @@ namespace ImageProcessor.Web.UnitTests
Assert.AreEqual(expected, result);
}
/// <summary>
/// The saturation regex unit test.
/// </summary>
/// <param name="input">
/// The input string.
/// </param>
/// <param name="expected">
/// The expected result.
/// </param>
[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);
}
/// <summary>
/// The rotate regex unit test.
/// </summary>
@ -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<string, Color> data = new Dictionary<string, Color>
{
{
"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<string, Color> item in data)
{
tint.MatchRegexIndex(item.Key);
Color result = tint.Processor.DynamicParameter;
Assert.AreEqual(item.Value, result);
}
}
}
}

42
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
/// </summary>
public static class CommonParameterParserUtility
{
/// <summary>
/// The collection of known colors.
/// </summary>
private static readonly Dictionary<string, KnownColor> KnownColors = new Dictionary<string, KnownColor>();
/// <summary>
/// The regular expression to search strings for colors.
/// </summary>
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();
/// <summary>
/// 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;
}
/// <summary>
/// Builds a regular expression for the three main colour types.
/// </summary>
/// <returns>
/// The <see cref="Regex"/> to match colors.
/// </returns>
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);
}
}
}

1
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;

Loading…
Cancel
Save