Keep structuring tests

This commit is contained in:
Stephan
2016-11-07 19:12:09 +01:00
parent 224986d56c
commit 2967add41a
56 changed files with 1541 additions and 1621 deletions

View File

@@ -0,0 +1,24 @@
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class AttemptTests
{
[Test]
public void AttemptIf()
{
// just making sure that it is ok to use TryParse as a condition
int value;
var attempt = Attempt.If(int.TryParse("1234", out value), value);
Assert.IsTrue(attempt.Success);
Assert.AreEqual(1234, attempt.Result);
attempt = Attempt.If(int.TryParse("12xxx34", out value), value);
Assert.IsFalse(attempt.Success);
}
}
}

View File

@@ -0,0 +1,84 @@
using System.Runtime.Remoting.Messaging;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class CallContextTests
{
private static bool _first;
static CallContextTests()
{
SafeCallContext.Register(() =>
{
CallContext.FreeNamedDataSlot("test1");
CallContext.FreeNamedDataSlot("test2");
return null;
}, o => {});
}
[TestFixtureSetUp]
public void SetUpFixture()
{
_first = true;
}
// logical call context leaks between tests
// is is required to clear it before tests begin
// (don't trust other tests properly tearing down)
[SetUp]
public void Setup()
{
SafeCallContext.Clear();
}
[TearDown]
public void TearDown()
{
SafeCallContext.Clear();
}
[Test]
public void Test1()
{
CallContext.LogicalSetData("test1", "test1");
Assert.IsNull(CallContext.LogicalGetData("test2"));
CallContext.LogicalSetData("test3b", "test3b");
if (_first)
{
_first = false;
}
else
{
Assert.IsNotNull(CallContext.LogicalGetData("test3a")); // leak!
}
}
[Test]
public void Test2()
{
CallContext.LogicalSetData("test2", "test2");
Assert.IsNull(CallContext.LogicalGetData("test1"));
}
[Test]
public void Test3()
{
CallContext.LogicalSetData("test3a", "test3a");
if (_first)
{
_first = false;
}
else
{
Assert.IsNotNull(CallContext.LogicalGetData("test3b")); // leak!
}
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using Lucene.Net.Index;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class DelegateExtensionsTests
{
[Test]
public void Only_Executes_Specific_Count()
{
const int maxTries = 5;
var totalTries = 0;
DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) =>
{
totalTries = currentTry;
return Attempt<IndexWriter>.Fail();
}, 5, TimeSpan.FromMilliseconds(10));
Assert.AreEqual(maxTries, totalTries);
}
[Test]
public void Quits_On_Success_Count()
{
var totalTries = 0;
DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) =>
{
totalTries = currentTry;
return totalTries == 2 ? Attempt<string>.Succeed() : Attempt<string>.Fail();
}, 5, TimeSpan.FromMilliseconds(10));
Assert.AreEqual(2, totalTries);
}
}
}

View File

