Files
Umbraco-CMS/src/Umbraco.Tests/StringExtensionsTests.cs
T
Shannon Deminick f7f83bc057 Changed IPublishedContentStore and IPublishedMediaStore back to internal as we don't need to expose these yet. Instead have added
an UmbracoHelper property to the PluginController class which will expose any methods needed by devs to retreive media or content.
Have updated UmbracoHelper with loads of new helpful methods and moved the Wrap methods to HtmlHelper extensions because this is purely to
do with rendering html (have written unit tests for it too).
Updated some 'library' methods to proxy calls to UmbracoHelper so we only have to maintain one set of code.
Added a convenience property to UmbracoContext to return the current NiceUrlProvider so you don't have to go through the RoutingContext to get it.
2012-09-27 08:30:35 +07:00

136 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Security;
using System.Text;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests
{
[TestFixture]
public class StringExtensionsTests
{
[TestCase("Hello this is my string", " string", "Hello this is my")]
[TestCase("Hello this is my string strung", " string", "Hello this is my string strung")]
[TestCase("Hello this is my string string", " string", "Hello this is my")]
[TestCase("Hello this is my string string", "g", "Hello this is my string strin")]
[TestCase("Hello this is my string string", "ello this is my string string", "H")]
[TestCase("Hello this is my string string", "Hello this is my string string", "")]
public void TrimEnd(string input, string forTrimming, string shouldBe)
{
var trimmed = input.TrimEnd(forTrimming);
Assert.AreEqual(shouldBe, trimmed);
}
[TestCase("Hello this is my string", "hello", " this is my string")]
[TestCase("Hello this is my string", "Hello this", " is my string")]
[TestCase("Hello this is my string", "Hello this is my ", "string")]
[TestCase("Hello this is my string", "Hello this is my string", "")]
public void TrimStart(string input, string forTrimming, string shouldBe)
{
var trimmed = input.TrimStart(forTrimming);
Assert.AreEqual(shouldBe, trimmed);
}
[TestCase]
public void StringExtensions_To_Url_Alias()
{
var replacements = new Dictionary<string, string>
{
{" ", "-"},
{"\"", ""},
{"&quot;", ""},
{"@", ""},
{"%", ""},
{".", ""},
{";", ""},
{"/", ""},
{":", ""},
{"#", ""},
{"+", ""},
{"*", ""},
{"&amp;", ""},
{"?", ""}
};
var name1 = "Home Page";
var name2 = "Shannon's Home Page!";
var name3 = "#Someones's Twitter $h1z%n";
var name4 = "Räksmörgås";
var name5 = "'em guys-over there, are#goin' a \"little\"bit crazy eh!! :)";
var name6 = "汉#字*/漢?字";
var url1 = name1.ToUrlAlias(replacements, true, true, false);
var url2 = name2.ToUrlAlias(replacements, true, true, false);
var url3 = name3.ToUrlAlias(replacements, true, true, false);
var url4 = name4.ToUrlAlias(replacements, true, true, false);
var url5 = name5.ToUrlAlias(replacements, true, true, false);
var url6 = name6.ToUrlAlias(replacements, true, true, false);
var url7 = name6.ToUrlAlias(replacements, true, false, false);
var url8 = name6.ToUrlAlias(replacements, true, false, true);
Assert.AreEqual("home-page", url1);
Assert.AreEqual("shannons-home-page", url2);
Assert.AreEqual("someoness-twitter-h1zn", url3);
Assert.AreEqual("rksmrgs", url4);
Assert.AreEqual("em-guys-over-there-aregoin-a-littlebit-crazy-eh", url5);
Assert.AreEqual("", url6);
Assert.AreEqual("汉字漢字", url7);
Assert.AreEqual("%e6%b1%89%e5%ad%97%e6%bc%a2%e5%ad%97", url8);
}
[TestCase]
public void StringExtensions_To_Camel_Case()
{
//Arrange
var name1 = "Tab 1";
var name2 = "Home - Page";
var name3 = "Shannon's document type";
//Act
var camelCase1 = name1.ConvertCase(StringAliasCaseType.CamelCase);
var camelCase2 = name2.ConvertCase(StringAliasCaseType.CamelCase);
var camelCase3 = name3.ConvertCase(StringAliasCaseType.CamelCase);
//Assert
Assert.AreEqual("tab1", camelCase1);
Assert.AreEqual("homePage", camelCase2);
Assert.AreEqual("shannon'sDocumentType", camelCase3);
}
[TestCase]
public void StringExtensions_To_Entity_Alias()
{
//Arrange
var name1 = "Tab 1";
var name2 = "Home - Page";
var name3 = "Shannon's Document Type";
var name4 = "!BADDLY nam-ed Document Type";
var name5 = "i %Want!thisTo end up In Proper@case";
//Act
var alias1 = name1.ToUmbracoAlias();
var alias2 = name2.ToUmbracoAlias();
var alias3 = name3.ToUmbracoAlias();
var alias4 = name4.ToUmbracoAlias();
var alias5 = name5.ToUmbracoAlias(StringAliasCaseType.PascalCase);
//Assert
Assert.AreEqual("tab1", alias1);
Assert.AreEqual("homePage", alias2);
Assert.AreEqual("shannonsDocumentType", alias3);
Assert.AreEqual("baddlyNamEdDocumentType", alias4);
Assert.AreEqual("IWantThisToEndUpInProperCase", alias5);
}
}
}