using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; //using Developpez.Dotnet.Collections; namespace Developpez.Dotnet.Tests { [TestFixture] public class StringExtensionsTests { [Test(Description = "Vérifie le bon fonctionnement de la méthode IsNullOrEmpty(String)")] public void Check_IsNullOrEmpty_String() { string nullString = null; string emptyString = string.Empty; string notNullOrEmptyString = "foo"; Assert.IsTrue(nullString.IsNullOrEmpty()); Assert.IsTrue(emptyString.IsNullOrEmpty()); Assert.IsFalse(notNullOrEmptyString.IsNullOrEmpty()); } [Test] public void Check_GetMD5Digest() { string text = "Hello world !"; string expected = "67c18d060479c5d867c9b91c80edeb4c"; string actual = text.GetMD5Digest(); Assert.AreEqual(expected, actual); } [Test] public void Test_FormatWith_StandardCall() { string expected = "Hello World !"; string actual = "Hello {0} !".FormatWith("World"); ; Assert.AreEqual(expected, actual); } [Test] public void Test_FormatWith_WhenNowildcard() { string expected = "Hello"; string actual = "Hello".FormatWith("bug"); ; Assert.AreEqual(expected, actual); } [Test] public void Test_Reverse() { string input = "Hello World !"; string expected = "! dlroW olleH"; string actual = input.Reverse(); Assert.AreEqual(expected, actual); } [Test] public void Test_Reverse_WithDiacriticsFormD() { string input = "Les Misérables".Normalize(NormalizationForm.FormD); string expected = "selbarésiM seL"; string actual = input.Reverse().Normalize(NormalizationForm.FormC); Assert.AreEqual(expected, actual); } [Test] public void Test_Join() { var list = new List { "Hello", "World", "!" }; string expected = "Hello World !"; string actual = list.Join(" "); Assert.AreEqual(expected, actual); } [Test] public void Test_Join_Chars() { char[] chars = new[] { 'h', 'e', 'l', 'l', 'o' }; string expected = new string(chars); string actual = chars.Join(); Assert.AreEqual(expected, actual); } [Test] public void Test_GetLines() { string input = @"Les sanglots longs Des violons De l'automne Blessent mon cœur D'une langueur Monotone."; var expected = new string[] { "Les sanglots longs", "Des violons", " De l'automne", "Blessent mon cœur", "D'une langueur", " Monotone." }; var actual = input.GetLines(); Assert.That(actual.SequenceEqual(expected)); } [Test] public void Test_RemoveDiacritics() { string input = "àâäçéèêñõ"; string expected = "aaaceeeno"; string actual = input.RemoveDiacritics(); Assert.AreEqual(expected, actual); } [Test] public void Test_Left() { string input = "Hello world !"; string expected = "Hell"; string actual = input.Left(4); Assert.AreEqual(expected, actual); expected = string.Empty; actual = input.Left(0); Assert.AreEqual(expected, actual); bool wasTriggered = false; try { actual = input.Left(-1); } catch (ArgumentOutOfRangeException) { wasTriggered = true; } Assert.IsTrue(wasTriggered, "ArgumentOutOfRangeException non levée"); wasTriggered = false; try { actual = input.Left(50); } catch (ArgumentOutOfRangeException) { wasTriggered = true; } Assert.IsTrue(wasTriggered, "ArgumentOutOfRangeException non levée"); } [Test] public void Test_Right() { string input = "Hello world !"; string expected = "ld !"; string actual = input.Right(4); Assert.AreEqual(expected, actual); expected = string.Empty; actual = input.Right(0); Assert.AreEqual(expected, actual); bool wasTriggered = false; try { actual = input.Right(-1); } catch (ArgumentOutOfRangeException) { wasTriggered = true; } Assert.IsTrue(wasTriggered, "ArgumentOutOfRangeException non levée"); wasTriggered = false; try { actual = input.Right(50); } catch (ArgumentOutOfRangeException) { wasTriggered = true; } Assert.IsTrue(wasTriggered, "ArgumentOutOfRangeException non levée"); } [Test] public void Test_GetCharFrequencies() { string input = "Hello world !"; var expected = new Dictionary { { 'H', 1 }, { 'e', 1 }, { 'l', 3 }, { 'o', 2 }, { ' ', 2 }, { 'w', 1 }, { 'r', 1 }, { 'd', 1 }, { '!', 1 } }; var actual = input.GetCharFrequencies(); Assert.IsTrue(actual.SequenceEqual(expected)); } [Test] public void Test_From_Index() { string input = "Hello world !"; string expected = "world !"; string actual = input.From(6); Assert.AreEqual(expected, actual); } [Test] public void Test_From_To_Index() { string input = "Hello world !"; string expected = "lo wo"; string actual = input.From(3).To(7); Assert.AreEqual(expected, actual); } [Test] public void Test_From_String() { string input = "Hello world !"; string expected = "rld !"; string actual = input.From("wo"); Assert.AreEqual(expected, actual); expected = "world !"; actual = input.From("wo", true); Assert.AreEqual(expected, actual); } [Test] public void Test_From_To_String() { string input = "Hello world !"; string expected = "world"; string actual = input.From("").To(""); Assert.AreEqual(expected, actual); expected = "world"; actual = input.From("", true).To("", true); Assert.AreEqual(expected, actual); expected = "world"; actual = input.From("", true).To("", false); Assert.AreEqual(expected, actual); expected = "world"; actual = input.From("", false).To("", true); Assert.AreEqual(expected, actual); } } }