From 24439da82256e103155cefbbf2e78be8dbcaedf9 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Thu, 8 Mar 2012 09:55:29 +0000 Subject: [PATCH] Unit tests for the StringBuilderHelper weren't properly localized, containing hard-coded decimal separators - fixed; StringBuilderHelper always used a dot as a decimal separate - behavior changed so it uses the system's current locale (StringBuilderHelper should be identical to the normal StringBuilder except not produce any garbage) git-svn-id: file:///srv/devel/repo-conversion/nusu@267 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Collections/Parentable.cs | 14 +++--- Source/StringBuilderHelper.Test.cs | 72 +++++++++++++++--------------- Source/StringBuilderHelper.cs | 17 +++++-- 3 files changed, 56 insertions(+), 47 deletions(-) diff --git a/Source/Collections/Parentable.cs b/Source/Collections/Parentable.cs index f50df11..46c683b 100644 --- a/Source/Collections/Parentable.cs +++ b/Source/Collections/Parentable.cs @@ -24,11 +24,11 @@ using System.Collections.Generic; namespace Nuclex.Support.Collections { /// Base class for objects that can be parented to an owner - /// Type of the parent object - public class Parentable { + /// Type of the parent object + public class Parentable { /// The parent object that owns this instance - protected ParentType Parent { + protected TParent Parent { get { return this.parent; } } @@ -39,18 +39,18 @@ namespace Nuclex.Support.Collections { /// current parent will be null. /// /// Previous owner of the instance - protected virtual void OnParentChanged(ParentType oldParent) { } + protected virtual void OnParentChanged(TParent oldParent) { } /// Assigns a new parent to this instance - internal void SetParent(ParentType parent) { - ParentType oldParent = this.parent; + internal void SetParent(TParent parent) { + TParent oldParent = this.parent; this.parent = parent; OnParentChanged(oldParent); } /// Current parent of this object - private ParentType parent; + private TParent parent; } diff --git a/Source/StringBuilderHelper.Test.cs b/Source/StringBuilderHelper.Test.cs index 19dae80..fea6693 100644 --- a/Source/StringBuilderHelper.Test.cs +++ b/Source/StringBuilderHelper.Test.cs @@ -42,12 +42,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append((byte)255, GarbagePolicy.Avoid); - Assert.AreEqual("255", builder.ToString()); + Assert.AreEqual(((byte)255).ToString(), builder.ToString()); builder.Clear(); builder.Append((byte)255, GarbagePolicy.Accept); - Assert.AreEqual("255", builder.ToString()); + Assert.AreEqual(((byte)255).ToString(), builder.ToString()); } /// @@ -58,12 +58,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append((byte)0, GarbagePolicy.Avoid); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual(((byte)0).ToString(), builder.ToString()); builder.Clear(); builder.Append((byte)0, GarbagePolicy.Accept); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual(((byte)0).ToString(), builder.ToString()); } /// @@ -74,12 +74,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(12345, GarbagePolicy.Avoid); - Assert.AreEqual("12345", builder.ToString()); + Assert.AreEqual((12345).ToString(), builder.ToString()); builder.Clear(); builder.Append(12345, GarbagePolicy.Accept); - Assert.AreEqual("12345", builder.ToString()); + Assert.AreEqual((12345).ToString(), builder.ToString()); } /// @@ -90,12 +90,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(0, GarbagePolicy.Avoid); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual((0).ToString(), builder.ToString()); builder.Clear(); builder.Append(0, GarbagePolicy.Accept); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual((0).ToString(), builder.ToString()); } /// @@ -106,12 +106,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(-12345, GarbagePolicy.Avoid); - Assert.AreEqual("-12345", builder.ToString()); + Assert.AreEqual((-12345).ToString(), builder.ToString()); builder.Clear(); builder.Append(-12345, GarbagePolicy.Accept); - Assert.AreEqual("-12345", builder.ToString()); + Assert.AreEqual((-12345).ToString(), builder.ToString()); } /// @@ -122,12 +122,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(12345L, GarbagePolicy.Avoid); - Assert.AreEqual("12345", builder.ToString()); + Assert.AreEqual((12345L).ToString(), builder.ToString()); builder.Clear(); builder.Append(12345L, GarbagePolicy.Accept); - Assert.AreEqual("12345", builder.ToString()); + Assert.AreEqual((12345L).ToString(), builder.ToString()); } /// @@ -138,12 +138,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(0L, GarbagePolicy.Avoid); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual((0L).ToString(), builder.ToString()); builder.Clear(); builder.Append(0L, GarbagePolicy.Accept); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual((0L).ToString(), builder.ToString()); } /// @@ -154,12 +154,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(-12345L, GarbagePolicy.Avoid); - Assert.AreEqual("-12345", builder.ToString()); + Assert.AreEqual((-12345L).ToString(), builder.ToString()); builder.Clear(); builder.Append(-12345L, GarbagePolicy.Accept); - Assert.AreEqual("-12345", builder.ToString()); + Assert.AreEqual((-12345L).ToString(), builder.ToString()); } /// @@ -170,12 +170,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(-0.125f, GarbagePolicy.Avoid); - Assert.AreEqual("-0.125", builder.ToString()); + Assert.AreEqual((-0.125f).ToString(), builder.ToString()); builder.Clear(); builder.Append(-0.125f, GarbagePolicy.Accept); - Assert.AreEqual("-0.125", builder.ToString()); + Assert.AreEqual((-0.125f).ToString(), builder.ToString()); } /// @@ -186,12 +186,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(10.0625f, GarbagePolicy.Avoid); - Assert.AreEqual("10.0625", builder.ToString()); + Assert.AreEqual((10.0625f).ToString(), builder.ToString()); builder.Clear(); builder.Append(10.0625f, GarbagePolicy.Accept); - Assert.AreEqual("10.0625", builder.ToString()); + Assert.AreEqual((10.0625f).ToString(), builder.ToString()); } /// @@ -202,12 +202,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(0.00390625f, GarbagePolicy.Avoid); - Assert.AreEqual("0.00390625", builder.ToString()); + Assert.AreEqual((0.00390625f).ToString(), builder.ToString()); builder.Clear(); builder.Append(0.00390625f, GarbagePolicy.Accept); - Assert.AreEqual("0.00390625", builder.ToString()); + Assert.AreEqual((0.00390625f).ToString(), builder.ToString()); } /// @@ -218,12 +218,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(1000000000.0f, GarbagePolicy.Avoid); - Assert.AreEqual("1000000000.0", builder.ToString()); + Assert.AreEqual((1000000000.0f).ToString("F1"), builder.ToString()); builder.Clear(); builder.Append(1000000000.0f, GarbagePolicy.Accept); - Assert.AreEqual("1E+09", builder.ToString()); + Assert.AreEqual((1000000000.0f).ToString(), builder.ToString()); } /// Tests whether the number of decimal places can be restricted @@ -231,7 +231,7 @@ namespace Nuclex.Support { public void TestAppendFloatLimitDecimalPlaces() { StringBuilder builder = new StringBuilder(); builder.Append(0.00390625f, 3); - Assert.AreEqual("0.003", builder.ToString()); + Assert.AreEqual((0.003f).ToString(), builder.ToString()); } /// @@ -241,7 +241,7 @@ namespace Nuclex.Support { public void TestAppendFloatWithoutDecimalPlaces() { StringBuilder builder = new StringBuilder(); builder.Append(0.00390625f, 0); - Assert.AreEqual("0", builder.ToString()); // Note: no rounding! + Assert.AreEqual((0.0f).ToString(), builder.ToString()); // Note: no rounding! } /// @@ -265,12 +265,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(-32.015625, GarbagePolicy.Avoid); - Assert.AreEqual("-32.015625", builder.ToString()); + Assert.AreEqual((-32.015625).ToString(), builder.ToString()); builder.Clear(); builder.Append(-32.015625, GarbagePolicy.Accept); - Assert.AreEqual("-32.015625", builder.ToString()); + Assert.AreEqual((-32.015625).ToString(), builder.ToString()); } /// @@ -282,12 +282,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(10.0625, GarbagePolicy.Avoid); - Assert.AreEqual("10.0625", builder.ToString()); + Assert.AreEqual((10.0625).ToString(), builder.ToString()); builder.Clear(); builder.Append(10.0625, GarbagePolicy.Accept); - Assert.AreEqual("10.0625", builder.ToString()); + Assert.AreEqual((10.0625).ToString(), builder.ToString()); } /// @@ -299,12 +299,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(0.00390625, GarbagePolicy.Avoid); - Assert.AreEqual("0.00390625", builder.ToString()); + Assert.AreEqual((0.00390625).ToString(), builder.ToString()); builder.Clear(); builder.Append(0.00390625, GarbagePolicy.Accept); - Assert.AreEqual("0.00390625", builder.ToString()); + Assert.AreEqual((0.00390625).ToString(), builder.ToString()); } /// @@ -316,12 +316,12 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); builder.Append(1000000000000000000.0, GarbagePolicy.Avoid); - Assert.AreEqual("1000000000000000000.0", builder.ToString()); + Assert.AreEqual((1000000000000000000.0).ToString("F1"), builder.ToString()); builder.Clear(); builder.Append(1000000000000000000.0, GarbagePolicy.Accept); - Assert.AreEqual("1E+18", builder.ToString()); + Assert.AreEqual((1000000000000000000.0).ToString(), builder.ToString()); } /// Tests whether the number of decimal places can be restricted @@ -330,7 +330,7 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); StringBuilderHelper.Append(builder, 0.00390625, 3); - Assert.AreEqual("0.003", builder.ToString()); // Note: no rounding! + Assert.AreEqual((0.003).ToString(), builder.ToString()); // Note: no rounding! } /// @@ -341,7 +341,7 @@ namespace Nuclex.Support { StringBuilder builder = new StringBuilder(); StringBuilderHelper.Append(builder, 0.00390625, 0); - Assert.AreEqual("0", builder.ToString()); + Assert.AreEqual((0.0).ToString(), builder.ToString()); } /// diff --git a/Source/StringBuilderHelper.cs b/Source/StringBuilderHelper.cs index fc81632..c0a34db 100644 --- a/Source/StringBuilderHelper.cs +++ b/Source/StringBuilderHelper.cs @@ -19,8 +19,7 @@ License along with this library #endregion using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Globalization; using System.Text; namespace Nuclex.Support { @@ -33,6 +32,9 @@ namespace Nuclex.Support { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; + /// Characters used to separate decimal places in a number + private static readonly string decimalSeparator = getDecimalSeparator(); + /// Clears the contents of a string builder /// String builder that will be cleared public static void Clear(this StringBuilder builder) { @@ -195,7 +197,7 @@ namespace Nuclex.Support { } if(decimalPlaces > 0) { - builder.Append('.'); + builder.Append(decimalSeparator); // Build the fractional part if(fractional == 0) { @@ -303,7 +305,7 @@ namespace Nuclex.Support { } if(decimalPlaces > 0) { - builder.Append('.'); + builder.Append(decimalSeparator); // Build the fractional part if(fractional == 0) { @@ -364,6 +366,13 @@ namespace Nuclex.Support { builder.Append(numbers[digit]); } + /// Looks up the decimal separator used by the current system + /// + private static string getDecimalSeparator() { + NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat; + return numberFormatInfo.NumberDecimalSeparator; + } + } } // namespace Nuclex.Support