From 8bc463d915f4a355908803fb192d3f0b81fc3bf2 Mon Sep 17 00:00:00 2001 From: Corniel Nobel Date: Fri, 29 Mar 2024 10:43:21 +0100 Subject: [PATCH] Rewritten some tests. --- .editorconfig | 2 +- specs/Qowaiv.Specs/Date_span_specs.cs | 14 +- .../Formatting_arguments_collection_specs.cs | 338 ++++++++++++++ .../Formatting/String_formatter_specs.cs | 32 +- ...ComparerTest.cs => UUID_comparer_specs.cs} | 59 +-- .../_Legacy/DateSpanOperationTest.cs | 12 - .../FormattingArgumentsCollectionTest.cs | 426 ------------------ .../_Legacy/Formatting/StringFormatterTest.cs | 59 --- .../FormattingArgumentsCollection.cs | 1 + 9 files changed, 406 insertions(+), 537 deletions(-) create mode 100644 specs/Qowaiv.Specs/Formatting/Formatting_arguments_collection_specs.cs rename specs/Qowaiv.Specs/{_Legacy/UuidComparerTest.cs => UUID_comparer_specs.cs} (53%) delete mode 100644 specs/Qowaiv.Specs/_Legacy/DateSpanOperationTest.cs delete mode 100644 specs/Qowaiv.Specs/_Legacy/Formatting/FormattingArgumentsCollectionTest.cs delete mode 100644 specs/Qowaiv.Specs/_Legacy/Formatting/StringFormatterTest.cs diff --git a/.editorconfig b/.editorconfig index 80ff5204..9ca038c9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -113,7 +113,6 @@ dotnet_diagnostic.S3925.severity = none # "ISerializable" should be implemented dotnet_diagnostic.S4136.severity = none # Method overloads should be grouped together dotnet_diagnostic.S1133.severity = suggestion # Deprecated code should be removed -dotnet_diagnostic.S3900.severity = suggestion # Arguments of public methods should be validated against null dotnet_diagnostic.S107.severity = warning # Methods should not have too many parameters dotnet_diagnostic.S127.severity = warning # Methods should not have too many parameters @@ -136,6 +135,7 @@ dotnet_diagnostic.S3215.severity = warning # Interface instances should not be c dotnet_diagnostic.S3218.severity = warning # Inner class members should not shadow outer class "static" or type members dotnet_diagnostic.S3257.severity = warning # Declarations and initializations should be as concise as possible dotnet_diagnostic.S3776.severity = warning # Cognitive Complexity of methods should not be too high +dotnet_diagnostic.S3900.severity = warning # Arguments of public methods should be validated against null dotnet_diagnostic.S6354.severity = warning # Use a testable (date) time provider instead dotnet_diagnostic.VSSpell001.severity = silent # Correct spelling \ No newline at end of file diff --git a/specs/Qowaiv.Specs/Date_span_specs.cs b/specs/Qowaiv.Specs/Date_span_specs.cs index c222fc05..4482db9a 100644 --- a/specs/Qowaiv.Specs/Date_span_specs.cs +++ b/specs/Qowaiv.Specs/Date_span_specs.cs @@ -1,4 +1,6 @@ -namespace Date_span_specs; +using FluentAssertions.Extensions; + +namespace Date_span_specs; public class Is_comparable { @@ -141,6 +143,16 @@ public void plus_does_nothing() } } +public class Can_be_added_to +{ + [Test] + public void Date_times() + { + var date = 30.January(1999).At(13, 42).AsUtc(); + date.Add(DateSpan.FromMonths(1)).Should().Be(28.February(1999).At(13, 42).AsUtc()); + } +} + public class Can_create { [Test] diff --git a/specs/Qowaiv.Specs/Formatting/Formatting_arguments_collection_specs.cs b/specs/Qowaiv.Specs/Formatting/Formatting_arguments_collection_specs.cs new file mode 100644 index 00000000..702864a0 --- /dev/null +++ b/specs/Qowaiv.Specs/Formatting/Formatting_arguments_collection_specs.cs @@ -0,0 +1,338 @@ +namespace Formatting_arguments_collection_specs; + +public class Can_be_constructed +{ + [Test] + public void based_on_parent_collection() + { + var parent = new FormattingArgumentsCollection(TestCultures.nl) + { + { typeof(Date), "yyyy-MM" }, + }; + + var collection = new FormattingArgumentsCollection(TestCultures.nl_BE, parent); + collection.Should().HaveCount(1); + } +} + +public class Formats +{ + [Test] + public void indexes_up_to_1_000_000() + { + var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); + var args = new object[1_000_001]; + args[1_000_000] = "Test"; + collection.Format("Begin {1000000} End", args).Should().Be("Begin Test End"); + } + + [Test] + public void with_allign_left() + { + var collection = new FormattingArgumentsCollection(); + collection.Format("{0,-4}", "a").Should().Be("a "); + } + + [Test] + public void with_allign_right() + { + var collection = new FormattingArgumentsCollection(); + collection.Format("{0,3}", "a").Should().Be(" a"); + } + + [Test] + public void null_arguments() + { + var collection = new FormattingArgumentsCollection(); + var args = new object?[] { null }; + collection.Format("Value: '{0}'", args).Should().Be("Value: ''"); + } + + [TestCase("{{", "{")] + [TestCase("}}", "}")] + public void with_escaped_placehodlers(string format, string formatted) + { + var collection = new FormattingArgumentsCollection(); + collection.Format(format).Should().Be(formatted); + } + + [Test] + public void applying_specified_formats() + { + using (CultureInfoScope.NewInvariant()) + { + var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")) + { + { typeof(Date), "yyyy-MM-dd HH:mm" }, + { typeof(decimal), "0.000" } + }; + + var formatted = collection.Format("{0:000.00} - {1} * {1:dd-MM-yyyy} - {2} - {3} - {4}", 3, new Date(2014, 10, 8), 666, 0.8m, 0.9); + formatted.Should().Be("003,00 - 2014-10-08 00:00 * 08-10-2014 - 666 - 0,800 - 0,9"); + } + } + + [Test] + public void applying_custom_format_provider() + { + using (CultureInfoScope.NewInvariant()) + { + var collection = new FormattingArgumentsCollection(FormatProvider.CustomFormatter) + { + { typeof(Date), "yyyy-MM-dd HH:mm" }, + { typeof(decimal), "0.000" } + }; + + var formattted = collection.Format("{0:yyyy-MM-dd} * {0}", new Date(2014, 10, 8)); + formattted.Should().Be("Unit Test Formatter, value: '2014-10-08', format: 'yyyy-MM-dd' * Unit Test Formatter, value: '10/08/2014', format: ''"); + } + } +} + +public class Does_not_support_formats +{ + private static readonly FormattingArgumentsCollection Collection = new(TestCultures.en_GB); + + [Test] + public void with_length_not_being_a_number() + => "{0,a}".Invoking(format => Collection.Format(format)).Should().Throw(); + + [Test] + public void with_index_not_being_a_number() + => "{x}".Invoking(format => Collection.Format(format)).Should().Throw(); + + [Test] + public void with_index_not_being_negative() + => "{-1}".Invoking(format => Collection.Format(format)).Should().Throw(); + + [TestCase("{")] + [TestCase("{0")] + [TestCase("}")] + public void with_mallformed_placeholders(string format) + => format.Invoking(f => Collection.Format(f)).Should().Throw(); + + [Test] + public void indexes_beging_out_of_range() + => "{0}{1}".Invoking(format => Collection.Format(format, 1)) + .Should() + .Throw() + .WithMessage("Index (zero based) must be greater than or equal to zero and less than the size of the argument list."); +} + +public class Stringifies +{ + [Test] + public void IFormattable_null_as_null() + { + IFormattable? obj = null; + var collection = new FormattingArgumentsCollection(); + collection.ToString(obj).Should().BeNull(); + } + [Test] + public void object_null_as_null() + { + var collection = new FormattingArgumentsCollection(); + collection.ToString((object?)null).Should().BeNull(); + } + [Test] + public void object_as_ToString_value() + { + var collection = new FormattingArgumentsCollection(); + collection.ToString(typeof(int)).Should().Be("System.Int32"); + } + + [Test] + public void appying_peferred_formats() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "000" } + }; + collection.ToString((object)7).Should().Be("007"); + } +} + +public class Can_be_modified +{ + public class Adding + { + [Test] + public void format() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Add(typeof(int), "Int32Format"); + collection.Should().ContainSingle(); + } + + [Test] + public void IFormatProvider() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Add(typeof(int), new CultureInfo("nl-NL")); + collection.Should().ContainSingle(); + } + + [Test] + public void format_with_IFormatProvider() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Add(typeof(int), "Int32Format", new CultureInfo("nl-NL")); + collection.Should().ContainSingle(); + } + } + + public class Setting + { + [Test] + public void format() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Set(typeof(int), "Int32Format"); + collection.Should().ContainSingle(); + } + + [Test] + public void IFormatProvider() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Set(typeof(int), new CultureInfo("nl-NL")); + collection.Should().ContainSingle(); + } + + [Test] + public void format_with_IFormatProvider() + { + var collection = new FormattingArgumentsCollection(); + collection.Should().BeEmpty(); + collection.Set(typeof(int), "Int32Format", new CultureInfo("nl-NL")); + collection.Should().ContainSingle(); + } + + [Test] + public void same_key_twice() + { + var collection = new FormattingArgumentsCollection(); + collection.Set(typeof(int), "New"); + collection.Set(typeof(int), "Update"); + collection.Should().ContainSingle(); + } + } + + public class Removing + { + [Test] + public void non_cotained_key() + { + var collection = new FormattingArgumentsCollection(); + collection.Remove(typeof(int)).Should().BeFalse(); + } + + [Test] + public void contained_key() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "Int32Format" }, + { typeof(long), "Int64Format" }, + }; + + collection.Remove(typeof(int)).Should().BeTrue(); + collection.Should().ContainSingle(); + } + } + + [Test] + public void Clear_CollectionWithTwoItems_0Items() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "Int32Format" }, + { typeof(Date), "Date" } + }; + collection.Clear(); + + var act = collection.Count; + var exp = 0; + + act.Should().Be(exp); + } +} + +public class Can_be_queried +{ + [Test] + public void on_types() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "Int32Format" }, + { typeof(Date), "Date" } + }; + + var act = collection.Types; + var exp = new[] { typeof(int), typeof(Date) }; + + act.Should().BeEquivalentTo(exp); + } + + [Test] + public void on_types_contained() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "00" } + }; + collection.Contains(typeof(int)).Should().BeTrue(); + } + + [Test] + public void on_types_not_contained() + { + var collection = new FormattingArgumentsCollection(); + collection.Contains(typeof(int)).Should().BeFalse(); + } + + [Test] + public void via_enumerator() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "Int32Format" }, + { typeof(Date), "Date" } + }; + + var ienumerable = collection as IEnumerable>; + ienumerable.Should().HaveCount(2); + } +} + +public class Does_not_support +{ + private static readonly FormattingArgumentsCollection Collection = new(TestCultures.en_GB); + + [Test] + public void types_who_are_not_formattable() + => Collection.Invoking(c => c.Add(typeof(Type), "")) + .Should() + .Throw() + .WithMessage("The argument must implement System.IFormattable.*"); + + [Test] + public void same_key_twice() + { + var collection = new FormattingArgumentsCollection + { + { typeof(int), "New" } + }; + + collection.Invoking(c => c.Add(typeof(int), "Update")) + .Should() + .Throw() + .WithMessage("An item with the same key has already been added.*"); + } +} \ No newline at end of file diff --git a/specs/Qowaiv.Specs/Formatting/String_formatter_specs.cs b/specs/Qowaiv.Specs/Formatting/String_formatter_specs.cs index b8ce9147..00d1a85f 100644 --- a/specs/Qowaiv.Specs/Formatting/String_formatter_specs.cs +++ b/specs/Qowaiv.Specs/Formatting/String_formatter_specs.cs @@ -1,6 +1,6 @@ namespace String_formatter_specs; -public class TryApplyCustomFormatter +public class Try_apply_custom_formatter { [Test] public void false_if_no_customformatter_could_be_found() @@ -38,3 +38,33 @@ public string Format(string? format, object? arg, IFormatProvider? formatProvide => formatType == typeof(ICustomFormatter) ? this : null; } } + +public class Apply +{ + + [Test] + public void Apply_InvalidFormat_ThrowsFormatException() + => int.MinValue.Invoking(v => StringFormatter.Apply(v, "\\", CultureInfo.InvariantCulture, [])) + .Should() + .Throw() + .WithMessage("Input string was not in a correct format."); +} + +public class To_non_diacritic +{ + [Test] + public void null_stays_null() + => StringFormatter.ToNonDiacritic(Nil.String).Should().Be(Nil.String); + + [Test] + public void string_empty_stays_string_empty() + => StringFormatter.ToNonDiacritic(string.Empty).Should().Be(string.Empty); + + [Test] + public void updates_diacritic() + => StringFormatter.ToNonDiacritic("Café & Straße").Should().Be("Cafe & Strasze"); + + [Test] + public void updates_diacritic_ignoring_specified() + => StringFormatter.ToNonDiacritic("Café & Straße", "é").Should().Be("Café & Strasze"); +} \ No newline at end of file diff --git a/specs/Qowaiv.Specs/_Legacy/UuidComparerTest.cs b/specs/Qowaiv.Specs/UUID_comparer_specs.cs similarity index 53% rename from specs/Qowaiv.Specs/_Legacy/UuidComparerTest.cs rename to specs/Qowaiv.Specs/UUID_comparer_specs.cs index e601fc36..1cc19ad9 100644 --- a/specs/Qowaiv.Specs/_Legacy/UuidComparerTest.cs +++ b/specs/Qowaiv.Specs/UUID_comparer_specs.cs @@ -1,6 +1,6 @@ -namespace Qowaiv.UnitTests; +namespace UUID_comparer_specs; -public class UuidComparerTest +public class Compares { private readonly Guid guid = Guid.Parse("40E56044-F781-43BD-A4AE-AA08882B4E28"); private readonly Uuid uuid = Uuid.Parse("BD411BB9-D8C9-4A4F-B739-57F2312E0BC5"); @@ -23,13 +23,13 @@ public class UuidComparerTest [TestCase(00, 01)] [TestCase(01, 02)] [TestCase(02, 03)] - public void Compare_SqlServer(int index0, int index1) + public void SQL_Server(int index0, int index1) { var l = Simple(index0, index1, 1, 2); var r = Simple(index0, index1, 2, 1); var compare = UuidComparer.SqlServer.Compare(l, r); - Assert.AreEqual(-1, compare); + compare.Should().Be(-1); } private static Uuid Simple(int i0, int i1, byte v0, byte v1) @@ -54,50 +54,35 @@ public void Is_ordered_Comparer_Guid_Default() } [Test] - public void Compare_NullNull_Zero() - { - Assert.AreEqual(0, UuidComparer.Default.Compare(null, null)); - } + public void Null_with_null_as_zero() + => UuidComparer.Default.Compare(null, null).Should().Be(0); [Test] - public void Compare_NullGuid_Minus1() - { - Assert.AreEqual(-1, UuidComparer.Default.Compare(null, Guid.NewGuid())); - } + public void Null_with_GUID_as_minus_1() + => UuidComparer.Default.Compare(null, Guid.NewGuid()).Should().Be(-1); [Test] - public void Compare_NullUuid_Minus1() - { - Assert.AreEqual(-1, UuidComparer.Default.Compare(null, Uuid.NewUuid())); - } + public void Null_with_UUID_as_minus_1() + => UuidComparer.Default.Compare(null, Uuid.NewUuid()).Should().Be(-1); - [Test] - public void Compare_GuidNull_Plus1() - { - Assert.AreEqual(+1, UuidComparer.Default.Compare(Guid.NewGuid(), null)); - } + [Test] + public void GUID_with_null_as_plus_1() + => UuidComparer.Default.Compare(Guid.NewGuid(), null).Should().Be(+1); - [Test] - public void Compare_UuidNull_Plus1() - { - Assert.AreEqual(+1, UuidComparer.Default.Compare(Uuid.NewUuid(), null)); - } + [Test] + public void UUID_with_null_as_plus_1() + => UuidComparer.Default.Compare(Uuid.NewUuid(), null).Should().Be(+1); [Test] - public void Compare_GuidUuid_Minus1() - { - Assert.AreEqual(-1, UuidComparer.Default.Compare((object)guid, (object)uuid)); - } + public void GUID_with_UUID() + => UuidComparer.Default.Compare((object)guid, (object)uuid).Should().Be(-1); [Test] - public void Compare_UuidGuid_Plus1() - { - Assert.AreEqual(+1, UuidComparer.Default.Compare((object)uuid, (object)guid)); - } + public void UUID_with_GUIDF() + => UuidComparer.Default.Compare((object)uuid, (object)guid).Should().Be(+1); [Test] public void Compare_UuidObject_Throws() - { - Assert.Throws(() => UuidComparer.Default.Compare((object)uuid, new object())); - } + => new object().Invoking(o => UuidComparer.Default.Compare((object)uuid, o)) + .Should().Throw(); } diff --git a/specs/Qowaiv.Specs/_Legacy/DateSpanOperationTest.cs b/specs/Qowaiv.Specs/_Legacy/DateSpanOperationTest.cs deleted file mode 100644 index 0dea8524..00000000 --- a/specs/Qowaiv.Specs/_Legacy/DateSpanOperationTest.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Qowaiv.UnitTests; - -public class DateSpanOperationTest -{ - [Test] - public void Add_DateSpan_ShouldAddDaysSecond() - { - var date = new DateTime(1999, 01, 30, 00, 00, 00, 000, DateTimeKind.Utc); - var span = DateSpan.FromMonths(1); - date.Add(span).Should().Be(new DateTime(1999, 02, 28, 00, 00, 000, DateTimeKind.Utc)); - } -} diff --git a/specs/Qowaiv.Specs/_Legacy/Formatting/FormattingArgumentsCollectionTest.cs b/specs/Qowaiv.Specs/_Legacy/Formatting/FormattingArgumentsCollectionTest.cs deleted file mode 100644 index 8c5355a5..00000000 --- a/specs/Qowaiv.Specs/_Legacy/Formatting/FormattingArgumentsCollectionTest.cs +++ /dev/null @@ -1,426 +0,0 @@ -namespace Qowaiv.UnitTests.Formatting; - -public class FormattingArgumentsCollectionTest -{ - [Test] - public void Ctor_WithParent_ThrowsFormatException() - { - var parent = new FormattingArgumentsCollection(new CultureInfo("nl")) - { - { typeof(Date), "yyyy-MM" } - }; - - var act = new FormattingArgumentsCollection(new CultureInfo("nl-BE"), parent); - Assert.AreEqual(1, act.Count, "act.Count"); - - parent.Add(typeof(Sex), "f"); - Assert.AreEqual(2, parent.Count, "parent.Count"); - Assert.AreEqual(1, act.Count, "act.Count"); - } - - [Test] - public void Format_LengthPlus_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{0,+}", 1); - }, - "Input string was not in a correct format."); - } - [Test] - public void Format_LengthA_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{0,a}", 1); - }, - "Input string was not in a correct format."); - } - - [Test] - public void Format_IndexPlus_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{+}"); - }, - "Input string was not in a correct format."); - } - [Test] - public void Format_IndexA_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{a}"); - }, - "Input string was not in a correct format."); - } - - [Test] - public void Format_Index1000000_AreEqual() - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - var args = new object[1000001]; - args[1000000] = "Test"; - var act = collection.Format("Begin {1000000} End", args); - var exp = "Begin Test End"; - - act.Should().Be(exp); - } - [Test] - public void Format_InvalidFormat_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("}"); - }, - "Input string was not in a correct format."); - } - [Test] - public void Format_ElementStartedButNotClosed_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("Test {0", 12); - }, - "Input string was not in a correct format."); - } - [Test] - public void Format_no_parsable_index_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{a}"); - }, - "Input string was not in a correct format."); - } - [Test] - public void Format_IndexOutOfRange_ThrowsFormatException() - { - Assert.Catch(() => - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")); - collection.Format("{0}{1}", 1); - }, - "Index (zero based) must be greater than or equal to zero and less than the size of the argument list."); - } - - [Test] - public void Format_ArrayWithNullItem_String() - { - var collection = new FormattingArgumentsCollection(); - var args = new object?[] { null }; - var act = collection.Format("Value: '{0}'", args); - var exp = "Value: ''"; - - act.Should().Be(exp); - } - [Test] - public void Format_AlignLeft_String() - { - var collection = new FormattingArgumentsCollection(); - - var act = collection.Format("{0,-4}", "a"); - var exp = "a "; - - act.Should().Be(exp); - } - [Test] - public void Format_AlignRight_String() - { - var collection = new FormattingArgumentsCollection(); - - var act = collection.Format("{0,3}", "a"); - var exp = " a"; - - act.Should().Be(exp); - } - [Test] - public void Format_EscapeLeft_String() - { - var collection = new FormattingArgumentsCollection(); - - var act = collection.Format("{{"); - var exp = "{"; - - act.Should().Be(exp); - } - [Test] - public void Format_EscapeRight_String() - { - var collection = new FormattingArgumentsCollection(); - - var act = collection.Format("}}"); - var exp = "}"; - - act.Should().Be(exp); - } - [Test] - public void Format_ComplexPattern_AreEqual() - { - using (CultureInfoScope.NewInvariant()) - { - var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE")) - { - { typeof(Date), "yyyy-MM-dd HH:mm" }, - { typeof(decimal), "0.000" } - }; - - var act = collection.Format("{0:000.00} - {1} * {1:dd-MM-yyyy} - {2} - {3} - {4}", 3, new Date(2014, 10, 8), 666, 0.8m, 0.9); - var exp = "003,00 - 2014-10-08 00:00 * 08-10-2014 - 666 - 0,800 - 0,9"; - - act.Should().Be(exp); - } - } - [Test] - public void Format_ComplexPatternWithUnitTestFormatProvider_AreEqual() - { - using (CultureInfoScope.NewInvariant()) - { - var collection = new FormattingArgumentsCollection(FormatProvider.CustomFormatter) - { - { typeof(Date), "yyyy-MM-dd HH:mm" }, - { typeof(decimal), "0.000" } - }; - - var act = collection.Format("{0:yyyy-MM-dd} * {0}", new Date(2014, 10, 8)); - var exp = "Unit Test Formatter, value: '2014-10-08', format: 'yyyy-MM-dd' * Unit Test Formatter, value: '10/08/2014', format: ''"; - - act.Should().Be(exp); - } - } - - [Test] - public void ToString_IFormattableNull_IsNull() - { - IFormattable? obj = null; - var collection = new FormattingArgumentsCollection(); - collection.ToString(obj) - .Should().BeNull(); - } - [Test] - public void ToString_ObjectNull_IsNull() - { - var collection = new FormattingArgumentsCollection(); - collection.ToString((object?)null) - .Should().BeNull(); } - [Test] - public void ToString_TypeInt32_SystemInt32() - { - var collection = new FormattingArgumentsCollection(); - string act = collection.ToString((object)typeof(int)); - string exp = "System.Int32"; - - act.Should().Be(exp); - } - [Test] - public void ToString_7_007() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "000" } - }; - collection.ToString((object)7).Should().Be("007"); - } - - #region Collection manipulation - - [Test] - public void Add_not_IFormattable_type_throws() - { - var collection = new FormattingArgumentsCollection(); - Action add = () => collection.Add(typeof(Type), ""); - add.Should() - .Throw() - .WithMessage("The argument must implement System.IFormattable.*"); - } - - [Test] - public void Add_DuplicateKey_ThrowsArgumentException() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "New" } - }; - Action add = () => collection.Add(typeof(int), "Update"); - add.Should() - .Throw() - .WithMessage("An item with the same key has already been added.*"); - } - - [Test] - public void Add_Int32Format_Contains1Item() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" } - }; - - Assert.AreEqual(1, collection.Count, "Count"); - } - [Test] - public void Add_Int32CultureInfo_Contains1Item() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), new CultureInfo("nl-NL") } - }; - - Assert.AreEqual(1, collection.Count, "Count"); - } - [Test] - public void Add_Int32FormatAndFormatProvider_Contains1Item() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format", new CultureInfo("nl-NL") } - }; - - Assert.AreEqual(1, collection.Count, "Count"); - } - - [Test] - public void Set_Int32Format_Contains1Item() - { - var collection = new FormattingArgumentsCollection(); - collection.Set(typeof(int), "Int32Format"); - - Assert.AreEqual(1, collection.Count, "Count"); - } - [Test] - public void Set_Int32CultureInfo_Contains1Item() - { - var collection = new FormattingArgumentsCollection(); - collection.Set(typeof(int), new CultureInfo("nl-NL")); - - Assert.AreEqual(1, collection.Count, "Count"); - } - [Test] - public void Set_Int32FormatAndFormatProviderContains1Item() - { - var collection = new FormattingArgumentsCollection(); - collection.Set(typeof(int), "Int32Format", new CultureInfo("nl-NL")); - - Assert.AreEqual(1, collection.Count, "Count"); - } - [Test] - public void Set_SameTypeTwice_Contains1Item() - { - var collection = new FormattingArgumentsCollection(); - collection.Set(typeof(int), "New"); - collection.Set(typeof(int), "Update"); - - Assert.AreEqual(1, collection.Count, "Count"); - } - - [Test] - public void Remove_Int32_Unsuccessful() - { - var collection = new FormattingArgumentsCollection(); - var act = collection.Remove(typeof(int)); - var exp = false; - - act.Should().Be(exp); - } - [Test] - public void Remove_AddedInt32_Successful() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" } - }; - var act = collection.Remove(typeof(int)); - var exp = true; - - act.Should().Be(exp); - } - - [Test] - public void Types_CollectionWithTwoItems_Int32AndDate() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" }, - { typeof(Date), "Date" } - }; - - var act = collection.Types; - var exp = new [] { typeof(int), typeof(Date) }; - - act.Should().BeEquivalentTo(exp); - } - - [Test] - public void GetEnumerator_IEnumerableKeyValuePair_IsNotNull() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" }, - { typeof(Date), "Date" } - }; - - var ienumerable = collection as IEnumerable>; - - var act = ienumerable.GetEnumerator(); - act.Should().NotBeNull(); - } - [Test] - public void GetEnumerator_IEnumerable_IsNotNull() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" }, - { typeof(Date), "Date" } - }; - - var ienumerable = collection as IEnumerable; - - var act = ienumerable.GetEnumerator(); - act.Should().NotBeNull(); - } - - [Test] - public void Clear_CollectionWithTwoItems_0Items() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "Int32Format" }, - { typeof(Date), "Date" } - }; - collection.Clear(); - - var act = collection.Count; - var exp = 0; - - act.Should().Be(exp); - } - - [Test] - public void Contains_FilledCollectionInt32_IsTrue() - { - var collection = new FormattingArgumentsCollection - { - { typeof(int), "00" } - }; - var act = collection.Contains(typeof(int)); - var exp = true; - act.Should().Be(exp); - } - [Test] - public void Contains_EmptyCollectionInt32_IsFalse() - { - var collection = new FormattingArgumentsCollection(); - var act = collection.Contains(typeof(int)); - var exp = false; - act.Should().Be(exp); - } - - #endregion -} diff --git a/specs/Qowaiv.Specs/_Legacy/Formatting/StringFormatterTest.cs b/specs/Qowaiv.Specs/_Legacy/Formatting/StringFormatterTest.cs deleted file mode 100644 index 59978195..00000000 --- a/specs/Qowaiv.Specs/_Legacy/Formatting/StringFormatterTest.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace Qowaiv.UnitTests.Formatting; - -[TestFixture] -public class StringFormatterTest -{ - [Test] - public void Apply_InvalidFormat_ThrowsFormatException() - { - Assert.Catch(() => - { - StringFormatter.Apply(int.MinValue, "\\", CultureInfo.InvariantCulture, []); - }, - "Input string was not in a correct format."); - } - - [Test] - public void ToNonDiacritic_Null_AreEqual() - { - var str = Nil.String; - - var exp = Nil.String; - var act = StringFormatter.ToNonDiacritic(str); - - act.Should().Be(exp); - } - - [Test] - public void ToNonDiacritic_StringEmpty_AreEqual() - { - var str = string.Empty; - - var exp = string.Empty; - var act = StringFormatter.ToNonDiacritic(str); - - act.Should().Be(exp); - } - - [Test] - public void To_non_diacritic_Cafe_und_Straße_AreEqual() - { - var str = "Café & Straße"; - - var exp = "Cafe & Strasze"; - var act = StringFormatter.ToNonDiacritic(str); - - act.Should().Be(exp); - } - - [Test] - public void To_non_diacritic_Cafe_und_Straße_ignore_e() - { - var str = "Café & Straße"; - - var exp = "Café & Strasze"; - var act = StringFormatter.ToNonDiacritic(str, "é"); - - act.Should().Be(exp); - } -} diff --git a/src/Qowaiv/Formatting/FormattingArgumentsCollection.cs b/src/Qowaiv/Formatting/FormattingArgumentsCollection.cs index 836db387..370d9169 100644 --- a/src/Qowaiv/Formatting/FormattingArgumentsCollection.cs +++ b/src/Qowaiv/Formatting/FormattingArgumentsCollection.cs @@ -484,6 +484,7 @@ public FormattingArguments Get(Type? type) /// Returns an enumerator that iterates through the collection. [Pure] + [ExcludeFromCodeCoverage/* Justification = "Legacy non-generic interface." */] IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// Returns an enumerator that iterates through the collection.