KeywordExpanderTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Framework » Services » 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 » Services » KeywordExpanderTests.cs
using System.Collections.Generic;
using MbUnit.Framework;
using Moq;
using Subtext.Framework.Components;
using Subtext.Framework.Providers;
using Subtext.Framework.Services;

namespace UnitTests.Subtext.Framework.Services{
    [TestFixture]
    public class KeywordExpanderTests
    {
        [Test]
        public void Replace_WithStringContainingKeyword_ExpandsKeyword()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "sucky example",
                    Text = "cool example",
                    Url = "http://example.com/",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("This is a sucky example");

            //assert
            Assert.AreEqual("This is a <a href=\"http://example.com/\">cool example</a>", result);
        }

        [Test]
        public void Replace_WithStringContainingKeyword_ExpandsKeywordWithFirstMatchOnly()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "sucky example",
                    Text = "cool example",
                    Url = "http://example.com/",
                    ReplaceFirstTimeOnly = true,
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("This is a sucky example");

            //assert
            Assert.AreEqual("This is a <a href=\"http://example.com/\">cool example</a>", result);
        }

        [Test]
        public void Replace_WithStringContainingKeyword_ExpandsKeywordWithTitle()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "sucky example",
                    Text = "cool example",
                    Url = "http://example.com/",
                    Title = "the title"
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("This is a sucky example");

            //assert
            Assert.AreEqual("This is a <a href=\"http://example.com/\" title=\"the title\">cool example</a>", result);
        }

        [Test]
        public void Replace_WithKeywordSurroundedByUnderscores_IsNotExpanded()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "is",
                    Text = "is",
                    Url = "http://example.com/{0}",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform(" _is_ ");

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

        [Test]
        public void Replace_WithStringContainingKeyword_IsNotCaseSensitive()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "is",
                    Text = "is",
                    Url = "http://example.com/",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform(" it IS true ");

            //assert
            Assert.AreEqual(" it <a href=\"http://example.com/\">is</a> true ", result);
        }

        [Test]
        public void Replace_WithStringContainingKeywordSpecifiedAsCaseSensitive_IsCaseSensitive()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "is",
                    Text = "is",
                    Url = "http://example.com/",
                    CaseSensitive = true
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform(" it IS true ");

            //assert
            Assert.AreEqual(" it IS true ", result);
        }

        [Test]
        public void Replace_WithStringContainingKeywordInsideAnchorTagAttribute_DoesNotExpandKeyword()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "keyword",
                    Text = "keyword",
                    Url = "http://example.com/",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("<a title=\"keyword\" href=\"http://x\">test</a>");

            //assert
            Assert.AreEqual("<a title=\"keyword\" href=\"http://x\">test</a>", result);
        }

        [Test]
        public void Replace_WithStringContainingKeywordInsideAnchorTagInnerText_DoesNotExpandKeyword()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "keyword",
                    Text = "keyword",
                    Url = "http://example.com/",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("<a href=\"http://x\">a keyword test</a>");

            //assert
            Assert.AreEqual("<a href=\"http://x\">a keyword test</a>", result);
        }

        [Test]
        public void Replace_WithStringContainingKeywordInAnotherWord_DoesNotExpandKeyword()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "is",
                    Text = "is",
                    Url = "http://example.com/",
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("This should not expand");

            //assert
            Assert.AreEqual("This should not expand", result);
        }

        // Issue #132: http://code.google.com/p/subtext/issues/detail?id=132
        [Test]
        public void Transform_WithStringContainingBracketsAndReplacingFirstOccurrenceOnly_ReturnsConvertedKeywordAndBrackets()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "OLD",
                    Text = "NEW",
                    Url = "http://example.com/",
                    Title = "NEW",
                    ReplaceFirstTimeOnly = true
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("OLD {} OLD");

            //assert
            Assert.AreEqual(@"<a href=""http://example.com/"" title=""NEW"">NEW</a> {} OLD", result);
        }

        [Test]
        public void Transform_WithStringContainingBrackets_ReturnsConvertedKeywordAndBrackets()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "OLD",
                    Text = "NEW",
                    Url = "http://example.com/",
                    Title = "NEW",
                    ReplaceFirstTimeOnly = false
                }
            };
            var keywordExpander = new KeywordExpander(keywords);

            //act
            string result = keywordExpander.Transform("OLD {} OLD {}");

            //assert
            Assert.AreEqual(@"<a href=""http://example.com/"" title=""NEW"">NEW</a> {} <a href=""http://example.com/"" title=""NEW"">NEW</a> {}", result);
        }

        [Test]
        public void Ctor_WithRepository_GetsKeywordsFromRepository()
        {
            //arrange
            var keywords = new List<KeyWord>
            {
                new KeyWord
                {
                    Word = "is",
                    Text = "is",
                    Url = "http://example.com/",
                }
            };
            var repository = new Mock<ObjectProvider>();
            repository.Setup(r => r.GetKeyWords()).Returns(keywords);

            //act
            var keywordExpander = new KeywordExpander(keywords);

            //assert
            Assert.AreEqual(keywords, keywordExpander.Keywords);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.