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.
Constrain custom selectors and operators (axuno#172)
* Custom selector chars can be added, if not disallowed or in use as an operator char * Custom operator chars can be added, if not disallowed or in use as a selector char * Added corresponding unit tests * Alphanumeric selector chars are the only option now and cannot be degraded to pure numeric chars * `PlaceholderBeginChar`, `PlaceholderEndChar`, `FormatterOptionsBeginChar` and `FormatterOptionsEndChar` now only have getters * Removed obsolete `formatter.Parser.AddOperators("[]");` from the `ListFormatter` CTOR.
- Loading branch information
Showing
5 changed files
with
204 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using SmartFormat.Core.Settings; | ||
|
||
namespace SmartFormat.Tests.Core | ||
{ | ||
[TestFixture] | ||
public class SettingsTests | ||
{ | ||
[Test] | ||
public void TryingToAddDisallowedSelectorCharacters_Should_Throw() | ||
{ | ||
var settings = new SmartSettings(); | ||
Assert.That(() => settings.Parser.AddCustomSelectorChars(new[] {settings.Parser.PlaceholderBeginChar}), | ||
Throws.ArgumentException.And.Message.Contains($"{settings.Parser.PlaceholderBeginChar}")); | ||
} | ||
|
||
[Test] | ||
public void ExistingSelectorCharacter_Should_Not_Be_Added() | ||
{ | ||
var settings = new SmartSettings(); | ||
settings.Parser.AddCustomSelectorChars(new[] {'A', ' '}); | ||
settings.Parser.AddCustomSelectorChars(new[] {' '}); | ||
|
||
Assert.That(settings.Parser.CustomSelectorChars.Count(c => c == 'A'), Is.EqualTo(0)); | ||
Assert.That(settings.Parser.CustomSelectorChars.Count(c => c == ' '), Is.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void TryingToAddDisallowedOperatorCharacters_Should_Throw() | ||
{ | ||
var settings = new SmartSettings(); | ||
Assert.That(() => settings.Parser.AddCustomOperatorChars(new[] {settings.Parser.PlaceholderBeginChar}), | ||
Throws.ArgumentException.And.Message.Contains($"{settings.Parser.PlaceholderBeginChar}")); | ||
} | ||
|
||
[Test] | ||
public void ExistingOperatorCharacter_Should_Not_Be_Added() | ||
{ | ||
var settings = new SmartSettings(); | ||
settings.Parser.AddCustomOperatorChars(new[] {settings.Parser.OperatorChars[0], '°'}); | ||
settings.Parser.AddCustomOperatorChars(new[] {'°'}); | ||
|
||
Assert.That(settings.Parser.CustomOperatorChars.Count(c => c == settings.Parser.OperatorChars[0]), Is.EqualTo(0)); | ||
Assert.That(settings.Parser.CustomOperatorChars.Count(c => c == '°'), Is.EqualTo(1)); | ||
} | ||
|
||
[Test] | ||
public void GetCaseSensitivityComparison() | ||
{ | ||
var settings = new SmartSettings(); | ||
foreach (var name in Enum.GetNames(typeof(CaseSensitivityType))) | ||
{ | ||
settings.CaseSensitivity = (CaseSensitivityType) Enum.Parse(typeof(CaseSensitivityType), name); | ||
Assert.DoesNotThrow(() => settings.GetCaseSensitivityComparison()); | ||
} | ||
|
||
settings.CaseSensitivity = (CaseSensitivityType) int.MaxValue; | ||
Assert.Throws<InvalidOperationException>(() => settings.GetCaseSensitivityComparison()); | ||
} | ||
|
||
[Test] | ||
public void CaseSensitivityComparer() | ||
{ | ||
var settings = new SmartSettings(); | ||
foreach (var name in Enum.GetNames(typeof(CaseSensitivityType))) | ||
{ | ||
settings.CaseSensitivity = (CaseSensitivityType) Enum.Parse(typeof(CaseSensitivityType), name); | ||
Assert.DoesNotThrow(() => settings.GetCaseSensitivityComparer()); | ||
} | ||
|
||
settings.CaseSensitivity = (CaseSensitivityType) int.MaxValue; | ||
Assert.Throws<InvalidOperationException>(() => settings.GetCaseSensitivityComparer()); | ||
} | ||
|
||
[TestCase('°')] // a custom char | ||
[TestCase('A')] // a standard selector char | ||
public void Add_CustomOperator_Used_As_Seperator_Should_Throw(char operatorChar) | ||
{ | ||
var settings = new SmartSettings(); | ||
settings.Parser.AddCustomSelectorChars(new[] {operatorChar}); // reserve as selector char | ||
|
||
// try to add the same char as operator | ||
Assert.That(() => settings.Parser.AddCustomOperatorChars(new[] {operatorChar}), | ||
Throws.ArgumentException.And.Message.Contains($"{operatorChar}")); | ||
} | ||
|
||
[TestCase('°')] // a custom char | ||
[TestCase('.')] // a standard operator char | ||
public void Add_CustomSelector_Used_As_Operator_Should_Throw(char selectorChar) | ||
{ | ||
var settings = new SmartSettings(); | ||
settings.Parser.AddCustomOperatorChars(new[] {selectorChar}); // reserve as operator char | ||
|
||
// try to add the same char as selector | ||
Assert.That(() => settings.Parser.AddCustomSelectorChars(new[] {selectorChar}), | ||
Throws.ArgumentException.And.Message.Contains($"{selectorChar}")); | ||
} | ||
} | ||
} |
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