forked from axuno/SmartFormat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better separation of built-in string.Format and Smart.Format features (…
…axuno#175) #### Better separation of built-in string.Format and Smart.Format features 1. Settings.StringFormatCompatibility == true * In this mode, *SmartFormat* is a drop-in replacement for `string.Format` regarding formatting syntax. * Curly braces `{}` **must** be escaped with double braces `{{` and `}}` * The `Parser` will not include the formatter name or formatting options. Like with `string.Format`, everything after the `Selector` separator (colon) is considered as format specifier. * `DefaultFormatter` is the only formatter which will be invoked. * Even in compatibility mode, *SmartFormat* will * Process named `Placeholder`s (beside indexed `Placeholder`s) * have `ISource`s available for `Placeholder`s. * be able to process escaped string literals like \n, \U1234 etc. 2. Settings.StringFormatCompatibility == false * Makes full use of all *SmartFormat* features * Any character can be used, as long as few special characters are escaped with a backslash `\`. These are e.g. `{}()[]():.`. * This leads to a slightly different format string syntax. Example: `string.Format("It is now {0:yyyy/MM/dd HH:mm:ss}, DateTime.Now)` vs. `Smart.Format("It is now {Date:yyyy/MM/dd HH\:mm\:ss}, DateTime.Now)`. 3. All modes * It is possible to use a `System.IFormatProvider` as argument to `Smart.Format` - same as with `string.Format`. This custom format provider can in turn call a `System.ICustomFormatter` for special formatting needs. * This feature is implemented in the `DefaultFormatter`.
- Loading branch information
Showing
12 changed files
with
204 additions
and
108 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
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
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
68 changes: 68 additions & 0 deletions
68
src/SmartFormat.Tests/Utilities/CustomFormatProviderTests.cs
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,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using SmartFormat.Extensions; | ||
|
||
namespace SmartFormat.Tests.Utilities | ||
{ | ||
[TestFixture] | ||
public class CustomFormatProviderTests | ||
{ | ||
private SmartFormatter GetSimpleFormatter() | ||
{ | ||
var formatter = new SmartFormatter(); | ||
formatter.FormatterExtensions.Add(new DefaultFormatter()); | ||
formatter.SourceExtensions.Add(new ReflectionSource(formatter)); | ||
formatter.SourceExtensions.Add(new DefaultSource(formatter)); | ||
return formatter; | ||
} | ||
|
||
#region *** Format with custom formatter *** | ||
|
||
[TestCase("format", "value", true)] | ||
[TestCase("tamrof", "eulav", true)] | ||
[TestCase("format", "value", false)] | ||
[TestCase("tamrof", "eulav", false)] | ||
public void Format_With_CustomFormatter(string format, string value, bool stringFormatCompatible) | ||
{ | ||
var smart = GetSimpleFormatter(); | ||
smart.Settings.StringFormatCompatibility = stringFormatCompatible; | ||
var expected = new string(format.Reverse().Select(c => c).ToArray()) + ": " + | ||
new string(value.Reverse().Select(c => c).ToArray()); | ||
var resultSmartFormat = smart.Format(new ReverseFormatProvider(), $"{{0:{format}}}", value); | ||
var resultStringFormat = string.Format(new ReverseFormatProvider(), $"{{0:{format}}}", value); | ||
Assert.That(resultSmartFormat, Is.EqualTo(expected)); | ||
Assert.That(resultStringFormat, Is.EqualTo(expected)); | ||
} | ||
|
||
/// <summary> | ||
/// Used for Format_With_CustomFormatter test | ||
/// </summary> | ||
public class ReverseFormatProvider : IFormatProvider | ||
{ | ||
public object GetFormat(Type formatType) | ||
{ | ||
if (formatType == typeof(ICustomFormatter)) return new ReverseFormatAndArgumentFormatter(); | ||
|
||
return new object(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Used for Format_With_CustomFormatter test | ||
/// </summary> | ||
public class ReverseFormatAndArgumentFormatter : ICustomFormatter | ||
{ | ||
public string Format(string format, object arg, IFormatProvider formatProvider) | ||
{ | ||
return new string(format.Reverse().Select(c => c).ToArray()) + ": " + | ||
new string((arg as string ?? "?").Reverse().Select(c => c).ToArray()); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |
Oops, something went wrong.