mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding a new string extension method, ReplaceNewLinesWith(this string text, string replacement), and add more unit test coverage for Orchard.Utility.Extensions.StringExtensions
--HG-- branch : dev
This commit is contained in:
@@ -5,6 +5,58 @@ using Orchard.Utility.Extensions;
|
|||||||
namespace Orchard.Tests.Utility.Extensions {
|
namespace Orchard.Tests.Utility.Extensions {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class StringExtensionsTests {
|
public class StringExtensionsTests {
|
||||||
|
[Test]
|
||||||
|
public void CamelFriendly_CamelCasedStringMadeFriendly() {
|
||||||
|
const string aCamel = "aCamel";
|
||||||
|
Assert.That(aCamel.CamelFriendly(), Is.StringMatching("a Camel"));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void CamelFriendly_PascalCasedStringMadeFriendly() {
|
||||||
|
const string aCamel = "ACamel";
|
||||||
|
Assert.That(aCamel.CamelFriendly(), Is.StringMatching("A Camel"));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void CamelFriendly_LowerCasedStringMadeFriendly() {
|
||||||
|
const string aCamel = "acamel";
|
||||||
|
Assert.That(aCamel.CamelFriendly(), Is.StringMatching("acamel"));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void CamelFriendly_EmptyStringReturnsEmptyString() {
|
||||||
|
const string aCamel = "";
|
||||||
|
Assert.That(aCamel.CamelFriendly(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void CamelFriendly_NullValueReturnsEmptyString() {
|
||||||
|
const string aCamel = null;
|
||||||
|
Assert.That(aCamel.CamelFriendly(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Ellipsize_LongStringTruncatedToNearestWord() {
|
||||||
|
const string toEllipsize = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed purus quis purus orci aliquam.";
|
||||||
|
Assert.That(toEllipsize.Ellipsize(45), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur …"));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Ellipsize_ShortStringReturnedAsSame() {
|
||||||
|
const string toEllipsize = "Lorem ipsum";
|
||||||
|
Assert.That(toEllipsize.Ellipsize(45), Is.StringMatching("Lorem ipsum"));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Ellipsize_EmptyStringReturnsEmptyString() {
|
||||||
|
const string toEllipsize = "";
|
||||||
|
Assert.That(toEllipsize.Ellipsize(45), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Ellipsize_NullValueReturnsEmptyString() {
|
||||||
|
const string toEllipsize = null;
|
||||||
|
Assert.That(toEllipsize.Ellipsize(45), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void Ellipsize_CustomEllipsisStringIsUsed() {
|
||||||
|
const string toEllipsize = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed purus quis purus orci aliquam.";
|
||||||
|
Assert.That(toEllipsize.Ellipsize(45, "........"), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur........"));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void HtmlClassify_ValidReallySimpleClassNameReturnsSame() {
|
public void HtmlClassify_ValidReallySimpleClassNameReturnsSame() {
|
||||||
const string toClassify = "someclass";
|
const string toClassify = "someclass";
|
||||||
@@ -40,6 +92,17 @@ namespace Orchard.Tests.Utility.Extensions {
|
|||||||
const string toClassify = "PascalCased";
|
const string toClassify = "PascalCased";
|
||||||
Assert.That(toClassify.HtmlClassify(), Is.StringMatching("pascal-cased"));
|
Assert.That(toClassify.HtmlClassify(), Is.StringMatching("pascal-cased"));
|
||||||
}
|
}
|
||||||
|
[Test]
|
||||||
|
public void HtmlClassify_EmptyStringReturnsEmptyString() {
|
||||||
|
const string toClassify = "";
|
||||||
|
Assert.That(toClassify.HtmlClassify(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void HtmlClassify_NullValueReturnsEmptyString() {
|
||||||
|
const string toClassify = null;
|
||||||
|
Assert.That(toClassify.HtmlClassify(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void OrDefault_ReturnsDefaultForNull() {
|
public void OrDefault_ReturnsDefaultForNull() {
|
||||||
const string s = null;
|
const string s = null;
|
||||||
@@ -47,15 +110,61 @@ namespace Orchard.Tests.Utility.Extensions {
|
|||||||
Assert.That(s.OrDefault(def).Text, Is.SameAs("test"));
|
Assert.That(s.OrDefault(def).Text, Is.SameAs("test"));
|
||||||
}
|
}
|
||||||
[Test]
|
[Test]
|
||||||
public void OrDefault_ReturnsDefault() {
|
public void OrDefault_ReturnsDefaultIfEmpty() {
|
||||||
var def = new LocalizedString("test");
|
var def = new LocalizedString("test");
|
||||||
Assert.That("".OrDefault(def).Text, Is.SameAs("test"));
|
Assert.That("".OrDefault(def).Text, Is.SameAs("test"));
|
||||||
}
|
}
|
||||||
|
[Test]
|
||||||
|
public void OrDefault_ReturnsDefaultIfNull() {
|
||||||
|
var def = new LocalizedString("test");
|
||||||
|
Assert.That(((string)null).OrDefault(def).Text, Is.SameAs("test"));
|
||||||
|
}
|
||||||
[Test]
|
[Test]
|
||||||
public void OrDefault_ReturnsString() {
|
public void OrDefault_ReturnsString() {
|
||||||
var def = new LocalizedString("test");
|
var def = new LocalizedString("test");
|
||||||
Assert.That("bar".OrDefault(def).Text, Is.SameAs("bar"));
|
Assert.That("bar".OrDefault(def).Text, Is.SameAs("bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RemoveTags_StringWithNoTagsReturnsSame() {
|
||||||
|
const string fullOfTags = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed purus quis purus orci aliquam.";
|
||||||
|
Assert.That(fullOfTags.RemoveTags(), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed purus quis purus orci aliquam."));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void RemoveTags_SimpleWellFormedTagsAreRemoved() {
|
||||||
|
const string fullOfTags = @"<p><em>Lorem ipsum</em> dolor sit amet, consectetur <a href=""#"">adipiscing</a> elit. Maecenas sed purus quis purus orci aliquam.</p>";
|
||||||
|
Assert.That(fullOfTags.RemoveTags(), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sed purus quis purus orci aliquam."));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void RemoveTags_EmptyStringReturnsEmptyString() {
|
||||||
|
const string fullOfTags = "";
|
||||||
|
Assert.That(fullOfTags.RemoveTags(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void RemoveTags_NullValueReturnsEmptyString() {
|
||||||
|
const string fullOfTags = null;
|
||||||
|
Assert.That(fullOfTags.RemoveTags(), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReplaceNewLinesWith_ReplaceCRLFWithHtmlBR() {
|
||||||
|
const string lotsOfLineFeeds = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\nMaecenas sed purus quis purus orci aliquam.";
|
||||||
|
Assert.That(lotsOfLineFeeds.ReplaceNewLinesWith("<br />"), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br />Maecenas sed purus quis purus orci aliquam."));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void ReplaceNewLinesWith_ReplaceCRLFWithHtmlPsAndCRLF() {
|
||||||
|
const string lotsOfLineFeeds = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\nMaecenas sed purus quis purus orci aliquam.";
|
||||||
|
Assert.That(lotsOfLineFeeds.ReplaceNewLinesWith(@"</p>$1<p>"), Is.StringMatching("Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>\r\n<p>Maecenas sed purus quis purus orci aliquam."));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void ReplaceNewLinesWith_EmptyStringReturnsEmptyString() {
|
||||||
|
const string lotsOfLineFeeds = "";
|
||||||
|
Assert.That(lotsOfLineFeeds.ReplaceNewLinesWith("<br />"), Is.StringMatching(""));
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void ReplaceNewLinesWith_NullValueReturnsEmptyString() {
|
||||||
|
const string lotsOfLineFeeds = null;
|
||||||
|
Assert.That(lotsOfLineFeeds.ReplaceNewLinesWith("<br />"), Is.StringMatching(""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,8 +20,11 @@ namespace Orchard.Utility.Extensions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static string Ellipsize(this string text, int characterCount, string ellipsis) {
|
public static string Ellipsize(this string text, int characterCount, string ellipsis) {
|
||||||
if (string.IsNullOrWhiteSpace(text) || characterCount < 0 || text.Length <= characterCount)
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
|
if (characterCount < 0 || text.Length <= characterCount)
|
||||||
|
return text;
|
||||||
|
|
||||||
return Regex.Replace(text.Substring(0, characterCount + 1), @"\s+\S*$", "") + ellipsis;
|
return Regex.Replace(text.Substring(0, characterCount + 1), @"\s+\S*$", "") + ellipsis;
|
||||||
}
|
}
|
||||||
@@ -41,7 +44,16 @@ namespace Orchard.Utility.Extensions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static string RemoveTags(this string html) {
|
public static string RemoveTags(this string html) {
|
||||||
return Regex.Replace(html, "<[^<>]*>", "", RegexOptions.Singleline);
|
return string.IsNullOrEmpty(html)
|
||||||
|
? ""
|
||||||
|
: Regex.Replace(html, "<[^<>]*>", "", RegexOptions.Singleline);
|
||||||
|
}
|
||||||
|
|
||||||
|
// not accounting for only \r (e.g. Apple OS 9 carriage return only new lines)
|
||||||
|
public static string ReplaceNewLinesWith(this string text, string replacement) {
|
||||||
|
return string.IsNullOrWhiteSpace(text)
|
||||||
|
? ""
|
||||||
|
: Regex.Replace(text, @"(\r?\n)", replacement, RegexOptions.Singleline);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user