StringHelperTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Framework » Text » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » Framework » Text » StringHelperTests.cs
using System;
using System.Collections.Generic;
using System.Linq;
using MbUnit.Framework;
using Subtext.Framework.Text;

namespace UnitTests.Subtext.Framework.Text{
    /// <summary>
    /// Summary description for StringHelperTests.
    /// </summary>
    [TestFixture]
    public class StringHelperTests
    {
        [Test]
        public void Remove_PassingInTextWithRepeatingSequenceAndOccurrenceCountOfOne_RemovesFirstOccurrence()
        {
            //act
            string result = "foo/bar/foo".Remove("Foo", 1, StringComparison.OrdinalIgnoreCase);

            //assert
            Assert.AreEqual("/bar/foo", result);
        }

        [Test]
        public void Remove_PassingInTextWithRepeatingSequenceAndOccurrenceCountOfTwo_RemovesAllOccurrences()
        {
            //act
            string result = "foo/bar/foo".Remove("Foo", 2, StringComparison.OrdinalIgnoreCase);

            //assert
            Assert.AreEqual("/bar/", result);
        }

        [Test]
        public void Remove_PassingInTextWithRepeatingSequenceAndOccurrenceCountOfFour_RemovesAllOccurrences()
        {
            //act
            string result = "foo/bar/foo".Remove("Foo", 4, StringComparison.OrdinalIgnoreCase);

            //assert
            Assert.AreEqual("/bar/", result);
        }

        [RowTest]
        [Row("Blah..Blah", '.', "Blah.Blah")]
        [Row("Blah...Blah", '.', "Blah.Blah")]
        [Row("Blah....Blah", '.', "Blah.Blah")]
        [Row("Blah- -Blah", '-', "Blah- -Blah")]
        [Row("Blah--Blah", '.', "Blah--Blah")]
        public void CanRemoveDoubleCharacter(string text, char character, string expected)
        {
            Assert.AreEqual(expected, text.RemoveDoubleCharacter(character));
        }

        [Test]
        public void RemoveDoubleCharacter_WithNullCharacter_ThrowsArgumentNullException()
        {
            UnitTestHelper.AssertThrowsArgumentNullException(
                () => "6 bdy.RemoveDoubleCharacter(e)".RemoveDoubleCharacter(Char.MinValue)
            );
        }


        /// <summary>
        /// Tests that we can properly pascal case text.
        /// </summary>
        /// <remarks>
        /// Does not remove punctuation.
        /// </remarks>
        /// <param name="original"></param>
        /// <param name="expected"></param>
        [RowTest]
        [Row("", "")]
        [Row("a", "A")]
        [Row("A", "A")]
        [Row("A B", "AB")]
        [Row("a bee keeper's dream.", "ABeeKeeper'sDream.")]
        public void PascalCaseTests(string original, string expected)
        {
            Assert.AreEqual(expected, original.ToPascalCase());
        }

        [Test]
        public void PascalCaseThrowsArgumentNullException()
        {
            UnitTestHelper.AssertThrowsArgumentNullException(() =>
                StringHelper.ToPascalCase(null)
            );
        }

        [RowTest]
        [Row("BLAH Tast", "a", 6, StringComparison.Ordinal)]
        [Row("BLAH Tast", "a", 2, StringComparison.InvariantCultureIgnoreCase)]
        public void IndexOfHandlesCaseSensitivity(string source, string search, int expectedIndex,
                                                  StringComparison comparison)
        {
            Assert.AreEqual(expectedIndex, source.IndexOf(search, comparison),
                            "Did not find the string '{0}' at the index {1}", search, expectedIndex);
        }

        [RowTest]
        [Row("Blah/Default.aspx", "Default.aspx", "Blah/", StringComparison.Ordinal)]
        [Row("Blah/Default.aspx", "default.aspx", "Blah/", StringComparison.InvariantCultureIgnoreCase)]
        [Row("Blah/Default.aspx", "default.aspx", "Blah/Default.aspx", StringComparison.Ordinal)]
        public void LeftBeforeOfHandlesCaseSensitivity(string source, string search, string expected,
                                                       StringComparison comparison)
        {
            Assert.AreEqual(expected, source.LeftBefore(search, comparison),
                            "Truncating did not return the correct result.");
        }

