-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interface IFormattingExtensionsToggle to skip formatting (#436)
- Loading branch information
Showing
8 changed files
with
86 additions
and
7 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
44 changes: 44 additions & 0 deletions
44
src/SmartFormat.Tests/Extensions/NoFormattingSourceTests.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,44 @@ | ||
using NUnit.Framework; | ||
using SmartFormat.Core.Extensions; | ||
using SmartFormat.Core.Formatting; | ||
using SmartFormat.Extensions; | ||
|
||
namespace SmartFormat.Tests.Extensions; | ||
|
||
[TestFixture] | ||
public class NoFormattingSourceTests | ||
{ | ||
[Test] | ||
public void Use_of_ToggleFormattingExtensions() | ||
{ | ||
var smart = new SmartFormatter(); | ||
smart.AddExtensions(new NoFormattingSource()); | ||
smart.AddExtensions(new DefaultFormatter()); | ||
|
||
Assert.That(smart.Format("{0}", 999), Is.EqualTo("No formatting")); | ||
} | ||
|
||
public class NoFormattingSource : ISource | ||
{ | ||
public bool TryEvaluateSelector(ISelectorInfo selectorInfo) | ||
{ | ||
// Split test for IFormattingExtensionsToggle and FormattingInfo | ||
// for clarity. This is not necessary in production code. | ||
|
||
// Disable all formatting extensions | ||
if (selectorInfo is IFormattingExtensionsToggle toggle) | ||
{ | ||
toggle.DisableFormattingExtensions = true; | ||
} | ||
|
||
if (selectorInfo is FormattingInfo fi) | ||
{ | ||
// Write a note or result directly to the output | ||
fi.Write("No formatting"); | ||
} | ||
|
||
selectorInfo.Result = selectorInfo.CurrentValue; | ||
return true; | ||
} | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/SmartFormat/Core/Extensions/IFormattingExtensionsToggler.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,27 @@ | ||
// | ||
// Copyright SmartFormat Project maintainers and contributors. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
|
||
namespace SmartFormat.Core.Extensions; | ||
|
||
/// <summary> | ||
/// Represents a toggle for enabling or disabling <see cref="IFormatter"/> extensions. | ||
/// This interface is primarily used by <see cref="ISource"/> extensions | ||
/// that receive it as part of the <see cref="ISelectorInfo"/> parameter. | ||
/// </summary> | ||
public interface IFormattingExtensionsToggle | ||
{ | ||
// This interface should become part of ISelectorInfo in the future. | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the <see cref="IFormatter"/> extensions are enabled. | ||
/// The value should be <see langword="false"/> (default), unless the <see cref="ISource"/> extension | ||
/// found a value in <seealso cref="ISource.TryEvaluateSelector"/> where default formatting cannot reasonably be done. | ||
/// <br/> | ||
/// In this case the <see cref="ISource"/> may directly write some output using <see cref="IFormattingInfo.Write(ReadOnlySpan{char})"/>, | ||
/// or produce no output at all. | ||
/// </summary> | ||
public bool DisableFormattingExtensions { 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
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