@@ -0,0 +1,197 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class EnumerableExtensionsTests
{
[Test]
public void Unsorted_Sequence_Equal()
{
var list1 = new[] { 1, 2, 3, 4, 5, 6 };
var list2 = new[] { 6, 5, 3, 2, 1, 4 };
var list3 = new[] { 6, 5, 4, 3, 2, 2 };
Assert.IsTrue(list1.UnsortedSequenceEqual(list2));
Assert.IsTrue(list2.UnsortedSequenceEqual(list1));
Assert.IsFalse(list1.UnsortedSequenceEqual(list3));
Assert.IsTrue(((IEnumerable<object>)null).UnsortedSequenceEqual(null));
Assert.IsFalse(((IEnumerable<int>)null).UnsortedSequenceEqual(list1));
Assert.IsFalse(list1.UnsortedSequenceEqual(null));
}
[Test]
public void Contains_All()
{
var list1 = new[] {1, 2, 3, 4, 5, 6};
var list2 = new[] {6, 5, 3, 2, 1, 4};
var list3 = new[] {6, 5, 4, 3};
Assert.IsTrue(list1.ContainsAll(list2));
Assert.IsTrue(list2.ContainsAll(list1));
Assert.IsTrue(list1.ContainsAll(list3));
Assert.IsFalse(list3.ContainsAll(list1));
}
[Test]
public void Flatten_List_2()
{
var hierarchy = new TestItem
{
Children = new List<TestItem>
{
new TestItem(),
new TestItem(),
new TestItem()
}
};
#pragma warning disable CS0618 // Type or member is obsolete
var flattened = hierarchy.Children.FlattenList(x => x.Children);
#pragma warning restore CS0618 // Type or member is obsolete
var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children);
Assert.AreEqual(3, flattened.Count());
Assert.AreEqual(3, selectRecursive.Count());
Assert.IsTrue(flattened.SequenceEqual(selectRecursive));
}
[Test]
public void Flatten_List()
{
var hierarchy = new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>
{
new TestItem(),
new TestItem()
}
}
}
}
}
},
new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>()
}
}
},
new TestItem
{
Children = new List<TestItem>
{
new TestItem
{
Children = new List<TestItem>()
}
}
}
}
}
}
};
#pragma warning disable CS0618 // Type or member is obsolete
var flattened = hierarchy.Children.FlattenList(x => x.Children);
#pragma warning restore CS0618 // Type or member is obsolete
var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children);
Assert.AreEqual(10, flattened.Count());
Assert.AreEqual(10, selectRecursive.Count());
Assert.IsTrue(flattened.SequenceEqual(selectRecursive));
}
private class TestItem
{
public TestItem()
{
Children = Enumerable.Empty<TestItem>();
}
public IEnumerable<TestItem> Children { get; set; }
}
[Test]
public void InGroupsOf_ReturnsAllElements()
{
var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
var groupsOfTwo = integers.InGroupsOf(2).ToArray();
var flattened = groupsOfTwo.SelectMany(x => x).ToArray();
Assert.That(groupsOfTwo.Length, Is.EqualTo(5));
Assert.That(flattened.Length, Is.EqualTo(integers.Length));
CollectionAssert.AreEquivalent(integers, flattened);
var groupsOfMassive = integers.InGroupsOf(100).ToArray();
Assert.That(groupsOfMassive.Length, Is.EqualTo(1));
flattened = groupsOfMassive.SelectMany(x => x).ToArray();
Assert.That(flattened.Length, Is.EqualTo(integers.Length));
CollectionAssert.AreEquivalent(integers, flattened);
}
[Test]
public void InGroupsOf_CanRepeat()
{
var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
var inGroupsOf = integers.InGroupsOf(2);
Assert.AreEqual(5, inGroupsOf.Count());
Assert.AreEqual(5, inGroupsOf.Count()); // again
}
[TestCase]
public void DistinctBy_ReturnsDistinctElements_AndResetsIteratorCorrectly()
{
// Arrange
var tuple1 = new System.Tuple<string, string>("fruit", "apple");
var tuple2 = new System.Tuple<string, string>("fruit", "orange");
var tuple3 = new System.Tuple<string, string>("fruit", "banana");
var tuple4 = new System.Tuple<string, string>("fruit", "banana"); // Should be filtered out
var list = new List<System.Tuple<string, string>>()
{
tuple1,
tuple2,
tuple3,
tuple4
};
// Act
var iteratorSource = list.DistinctBy(x => x.Item2);
// Assert
// First check distinction
Assert.AreEqual(3, iteratorSource.Count());
// Check for iterator block mistakes - reset to original query first
iteratorSource = list.DistinctBy(x => x.Item2);
Assert.AreEqual(iteratorSource.Count(), iteratorSource.ToList().Count());
}
}
}

View File