        [Test]
        public void SplitIntoWords_WithStringContainingSpaces_SplitsIntoWords()
        {
            //arrange, act
            IEnumerable<string> words = "this is a test".SplitIntoWords().ToList();

            //assert
            Assert.AreEqual(4, words.Count());
            Assert.AreEqual("this", words.First());
            Assert.AreEqual("is", words.ElementAt(1));
            Assert.AreEqual("a", words.ElementAt(2));
            Assert.AreEqual("test", words.ElementAt(3));
        }

        [Test]
        public void SplitIntoWords_WithStringContainingTabsAndDoubleSpaces_SplitsIntoWords()
        {
            //arrange, act
            IEnumerable<string> words = "  this \t is\ta  test  \t".SplitIntoWords().ToList();

            //assert
            Assert.AreEqual(4, words.Count());
            Assert.AreEqual("this", words.First());
            Assert.AreEqual("is", words.ElementAt(1));
            Assert.AreEqual("a", words.ElementAt(2));
            Assert.AreEqual("test", words.ElementAt(3));
        }


        /*
               "string\r\n".chop   #=> "string"
               "string\n\r".chop   #=> "string\n"
               "string\n".chop     #=> "string"
               "string".chop       #=> "strin"
               "x".chop.chop       #=> ""
             */

        [Test]
        public void Chop_WithStringEndingWithWindowsNewLine_ReturnsStringWithoutNewline()
        {
            Assert.AreEqual("string", "string\r\n".Chop());
        }

        [Test]
        public void Chop_WithStringEndingWithSlashR_OnlyChopsSlashR()
        {
            Assert.AreEqual("string\n", "string\n\r".Chop());
        }

        [Test]
        public void Chop_WithStringEndingWithNewline_ChopsNewline()
        {
            Assert.AreEqual("string", "string\n".Chop());
        }

        [Test]
        public void Chop_WithStringEndingWithLetter_ReturnsStringWithoutLastLetter()
        {
            Assert.AreEqual("strin", "string".Chop());
        }

        [Test]
        public void Chop_WithOneLetter_ReturnsEmptyString()
        {
            Assert.AreEqual(string.Empty, "x".Chop());
        }

        /*
         "hello".chomp            #=> "hello"
         "hello\n".chomp          #=> "hello"
         "hello\r\n".chomp        #=> "hello"
         "hello\n\r".chomp        #=> "hello\n"
         "hello\r".chomp          #=> "hello"
         "hello \n there".chomp   #=> "hello \n there"
         "hello".chomp("llo")     #=> "he"
         */

        [Test]
        public void Chomp_WithStringNotEndingWithDefaultSeparator_ReturnsString()
        {
            Assert.AreEqual("hello", "hello".Chomp());
        }

        [Test]
        public void Chomp_WithStringEndingWithNewline_ChopsNewline()
        {
            Assert.AreEqual("hello", "hello\n".Chop());
        }

        [Test]
        public void Chomp_WithStringEndingWithWindowsNewLine_ReturnsStringWithoutNewline()
        {
            Assert.AreEqual("hello", "hello\r\n".Chomp());
        }

        [Test]
        public void Chomp_WithStringEndingWithSlashNSlashR_OnlyChopsSlashR()
        {
            Assert.AreEqual("hello\n", "hello\n\r".Chop());
        }

        [Test]
        public void Chomp_WithStringEndingWithSlashR_OnlyChopsSlashR()
        {
            Assert.AreEqual("hello", "hello\r".Chop());
        }

        [Test]
        public void Chomp_WithSeparator_ChopsSeparator()
        {
            Assert.AreEqual("he", "hello".Chomp("llo", StringComparison.Ordinal));
        }

        [Test]
        public void Chomp_WithSeparatorButStringNotEndingWithSeparator_LeavesStringAlone()
        {
            Assert.AreEqual("hello world", "hello world".Chomp("llo", StringComparison.Ordinal));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.