Skip to content

Commit

Permalink
Closes issue #88
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed May 19, 2018
1 parent 19e7f9c commit 07a9439
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/SmartFormat.Tests/Extensions/IsMatchFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using SmartFormat.Core.Formatting;
using SmartFormat.Core.Settings;
using SmartFormat.Extensions;

namespace SmartFormat.Tests.Extensions
{
[TestFixture]
public class IsMatchFormatterTests
{
private Dictionary<string, object> _variable = new Dictionary<string,object>() { {"theKey", "Some123Content"}};
private SmartFormatter _formatter;

public IsMatchFormatterTests()
{
_formatter = Smart.CreateDefaultSmartFormat();
_formatter.FormatterExtensions.Add(new IsMatchFormatter {RegexOptions = RegexOptions.CultureInvariant});
_formatter.Settings.FormatErrorAction = ErrorAction.ThrowError;
}

[TestCase("{theKey:ismatch(^.+123.+$):Okay - {}|No match content}", RegexOptions.None, "Okay - Some123Content")]
[TestCase("{theKey:ismatch(^.+123.+$):Fixed content if match|No match content}", RegexOptions.None, "Fixed content if match")]
[TestCase("{theKey:ismatch(^.+999.+$):{}|No match content}", RegexOptions.None, "No match content")]
[TestCase("{theKey:ismatch(^.+123.+$):|Only content with no match}", RegexOptions.None, "")]
[TestCase("{theKey:ismatch(^.+999.+$):|Only content with no match}", RegexOptions.None, "Only content with no match")]
[TestCase("{theKey:ismatch(^SOME123.+$):Okay - {}|No match content}", RegexOptions.IgnoreCase, "Okay - Some123Content")]
[TestCase("{theKey:ismatch(^SOME123.+$):Okay - {}|No match content}", RegexOptions.None, "No match content")]
public void Test_Formats_And_CaseSensitivity(string format, RegexOptions options, string expected)
{
((IsMatchFormatter) _formatter.FormatterExtensions.First(fex =>
fex.GetType() == typeof(IsMatchFormatter))).RegexOptions = options;

Assert.AreEqual(expected, _formatter.Format(format, _variable));
}

[Test]
public void Test_FormatException()
{
// less than 2 format options throw exception
Assert.Throws<FormattingException>(() =>
_formatter.Format("{theKey:ismatch(^.+123.+$):Dummy content}", _variable));
}

[Test]
public void Test_List()
{
var myList = new List<int> {100, 200, 300};
Assert.AreEqual("100.00, 200.00 and 'no match'",
_formatter.Format(CultureInfo.InvariantCulture,
"{0:list:{:ismatch(^100|200|999$):{:0.00}|'no match'}|, | and }", myList));

Assert.AreEqual("'match', 'match' and 'no match'",
_formatter.Format(CultureInfo.InvariantCulture,
"{0:list:{:ismatch(^100|200|999$):'match'|'no match'}|, | and }", myList));
}
}
}
1 change: 1 addition & 0 deletions src/SmartFormat.Tests/SmartFormat.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Extensions\ChooseFormatterTests.cs" />
<Compile Include="Extensions\ConditionalFormatterTests.cs" />
<Compile Include="Extensions\DictionaryFormatterTests.cs" />
<Compile Include="Extensions\IsMatchFormatterTests.cs" />
<Compile Include="Extensions\ListFormatterTests.cs" />
<Compile Include="Extensions\PluralLocalizationFormatterTests.cs" />
<Compile Include="Extensions\ReflectionFormatterTests.cs" />
Expand Down
44 changes: 44 additions & 0 deletions src/SmartFormat/Extensions/IsMatchFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using SmartFormat.Core.Extensions;

namespace SmartFormat.Extensions
{
/// <summary>
/// Formatter with evaluation of regular expressions.
/// </summary>
/// <remarks>
/// Syntax:
/// {value:ismatch(regex): format | default}
/// Or in context of a list:
/// {myList:list:{:ismatch(^regex$):{:format}|'no match'}|, | and }
/// </remarks>
public class IsMatchFormatter : IFormatter
{
public string[] Names { get; set; } = { "ismatch" };

public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
var expression = formattingInfo.FormatterOptions;
var formats = formattingInfo.Format.Split('|');

if (formats.Count == 0)
return true;

if (formats.Count != 2)
throw new FormatException("Exactly 2 format options are required.");

var regEx = new Regex(expression, RegexOptions);

if (regEx.IsMatch(formattingInfo.CurrentValue.ToString()))
formattingInfo.Write(formats[0], formattingInfo.CurrentValue);
else if (formats.Count == 2)
formattingInfo.Write(formats[1], formattingInfo.CurrentValue);

return true;
}

public RegexOptions RegexOptions { get; set; }
}
}
1 change: 1 addition & 0 deletions src/SmartFormat/SmartFormat.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="Extensions\DefaultFormatter.cs" />
<Compile Include="Extensions\DefaultSource.cs" />
<Compile Include="Extensions\DictionarySource.cs" />
<Compile Include="Extensions\IsMatchFormatter.cs" />
<Compile Include="Extensions\ListFormatter.cs" />
<Compile Include="Extensions\PluralLocalizationFormatter.cs" />
<Compile Include="Extensions\ReflectionSource.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/SmartFormat/SmartFormat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Extensions\DefaultFormatter.cs" />
<Compile Include="Extensions\DefaultSource.cs" />
<Compile Include="Extensions\DictionarySource.cs" />
<Compile Include="Extensions\IsMatchFormatter.cs" />
<Compile Include="Extensions\ListFormatter.cs" />
<Compile Include="Extensions\PluralLocalizationFormatter.cs" />
<Compile Include="Extensions\ReflectionSource.cs" />
Expand Down

0 comments on commit 07a9439

Please sign in to comment.