@@ -0,0 +1,185 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web.UI.WebControls;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class ObjectExtensionsTests
{
private CultureInfo _savedCulture;
[SetUp]
public void TestSetup()
{
_savedCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // make sure the dates parse correctly
}
[TearDown]
public void TestTearDown()
{
Thread.CurrentThread.CurrentCulture = _savedCulture;
}
[Test]
public void CanParseStringToUnit()
{
const string stringUnit = "1234px";
object objUnit = "1234px";
var result = stringUnit.TryConvertTo<Unit>();
var result2 = objUnit.TryConvertTo<Unit>();
var unit = new Unit("1234px");
Assert.IsTrue(result.Success);
Assert.IsTrue(result2.Success);
Assert.AreEqual(unit, result.Result);
Assert.AreEqual(unit, result2.Result);
}
[Test]
public void Can_Convert_List_To_Enumerable()
{
var list = new List<string> {"hello", "world", "awesome"};
var result = list.TryConvertTo<IEnumerable<string>>();
Assert.IsTrue(result.Success);
Assert.AreEqual(3, result.Result.Count());
}
[Test]
public void ObjectExtensions_Object_To_Dictionary()
{
//Arrange
var obj = new { Key1 = "value1", Key2 = "value2", Key3 = "value3" };
//Act
var d = obj.ToDictionary<string>();
//Assert
Assert.IsTrue(d.Keys.Contains("Key1"));
Assert.IsTrue(d.Keys.Contains("Key2"));
Assert.IsTrue(d.Keys.Contains("Key3"));
Assert.AreEqual(d["Key1"], "value1");
Assert.AreEqual(d["Key2"], "value2");
Assert.AreEqual(d["Key3"], "value3");
}
[Test]
public void CanConvertIntToNullableInt()
{
var i = 1;
var result = i.TryConvertTo<int?>();
Assert.That(result.Success, Is.True);
}
[Test]
public void CanConvertNullableIntToInt()
{
int? i = 1;
var result = i.TryConvertTo<int>();
Assert.That(result.Success, Is.True);
}
[Test]
public virtual void CanConvertStringToBool()
{
var testCases = new Dictionary<string, bool>
{
{"TRUE", true},
{"True", true},
{"true", true},
{"1", true},
{"FALSE", false},
{"False", false},
{"false", false},
{"0", false},
{"", false}
};
foreach (var testCase in testCases)
{
var result = testCase.Key.TryConvertTo<bool>();
Assert.IsTrue(result.Success, testCase.Key);
Assert.AreEqual(testCase.Value, result.Result, testCase.Key);
}
}
[TestCase("2012-11-10", true)]
[TestCase("2012/11/10", true)]
[TestCase("10/11/2012", true)]// assuming your culture uses DD/MM/YYYY
[TestCase("11/10/2012", false)]// assuming your culture uses DD/MM/YYYY
[TestCase("Sat 10, Nov 2012", true)]
[TestCase("Saturday 10, Nov 2012", true)]
[TestCase("Sat 10, November 2012", true)]
[TestCase("Saturday 10, November 2012", true)]
[TestCase("2012-11-10 13:14:15", true)]
[TestCase("2012-11-10T13:14:15Z", true)]
public virtual void CanConvertStringToDateTime(string date, bool outcome)
{
var dateTime = new DateTime(2012, 11, 10, 13, 14, 15);
var result = date.TryConvertTo<DateTime>();
Assert.IsTrue(result.Success, date);
Assert.AreEqual(DateTime.Equals(dateTime.Date, result.Result.Date), outcome, date);
}
[Test]
public virtual void CanConvertBlankStringToNullDateTime()
{
var result = "".TryConvertTo<DateTime?>();
Assert.IsTrue(result.Success);
Assert.IsNull(result.Result);
}
[Test]
public virtual void CanConvertBlankStringToNullBool()
{
var result = "".TryConvertTo<bool?>();
Assert.IsTrue(result.Success);
Assert.IsNull(result.Result);
}
[Test]
public virtual void CanConvertBlankStringToDateTime()
{
var result = "".TryConvertTo<DateTime>();
Assert.IsTrue(result.Success);
Assert.AreEqual(DateTime.MinValue, result.Result);
}
[Test]
public virtual void CanConvertObjectToString_Using_ToString_Overload()
{
var result = new MyTestObject().TryConvertTo<string>();
Assert.IsTrue(result.Success);
Assert.AreEqual("Hello world", result.Result);
}
[Test]
public virtual void CanConvertObjectToSameObject()
{
var obj = new MyTestObject();
var result = obj.TryConvertTo<object>();
Assert.AreEqual(obj, result.Result);
}
private class MyTestObject
{
public override string ToString()
{
return "Hello world";
}
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Strings;
using Umbraco.Tests.TestHelpers;
using Umbraco.Core.DI;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class TryConvertToTests : UmbracoTestBase
{
public override void SetUp()
{
base.SetUp();
var settings = SettingsForTests.GetDefault();
// fixme - base should do it!
Container.RegisterSingleton<IShortStringHelper>(_ => new DefaultShortStringHelper(settings));
}
[Test]
public void ConvertToIntegerTest()
{
var conv = "100".TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
conv = "100.000".TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
conv = "100,000".TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
// oops
conv = "100.001".TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
conv = 100m.TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
conv = 100.000m.TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
// oops
conv = 100.001m.TryConvertTo<int>();
Assert.IsTrue(conv);
Assert.AreEqual(100, conv.Result);
}
[Test]
public void ConvertToDecimalTest()
{
var conv = "100".TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
conv = "100.000".TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
conv = "100,000".TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
conv = "100.001".TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100.001m, conv.Result);
conv = 100m.TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
conv = 100.000m.TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
conv = 100.001m.TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100.001m, conv.Result);
conv = 100.TryConvertTo<decimal>();
Assert.IsTrue(conv);
Assert.AreEqual(100m, conv.Result);
}
[Test]
public void ConvertToDateTimeTest()
{
var conv = "2016-06-07".TryConvertTo<DateTime>();
Assert.IsTrue(conv);
Assert.AreEqual(new DateTime(2016, 6, 7), conv.Result);
}
}
}

View File

@@ -0,0 +1,218 @@
using System;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.IO;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class UriExtensionsTests
{
private string _root;
[SetUp]
public void SetUp()
{
_root = SystemDirectories.Root;
}
[TearDown]
public void TearDown()
{
SystemDirectories.Root = _root;
}
[TestCase("http://www.domain.com/umbraco", "", true)]
[TestCase("http://www.domain.com/Umbraco/", "", true)]
[TestCase("http://www.domain.com/umbraco/default.aspx", "", true)]
[TestCase("http://www.domain.com/umbraco/test/test", "", false)]
[TestCase("http://www.domain.com/umbraco/test/test/test", "", false)]
[TestCase("http://www.domain.com/Umbraco/test/test.aspx", "", true)]
[TestCase("http://www.domain.com/umbraco/test/test.js", "", true)]
[TestCase("http://www.domain.com/umbrac", "", false)]
[TestCase("http://www.domain.com/test", "", false)]
[TestCase("http://www.domain.com/test/umbraco", "", false)]
[TestCase("http://www.domain.com/test/umbraco.aspx", "", false)]
[TestCase("http://www.domain.com/Umbraco/restServices/blah", "", true)]
[TestCase("http://www.domain.com/Umbraco/Backoffice/blah", "", true)]
[TestCase("http://www.domain.com/Umbraco/anything", "", true)]
[TestCase("http://www.domain.com/Umbraco/anything/", "", true)]
[TestCase("http://www.domain.com/Umbraco/surface/blah", "", false)]
[TestCase("http://www.domain.com/umbraco/api/blah", "", false)]
[TestCase("http://www.domain.com/myvdir/umbraco/api/blah", "myvdir", false)]
[TestCase("http://www.domain.com/MyVdir/umbraco/api/blah", "/myvdir", false)]
[TestCase("http://www.domain.com/MyVdir/Umbraco/", "myvdir", true)]
[TestCase("http://www.domain.com/MyVdir/Umbraco/restServices/blah", "/myvdir", true)]
[TestCase("http://www.domain.com/umbraco/webservices/legacyAjaxCalls.asmx/js", "", true)]
[TestCase("http://www.domain.com/umbraco/test/legacyAjaxCalls.ashx?some=query&blah=js", "", true)]
public void Is_Back_Office_Request(string input, string virtualPath, bool expected)
{
SystemDirectories.Root = virtualPath;
var source = new Uri(input);
Assert.AreEqual(expected, source.IsBackOfficeRequest(virtualPath));
}
[TestCase("http://www.domain.com/install", true)]
[TestCase("http://www.domain.com/Install/", true)]
[TestCase("http://www.domain.com/install/default.aspx", true)]
[TestCase("http://www.domain.com/install/test/test", true)]
[TestCase("http://www.domain.com/Install/test/test.aspx", true)]
[TestCase("http://www.domain.com/install/test/test.js", true)]
[TestCase("http://www.domain.com/instal", false)]
[TestCase("http://www.domain.com/umbraco", false)]
[TestCase("http://www.domain.com/umbraco/umbraco", false)]
[TestCase("http://www.domain.com/test/umbraco.aspx", false)]
public void Is_Installer_Request(string input, bool expected)
{
var source = new Uri(input);
Assert.AreEqual(expected, source.IsInstallerRequest());
}
[TestCase("http://www.domain.com/foo/bar", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar#hop", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar?q=2#hop", "/", "http://www.domain.com/?q=2")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
[TestCase("http://www.domain.com/foo#bang", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3#bang", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
public void RewritePath(string input, string path, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", typeof(ArgumentException))]
[TestCase("http://www.domain.com", "path/to/page/", typeof(ArgumentException))]
public void RewritePath_Exceptions(string input, string path, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path);
});
}
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
public void RewritePathAndQuery(string input, string path, string query, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path, query);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", "", typeof(ArgumentException))]
[TestCase("http://www.domain.com/", "/path/to/page/", "x=27", typeof(ArgumentException))]
public void RewritePathAndQuery_Exceptions(string input, string path, string query, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path, query);
});
}
[TestCase("http://www.domain.com", "/")]
[TestCase("http://www.domain.com/", "/")]
[TestCase("http://www.domain.com/foo", "/foo")]
[TestCase("http://www.domain.com/foo/", "/foo/")]
[TestCase("http://www.domain.com/foo/bar", "/foo/bar")]
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar%20nix")]
[TestCase("http://www.domain.com/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/", "/")]
[TestCase("/foo", "/foo")]
[TestCase("/foo/", "/foo/")]
[TestCase("/foo/bar", "/foo/bar")]
[TestCase("/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/foo%20bar/pof", "/foo%20bar/pof")]
public void GetSafeAbsolutePath(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePath();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
public void GetAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
[TestCase("/foo%20bar/pof", "/foo bar/pof")]
public void GetSafeAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to/?q=3")]
public void EndPathWithSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.EndPathWithSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to?q=3")]
public void TrimPathEndSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.TrimPathEndSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("/foo/bar", "http://www.domain.com", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang", "http://www.domain.com/foo/bar")]
[TestCase("/", "http://www.domain.com/dang/dang", "http://www.domain.com/")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar?k=6#yop", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar?k=6")]
public void MakeAbsolute(string input, string reference, string expected)
{
var source = new Uri(input, UriKind.Relative);
var absolute = new Uri(reference);
var output = source.MakeAbsolute(absolute);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com:666/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com:666", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
public void WithoutPort(string input, string expected)
{
var source = new Uri(input);
var output = source.WithoutPort();
Assert.AreEqual(expected, output.ToString());
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class VersionExtensionTests
{
[TestCase(1, 0, 0, 0, "0.2147483647.2147483647.2147483647")]
[TestCase(1, 1, 0, 0, "1.0.2147483647.2147483647")]
[TestCase(1, 1, 1, 0, "1.1.0.2147483647")]
[TestCase(1, 1, 1, 1, "1.1.1.0")]
[TestCase(0, 1, 0, 0, "0.0.2147483647.2147483647")]
[TestCase(0, 1, 1, 0, "0.1.0.2147483647")]
[TestCase(0, 1, 1, 1, "0.1.1.0")]
[TestCase(0, 0, 1, 0, "0.0.0.2147483647")]
[TestCase(0, 0, 1, 1, "0.0.1.0")]
[TestCase(0, 0, 0, 1, "0.0.0.0")]
[TestCase(7, 3, 0, 0, "7.2.2147483647.2147483647")]
public void Subract_Revision(int major, int minor, int build, int rev, string outcome)
{
var version = new Version(major, minor, build, rev);
var result = version.SubtractRevision();
Assert.AreEqual(new Version(outcome), result);
}
}
}

View File

@@ -0,0 +1,48 @@
using System.Xml;
using System.Xml.Linq;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.CoreThings
{
[TestFixture]
public class XmlExtensionsTests
{
[Test]
public void XCDataToXmlNode()
{
var cdata = new XElement("test", new XCData("hello world"));
var xdoc = new XmlDocument();
var xmlNode = cdata.GetXmlNode(xdoc);
Assert.AreEqual(xmlNode.InnerText, "hello world");
}
[Test]
public void XTextToXmlNode()
{
var cdata = new XElement("test", new XText("hello world"));
var xdoc = new XmlDocument();
var xmlNode = cdata.GetXmlNode(xdoc);
Assert.AreEqual(xmlNode.InnerText, "hello world");
}
[Test]
public void ToXmlNodeIsNonDestructive()
{
const string xml = "<root><foo attr=\"123\">hello</foo><bar>world</bar></root>";
var cdata = new XElement("test", new XText("hello world"));
var xdoc = new XmlDocument();
xdoc.LoadXml(xml);
var xmlNode = cdata.GetXmlNode(xdoc);
Assert.AreEqual(xmlNode.InnerText, "hello world");
Assert.AreEqual(xml, xdoc.OuterXml);
}
}
}