-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters