merge v10 to v11
This commit is contained in:
@@ -7,36 +7,36 @@ using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class CmsHelperCasingTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CmsHelperCasingTests
|
||||
private IShortStringHelper ShortStringHelper =>
|
||||
new DefaultShortStringHelper(Options.Create(new RequestHandlerSettings()));
|
||||
|
||||
[TestCase("thisIsTheEnd", "This Is The End")]
|
||||
[TestCase("th", "Th")]
|
||||
[TestCase("t", "t")]
|
||||
[TestCase("thisis", "Thisis")]
|
||||
[TestCase("ThisIsTheEnd", "This Is The End")]
|
||||
//// [TestCase("WhoIsNumber6InTheVillage", "Who Is Number6In The Village")] // note the issue with Number6In
|
||||
[TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // now fixed since DefaultShortStringHelper is the default
|
||||
public void SpaceCamelCasing(string input, string expected)
|
||||
{
|
||||
private IShortStringHelper ShortStringHelper => new DefaultShortStringHelper(Options.Create(new RequestHandlerSettings()));
|
||||
var output = input.SpaceCamelCasing(ShortStringHelper);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("thisIsTheEnd", "This Is The End")]
|
||||
[TestCase("th", "Th")]
|
||||
[TestCase("t", "t")]
|
||||
[TestCase("thisis", "Thisis")]
|
||||
[TestCase("ThisIsTheEnd", "This Is The End")]
|
||||
//// [TestCase("WhoIsNumber6InTheVillage", "Who Is Number6In The Village")] // note the issue with Number6In
|
||||
[TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // now fixed since DefaultShortStringHelper is the default
|
||||
public void SpaceCamelCasing(string input, string expected)
|
||||
{
|
||||
var output = input.SpaceCamelCasing(ShortStringHelper);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("thisIsTheEnd", "This Is The End")]
|
||||
[TestCase("th", "Th")]
|
||||
[TestCase("t", "t")]
|
||||
[TestCase("thisis", "Thisis")]
|
||||
[TestCase("ThisIsTheEnd", "This Is The End")]
|
||||
[TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // issue is fixed
|
||||
public void CompatibleDefaultReplacement(string input, string expected)
|
||||
{
|
||||
var output = input.Length < 2 ? input : ShortStringHelper.SplitPascalCasing(input, ' ').ToFirstUpperInvariant();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase("thisIsTheEnd", "This Is The End")]
|
||||
[TestCase("th", "Th")]
|
||||
[TestCase("t", "t")]
|
||||
[TestCase("thisis", "Thisis")]
|
||||
[TestCase("ThisIsTheEnd", "This Is The End")]
|
||||
[TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // issue is fixed
|
||||
public void CompatibleDefaultReplacement(string input, string expected)
|
||||
{
|
||||
var output = input.Length < 2 ? input : ShortStringHelper.SplitPascalCasing(input, ' ').ToFirstUpperInvariant();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,178 +8,194 @@ using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class DefaultShortStringHelperTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DefaultShortStringHelperTests
|
||||
{
|
||||
private IShortStringHelper ShortStringHelper { get; set; }
|
||||
[SetUp]
|
||||
public void SetUp() =>
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() =>
|
||||
|
||||
// NOTE pre-filters runs _before_ Recode takes place
|
||||
// so there still may be utf8 chars even though you want ascii
|
||||
ShortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.FileName, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// PreFilter = ClearFileChars, // done in IsTerm
|
||||
IsTerm = (c, leading) => (char.IsLetterOrDigit(c) || c == '_') && DefaultShortStringHelper.IsValidFileNameChar(c),
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
|
||||
Separator = '-'
|
||||
})
|
||||
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
// NOTE pre-filters runs _before_ Recode takes place
|
||||
// so there still may be utf8 chars even though you want ascii
|
||||
ShortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.FileName, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// PreFilter = ClearFileChars, // done in IsTerm
|
||||
IsTerm = (c, leading) =>
|
||||
(char.IsLetterOrDigit(c) || c == '_') && DefaultShortStringHelper.IsValidFileNameChar(c),
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
|
||||
Separator = '-',
|
||||
})
|
||||
.WithConfig(
|
||||
CleanStringType.UrlSegment,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = StripQuotes,
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
|
||||
Separator = '-'
|
||||
Separator = '-',
|
||||
})
|
||||
.WithConfig("fr-FR", CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
.WithConfig(
|
||||
"fr-FR",
|
||||
CleanStringType.UrlSegment,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = FilterFrenchElisions,
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : (char.IsLetterOrDigit(c) || c == '_'),
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
|
||||
Separator = '-'
|
||||
Separator = '-',
|
||||
})
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = StripQuotes,
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
|
||||
StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii,
|
||||
})
|
||||
.WithConfig("fr-FR", CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
.WithConfig(
|
||||
"fr-FR",
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = WhiteQuotes,
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
|
||||
StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii,
|
||||
})
|
||||
.WithConfig(CleanStringType.ConvertCase, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = null,
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
|
||||
StringType = CleanStringType.Ascii,
|
||||
BreakTermsOnUpper = true
|
||||
}));
|
||||
.WithConfig(CleanStringType.ConvertCase, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
PreFilter = null,
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
|
||||
StringType = CleanStringType.Ascii,
|
||||
BreakTermsOnUpper = true,
|
||||
}));
|
||||
|
||||
private static readonly Regex s_frenchElisionsRegex = new Regex("\\b(c|d|j|l|m|n|qu|s|t)('|\u8217)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
private IShortStringHelper ShortStringHelper { get; set; }
|
||||
|
||||
private static string FilterFrenchElisions(string s) => s_frenchElisionsRegex.Replace(s, string.Empty);
|
||||
private static readonly Regex s_frenchElisionsRegex =
|
||||
new("\\b(c|d|j|l|m|n|qu|s|t)('|\u8217)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static string StripQuotes(string s)
|
||||
{
|
||||
s = s.ReplaceMany(new Dictionary<string, string> { { "'", string.Empty }, { "\u8217", string.Empty } });
|
||||
return s;
|
||||
}
|
||||
private static string FilterFrenchElisions(string s) => s_frenchElisionsRegex.Replace(s, string.Empty);
|
||||
|
||||
private static string WhiteQuotes(string s)
|
||||
{
|
||||
s = s.ReplaceMany(new Dictionary<string, string> { { "'", " " }, { "\u8217", " " } });
|
||||
return s;
|
||||
}
|
||||
private static string StripQuotes(string s)
|
||||
{
|
||||
s = s.ReplaceMany(new Dictionary<string, string> { { "'", string.Empty }, { "\u8217", string.Empty } });
|
||||
return s;
|
||||
}
|
||||
|
||||
[TestCase("foo", "foo")]
|
||||
[TestCase(" foo ", "foo")]
|
||||
[TestCase("Foo", "Foo")]
|
||||
[TestCase("FoO", "FoO")]
|
||||
[TestCase("FoO bar", "FoOBar")]
|
||||
[TestCase("FoO bar NIL", "FoOBarNIL")]
|
||||
[TestCase("FoO 33bar 22NIL", "FoO33bar22NIL")]
|
||||
[TestCase("FoO 33bar 22NI", "FoO33bar22NI")]
|
||||
[TestCase("0foo", "foo")]
|
||||
[TestCase("2foo bar", "fooBar")]
|
||||
[TestCase("9FOO", "FOO")]
|
||||
[TestCase("foo-BAR", "fooBAR")]
|
||||
[TestCase("foo-BA-dang", "fooBADang")]
|
||||
[TestCase("foo_BAR", "fooBAR")]
|
||||
[TestCase("foo'BAR", "fooBAR")]
|
||||
[TestCase("sauté dans l'espace", "sauteDansLespace")]
|
||||
[TestCase("foo\"\"bar", "fooBar")]
|
||||
[TestCase("-foo-", "foo")]
|
||||
[TestCase("_foo_", "foo")]
|
||||
[TestCase("spécial", "special")]
|
||||
[TestCase("brô dëk ", "broDek")]
|
||||
[TestCase("1235brô dëk ", "broDek")]
|
||||
[TestCase("汉#字*/漢?字", "")]
|
||||
[TestCase("aa DB cd EFG X KLMN OP qrst", "aaDBCdEFGXKLMNOPQrst")]
|
||||
[TestCase("AA db cd EFG X KLMN OP qrst", "AADbCdEFGXKLMNOPQrst")]
|
||||
[TestCase("AAA db cd EFG X KLMN OP qrst", "AAADbCdEFGXKLMNOPQrst")]
|
||||
[TestCase("4 ways selector", "waysSelector")]
|
||||
[TestCase("WhatIfWeDoItAgain", "WhatIfWeDoItAgain")]
|
||||
[TestCase("whatIfWeDoItAgain", "whatIfWeDoItAgain")]
|
||||
[TestCase("WhatIfWEDOITAgain", "WhatIfWEDOITAgain")]
|
||||
[TestCase("WhatIfWe doItAgain", "WhatIfWeDoItAgain")]
|
||||
[TestCase("What if I have emojis 🎈", "WhatIfIHaveEmojis")]
|
||||
public void CleanStringForSafeAlias(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForSafeAlias(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
private static string WhiteQuotes(string s)
|
||||
{
|
||||
s = s.ReplaceMany(new Dictionary<string, string> { { "'", " " }, { "\u8217", " " } });
|
||||
return s;
|
||||
}
|
||||
|
||||
[TestCase("Home Page", "home-page")]
|
||||
[TestCase("Shannon's Home Page!", "shannons-home-page")]
|
||||
[TestCase("#Someones's Twitter $h1z%n", "someoness-twitter-h1z-n")]
|
||||
[TestCase("Räksmörgås", "raksmorgas")]
|
||||
[TestCase("'em guys-over there, are#goin' a \"little\"bit crazy eh!! :)", "em-guys-over-there-are-goin-a-little-bit-crazy-eh")]
|
||||
[TestCase("汉#字*/漢?字", "")]
|
||||
[TestCase("Réalösk fix bran#lo'sk", "realosk-fix-bran-losk")]
|
||||
[TestCase("200 ways to be happy", "200-ways-to-be-happy")]
|
||||
[TestCase("What if I have emojis 🎈", "what-if-i-have-emojis")]
|
||||
public void CleanStringForUrlSegment(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase("foo", "foo")]
|
||||
[TestCase(" foo ", "foo")]
|
||||
[TestCase("Foo", "Foo")]
|
||||
[TestCase("FoO", "FoO")]
|
||||
[TestCase("FoO bar", "FoOBar")]
|
||||
[TestCase("FoO bar NIL", "FoOBarNIL")]
|
||||
[TestCase("FoO 33bar 22NIL", "FoO33bar22NIL")]
|
||||
[TestCase("FoO 33bar 22NI", "FoO33bar22NI")]
|
||||
[TestCase("0foo", "foo")]
|
||||
[TestCase("2foo bar", "fooBar")]
|
||||
[TestCase("9FOO", "FOO")]
|
||||
[TestCase("foo-BAR", "fooBAR")]
|
||||
[TestCase("foo-BA-dang", "fooBADang")]
|
||||
[TestCase("foo_BAR", "fooBAR")]
|
||||
[TestCase("foo'BAR", "fooBAR")]
|
||||
[TestCase("sauté dans l'espace", "sauteDansLespace")]
|
||||
[TestCase("foo\"\"bar", "fooBar")]
|
||||
[TestCase("-foo-", "foo")]
|
||||
[TestCase("_foo_", "foo")]
|
||||
[TestCase("spécial", "special")]
|
||||
[TestCase("brô dëk ", "broDek")]
|
||||
[TestCase("1235brô dëk ", "broDek")]
|
||||
[TestCase("汉#字*/漢?字", "")]
|
||||
[TestCase("aa DB cd EFG X KLMN OP qrst", "aaDBCdEFGXKLMNOPQrst")]
|
||||
[TestCase("AA db cd EFG X KLMN OP qrst", "AADbCdEFGXKLMNOPQrst")]
|
||||
[TestCase("AAA db cd EFG X KLMN OP qrst", "AAADbCdEFGXKLMNOPQrst")]
|
||||
[TestCase("4 ways selector", "waysSelector")]
|
||||
[TestCase("WhatIfWeDoItAgain", "WhatIfWeDoItAgain")]
|
||||
[TestCase("whatIfWeDoItAgain", "whatIfWeDoItAgain")]
|
||||
[TestCase("WhatIfWEDOITAgain", "WhatIfWEDOITAgain")]
|
||||
[TestCase("WhatIfWe doItAgain", "WhatIfWeDoItAgain")]
|
||||
[TestCase("What if I have emojis 🎈", "WhatIfIHaveEmojis")]
|
||||
public void CleanStringForSafeAlias(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForSafeAlias(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("ThisIsTheEndMyFriend", "This Is The End My Friend")]
|
||||
[TestCase("ThisIsTHEEndMyFriend", "This Is THE End My Friend")]
|
||||
[TestCase("THISIsTHEEndMyFriend", "THIS Is THE End My Friend")]
|
||||
[TestCase("This33I33sThe33EndMyFriend", "This33 I33s The33 End My Friend")] // works!
|
||||
[TestCase("ThisIsTHEEndMyFriendX", "This Is THE End My Friend X")]
|
||||
[TestCase("ThisIsTHEEndMyFriendXYZ", "This Is THE End My Friend XYZ")]
|
||||
[TestCase("ThisIsTHEEndMyFriendXYZt", "This Is THE End My Friend XY Zt")]
|
||||
[TestCase("UneÉlévationÀPartir", "Une Élévation À Partir")]
|
||||
[TestCase("WhatIfIHaveEmojis🎈", "What If I Have Emojis🎈")]
|
||||
public void SplitPascalCasing(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.SplitPascalCasing(input, ' ');
|
||||
Assert.AreEqual(expected, output);
|
||||
[TestCase("Home Page", "home-page")]
|
||||
[TestCase("Shannon's Home Page!", "shannons-home-page")]
|
||||
[TestCase("#Someones's Twitter $h1z%n", "someoness-twitter-h1z-n")]
|
||||
[TestCase("Räksmörgås", "raksmorgas")]
|
||||
[TestCase("'em guys-over there, are#goin' a \"little\"bit crazy eh!! :)", "em-guys-over-there-are-goin-a-little-bit-crazy-eh")]
|
||||
[TestCase("汉#字*/漢?字", "")]
|
||||
[TestCase("Réalösk fix bran#lo'sk", "realosk-fix-bran-losk")]
|
||||
[TestCase("200 ways to be happy", "200-ways-to-be-happy")]
|
||||
[TestCase("What if I have emojis 🎈", "what-if-i-have-emojis")]
|
||||
public void CleanStringForUrlSegment(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
output = ShortStringHelper.SplitPascalCasing(input, '*');
|
||||
expected = expected.Replace(' ', '*');
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase("ThisIsTheEndMyFriend", "This Is The End My Friend")]
|
||||
[TestCase("ThisIsTHEEndMyFriend", "This Is THE End My Friend")]
|
||||
[TestCase("THISIsTHEEndMyFriend", "THIS Is THE End My Friend")]
|
||||
[TestCase("This33I33sThe33EndMyFriend", "This33 I33s The33 End My Friend")] // works!
|
||||
[TestCase("ThisIsTHEEndMyFriendX", "This Is THE End My Friend X")]
|
||||
[TestCase("ThisIsTHEEndMyFriendXYZ", "This Is THE End My Friend XYZ")]
|
||||
[TestCase("ThisIsTHEEndMyFriendXYZt", "This Is THE End My Friend XY Zt")]
|
||||
[TestCase("UneÉlévationÀPartir", "Une Élévation À Partir")]
|
||||
[TestCase("WhatIfIHaveEmojis🎈", "What If I Have Emojis🎈")]
|
||||
public void SplitPascalCasing(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.SplitPascalCasing(input, ' ');
|
||||
Assert.AreEqual(expected, output);
|
||||
|
||||
[TestCase("sauté dans l'espace", "saute-dans-espace", "fr-FR", CleanStringType.UrlSegment | CleanStringType.Ascii | CleanStringType.LowerCase)]
|
||||
[TestCase("sauté dans l'espace", "sauté-dans-espace", "fr-FR", CleanStringType.UrlSegment | CleanStringType.Utf8 | CleanStringType.LowerCase)]
|
||||
[TestCase("sauté dans l'espace", "SauteDansLEspace", "fr-FR", CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.PascalCase)]
|
||||
[TestCase("he doesn't want", "he-doesnt-want", null, CleanStringType.UrlSegment | CleanStringType.Ascii | CleanStringType.LowerCase)]
|
||||
[TestCase("he doesn't want", "heDoesntWant", null, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase)]
|
||||
public void CleanStringWithTypeAndCulture(string input, string expected, string culture, CleanStringType stringType)
|
||||
{
|
||||
// picks the proper config per culture
|
||||
// and overrides some stringType params (ascii...)
|
||||
var output = ShortStringHelper.CleanString(input, stringType, culture);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
output = ShortStringHelper.SplitPascalCasing(input, '*');
|
||||
expected = expected.Replace(' ', '*');
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("foo.txt", "foo.txt")]
|
||||
[TestCase("foo", "foo")]
|
||||
[TestCase(".txt", ".txt")]
|
||||
[TestCase("nag*dog/poo:xit.txt", "nag-dog-poo-xit.txt")]
|
||||
[TestCase("the dog is in the house.txt", "the-dog-is-in-the-house.txt")]
|
||||
[TestCase("nil.nil.nil.txt", "nil-nil-nil.txt")]
|
||||
[TestCase("taradabum", "taradabum")]
|
||||
[TestCase("tara$$da:b/u<m", "tara-da-b-u-m")]
|
||||
[TestCase("Straße Zvöskî.yop", "strasse-zvoski.yop")]
|
||||
[TestCase("yop.Straße Zvöskî", "yop.strasse-zvoski")]
|
||||
[TestCase("yop.Straße Zvös--kî", "yop.strasse-zvos-ki")]
|
||||
[TestCase("ma--ma---ma.ma-----ma", "ma-ma-ma.ma-ma")]
|
||||
[TestCase("What if I have emojis 🎈", "what-if-i-have-emojis")]
|
||||
public void CleanStringForSafeFileName(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForSafeFileName(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase(
|
||||
"sauté dans l'espace",
|
||||
"saute-dans-espace",
|
||||
"fr-FR",
|
||||
CleanStringType.UrlSegment | CleanStringType.Ascii | CleanStringType.LowerCase)]
|
||||
[TestCase("sauté dans l'espace", "sauté-dans-espace", "fr-FR", CleanStringType.UrlSegment | CleanStringType.Utf8 | CleanStringType.LowerCase)]
|
||||
[TestCase("sauté dans l'espace", "SauteDansLEspace", "fr-FR", CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.PascalCase)]
|
||||
[TestCase("he doesn't want", "he-doesnt-want", null, CleanStringType.UrlSegment | CleanStringType.Ascii | CleanStringType.LowerCase)]
|
||||
[TestCase("he doesn't want", "heDoesntWant", null, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase)]
|
||||
public void CleanStringWithTypeAndCulture(string input, string expected, string culture, CleanStringType stringType)
|
||||
{
|
||||
// picks the proper config per culture
|
||||
// and overrides some stringType params (ascii...)
|
||||
var output = ShortStringHelper.CleanString(input, stringType, culture);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("foo.txt", "foo.txt")]
|
||||
[TestCase("foo", "foo")]
|
||||
[TestCase(".txt", ".txt")]
|
||||
[TestCase("nag*dog/poo:xit.txt", "nag-dog-poo-xit.txt")]
|
||||
[TestCase("the dog is in the house.txt", "the-dog-is-in-the-house.txt")]
|
||||
[TestCase("nil.nil.nil.txt", "nil-nil-nil.txt")]
|
||||
[TestCase("taradabum", "taradabum")]
|
||||
[TestCase("tara$$da:b/u<m", "tara-da-b-u-m")]
|
||||
[TestCase("Straße Zvöskî.yop", "strasse-zvoski.yop")]
|
||||
[TestCase("yop.Straße Zvöskî", "yop.strasse-zvoski")]
|
||||
[TestCase("yop.Straße Zvös--kî", "yop.strasse-zvos-ki")]
|
||||
[TestCase("ma--ma---ma.ma-----ma", "ma-ma-ma.ma-ma")]
|
||||
[TestCase("What if I have emojis 🎈", "what-if-i-have-emojis")]
|
||||
public void CleanStringForSafeFileName(string input, string expected)
|
||||
{
|
||||
var output = ShortStringHelper.CleanStringForSafeFileName(input);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,437 +3,515 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class DefaultShortStringHelperTestsWithoutSetup
|
||||
{
|
||||
[TestFixture]
|
||||
public class DefaultShortStringHelperTestsWithoutSetup
|
||||
[Test]
|
||||
public void U4_4056()
|
||||
{
|
||||
[Test]
|
||||
public void U4_4056()
|
||||
var requestHandlerSettings = new RequestHandlerSettings
|
||||
{
|
||||
var requestHandlerSettings = new RequestHandlerSettings()
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false",
|
||||
};
|
||||
|
||||
const string input = "ÆØÅ and æøå and 中文测试 and אודות האתר and größer БбДдЖж page";
|
||||
|
||||
var helper =
|
||||
new DefaultShortStringHelper(
|
||||
new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode
|
||||
var output = helper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", output);
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
|
||||
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false"
|
||||
};
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii, // ascii
|
||||
Separator = '-',
|
||||
}));
|
||||
output = helper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual("aeoa-and-aeoa-and-and-and-grosser-bbddzhzh-page", output);
|
||||
}
|
||||
|
||||
const string input = "ÆØÅ and æøå and 中文测试 and אודות האתר and größer БбДдЖж page";
|
||||
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode
|
||||
var output = helper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", output);
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
|
||||
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.Ascii, // ascii
|
||||
Separator = '-'
|
||||
}));
|
||||
output = helper.CleanStringForUrlSegment(input);
|
||||
Assert.AreEqual("aeoa-and-aeoa-and-and-and-grosser-bbddzhzh-page", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void U4_4056_TryAscii()
|
||||
[Test]
|
||||
public void U4_4056_TryAscii()
|
||||
{
|
||||
var requestHandlerSettings = new RequestHandlerSettings
|
||||
{
|
||||
var requestHandlerSettings = new RequestHandlerSettings()
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false",
|
||||
};
|
||||
|
||||
const string input1 = "ÆØÅ and æøå and 中文测试 and אודות האתר and größer БбДдЖж page";
|
||||
const string input2 = "ÆØÅ and æøå and größer БбДдЖж page";
|
||||
|
||||
var helper =
|
||||
new DefaultShortStringHelper(
|
||||
new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
|
||||
Assert.AreEqual("æøå-and-æøå-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input2));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
|
||||
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false"
|
||||
};
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.TryAscii, // try ascii
|
||||
Separator = '-',
|
||||
}));
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
|
||||
Assert.AreEqual("aeoa-and-aeoa-and-grosser-bbddzhzh-page", helper.CleanStringForUrlSegment(input2));
|
||||
}
|
||||
|
||||
const string input1 = "ÆØÅ and æøå and 中文测试 and אודות האתר and größer БбДдЖж page";
|
||||
const string input2 = "ÆØÅ and æøå and größer БбДдЖж page";
|
||||
[Test]
|
||||
public void CleanStringUnderscoreInTerm()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// underscore is accepted within terms
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo_bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
|
||||
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
|
||||
Assert.AreEqual("æøå-and-æøå-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input2));
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// underscore is not accepted within terms
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
|
||||
.WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.LowerCase | CleanStringType.TryAscii, // try ascii
|
||||
Separator = '-'
|
||||
}));
|
||||
Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
|
||||
Assert.AreEqual("aeoa-and-aeoa-and-grosser-bbddzhzh-page", helper.CleanStringForUrlSegment(input2));
|
||||
}
|
||||
[Test]
|
||||
public void CleanStringLeadingChars()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// letters and digits are valid leading chars
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("0123foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
|
||||
[Test]
|
||||
public void CleanStringUnderscoreInTerm()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// underscore is accepted within terms
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo_bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// only letters are valid leading chars
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123 foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// underscore is not accepted within terms
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo*bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
|
||||
}
|
||||
helper = new DefaultShortStringHelper(
|
||||
new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings()));
|
||||
Assert.AreEqual("child2", helper.CleanStringForSafeAlias("1child2"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringLeadingChars()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// letters and digits are valid leading chars
|
||||
IsTerm = (c, leading) => char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("0123foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
[Test]
|
||||
public void CleanStringTermOnUpper()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
// only letters are valid leading chars
|
||||
IsTerm = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123 foo_bar 543 nil 321", CleanStringType.Alias));
|
||||
// uppercase letter means new term
|
||||
BreakTermsOnUpper = true,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*Bar", helper.CleanString("fooBar", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings()));
|
||||
Assert.AreEqual("child2", helper.CleanStringForSafeAlias("1child2"));
|
||||
}
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
|
||||
[Test]
|
||||
public void CleanStringTermOnUpper()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
// uppercase letter is part of term
|
||||
BreakTermsOnUpper = false,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("fooBar", helper.CleanString("fooBar", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
// uppercase letter means new term
|
||||
BreakTermsOnUpper = true,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo*Bar", helper.CleanString("fooBar", CleanStringType.Alias));
|
||||
[Test]
|
||||
public void CleanStringAcronymOnNonUpper()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
// non-uppercase letter means cut acronym
|
||||
CutAcronymOnNonUpper = true,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
|
||||
// uppercase letter is part of term
|
||||
BreakTermsOnUpper = false,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("fooBar", helper.CleanString("fooBar", CleanStringType.Alias));
|
||||
}
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
|
||||
[Test]
|
||||
public void CleanStringAcronymOnNonUpper()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
// non-uppercase letter means word
|
||||
CutAcronymOnNonUpper = false,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*BARRnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BARnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
// non-uppercase letter means cut acronym
|
||||
CutAcronymOnNonUpper = true,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
|
||||
// non-uppercase letter means word
|
||||
CutAcronymOnNonUpper = false,
|
||||
Separator = '*'
|
||||
}));
|
||||
Assert.AreEqual("foo*BARRnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BARnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringGreedyAcronyms()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
[Test]
|
||||
public void CleanStringGreedyAcronyms()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
CutAcronymOnNonUpper = true,
|
||||
GreedyAcronyms = true,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*BARR*nil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAR*nil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*nil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BARR*nil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAR*nil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*nil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
CutAcronymOnNonUpper = true,
|
||||
GreedyAcronyms = false,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
}
|
||||
Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringWhiteSpace()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
[Test]
|
||||
public void CleanStringWhiteSpace()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo", helper.CleanString(" foo ", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*bar", helper.CleanString(" foo bar ", CleanStringType.Alias));
|
||||
}
|
||||
Assert.AreEqual("foo", helper.CleanString(" foo ", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*bar", helper.CleanString(" foo bar ", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringSeparator()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
[Test]
|
||||
public void CleanStringSeparator()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("foo*bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo*bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = ' '
|
||||
Separator = ' ',
|
||||
}));
|
||||
Assert.AreEqual("foo bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged
|
||||
}));
|
||||
Assert.AreEqual("foobar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '文'
|
||||
}));
|
||||
Assert.AreEqual("foo文bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
}
|
||||
Assert.AreEqual("foobar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
|
||||
[Test]
|
||||
public void CleanStringSymbols()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
Separator = '文',
|
||||
}));
|
||||
Assert.AreEqual("house*2", helper.CleanString("house (2)", CleanStringType.Alias));
|
||||
Assert.AreEqual("foo文bar", helper.CleanString("foo bar", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
// FIXME: but for a filename we want to keep them!
|
||||
// FIXME: and what about a URL?
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Utf8Surrogates()
|
||||
{
|
||||
// Unicode values between 0x10000 and 0x10FFF are represented by two 16-bit "surrogate" characters
|
||||
const string str = "a\U00010F00z\uA74Ft";
|
||||
Assert.AreEqual(6, str.Length);
|
||||
Assert.IsTrue(char.IsSurrogate(str[1]));
|
||||
Assert.IsTrue(char.IsHighSurrogate(str[1]));
|
||||
Assert.IsTrue(char.IsSurrogate(str[2]));
|
||||
Assert.IsTrue(char.IsLowSurrogate(str[2]));
|
||||
Assert.AreEqual('z', str[3]);
|
||||
Assert.IsFalse(char.IsSurrogate(str[4]));
|
||||
Assert.AreEqual('\uA74F', str[4]);
|
||||
Assert.AreEqual('t', str[5]);
|
||||
|
||||
Assert.AreEqual("z", str.Substring(3, 1));
|
||||
Assert.AreEqual("\U00010F00", str.Substring(1, 2));
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(str);
|
||||
Assert.AreEqual(10, bytes.Length);
|
||||
Assert.AreEqual('a', bytes[0]);
|
||||
|
||||
// Then next string element is two chars (surrogate pair) or 4 bytes, 21 bits of code point.
|
||||
Assert.AreEqual('z', bytes[5]);
|
||||
|
||||
// Then next string element is one char and 3 bytes, 16 bits of code point.
|
||||
Assert.AreEqual('t', bytes[9]);
|
||||
|
||||
//// foreach (var b in bytes)
|
||||
//// Debug.Print("{0:X}", b);
|
||||
|
||||
Debug.Print("\U00010B70");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Utf8ToAsciiConverter()
|
||||
{
|
||||
const string str = "a\U00010F00z\uA74Ftéô";
|
||||
var output = Cms.Core.Strings.Utf8ToAsciiConverter.ToAsciiString(str);
|
||||
Assert.AreEqual("a?zooteo", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringEncoding()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
[Test]
|
||||
public void CleanStringSymbols()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("中文测试", helper.CleanString("中文测试", CleanStringType.Alias));
|
||||
Assert.AreEqual("léger*中文测试*ZÔRG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
|
||||
Assert.AreEqual("house*2", helper.CleanString("house (2)", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
// FIXME: but for a filename we want to keep them!
|
||||
// FIXME: and what about a URL?
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Utf8Surrogates()
|
||||
{
|
||||
// Unicode values between 0x10000 and 0x10FFF are represented by two 16-bit "surrogate" characters
|
||||
const string str = "a\U00010F00z\uA74Ft";
|
||||
Assert.AreEqual(6, str.Length);
|
||||
Assert.IsTrue(char.IsSurrogate(str[1]));
|
||||
Assert.IsTrue(char.IsHighSurrogate(str[1]));
|
||||
Assert.IsTrue(char.IsSurrogate(str[2]));
|
||||
Assert.IsTrue(char.IsLowSurrogate(str[2]));
|
||||
Assert.AreEqual('z', str[3]);
|
||||
Assert.IsFalse(char.IsSurrogate(str[4]));
|
||||
Assert.AreEqual('\uA74F', str[4]);
|
||||
Assert.AreEqual('t', str[5]);
|
||||
|
||||
Assert.AreEqual("z", str.Substring(3, 1));
|
||||
Assert.AreEqual("\U00010F00", str.Substring(1, 2));
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(str);
|
||||
Assert.AreEqual(10, bytes.Length);
|
||||
Assert.AreEqual('a', bytes[0]);
|
||||
|
||||
// Then next string element is two chars (surrogate pair) or 4 bytes, 21 bits of code point.
|
||||
Assert.AreEqual('z', bytes[5]);
|
||||
|
||||
// Then next string element is one char and 3 bytes, 16 bits of code point.
|
||||
Assert.AreEqual('t', bytes[9]);
|
||||
|
||||
//// foreach (var b in bytes)
|
||||
//// Debug.Print("{0:X}", b);
|
||||
|
||||
Debug.Print("\U00010B70");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Utf8ToAsciiConverter()
|
||||
{
|
||||
const string str = "a\U00010F00z\uA74Ftéô";
|
||||
var output = Cms.Core.Strings.Utf8ToAsciiConverter.ToAsciiString(str);
|
||||
Assert.AreEqual("a?zooteo", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringEncoding()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual("中文测试", helper.CleanString("中文测试", CleanStringType.Alias));
|
||||
Assert.AreEqual("léger*中文测试*ZÔRG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
|
||||
|
||||
helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Ascii | CleanStringType.Unchanged,
|
||||
Separator = '*'
|
||||
Separator = '*',
|
||||
}));
|
||||
Assert.AreEqual(string.Empty, helper.CleanString("中文测试", CleanStringType.Alias));
|
||||
Assert.AreEqual("leger*ZORG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
|
||||
}
|
||||
Assert.AreEqual(string.Empty, helper.CleanString("中文测试", CleanStringType.Alias));
|
||||
Assert.AreEqual("leger*ZORG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringDefaultConfig()
|
||||
[Test]
|
||||
public void CleanStringDefaultConfig()
|
||||
{
|
||||
var requestHandlerSettings = new RequestHandlerSettings
|
||||
{
|
||||
var requestHandlerSettings = new RequestHandlerSettings()
|
||||
{
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false"
|
||||
};
|
||||
UserDefinedCharCollection = Array.Empty<CharItem>(),
|
||||
EnableDefaultCharReplacements = false,
|
||||
ConvertUrlsToAscii = "false",
|
||||
};
|
||||
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings));
|
||||
var helper =
|
||||
new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings));
|
||||
|
||||
const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";
|
||||
const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";
|
||||
|
||||
var alias = helper.CleanStringForSafeAlias(input);
|
||||
var filename = helper.CleanStringForSafeFileName(input);
|
||||
var segment = helper.CleanStringForUrlSegment(input);
|
||||
var alias = helper.CleanStringForSafeAlias(input);
|
||||
var filename = helper.CleanStringForSafeFileName(input);
|
||||
var segment = helper.CleanStringForUrlSegment(input);
|
||||
|
||||
// umbraco-cased ascii alias, must begin with a proper letter
|
||||
Assert.AreEqual("legerZORG2AX", alias, "alias");
|
||||
// umbraco-cased ascii alias, must begin with a proper letter
|
||||
Assert.AreEqual("legerZORG2AX", alias, "alias");
|
||||
|
||||
// lower-cased, utf8 filename, removing illegal filename chars, using dash-separator
|
||||
Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", filename, "filename");
|
||||
// lower-cased, utf8 filename, removing illegal filename chars, using dash-separator
|
||||
Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", filename, "filename");
|
||||
|
||||
// lower-cased, utf8 URL segment, only letters and digits, using dash-separator
|
||||
Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", segment, "segment");
|
||||
}
|
||||
// lower-cased, utf8 URL segment, only letters and digits, using dash-separator
|
||||
Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", segment, "segment");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CleanStringCasing()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
|
||||
[Test]
|
||||
public void CleanStringCasing()
|
||||
{
|
||||
var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
|
||||
.WithDefault(new RequestHandlerSettings())
|
||||
.WithConfig(
|
||||
CleanStringType.Alias,
|
||||
new DefaultShortStringHelperConfig.Config
|
||||
{
|
||||
StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
|
||||
Separator = ' '
|
||||
Separator = ' ',
|
||||
}));
|
||||
|
||||
// BBB is an acronym
|
||||
// E is a word (too short to be an acronym)
|
||||
// FF is an acronym
|
||||
// BBB is an acronym
|
||||
// E is a word (too short to be an acronym)
|
||||
// FF is an acronym
|
||||
|
||||
// FIXME: "C" can't be an acronym
|
||||
// FIXME: "DBXreview" = acronym?!
|
||||
Assert.AreEqual("aaa BBB CCc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias)); // unchanged
|
||||
Assert.AreEqual("aaa Bbb Ccc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("Aaa Bbb Ccc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("aaa bbb ccc ddd e ff", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.LowerCase));
|
||||
Assert.AreEqual("AAA BBB CCC DDD E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UpperCase));
|
||||
Assert.AreEqual("aaa BBB CCc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UmbracoCase));
|
||||
// FIXME: "C" can't be an acronym
|
||||
// FIXME: "DBXreview" = acronym?!
|
||||
Assert.AreEqual(
|
||||
"aaa BBB CCc Ddd E FF",
|
||||
helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias)); // unchanged
|
||||
Assert.AreEqual(
|
||||
"aaa Bbb Ccc Ddd E FF",
|
||||
helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("Aaa Bbb Ccc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"aaa bbb ccc ddd e ff",
|
||||
helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.LowerCase));
|
||||
Assert.AreEqual(
|
||||
"AAA BBB CCC DDD E FF",
|
||||
helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UpperCase));
|
||||
Assert.AreEqual(
|
||||
"aaa BBB CCc Ddd E FF",
|
||||
helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UmbracoCase));
|
||||
|
||||
// MS rules & guidelines:
|
||||
// - Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.
|
||||
// eg "DBRate" (pascal) or "ioHelper" (camel) - "SpecialDBRate" (pascal) or "specialIOHelper" (camel)
|
||||
// - Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.
|
||||
// eg "XmlWriter (pascal) or "htmlReader" (camel) - "SpecialXmlWriter" (pascal) or "specialHtmlReader" (camel)
|
||||
// - Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.
|
||||
// eg "xmlWriter" or "dbWriter" (camel)
|
||||
Assert.AreEqual("aaa BB Ccc", helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("aa Bb Ccc", helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("aaa Bb Ccc", helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("db Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("special DB Rate", helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("xml Writer", helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("special Xml Writer", helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
// MS rules & guidelines:
|
||||
// - Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.
|
||||
// eg "DBRate" (pascal) or "ioHelper" (camel) - "SpecialDBRate" (pascal) or "specialIOHelper" (camel)
|
||||
// - Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.
|
||||
// eg "XmlWriter (pascal) or "htmlReader" (camel) - "SpecialXmlWriter" (pascal) or "specialHtmlReader" (camel)
|
||||
// - Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.
|
||||
// eg "xmlWriter" or "dbWriter" (camel)
|
||||
Assert.AreEqual(
|
||||
"aaa BB Ccc",
|
||||
helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual(
|
||||
"aa Bb Ccc",
|
||||
helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual(
|
||||
"aaa Bb Ccc",
|
||||
helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual("db Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual(
|
||||
"special DB Rate",
|
||||
helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual(
|
||||
"xml Writer",
|
||||
helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
Assert.AreEqual(
|
||||
"special Xml Writer",
|
||||
helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.CamelCase));
|
||||
|
||||
Assert.AreEqual("Aaa BB Ccc", helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("AA Bb Ccc", helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("Aaa Bb Ccc", helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("DB Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("Special DB Rate", helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("Xml Writer", helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("Special Xml Writer", helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
}
|
||||
|
||||
// #region Cases
|
||||
// [TestCase("This is my_little_house so cute.", "thisIsMyLittleHouseSoCute", false)]
|
||||
// [TestCase("This is my_little_house so cute.", "thisIsMy_little_houseSoCute", true)]
|
||||
// [TestCase("This is my_Little_House so cute.", "thisIsMyLittleHouseSoCute", false)]
|
||||
// [TestCase("This is my_Little_House so cute.", "thisIsMy_Little_HouseSoCute", true)]
|
||||
// [TestCase("An UPPER_CASE_TEST to check", "anUpperCaseTestToCheck", false)]
|
||||
// [TestCase("An UPPER_CASE_TEST to check", "anUpper_case_testToCheck", true)]
|
||||
// [TestCase("Trailing_", "trailing", false)]
|
||||
// [TestCase("Trailing_", "trailing_", true)]
|
||||
// [TestCase("_Leading", "leading", false)]
|
||||
// [TestCase("_Leading", "leading", true)]
|
||||
// [TestCase("Repeat___Repeat", "repeatRepeat", false)]
|
||||
// [TestCase("Repeat___Repeat", "repeat___Repeat", true)]
|
||||
// [TestCase("Repeat___repeat", "repeatRepeat", false)]
|
||||
// [TestCase("Repeat___repeat", "repeat___repeat", true)]
|
||||
// #endregion
|
||||
// public void CleanStringWithUnderscore(string input, string expected, bool allowUnderscoreInTerm)
|
||||
// {
|
||||
// var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
|
||||
// .WithConfig(allowUnderscoreInTerm: allowUnderscoreInTerm);
|
||||
// var output = helper.CleanString(input, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase);
|
||||
// Assert.AreEqual(expected, output);
|
||||
// }
|
||||
Assert.AreEqual(
|
||||
"Aaa BB Ccc",
|
||||
helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"AA Bb Ccc",
|
||||
helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"Aaa Bb Ccc",
|
||||
helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual("DB Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"Special DB Rate",
|
||||
helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"Xml Writer",
|
||||
helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
Assert.AreEqual(
|
||||
"Special Xml Writer",
|
||||
helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
|
||||
}
|
||||
|
||||
// #region Cases
|
||||
// [TestCase("This is my_little_house so cute.", "thisIsMyLittleHouseSoCute", false)]
|
||||
// [TestCase("This is my_little_house so cute.", "thisIsMy_little_houseSoCute", true)]
|
||||
// [TestCase("This is my_Little_House so cute.", "thisIsMyLittleHouseSoCute", false)]
|
||||
// [TestCase("This is my_Little_House so cute.", "thisIsMy_Little_HouseSoCute", true)]
|
||||
// [TestCase("An UPPER_CASE_TEST to check", "anUpperCaseTestToCheck", false)]
|
||||
// [TestCase("An UPPER_CASE_TEST to check", "anUpper_case_testToCheck", true)]
|
||||
// [TestCase("Trailing_", "trailing", false)]
|
||||
// [TestCase("Trailing_", "trailing_", true)]
|
||||
// [TestCase("_Leading", "leading", false)]
|
||||
// [TestCase("_Leading", "leading", true)]
|
||||
// [TestCase("Repeat___Repeat", "repeatRepeat", false)]
|
||||
// [TestCase("Repeat___Repeat", "repeat___Repeat", true)]
|
||||
// [TestCase("Repeat___repeat", "repeatRepeat", false)]
|
||||
// [TestCase("Repeat___repeat", "repeat___repeat", true)]
|
||||
// #endregion
|
||||
// public void CleanStringWithUnderscore(string input, string expected, bool allowUnderscoreInTerm)
|
||||
// {
|
||||
// var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
|
||||
// .WithConfig(allowUnderscoreInTerm: allowUnderscoreInTerm);
|
||||
// var output = helper.CleanString(input, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase);
|
||||
// Assert.AreEqual(expected, output);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -3,34 +3,34 @@
|
||||
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
internal class MockShortStringHelper : IShortStringHelper
|
||||
{
|
||||
internal class MockShortStringHelper : IShortStringHelper
|
||||
{
|
||||
public void Freeze() => IsFrozen = true;
|
||||
public bool IsFrozen { get; private set; }
|
||||
|
||||
public bool IsFrozen { get; private set; }
|
||||
public string CleanStringForSafeAlias(string text) => "SAFE-ALIAS::" + text;
|
||||
|
||||
public string CleanStringForSafeAlias(string text) => "SAFE-ALIAS::" + text;
|
||||
public string CleanStringForSafeAlias(string text, string culture) => "SAFE-ALIAS-CULTURE::" + text;
|
||||
|
||||
public string CleanStringForSafeAlias(string text, string culture) => "SAFE-ALIAS-CULTURE::" + text;
|
||||
public string CleanStringForUrlSegment(string text) => "URL-SEGMENT::" + text;
|
||||
|
||||
public string CleanStringForUrlSegment(string text) => "URL-SEGMENT::" + text;
|
||||
public string CleanStringForUrlSegment(string text, string culture) => "URL-SEGMENT-CULTURE::" + text;
|
||||
|
||||
public string CleanStringForUrlSegment(string text, string culture) => "URL-SEGMENT-CULTURE::" + text;
|
||||
public string CleanStringForSafeFileName(string text) => "SAFE-FILE-NAME::" + text;
|
||||
|
||||
public string CleanStringForSafeFileName(string text) => "SAFE-FILE-NAME::" + text;
|
||||
public string CleanStringForSafeFileName(string text, string culture) => "SAFE-FILE-NAME-CULTURE::" + text;
|
||||
|
||||
public string CleanStringForSafeFileName(string text, string culture) => "SAFE-FILE-NAME-CULTURE::" + text;
|
||||
public string SplitPascalCasing(string text, char separator) => "SPLIT-PASCAL-CASING::" + text;
|
||||
|
||||
public string SplitPascalCasing(string text, char separator) => "SPLIT-PASCAL-CASING::" + text;
|
||||
public string CleanString(string text, CleanStringType stringType) => "CLEAN-STRING-A::" + text;
|
||||
|
||||
public string CleanString(string text, CleanStringType stringType) => "CLEAN-STRING-A::" + text;
|
||||
public string CleanString(string text, CleanStringType stringType, char separator) => "CLEAN-STRING-B::" + text;
|
||||
|
||||
public string CleanString(string text, CleanStringType stringType, char separator) => "CLEAN-STRING-B::" + text;
|
||||
public string CleanString(string text, CleanStringType stringType, string culture) => "CLEAN-STRING-C::" + text;
|
||||
|
||||
public string CleanString(string text, CleanStringType stringType, string culture) => "CLEAN-STRING-C::" + text;
|
||||
public string CleanString(string text, CleanStringType stringType, char separator, string culture) =>
|
||||
"CLEAN-STRING-D::" + text;
|
||||
|
||||
public string CleanString(string text, CleanStringType stringType, char separator, string culture) => "CLEAN-STRING-D::" + text;
|
||||
}
|
||||
public void Freeze() => IsFrozen = true;
|
||||
}
|
||||
|
||||
@@ -6,289 +6,321 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class StringExtensionsTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class StringExtensionsTests
|
||||
private readonly IShortStringHelper _mockShortStringHelper = new MockShortStringHelper();
|
||||
|
||||
[TestCase("hello-world.png", "Hello World")]
|
||||
[TestCase("hello-world .png", "Hello World")]
|
||||
[TestCase("_hello-world __1.png", "Hello World 1")]
|
||||
public void To_Friendly_Name(string first, string second) => Assert.AreEqual(first.ToFriendlyName(), second);
|
||||
|
||||
[TestCase("hello", "world", false)]
|
||||
[TestCase("hello", "hello", true)]
|
||||
[TestCase("hellohellohellohellohellohellohello", "hellohellohellohellohellohellohelloo", false)]
|
||||
[TestCase(
|
||||
"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
|
||||
"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohelloo",
|
||||
false)]
|
||||
[TestCase(
|
||||
"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
|
||||
"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
|
||||
true)]
|
||||
public void String_To_Guid(string first, string second, bool result)
|
||||
{
|
||||
private readonly IShortStringHelper _mockShortStringHelper = new MockShortStringHelper();
|
||||
Debug.Print("First: " + first.ToGuid());
|
||||
Debug.Print("Second: " + second.ToGuid());
|
||||
Assert.AreEqual(result, first.ToGuid() == second.ToGuid());
|
||||
}
|
||||
|
||||
[TestCase("hello-world.png", "Hello World")]
|
||||
[TestCase("hello-world .png", "Hello World")]
|
||||
[TestCase("_hello-world __1.png", "Hello World 1")]
|
||||
public void To_Friendly_Name(string first, string second) => Assert.AreEqual(first.ToFriendlyName(), second);
|
||||
[TestCase("hello.txt", "hello")]
|
||||
[TestCase("this.is.a.Txt", "this.is.a")]
|
||||
[TestCase("this.is.not.a. Txt", "this.is.not.a. Txt")]
|
||||
[TestCase("not a file", "not a file")]
|
||||
public void Strip_File_Extension(string input, string result)
|
||||
{
|
||||
var stripped = input.StripFileExtension();
|
||||
Assert.AreEqual(stripped, result);
|
||||
}
|
||||
|
||||
[TestCase("hello", "world", false)]
|
||||
[TestCase("hello", "hello", true)]
|
||||
[TestCase("hellohellohellohellohellohellohello", "hellohellohellohellohellohellohelloo", false)]
|
||||
[TestCase("hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohelloo", false)]
|
||||
[TestCase("hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", true)]
|
||||
public void String_To_Guid(string first, string second, bool result)
|
||||
{
|
||||
Debug.Print("First: " + first.ToGuid());
|
||||
Debug.Print("Second: " + second.ToGuid());
|
||||
Assert.AreEqual(result, first.ToGuid() == second.ToGuid());
|
||||
}
|
||||
[TestCase("../wtf.js?x=wtf", ".js")]
|
||||
[TestCase(".htaccess", ".htaccess")]
|
||||
[TestCase("path/to/file/image.png", ".png")]
|
||||
[TestCase("c:\\abc\\def\\ghi.jkl", ".jkl")]
|
||||
[TestCase("/root/folder.name/file.ext", ".ext")]
|
||||
[TestCase("http://www.domain.com/folder/name/file.txt", ".txt")]
|
||||
[TestCase("i/don't\\have\\an/extension", "")]
|
||||
[TestCase("https://some.where/path/to/file.ext?query=this&more=that", ".ext")]
|
||||
[TestCase("double_query.string/file.ext?query=abc?something.else", ".ext")]
|
||||
[TestCase("test.tar.gz", ".gz")]
|
||||
[TestCase("wierd.file,but._legal", "._legal")]
|
||||
[TestCase("one_char.x", ".x")]
|
||||
public void Get_File_Extension(string input, string result)
|
||||
{
|
||||
var extension = input.GetFileExtension();
|
||||
Assert.AreEqual(result, extension);
|
||||
}
|
||||
|
||||
[TestCase("hello.txt", "hello")]
|
||||
[TestCase("this.is.a.Txt", "this.is.a")]
|
||||
[TestCase("this.is.not.a. Txt", "this.is.not.a. Txt")]
|
||||
[TestCase("not a file", "not a file")]
|
||||
public void Strip_File_Extension(string input, string result)
|
||||
{
|
||||
var stripped = input.StripFileExtension();
|
||||
Assert.AreEqual(stripped, result);
|
||||
}
|
||||
[TestCase("'+alert(1234)+'", "+alert1234+")]
|
||||
[TestCase("'+alert(56+78)+'", "+alert56+78+")]
|
||||
[TestCase("{{file}}", "file")]
|
||||
[TestCase("'+alert('hello')+'", "+alerthello+")]
|
||||
[TestCase("Test", "Test")]
|
||||
public void Clean_From_XSS(string input, string result)
|
||||
{
|
||||
var cleaned = input.CleanForXss();
|
||||
Assert.AreEqual(cleaned, result);
|
||||
}
|
||||
|
||||
[TestCase("../wtf.js?x=wtf", ".js")]
|
||||
[TestCase(".htaccess", ".htaccess")]
|
||||
[TestCase("path/to/file/image.png", ".png")]
|
||||
[TestCase("c:\\abc\\def\\ghi.jkl", ".jkl")]
|
||||
[TestCase("/root/folder.name/file.ext", ".ext")]
|
||||
[TestCase("http://www.domain.com/folder/name/file.txt", ".txt")]
|
||||
[TestCase("i/don't\\have\\an/extension", "")]
|
||||
[TestCase("https://some.where/path/to/file.ext?query=this&more=that", ".ext")]
|
||||
[TestCase("double_query.string/file.ext?query=abc?something.else", ".ext")]
|
||||
[TestCase("test.tar.gz", ".gz")]
|
||||
[TestCase("wierd.file,but._legal", "._legal")]
|
||||
[TestCase("one_char.x", ".x")]
|
||||
public void Get_File_Extension(string input, string result)
|
||||
{
|
||||
var extension = input.GetFileExtension();
|
||||
Assert.AreEqual(result, extension);
|
||||
}
|
||||
[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("'+alert(1234)+'", "+alert1234+")]
|
||||
[TestCase("'+alert(56+78)+'", "+alert56+78+")]
|
||||
[TestCase("{{file}}", "file")]
|
||||
[TestCase("'+alert('hello')+'", "+alerthello+")]
|
||||
[TestCase("Test", "Test")]
|
||||
public void Clean_From_XSS(string input, string result)
|
||||
{
|
||||
var cleaned = input.CleanForXss();
|
||||
Assert.AreEqual(cleaned, result);
|
||||
}
|
||||
[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("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",
|
||||
"replaced",
|
||||
"replaced this is my string",
|
||||
StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase(
|
||||
"Hello this is hello my string",
|
||||
"hello",
|
||||
"replaced",
|
||||
"replaced this is replaced my string",
|
||||
StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase(
|
||||
"Hello this is my string",
|
||||
"nonexistent",
|
||||
"replaced",
|
||||
"Hello this is my string",
|
||||
StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase(
|
||||
"Hellohello this is my string",
|
||||
"hello",
|
||||
"replaced",
|
||||
"replacedreplaced this is my string",
|
||||
StringComparison.CurrentCultureIgnoreCase)]
|
||||
|
||||
[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);
|
||||
}
|
||||
// Ensure replacing with the same string doesn't cause infinite loop.
|
||||
[TestCase(
|
||||
"Hello this is my string",
|
||||
"hello",
|
||||
"hello",
|
||||
"hello this is my string",
|
||||
StringComparison.CurrentCultureIgnoreCase)]
|
||||
public void ReplaceWithStringComparison(
|
||||
string input,
|
||||
string oldString,
|
||||
string newString,
|
||||
string shouldBe,
|
||||
StringComparison stringComparison)
|
||||
{
|
||||
var replaced = input.Replace(oldString, newString, stringComparison);
|
||||
Assert.AreEqual(shouldBe, replaced);
|
||||
}
|
||||
|
||||
[TestCase("Hello this is my string", "hello", "replaced", "replaced this is my string", StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase("Hello this is hello my string", "hello", "replaced", "replaced this is replaced my string", StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase("Hello this is my string", "nonexistent", "replaced", "Hello this is my string", StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase("Hellohello this is my string", "hello", "replaced", "replacedreplaced this is my string", StringComparison.CurrentCultureIgnoreCase)]
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", "")]
|
||||
[TestCase("x", "X")]
|
||||
[TestCase("xyzT", "XyzT")]
|
||||
[TestCase("XyzT", "XyzT")]
|
||||
public void ToFirstUpper(string input, string expected)
|
||||
{
|
||||
var output = input.ToFirstUpper();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
// Ensure replacing with the same string doesn't cause infinite loop.
|
||||
[TestCase("Hello this is my string", "hello", "hello", "hello this is my string", StringComparison.CurrentCultureIgnoreCase)]
|
||||
public void ReplaceWithStringComparison(string input, string oldString, string newString, string shouldBe, StringComparison stringComparison)
|
||||
{
|
||||
var replaced = input.Replace(oldString, newString, stringComparison);
|
||||
Assert.AreEqual(shouldBe, replaced);
|
||||
}
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", "")]
|
||||
[TestCase("X", "x")]
|
||||
[TestCase("XyZ", "xyZ")]
|
||||
[TestCase("xyZ", "xyZ")]
|
||||
public void ToFirstLower(string input, string expected)
|
||||
{
|
||||
var output = input.ToFirstLower();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", "")]
|
||||
[TestCase("x", "X")]
|
||||
[TestCase("xyzT", "XyzT")]
|
||||
[TestCase("XyzT", "XyzT")]
|
||||
public void ToFirstUpper(string input, string expected)
|
||||
{
|
||||
var output = input.ToFirstUpper();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase("pineapple", new[] { "banana", "apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, true)]
|
||||
[TestCase("PINEAPPLE", new[] { "banana", "apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, false)]
|
||||
[TestCase("pineapple", new[] { "banana", "Apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, false)]
|
||||
[TestCase("pineapple", new[] { "banana", "Apple", "blueberry", "strawberry" }, StringComparison.OrdinalIgnoreCase, true)]
|
||||
[TestCase("pineapple", new[] { "banana", "blueberry", "strawberry" }, StringComparison.OrdinalIgnoreCase, false)]
|
||||
[TestCase("Strawberry unicorn pie", new[] { "Berry" }, StringComparison.OrdinalIgnoreCase, true)]
|
||||
[TestCase("empty pie", new string[0], StringComparison.OrdinalIgnoreCase, false)]
|
||||
public void ContainsAny(string haystack, IEnumerable<string> needles, StringComparison comparison, bool expected)
|
||||
{
|
||||
var output = haystack.ContainsAny(needles, comparison);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", "")]
|
||||
[TestCase("X", "x")]
|
||||
[TestCase("XyZ", "xyZ")]
|
||||
[TestCase("xyZ", "xyZ")]
|
||||
public void ToFirstLower(string input, string expected)
|
||||
{
|
||||
var output = input.ToFirstLower();
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("pineapple", new string[] { "banana", "apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, true)]
|
||||
[TestCase("PINEAPPLE", new string[] { "banana", "apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, false)]
|
||||
[TestCase("pineapple", new string[] { "banana", "Apple", "blueberry", "strawberry" }, StringComparison.CurrentCulture, false)]
|
||||
[TestCase("pineapple", new string[] { "banana", "Apple", "blueberry", "strawberry" }, StringComparison.OrdinalIgnoreCase, true)]
|
||||
[TestCase("pineapple", new string[] { "banana", "blueberry", "strawberry" }, StringComparison.OrdinalIgnoreCase, false)]
|
||||
[TestCase("Strawberry unicorn pie", new string[] { "Berry" }, StringComparison.OrdinalIgnoreCase, true)]
|
||||
[TestCase("empty pie", new string[0], StringComparison.OrdinalIgnoreCase, false)]
|
||||
public void ContainsAny(string haystack, IEnumerable<string> needles, StringComparison comparison, bool expected)
|
||||
{
|
||||
bool output = haystack.ContainsAny(needles, comparison);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("", true)]
|
||||
[TestCase(" ", true)]
|
||||
[TestCase("\r\n\r\n", true)]
|
||||
[TestCase("\r\n", true)]
|
||||
[TestCase(
|
||||
@"
|
||||
[TestCase("", true)]
|
||||
[TestCase(" ", true)]
|
||||
[TestCase("\r\n\r\n", true)]
|
||||
[TestCase("\r\n", true)]
|
||||
[TestCase(
|
||||
@"
|
||||
Hello
|
||||
", false)]
|
||||
[TestCase(null, true)]
|
||||
[TestCase("a", false)]
|
||||
[TestCase("abc", false)]
|
||||
[TestCase("abc ", false)]
|
||||
[TestCase(" abc", false)]
|
||||
[TestCase(" abc ", false)]
|
||||
public void IsNullOrWhiteSpace(string value, bool expected)
|
||||
",
|
||||
false)]
|
||||
[TestCase(null, true)]
|
||||
[TestCase("a", false)]
|
||||
[TestCase("abc", false)]
|
||||
[TestCase("abc ", false)]
|
||||
[TestCase(" abc", false)]
|
||||
[TestCase(" abc ", false)]
|
||||
public void IsNullOrWhiteSpace(string value, bool expected)
|
||||
{
|
||||
// Act
|
||||
var result = value.IsNullOrWhiteSpace();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[TestCase("hello", "aGVsbG8")]
|
||||
[TestCase("tad", "dGFk")]
|
||||
[TestCase("AmqGr+Fd!~ééé", "QW1xR3IrRmQhfsOpw6nDqQ")]
|
||||
public void UrlTokenEncoding(string value, string expected)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(value);
|
||||
Console.WriteLine("base64: " + Convert.ToBase64String(bytes));
|
||||
var encoded = bytes.UrlTokenEncode();
|
||||
Assert.AreEqual(expected, encoded);
|
||||
|
||||
var backBytes = encoded.UrlTokenDecode();
|
||||
var backString = Encoding.UTF8.GetString(backBytes);
|
||||
Assert.AreEqual(value, backString);
|
||||
}
|
||||
|
||||
// FORMAT STRINGS
|
||||
|
||||
// note: here we just ensure that the proper helper gets called properly
|
||||
// but the "legacy" tests have moved to the legacy helper tests
|
||||
[Test]
|
||||
public void ToUrlAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FormatUrl()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUmbracoAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-ALIAS::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-ALIAS::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeAliasWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper, null);
|
||||
Assert.AreEqual("SAFE-ALIAS-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUrlSegment()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUrlSegmentWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper, null);
|
||||
Assert.AreEqual("URL-SEGMENT-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeFileName()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeFileName(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-FILE-NAME::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeFileNameWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeFileName(_mockShortStringHelper, null);
|
||||
Assert.AreEqual("SAFE-FILE-NAME-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertCase()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToCleanString(_mockShortStringHelper, CleanStringType.Unchanged);
|
||||
Assert.AreEqual("CLEAN-STRING-A::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SplitPascalCasing()
|
||||
{
|
||||
var output = "JUST-ANYTHING".SplitPascalCasing(_mockShortStringHelper);
|
||||
Assert.AreEqual("SPLIT-PASCAL-CASING::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test] // can't do cases with an IDictionary
|
||||
public void ReplaceManyWithCharMap()
|
||||
{
|
||||
const string input = "télévisiön tzvâr ßup pof";
|
||||
const string expected = "television tzvar ssup pof";
|
||||
IDictionary<string, string> replacements = new Dictionary<string, string>
|
||||
{
|
||||
// Act
|
||||
bool result = value.IsNullOrWhiteSpace();
|
||||
{ "é", "e" },
|
||||
{ "ö", "o" },
|
||||
{ "â", "a" },
|
||||
{ "ß", "ss" },
|
||||
{ " ", " " },
|
||||
};
|
||||
var output = input.ReplaceMany(replacements);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[TestCase("hello", "aGVsbG8")]
|
||||
[TestCase("tad", "dGFk")]
|
||||
[TestCase("AmqGr+Fd!~ééé", "QW1xR3IrRmQhfsOpw6nDqQ")]
|
||||
public void UrlTokenEncoding(string value, string expected)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(value);
|
||||
Console.WriteLine("base64: " + Convert.ToBase64String(bytes));
|
||||
var encoded = bytes.UrlTokenEncode();
|
||||
Assert.AreEqual(expected, encoded);
|
||||
|
||||
var backBytes = encoded.UrlTokenDecode();
|
||||
var backString = Encoding.UTF8.GetString(backBytes);
|
||||
Assert.AreEqual(value, backString);
|
||||
}
|
||||
|
||||
// FORMAT STRINGS
|
||||
|
||||
// note: here we just ensure that the proper helper gets called properly
|
||||
// but the "legacy" tests have moved to the legacy helper tests
|
||||
|
||||
[Test]
|
||||
public void ToUrlAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FormatUrl()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUmbracoAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-ALIAS::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeAlias()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-ALIAS::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeAliasWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeAlias(_mockShortStringHelper, (string)null);
|
||||
Assert.AreEqual("SAFE-ALIAS-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUrlSegment()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper);
|
||||
Assert.AreEqual("URL-SEGMENT::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToUrlSegmentWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToUrlSegment(_mockShortStringHelper, (string)null);
|
||||
Assert.AreEqual("URL-SEGMENT-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeFileName()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeFileName(_mockShortStringHelper);
|
||||
Assert.AreEqual("SAFE-FILE-NAME::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToSafeFileNameWithCulture()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToSafeFileName(_mockShortStringHelper, null);
|
||||
Assert.AreEqual("SAFE-FILE-NAME-CULTURE::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertCase()
|
||||
{
|
||||
var output = "JUST-ANYTHING".ToCleanString(_mockShortStringHelper, CleanStringType.Unchanged);
|
||||
Assert.AreEqual("CLEAN-STRING-A::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SplitPascalCasing()
|
||||
{
|
||||
var output = "JUST-ANYTHING".SplitPascalCasing(_mockShortStringHelper);
|
||||
Assert.AreEqual("SPLIT-PASCAL-CASING::JUST-ANYTHING", output);
|
||||
}
|
||||
|
||||
[Test] // can't do cases with an IDictionary
|
||||
public void ReplaceManyWithCharMap()
|
||||
{
|
||||
const string input = "télévisiön tzvâr ßup pof";
|
||||
const string expected = "television tzvar ssup pof";
|
||||
IDictionary<string, string> replacements = new Dictionary<string, string>
|
||||
{
|
||||
{ "é", "e" },
|
||||
{ "ö", "o" },
|
||||
{ "â", "a" },
|
||||
{ "ß", "ss" },
|
||||
{ " ", " " },
|
||||
};
|
||||
var output = input.ReplaceMany(replacements);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
|
||||
[TestCase("val$id!ate|this|str'ing", "$!'", '-', "val-id-ate|this|str-ing")]
|
||||
[TestCase("val$id!ate|this|str'ing", "$!'", '*', "val*id*ate|this|str*ing")]
|
||||
public void ReplaceManyByOneChar(string input, string toReplace, char replacement, string expected)
|
||||
{
|
||||
var output = input.ReplaceMany(toReplace.ToArray(), replacement);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
[TestCase("val$id!ate|this|str'ing", "$!'", '-', "val-id-ate|this|str-ing")]
|
||||
[TestCase("val$id!ate|this|str'ing", "$!'", '*', "val*id*ate|this|str*ing")]
|
||||
public void ReplaceManyByOneChar(string input, string toReplace, char replacement, string expected)
|
||||
{
|
||||
var output = input.ReplaceMany(toReplace.ToArray(), replacement);
|
||||
Assert.AreEqual(expected, output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,40 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class StringValidationTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class StringValidationTests
|
||||
[TestCase("someone@somewhere.com", ExpectedResult = true)]
|
||||
[TestCase("someone@somewhere.co.uk", ExpectedResult = true)]
|
||||
[TestCase("someone+tag@somewhere.net", ExpectedResult = true)]
|
||||
[TestCase("futureTLD@somewhere.fooo", ExpectedResult = true)]
|
||||
[TestCase("abc@xyz.financial", ExpectedResult = true)]
|
||||
[TestCase("admin+gmail-syntax@c.pizza", ExpectedResult = true)]
|
||||
[TestCase("admin@c.pizza", ExpectedResult = true)]
|
||||
[TestCase("fdsa", ExpectedResult = false)]
|
||||
[TestCase("fdsa@", ExpectedResult = false)]
|
||||
|
||||
// IsValid can be either a powerful regex OR a dummy test,
|
||||
// and by default it depends on System.ComponentModel.DataAnnotations.AppSettings.DisableRegEx
|
||||
// which ends up using BinaryCompatibility.Current.TargetsAtLeastFramework472 so for some reason
|
||||
// in 472 we are not using the regex anymore
|
||||
//
|
||||
// it can be forced, though with an app settings
|
||||
// dataAnnotations:dataTypeAttribute:disableRegEx = false
|
||||
//
|
||||
// since Umbraco is now 4.7.2+, the setting is required for the following tests to pass
|
||||
|
||||
// [TestCase("fdsa@fdsa", ExpectedResult = false)]
|
||||
// [TestCase("fdsa@fdsa.", ExpectedResult = false)]
|
||||
public bool Validate_Email_Address(string input)
|
||||
{
|
||||
[TestCase("someone@somewhere.com", ExpectedResult = true)]
|
||||
[TestCase("someone@somewhere.co.uk", ExpectedResult = true)]
|
||||
[TestCase("someone+tag@somewhere.net", ExpectedResult = true)]
|
||||
[TestCase("futureTLD@somewhere.fooo", ExpectedResult = true)]
|
||||
var foo = new EmailAddressAttribute();
|
||||
|
||||
[TestCase("abc@xyz.financial", ExpectedResult = true)]
|
||||
[TestCase("admin+gmail-syntax@c.pizza", ExpectedResult = true)]
|
||||
[TestCase("admin@c.pizza", ExpectedResult = true)]
|
||||
|
||||
[TestCase("fdsa", ExpectedResult = false)]
|
||||
[TestCase("fdsa@", ExpectedResult = false)]
|
||||
|
||||
// IsValid can be either a powerful regex OR a dummy test,
|
||||
// and by default it depends on System.ComponentModel.DataAnnotations.AppSettings.DisableRegEx
|
||||
// which ends up using BinaryCompatibility.Current.TargetsAtLeastFramework472 so for some reason
|
||||
// in 472 we are not using the regex anymore
|
||||
//
|
||||
// it can be forced, though with an app settings
|
||||
// dataAnnotations:dataTypeAttribute:disableRegEx = false
|
||||
//
|
||||
// since Umbraco is now 4.7.2+, the setting is required for the following tests to pass
|
||||
|
||||
// [TestCase("fdsa@fdsa", ExpectedResult = false)]
|
||||
// [TestCase("fdsa@fdsa.", ExpectedResult = false)]
|
||||
public bool Validate_Email_Address(string input)
|
||||
{
|
||||
var foo = new EmailAddressAttribute();
|
||||
|
||||
return foo.IsValid(input);
|
||||
}
|
||||
return foo.IsValid(input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,87 +1,100 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Strings.Css;
|
||||
using Umbraco.Cms.Tests.Common.Extensions;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.ShortStringHelper;
|
||||
|
||||
[TestFixture]
|
||||
public class StylesheetHelperTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class StylesheetHelperTests
|
||||
[Test]
|
||||
public void Replace_Rule()
|
||||
{
|
||||
[Test]
|
||||
public void Replace_Rule()
|
||||
{
|
||||
string css = @"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}";
|
||||
IEnumerable<StylesheetRule> results = StylesheetHelper.ParseRules(css);
|
||||
var css =
|
||||
@"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}";
|
||||
var results = StylesheetHelper.ParseRules(css);
|
||||
|
||||
string result = StylesheetHelper.ReplaceRule(css, results.First().Name, new StylesheetRule()
|
||||
var result = StylesheetHelper.ReplaceRule(
|
||||
css,
|
||||
results.First().Name,
|
||||
new StylesheetRule
|
||||
{
|
||||
Name = "My new rule",
|
||||
Selector = "p",
|
||||
Styles = "font-size:1em; color:blue;"
|
||||
Styles = "font-size:1em; color:blue;",
|
||||
});
|
||||
|
||||
Assert.AreEqual(
|
||||
@"body {font-family:Arial;}/**umb_name:My new rule*/
|
||||
p{font-size:1em; color:blue;} /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}".StripWhitespace(), result.StripWhitespace());
|
||||
}
|
||||
Assert.AreEqual(
|
||||
@"body {font-family:Arial;}/**umb_name:My new rule*/
|
||||
p{font-size:1em; color:blue;} /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}".StripWhitespace(),
|
||||
result.StripWhitespace());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Append_Rule()
|
||||
{
|
||||
string css = @"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}";
|
||||
[Test]
|
||||
public void Append_Rule()
|
||||
{
|
||||
var css =
|
||||
@"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}";
|
||||
|
||||
string result = StylesheetHelper.AppendRule(css, new StylesheetRule()
|
||||
{
|
||||
Name = "My new rule",
|
||||
Selector = "p",
|
||||
Styles = "font-size:1em; color:blue;"
|
||||
});
|
||||
var result = StylesheetHelper.AppendRule(
|
||||
css,
|
||||
new StylesheetRule { Name = "My new rule", Selector = "p", Styles = "font-size:1em; color:blue;" });
|
||||
|
||||
Assert.AreEqual(
|
||||
@"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}
|
||||
Assert.AreEqual(
|
||||
@"body {font-family:Arial;}/** Umb_Name: Test1 */ p { font-size: 1em; } /** umb_name: Test2 */ li {padding:0px;} table {margin:0;}
|
||||
|
||||
/**umb_name:My new rule*/
|
||||
p{font-size:1em; color:blue;}".StripWhitespace(), result.StripWhitespace());
|
||||
}
|
||||
p{font-size:1em; color:blue;}".StripWhitespace(),
|
||||
result.StripWhitespace());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Duplicate_Names()
|
||||
{
|
||||
string css = @"/** Umb_Name: Test */ p { font-size: 1em; } /** umb_name: Test */ li {padding:0px;}";
|
||||
IEnumerable<StylesheetRule> results = StylesheetHelper.ParseRules(css);
|
||||
Assert.AreEqual(1, results.Count());
|
||||
}
|
||||
[Test]
|
||||
public void Duplicate_Names()
|
||||
{
|
||||
var css = @"/** Umb_Name: Test */ p { font-size: 1em; } /** umb_name: Test */ li {padding:0px;}";
|
||||
var results = StylesheetHelper.ParseRules(css);
|
||||
Assert.AreEqual(1, results.Count());
|
||||
}
|
||||
|
||||
// Standard rule stle
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**
|
||||
// Standard rule stle
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**
|
||||
Umb_Name: Test
|
||||
*/
|
||||
p {
|
||||
font-size: 1em;
|
||||
}")]
|
||||
// All on one line, different casing
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/** Umb_Name: Test */ p { font-size: 1em; }")]
|
||||
// styles on several lines
|
||||
[TestCase("Test", "p", @"font-size: 1em;
|
||||
|
||||
// All on one line, different casing
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/** Umb_Name: Test */ p { font-size: 1em; }")]
|
||||
|
||||
// styles on several lines
|
||||
[TestCase(
|
||||
"Test",
|
||||
"p",
|
||||
@"font-size: 1em;
|
||||
color:red; font-weight:bold;
|
||||
|
||||
text-align:left;", @"/** umb_name: Test */ p { font-size: 1em;
|
||||
text-align:left;",
|
||||
@"/** umb_name: Test */ p { font-size: 1em;
|
||||
color:red; font-weight:bold;
|
||||
|
||||
text-align:left;
|
||||
|
||||
}")]
|
||||
// All on one line with no spaces
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**UMB_NAME:Test*/p{font-size: 1em;}")]
|
||||
// Has a name with spaces
|
||||
[TestCase("Hello world", "p", "font-size: 1em;", @"/**UMB_NAME:Hello world */p{font-size: 1em;}")]
|
||||
// Every part on a new line
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**
|
||||
|
||||
// All on one line with no spaces
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**UMB_NAME:Test*/p{font-size: 1em;}")]
|
||||
|
||||
// Has a name with spaces
|
||||
[TestCase("Hello world", "p", "font-size: 1em;", @"/**UMB_NAME:Hello world */p{font-size: 1em;}")]
|
||||
|
||||
// Every part on a new line
|
||||
[TestCase("Test", "p", "font-size: 1em;", @"/**
|
||||
umb_name:
|
||||
Test
|
||||
*/
|
||||
@@ -89,130 +102,126 @@ p
|
||||
{
|
||||
font-size: 1em;
|
||||
}")]
|
||||
public void ParseRules_Parses(string name, string selector, string styles, string css)
|
||||
{
|
||||
// Act
|
||||
IEnumerable<StylesheetRule> results = StylesheetHelper.ParseRules(css);
|
||||
public void ParseRules_Parses(string name, string selector, string styles, string css)
|
||||
{
|
||||
// Act
|
||||
var results = StylesheetHelper.ParseRules(css).ToArray();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(1, results.Count());
|
||||
// Assert
|
||||
Assert.AreEqual(1, results.Length);
|
||||
|
||||
// Assert.IsTrue(results.First().RuleId.Value.Value.ToString() == file.Id.Value.Value + "/" + name);
|
||||
Assert.AreEqual(name, results.First().Name);
|
||||
Assert.AreEqual(selector, results.First().Selector);
|
||||
Assert.AreEqual(styles.StripWhitespace(), results.First().Styles.StripWhitespace());
|
||||
}
|
||||
// Assert.IsTrue(results.First().RuleId.Value.Value.ToString() == file.Id.Value.Value + "/" + name);
|
||||
Assert.AreEqual(name, results.First().Name);
|
||||
Assert.AreEqual(selector, results.First().Selector);
|
||||
Assert.AreEqual(styles.StripWhitespace(), results.First().Styles.StripWhitespace());
|
||||
}
|
||||
|
||||
// No Name: keyword
|
||||
[TestCase(@"/** Test2 */
|
||||
// No Name: keyword
|
||||
[TestCase(@"/** Test2 */
|
||||
p
|
||||
{
|
||||
font-size: 1em;
|
||||
}")]
|
||||
|
||||
// Has a Name: keyword, but applies to 2 rules, so shouldn't parse
|
||||
[TestCase(@"/** umb_name: Test2 */
|
||||
// Has a Name: keyword, but applies to 2 rules, so shouldn't parse
|
||||
[TestCase(@"/** umb_name: Test2 */
|
||||
p, h2
|
||||
{
|
||||
font-size: 1em;
|
||||
}")]
|
||||
|
||||
// Has it's name wrapping over two lines
|
||||
[TestCase("/** umb_name: Test\r\n2 */ p { font-size: 1em; }")]
|
||||
[TestCase("/** umb_name: Test\n2 */ p { font-size: 1em; }")]
|
||||
// Has it's name wrapping over two lines
|
||||
[TestCase("/** umb_name: Test\r\n2 */ p { font-size: 1em; }")]
|
||||
[TestCase("/** umb_name: Test\n2 */ p { font-size: 1em; }")]
|
||||
|
||||
// Only a single asterisk
|
||||
[TestCase("/* umb_name: Test */ p { font-size: 1em; }")]
|
||||
// Only a single asterisk
|
||||
[TestCase("/* umb_name: Test */ p { font-size: 1em; }")]
|
||||
|
||||
// Has a name with spaces over multiple lines
|
||||
[TestCase(@"/**UMB_NAME:Hello
|
||||
// Has a name with spaces over multiple lines
|
||||
[TestCase(@"/**UMB_NAME:Hello
|
||||
|
||||
world */p{font-size: 1em;}")]
|
||||
public void ParseRules_DoesntParse(string css)
|
||||
{
|
||||
// Act
|
||||
IEnumerable<StylesheetRule> results = StylesheetHelper.ParseRules(css);
|
||||
public void ParseRules_DoesntParse(string css)
|
||||
{
|
||||
// Act
|
||||
var results = StylesheetHelper.ParseRules(css);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(results.Any() == false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AppendRules_IsFormatted()
|
||||
{
|
||||
// base CSS
|
||||
string css = Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}");
|
||||
// add a couple of rules
|
||||
string result = StylesheetHelper.AppendRule(css, new StylesheetRule
|
||||
{
|
||||
Name = "Test",
|
||||
Selector = ".test",
|
||||
Styles = "font-color: red;margin: 1rem;"
|
||||
});
|
||||
result = StylesheetHelper.AppendRule(result, new StylesheetRule
|
||||
{
|
||||
Name = "Test2",
|
||||
Selector = ".test2",
|
||||
Styles = "font-color: green;"
|
||||
});
|
||||
|
||||
// verify the CSS formatting including the indents
|
||||
Assert.AreEqual(
|
||||
Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}
|
||||
|
||||
/**umb_name:Test*/
|
||||
.test {
|
||||
#font-color: red;
|
||||
#margin: 1rem;
|
||||
}
|
||||
|
||||
/**umb_name:Test2*/
|
||||
.test2 {
|
||||
#font-color: green;
|
||||
}"), result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseFormattedRules_CanParse()
|
||||
{
|
||||
// base CSS
|
||||
string css = Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}
|
||||
|
||||
/**umb_name:Test*/
|
||||
.test {
|
||||
#font-color: red;
|
||||
#margin: 1rem;
|
||||
}
|
||||
|
||||
/**umb_name:Test2*/
|
||||
.test2 {
|
||||
#font-color: green;
|
||||
}");
|
||||
IEnumerable<StylesheetRule> rules = StylesheetHelper.ParseRules(css);
|
||||
Assert.AreEqual(2, rules.Count());
|
||||
|
||||
Assert.AreEqual("Test", rules.First().Name);
|
||||
Assert.AreEqual(".test", rules.First().Selector);
|
||||
Assert.AreEqual(
|
||||
@"font-color: red;
|
||||
margin: 1rem;", rules.First().Styles);
|
||||
|
||||
Assert.AreEqual("Test2", rules.Last().Name);
|
||||
Assert.AreEqual(".test2", rules.Last().Selector);
|
||||
Assert.AreEqual("font-color: green;", rules.Last().Styles);
|
||||
}
|
||||
|
||||
// can't put tabs in verbatim strings, so this will replace # with \t to test the CSS indents
|
||||
// - and it's tabs because the editor uses tabs, not spaces...
|
||||
private static string Tabbed(string input) => input.Replace("#", "\t");
|
||||
// Assert
|
||||
Assert.IsTrue(results.Any() == false);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AppendRules_IsFormatted()
|
||||
{
|
||||
// base CSS
|
||||
var css = Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}");
|
||||
|
||||
// add a couple of rules
|
||||
var result = StylesheetHelper.AppendRule(
|
||||
css,
|
||||
new StylesheetRule { Name = "Test", Selector = ".test", Styles = "font-color: red;margin: 1rem;" });
|
||||
result = StylesheetHelper.AppendRule(
|
||||
result,
|
||||
new StylesheetRule { Name = "Test2", Selector = ".test2", Styles = "font-color: green;" });
|
||||
|
||||
// verify the CSS formatting including the indents
|
||||
Assert.AreEqual(
|
||||
Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}
|
||||
|
||||
/**umb_name:Test*/
|
||||
.test {
|
||||
#font-color: red;
|
||||
#margin: 1rem;
|
||||
}
|
||||
|
||||
/**umb_name:Test2*/
|
||||
.test2 {
|
||||
#font-color: green;
|
||||
}").NormalizeNewLines(),
|
||||
result.NormalizeNewLines());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseFormattedRules_CanParse()
|
||||
{
|
||||
// base CSS
|
||||
var css = Tabbed(
|
||||
@"body {
|
||||
#font-family:Arial;
|
||||
}
|
||||
|
||||
/**umb_name:Test*/
|
||||
.test {
|
||||
#font-color: red;
|
||||
#margin: 1rem;
|
||||
}
|
||||
|
||||
/**umb_name:Test2*/
|
||||
.test2 {
|
||||
#font-color: green;
|
||||
}");
|
||||
var rules = StylesheetHelper.ParseRules(css).ToArray();
|
||||
Assert.AreEqual(2, rules.Count());
|
||||
|
||||
Assert.AreEqual("Test", rules.First().Name);
|
||||
Assert.AreEqual(".test", rules.First().Selector);
|
||||
Assert.AreEqual(
|
||||
@"font-color: red;
|
||||
margin: 1rem;".NormalizeNewLines(),
|
||||
rules.First().Styles.NormalizeNewLines());
|
||||
|
||||
Assert.AreEqual("Test2", rules.Last().Name);
|
||||
Assert.AreEqual(".test2", rules.Last().Selector);
|
||||
Assert.AreEqual("font-color: green;", rules.Last().Styles);
|
||||
}
|
||||
|
||||
// can't put tabs in verbatim strings, so this will replace # with \t to test the CSS indents
|
||||
// - and it's tabs because the editor uses tabs, not spaces...
|
||||
private static string Tabbed(string input) => input.Replace("#", "\t");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user