From 2271c4644da8a3ec9daa3866648e6eda3d034359 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:32:24 -0400 Subject: [PATCH 01/12] Update TestStrings.cs Added test values for left-pad formatting. --- FormatWithTests/TestStrings.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/FormatWithTests/TestStrings.cs b/FormatWithTests/TestStrings.cs index a16ce7f..8f5e333 100644 --- a/FormatWithTests/TestStrings.cs +++ b/FormatWithTests/TestStrings.cs @@ -28,10 +28,14 @@ public static class TestStrings public static readonly string TestFormat6 = "abc{Replacement1:upper}"; public static readonly string TestFormat6Composite = "abc{0:upper}"; public static readonly string TestFormat6Solution = $"abc{Replacement1.ToUpper()}"; - + public static readonly string TestFormat7 = "Today is {Today:YYYYMMDD HH:mm}"; public static readonly string TestFormat7Composite = "Today is {0:YYYYMMDD HH:mm}"; public static readonly DateTime TestFormat7Date = new DateTime(2018, 10, 30, 17, 25, 0); public static readonly string TestFormat7Solution = $"Today is {TestFormat7Date:YYYYMMDD HH:mm}"; + + public static readonly Decimal LeftPadTestValue = 1234.56; + public static readonly string LeftPadTestFormat = "'{LeftPadTestValue,15:#,##.00}'"; + public static readonly string LeftPadTestFormatComposite = "'{0,15:#,##.00}'"; } } From 2fb3f13b4a6a69a2a6a3c8670060194ac7e1f6f9 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:52:07 -0400 Subject: [PATCH 02/12] Update FormatWithTests.cs Added test for left-pad formatting. --- FormatWithTests/FormatWithTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/FormatWithTests/FormatWithTests.cs b/FormatWithTests/FormatWithTests.cs index 1caf17d..4664a32 100644 --- a/FormatWithTests/FormatWithTests.cs +++ b/FormatWithTests/FormatWithTests.cs @@ -170,6 +170,18 @@ public void TestCustomHandler2() Assert.Equal("321FEDcba, ABCDEF123, abcdef123.", replacement); } + [Fact] + public void LeftPadTest() + { + Dictionary replacementDictionary = new Dictionary() + { + ["LeftPadTestValue"] = LeftPadTestValue + }; + System.String expectedValue = "' 1,234.56'"; + Assert.Equal(System.String.Format(LeftPadTestFormatComposite, LeftPadTestValue), exectedValue); + Assert.Equal(LeftPadTestValue.FormatWith(replacementDictionary), expectedValue); + } + [Fact] public void SpeedTest() { From 35595ca8c73b0b47ae436aaa400624e3c942f6d6 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:33:31 -0400 Subject: [PATCH 03/12] Update FormatHelpers.cs Initial swag at parsing and using both the alignment and format segments. I don't understand the tokenization, and suspect a new handler might be needed. --- FormatWith/Internal/FormatHelpers.cs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/FormatWith/Internal/FormatHelpers.cs b/FormatWith/Internal/FormatHelpers.cs index ed55fb6..35e6ead 100644 --- a/FormatWith/Internal/FormatHelpers.cs +++ b/FormatWith/Internal/FormatHelpers.cs @@ -51,6 +51,16 @@ public static string ProcessTokens( tokenKey = thisToken.Value.Substring(0, separatorIdx); format = thisToken.Value.Substring(separatorIdx + 1); } + /// Parse out alignment + string alignment = null; + var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); + If(alignmentIdx > -1) + { + tokeyKey = thisToken.Value.Substring(0, alignmentIdx); + alignment = thisToken.Value.Substring(alignmentIdx + 1); + } + /// assemble full format string ([,ALIGNMENT][:FORMAT]) + string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty) // append the replacement for this parameter ReplacementResult replacementResult = handler(tokenKey, format); @@ -65,7 +75,7 @@ public static string ProcessTokens( } else { - resultBuilder.AppendFormat("{0:" + format + "}", replacementResult.Value); + resultBuilder.AppendFormat("{0" + formatString + "}", replacementResult.Value); } } else @@ -141,6 +151,16 @@ public static FormattableString ProcessTokensIntoFormattableString( tokenKey = thisToken.Value.Substring(0, separatorIdx); format = thisToken.Value.Substring(separatorIdx + 1); } + /// Parse out alignment + string alignment = null; + var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); + If(alignmentIdx > -1) + { + tokeyKey = thisToken.Value.Substring(0, alignmentIdx); + alignment = thisToken.Value.Substring(alignmentIdx + 1); + } + /// assemble full format string ([,ALIGNMENT][:FORMAT]) + string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty) // append the replacement for this parameter ReplacementResult replacementResult = handler(tokenKey); @@ -152,7 +172,7 @@ string IndexAndFormat() return "{" + placeholderIndex + "}"; } - return "{" + placeholderIndex + ":" + format + "}"; + return "{" + placeholderIndex + formatString + "}"; } // append the replacement for this parameter From 6f010ab17ed2db98de08ba4c105176ecf166fb11 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:34:13 -0400 Subject: [PATCH 04/12] Update FormatHelpers.cs Fixed some minor typos. --- FormatWith/Internal/FormatHelpers.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/FormatWith/Internal/FormatHelpers.cs b/FormatWith/Internal/FormatHelpers.cs index 35e6ead..9d0463c 100644 --- a/FormatWith/Internal/FormatHelpers.cs +++ b/FormatWith/Internal/FormatHelpers.cs @@ -48,19 +48,19 @@ public static string ProcessTokens( var separatorIdx = tokenKey.IndexOf(":", StringComparison.Ordinal); if (separatorIdx > -1) { - tokenKey = thisToken.Value.Substring(0, separatorIdx); format = thisToken.Value.Substring(separatorIdx + 1); + tokenKey = thisToken.Value.Substring(0, separatorIdx); } /// Parse out alignment string alignment = null; var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); - If(alignmentIdx > -1) + if(alignmentIdx > -1) { - tokeyKey = thisToken.Value.Substring(0, alignmentIdx); alignment = thisToken.Value.Substring(alignmentIdx + 1); + tokenKey = thisToken.Value.Substring(0, alignmentIdx); } /// assemble full format string ([,ALIGNMENT][:FORMAT]) - string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty) + string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty); // append the replacement for this parameter ReplacementResult replacementResult = handler(tokenKey, format); From 3aa0bf09254a8a2b5d8d9df01c8ee5af2ddcbfed Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:35:30 -0400 Subject: [PATCH 05/12] Update FormatHelpers.cs Fixed typos. --- FormatWith/Internal/FormatHelpers.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FormatWith/Internal/FormatHelpers.cs b/FormatWith/Internal/FormatHelpers.cs index 9d0463c..0cc165d 100644 --- a/FormatWith/Internal/FormatHelpers.cs +++ b/FormatWith/Internal/FormatHelpers.cs @@ -148,19 +148,19 @@ public static FormattableString ProcessTokensIntoFormattableString( var separatorIdx = tokenKey.IndexOf(":", StringComparison.Ordinal); if (separatorIdx > -1) { - tokenKey = thisToken.Value.Substring(0, separatorIdx); format = thisToken.Value.Substring(separatorIdx + 1); + tokenKey = thisToken.Value.Substring(0, separatorIdx); } /// Parse out alignment string alignment = null; var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); If(alignmentIdx > -1) { - tokeyKey = thisToken.Value.Substring(0, alignmentIdx); alignment = thisToken.Value.Substring(alignmentIdx + 1); + tokenKey = thisToken.Value.Substring(0, alignmentIdx); } /// assemble full format string ([,ALIGNMENT][:FORMAT]) - string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty) + string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty); // append the replacement for this parameter ReplacementResult replacementResult = handler(tokenKey); From bf4b4279dcc1dada45059706bec8a10432ef01e9 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Tue, 27 Aug 2024 13:47:36 -0400 Subject: [PATCH 06/12] Update FormatHelpers.cs Updated null/blank check on format string to include the full alignment+format string. --- FormatWith/Internal/FormatHelpers.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FormatWith/Internal/FormatHelpers.cs b/FormatWith/Internal/FormatHelpers.cs index 0cc165d..6db43a5 100644 --- a/FormatWith/Internal/FormatHelpers.cs +++ b/FormatWith/Internal/FormatHelpers.cs @@ -69,7 +69,7 @@ public static string ProcessTokens( { // the key exists, add the replacement value // this does nothing if replacement value is null - if (string.IsNullOrWhiteSpace(format)) + if (string.IsNullOrWhiteSpace(formatString)) { resultBuilder.Append(replacementResult.Value); } @@ -167,7 +167,7 @@ public static FormattableString ProcessTokensIntoFormattableString( string IndexAndFormat() { - if (string.IsNullOrWhiteSpace(format)) + if (string.IsNullOrWhiteSpace(formatString)) { return "{" + placeholderIndex + "}"; } From 276803d5ecd8cba08659cd0e0a8850161707ba47 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:41:18 -0400 Subject: [PATCH 07/12] Update FormatHelpers.cs Finally had a chance to work in an actual dev environment. Sorry about the mish-mash of prior edits in this branch. --- FormatWith/Internal/FormatHelpers.cs | 54 ++++++---------------------- 1 file changed, 10 insertions(+), 44 deletions(-) diff --git a/FormatWith/Internal/FormatHelpers.cs b/FormatWith/Internal/FormatHelpers.cs index 6db43a5..2d8e72d 100644 --- a/FormatWith/Internal/FormatHelpers.cs +++ b/FormatWith/Internal/FormatHelpers.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; @@ -43,39 +43,22 @@ public static string ProcessTokens( { // token is a parameter token // perform parameter logic now. - var tokenKey = thisToken.Value; - string format = null; - var separatorIdx = tokenKey.IndexOf(":", StringComparison.Ordinal); - if (separatorIdx > -1) - { - format = thisToken.Value.Substring(separatorIdx + 1); - tokenKey = thisToken.Value.Substring(0, separatorIdx); - } - /// Parse out alignment - string alignment = null; - var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); - if(alignmentIdx > -1) - { - alignment = thisToken.Value.Substring(alignmentIdx + 1); - tokenKey = thisToken.Value.Substring(0, alignmentIdx); - } - /// assemble full format string ([,ALIGNMENT][:FORMAT]) - string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty); + TokenInformation tokenInfo = new TokenInformation(thisToken.Value); // append the replacement for this parameter - ReplacementResult replacementResult = handler(tokenKey, format); - + ReplacementResult replacementResult = handler(tokenInfo.TokenKey, tokenInfo.Format); + if (replacementResult.Success) { // the key exists, add the replacement value // this does nothing if replacement value is null - if (string.IsNullOrWhiteSpace(formatString)) + if (string.IsNullOrWhiteSpace(tokenInfo.FormatString)) { resultBuilder.Append(replacementResult.Value); } else { - resultBuilder.AppendFormat("{0" + formatString + "}", replacementResult.Value); + resultBuilder.AppendFormat("{0" + tokenInfo.FormatString + "}", replacementResult.Value); } } else @@ -143,36 +126,19 @@ public static FormattableString ProcessTokensIntoFormattableString( { // token is a parameter token // perform parameter logic now. - var tokenKey = thisToken.Value; - string format = null; - var separatorIdx = tokenKey.IndexOf(":", StringComparison.Ordinal); - if (separatorIdx > -1) - { - format = thisToken.Value.Substring(separatorIdx + 1); - tokenKey = thisToken.Value.Substring(0, separatorIdx); - } - /// Parse out alignment - string alignment = null; - var alignmentIdx = tokenKey.LastIndexOf(",", StringComparison.Ordinal); - If(alignmentIdx > -1) - { - alignment = thisToken.Value.Substring(alignmentIdx + 1); - tokenKey = thisToken.Value.Substring(0, alignmentIdx); - } - /// assemble full format string ([,ALIGNMENT][:FORMAT]) - string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty); + TokenInformation tokenInfo = new TokenInformation(thisToken.Value); // append the replacement for this parameter - ReplacementResult replacementResult = handler(tokenKey); + ReplacementResult replacementResult = handler(tokenInfo.TokenKey); string IndexAndFormat() { - if (string.IsNullOrWhiteSpace(formatString)) + if (string.IsNullOrWhiteSpace(tokenInfo.FormatString)) { return "{" + placeholderIndex + "}"; } - return "{" + placeholderIndex + formatString + "}"; + return "{" + placeholderIndex + tokenInfo.FormatString + "}"; } // append the replacement for this parameter From 933c8ba0cf3e0dce17a13bc41b77ac43012fe846 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:42:02 -0400 Subject: [PATCH 08/12] Create TokenInformation.cs Finally had a chance to work in an actual dev environment. Sorry about the mish-mash of prior edits in this branch. --- FormatWith/TokenInformation.cs | 192 +++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 FormatWith/TokenInformation.cs diff --git a/FormatWith/TokenInformation.cs b/FormatWith/TokenInformation.cs new file mode 100644 index 0000000..b02a539 --- /dev/null +++ b/FormatWith/TokenInformation.cs @@ -0,0 +1,192 @@ +namespace FormatWith +{ + + /// + /// Encapsulates the token-parsing logic and values, ensuring consistent behavior and allowing them to be passed to handlers more concisely. + /// + public class TokenInformation + { + + #region Static & Const + + ///// + ///// A regular expression to parse the token + ///// + //private static readonly System.Text.RegularExpressions.Regex _parseRegEx = new System.Text.RegularExpressions.Regex(@"(?([^,;:\s]+))(,(?-?\d+))?(:(?.*))?", System.Text.RegularExpressions.RegexOptions.Compiled); + + #endregion + + #region Properties + + /// + /// The raw token parsed. + /// + public string RawToken { get; private set; } + + /// + /// The token key. + /// + public string TokenKey { get; private set; } + + /// + /// The alignment indicator + /// + public string Alignment { get; private set; } + + /// + /// The base format string. (no alignment) + /// + public string Format { get; private set; } + + private string _formatString = null; + /// + /// The full composite formatting string. [,align][:format] + /// + public string FormatString + { + get + { + if (null == this._formatString) + { + this._formatString = string.Concat((!string.IsNullOrEmpty(this.Alignment) ? $",{this.Alignment}" : string.Empty), (!string.IsNullOrEmpty(this.Format) ? $":{this.Format}" : string.Empty)); + } + return (this._formatString); + } + } + + #endregion + + #region Constructors + + /// + /// Parses token information from a raw token string value. + /// + /// The raw token string. + /// IndexOf/Substring version of parser based on existing code + public TokenInformation(string rawToken) + { + this.RawToken = rawToken; + this.Format = null; + this.Alignment = null; + + // Parse out the format + var separatorIdx = rawToken.IndexOf(":", System.StringComparison.Ordinal); + if (separatorIdx > -1) + { + this.Format = rawToken.Substring(separatorIdx + 1); + rawToken = rawToken.Substring(0, separatorIdx); + } + + // Parse out alignment + var alignmentIdx = rawToken.LastIndexOf(",", System.StringComparison.Ordinal); + if (alignmentIdx > -1) + { + this.Alignment = rawToken.Substring(alignmentIdx + 1); + rawToken = rawToken.Substring(0, alignmentIdx); + } + + // Grab the token key + this.TokenKey = rawToken; + } + + ///// + ///// Parses token information from a raw token string value. + ///// + ///// The raw token string. + ///// RegEx version of parser, for performance comparison, and in case someone else knows how to optimize it more + //public TokenInformation(string rawToken, bool ignoreThisParameterItIsHereForPerformanceComparisons) + //{ + // this.RawToken = rawToken; + // this.Alignment = null; + // this.Format = null; + // + // System.Text.RegularExpressions.Match results = _parseRegEx.Match(rawToken); + // if(results.Success) + // { + // this.TokenKey = results.Groups["token"].Value; + // this.Alignment = results.Groups["alignment"].Value; + // this.Format = results.Groups["format"].Value; + // } + //} + + #endregion + + #region Methods + + private static string BuildString(string tokenKey, string alignment, string format) + { + string rawToken = string.Concat(tokenKey, (!string.IsNullOrEmpty(alignment) ? $",{alignment}" : string.Empty), (!string.IsNullOrEmpty(format) ? $":{format}" : string.Empty)); + return (rawToken); + } + + /// + /// Builds a raw token string given the desired tokenKey and format + /// + /// The key value for the token. + /// The left- or right-padding indicator desired. + /// The format string. + /// + public static string BuildString(string tokenKey, int? alignment, string format) + { + return (TokenInformation.BuildString(tokenKey, alignment.HasValue ? $"{alignment}" : string.Empty, format)); + } + + /// + /// Builds a raw token string given the desired tokenKey and format + /// + /// The key value for the token. + /// The format string. + /// + public static string BuildString(string tokenKey, string format) + { + return (TokenInformation.BuildString(tokenKey, (int?)null, format)); + } + + /// + /// Builds a raw token string given the desired tokenKey and format + /// + /// The key value for the token. + /// The left- or right-padding indicator desired. + /// + public static string BuildString(string tokenKey, int? alignment) + { + return (TokenInformation.BuildString(tokenKey, alignment, null)); + } + + /// + /// Builds a raw token string given the desired tokenKey and format + /// + /// The key value for the token. + /// + public static string BuildString(string tokenKey) + { + return(TokenInformation.BuildString(tokenKey, (int?)null, null)); + } + + /// + /// Builds a TokenInfo object given the desired tokenKey and format + /// + /// The key value for the token. + /// The left- or right-padding indicator desired. + /// The format string. + /// + public static TokenInformation Build(string tokenKey, int? alignment, string format) + { + return (new TokenInformation(TokenInformation.BuildString(tokenKey, alignment, format))); + } + + /// + /// Builds a TokenInfo object given the desired tokenKey and format + /// + /// The key value for the token. + /// The format string. + /// + public static TokenInformation Build(string tokenKey, string format) + { + return (TokenInformation.Build(tokenKey, null, format)); + } + + #endregion + + } // end class +} // end namespace From d28a86eab85a82931848f16b0ba49462b3205f33 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:42:32 -0400 Subject: [PATCH 09/12] Create TokenInformationTests.cs Finally had a chance to work in an actual dev environment. Sorry about the mish-mash of prior edits in this branch. --- FormatWithTests/TokenInformationTests.cs | 122 +++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 FormatWithTests/TokenInformationTests.cs diff --git a/FormatWithTests/TokenInformationTests.cs b/FormatWithTests/TokenInformationTests.cs new file mode 100644 index 0000000..af7b79d --- /dev/null +++ b/FormatWithTests/TokenInformationTests.cs @@ -0,0 +1,122 @@ +using Xunit; +using FormatWith; +using Xunit.Sdk; + +namespace FormatWithTests +{ + public class TokenInformationTests + { + + #region Test Values + + private static string testTokenKey = "token"; + private static int? testLeftPad = 15; + private static int? testRightPad = -15; + private static string testFormat = "yyyy-MM-dd"; + private static System.DateTime testDate = new System.DateTime(2024, 8, 29); + + public class Parse_TestData : TheoryData + { + public Parse_TestData() + { + } + }; + + #endregion + + #region BuildString + + [Fact] + public void BuildString_TokenOnly() + { + string expected = "token"; + + string result = TokenInformation.BuildString(testTokenKey); + Assert.Equal(result, expected); + } + + [Fact] + public void BuildString_TokenAndLeftPad() + { + string expected = "token,15"; + + string result = TokenInformation.BuildString(testTokenKey, testLeftPad); + Assert.Equal(result, expected); + } + + [Fact] + public void BuildString_TokenAndRightPad() + { + string expected = "token,-15"; + + string result = TokenInformation.BuildString(testTokenKey, testRightPad); + Assert.Equal(result, expected); + } + + [Fact] + public void BuildString_TokenAndFormat() + { + string expected = "token:yyyy-MM-dd"; + + string result = TokenInformation.BuildString(testTokenKey, testFormat); + Assert.Equal(result, expected); + } + + [Fact] + public void BuildString_TokenLeftPadAndFormat() + { + string expected = "token,15:yyyy-MM-dd"; + + string result = TokenInformation.BuildString(testTokenKey, testLeftPad, testFormat); + Assert.Equal(result, expected); + } + + [Fact] + public void BuildString_TokenAndRightPadAndFormat() + { + string expected = "token,-15:yyyy-MM-dd"; + + string result = TokenInformation.BuildString(testTokenKey, testRightPad, testFormat); + Assert.Equal(result, expected); + } + + #endregion + + #region Parse + + [Theory] + [InlineData("token", "token", null, null, "")] + [InlineData("token,15", "token", "15", null, ",15")] + [InlineData("token,-15", "token", "-15", null, ",-15")] + [InlineData("token:yyyy-MM-dd", "token", null, "yyyy-MM-dd", ":yyyy-MM-dd")] + [InlineData("token,15:yyyy-MM-dd", "token", "15", "yyyy-MM-dd", ",15:yyyy-MM-dd")] + [InlineData("token,-15:yyyy-MM-dd", "token", "-15", "yyyy-MM-dd", ",-15:yyyy-MM-dd")] + public void Parse(string rawToken, string expectedTokenKey, string expectedAlignment, string expectedFormat, string expectedFormatString) + { + TokenInformation result = new TokenInformation(rawToken); + + Assert.Equal(result.RawToken, rawToken); + Assert.Equal(result.TokenKey, expectedTokenKey); + if (null == expectedAlignment) + { + Assert.Null(result.Alignment); + } + else + { + Assert.Equal(result.Alignment, expectedAlignment); + } + if (null == expectedFormat) + { + Assert.Null(result.Format); + } + else + { + Assert.Equal(result.Format, expectedFormat); + } + Assert.Equal(result.FormatString, expectedFormatString); + } + + #endregion + + } // end class +} // end namespace From a0911ecab2334468d660fd0940a41efed9831c43 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:43:23 -0400 Subject: [PATCH 10/12] Update TestStrings.cs Finally had a chance to work in an actual dev environment. Sorry about the mish-mash of prior edits in this branch. Moved new tests into their own file. --- FormatWithTests/TestStrings.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/FormatWithTests/TestStrings.cs b/FormatWithTests/TestStrings.cs index 8f5e333..ce76ce6 100644 --- a/FormatWithTests/TestStrings.cs +++ b/FormatWithTests/TestStrings.cs @@ -33,9 +33,5 @@ public static class TestStrings public static readonly string TestFormat7Composite = "Today is {0:YYYYMMDD HH:mm}"; public static readonly DateTime TestFormat7Date = new DateTime(2018, 10, 30, 17, 25, 0); public static readonly string TestFormat7Solution = $"Today is {TestFormat7Date:YYYYMMDD HH:mm}"; - - public static readonly Decimal LeftPadTestValue = 1234.56; - public static readonly string LeftPadTestFormat = "'{LeftPadTestValue,15:#,##.00}'"; - public static readonly string LeftPadTestFormatComposite = "'{0,15:#,##.00}'"; } } From 7ef3db58371ce91fbed83e39e1a404ff7c6c7e19 Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:45:46 -0400 Subject: [PATCH 11/12] Update FormatWithTests.cs Finally had a chance to work in an actual dev environment. Sorry about the mish-mash of prior edits in this branch. Moved tests into their own file. --- FormatWithTests/FormatWithTests.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/FormatWithTests/FormatWithTests.cs b/FormatWithTests/FormatWithTests.cs index 4664a32..1caf17d 100644 --- a/FormatWithTests/FormatWithTests.cs +++ b/FormatWithTests/FormatWithTests.cs @@ -170,18 +170,6 @@ public void TestCustomHandler2() Assert.Equal("321FEDcba, ABCDEF123, abcdef123.", replacement); } - [Fact] - public void LeftPadTest() - { - Dictionary replacementDictionary = new Dictionary() - { - ["LeftPadTestValue"] = LeftPadTestValue - }; - System.String expectedValue = "' 1,234.56'"; - Assert.Equal(System.String.Format(LeftPadTestFormatComposite, LeftPadTestValue), exectedValue); - Assert.Equal(LeftPadTestValue.FormatWith(replacementDictionary), expectedValue); - } - [Fact] public void SpeedTest() { From 2b578b0a1000eb0dbd4c22857b576f57fb59f97c Mon Sep 17 00:00:00 2001 From: Voice <53106311+TheodoreBrinkman@users.noreply.github.com> Date: Thu, 29 Aug 2024 23:54:20 -0400 Subject: [PATCH 12/12] Update TokenInformationTests.cs Additional tests to validate FormatWith usage against the new code. --- FormatWithTests/TokenInformationTests.cs | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/FormatWithTests/TokenInformationTests.cs b/FormatWithTests/TokenInformationTests.cs index af7b79d..9f8aa96 100644 --- a/FormatWithTests/TokenInformationTests.cs +++ b/FormatWithTests/TokenInformationTests.cs @@ -118,5 +118,45 @@ public void Parse(string rawToken, string expectedTokenKey, string expectedAlign #endregion + #region FormatWith + + [Fact] + public void FormatWith_DateTest_FormatOnly() + { + System.Collections.Generic.Dictionary replacmentValues = new System.Collections.Generic.Dictionary(); + replacmentValues.Add(testTokenKey, testDate); + + string expectedResult = "'2024-08-29'"; + string result = "'{token:yyyy-MM-dd}'".FormatWith(replacmentValues); + + Assert.Equal(result, expectedResult); + } + + [Fact] + public void FormatWith_DateTest_FormatAndLeftPad() + { + System.Collections.Generic.Dictionary replacmentValues = new System.Collections.Generic.Dictionary(); + replacmentValues.Add(testTokenKey, testDate); + + string expectedResult = "' 2024-08-29'"; + string result = "'{token,15:yyyy-MM-dd}'".FormatWith(replacmentValues); + + Assert.Equal(result, expectedResult); + } + + [Fact] + public void FormatWith_DateTest_FormatAndRightPad() + { + System.Collections.Generic.Dictionary replacmentValues = new System.Collections.Generic.Dictionary(); + replacmentValues.Add(testTokenKey, testDate); + + string expectedResult = "'2024-08-29 '"; + string result = "'{token,-15:yyyy-MM-dd}'".FormatWith(replacmentValues); + + Assert.Equal(result, expectedResult); + } + + #endregion + } // end class } // end namespace