1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:45:40 +00:00

Add MSTests project

This commit is contained in:
Oliver Booth 2020-04-18 14:41:53 +01:00
parent 90f0e7efd4
commit 686b915be8
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
11 changed files with 579 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\X10D\X10D.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
namespace X10D.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="BooleanExtensions"/>.
/// </summary>
[TestClass]
public class BooleanTests
{
/// <summary>
/// Tests for <see cref="BooleanExtensions.ToInt32"/>.
/// </summary>
[TestMethod]
public void ToInt32()
{
const bool a = true;
const bool b = false;
Assert.IsTrue(a);
Assert.IsFalse(b);
Assert.AreEqual(1, a.ToInt32());
Assert.AreEqual(0, b.ToInt32());
}
}
}

103
X10D.Tests/src/ByteTests.cs Normal file
View File

@ -0,0 +1,103 @@
namespace X10D.Tests
{
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="ByteExtensions"/>.
/// </summary>
[TestClass]
public class ByteTests
{
/// <summary>
/// Tests for <see cref="ByteExtensions.AsString"/>.
/// </summary>
[TestMethod]
public void AsString()
{
byte[] a = { 0x00, 0x73, 0xc6, 0xff };
Assert.AreEqual("00-73-C6-FF", a.AsString());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetInt16"/>.
/// </summary>
[TestMethod]
public void GetInt16()
{
byte[] a = { 0xF3, 0x3F };
Assert.AreEqual(16371, a.GetInt16());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetInt32"/>.
/// </summary>
[TestMethod]
public void GetInt32()
{
byte[] a = { 0xB0, 0x0B, 0x13, 0x5F };
Assert.AreEqual(1595083696, a.GetInt32());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetInt64"/>.
/// </summary>
[TestMethod]
public void GetInt64()
{
byte[] a = { 0xB0, 0x0B, 0x13, 0x50, 0x05, 0x31, 0xB0, 0x0B };
Assert.AreEqual(842227029206305712L, a.GetInt64());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetString(IEnumerable{byte})"/>.
/// </summary>
[TestMethod]
public void GetString()
{
byte[] a = { 0x48, 0xc3, 0xa9, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
Assert.AreEqual("Héllo World", a.GetString());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetString(IEnumerable{byte}, Encoding)"/>.
/// </summary>
[TestMethod]
public void GetStringAscii()
{
byte[] a = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64 };
Assert.AreEqual("Hello World", a.GetString(Encoding.ASCII));
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetUInt16"/>.
/// </summary>
[TestMethod]
public void GetUInt16()
{
byte[] a = { 0xF3, 0x3F };
Assert.AreEqual(16371, a.GetUInt16());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetUInt32"/>.
/// </summary>
[TestMethod]
public void GetUInt32()
{
byte[] a = { 0xB0, 0x0B, 0x13, 0x5F };
Assert.AreEqual(1595083696U, a.GetUInt32());
}
/// <summary>
/// Tests for <see cref="ByteExtensions.GetUInt64"/>.
/// </summary>
[TestMethod]
public void GetUInt64()
{
byte[] a = { 0xB0, 0x0B, 0x13, 0x50, 0x05, 0x31, 0xB0, 0x0B };
Assert.AreEqual(842227029206305712UL, a.GetUInt64());
}
}
}

View File

@ -0,0 +1,35 @@
namespace X10D.Tests
{
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="CharExtensions"/>.
/// </summary>
[TestClass]
public class CharTests
{
/// <summary>
/// Tests for <see cref="CharExtensions.Repeat"/>.
/// </summary>
[TestMethod]
public void Random()
{
var set = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
var random = set.Random(20);
Assert.IsTrue(random.All(c => Array.IndexOf(set, c) >= 0));
Assert.IsFalse(random.Any(c => Array.IndexOf(set, c) < -1));
}
/// <summary>
/// Tests for <see cref="CharExtensions.Repeat"/>.
/// </summary>
[TestMethod]
public void Repeat()
{
Assert.AreEqual("aaaaaaaaaa", 'a'.Repeat(10));
}
}
}

View File

@ -0,0 +1,22 @@
namespace X10D.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="ComparableExtensions"/>.
/// </summary>
[TestClass]
public class ComparableTests
{
/// <summary>
/// Tests for <see cref="ComparableExtensions.Between{T}"/>.
/// </summary>
[TestMethod]
public void Between()
{
Assert.IsTrue(5.Between(2, 7));
Assert.IsTrue(10.Between(9, 11));
Assert.IsFalse(100.Between(80, 99));
}
}
}

View File

@ -0,0 +1,58 @@
namespace X10D.Tests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="ConvertibleExtensions"/>.
/// </summary>
[TestClass]
public class ConvertibleTests
{
/// <summary>
/// Tests for <see cref="ConvertibleExtensions.To{T}"/>.
/// </summary>
[TestMethod]
public void To()
{
Assert.AreEqual(2, "2".To<int>());
Assert.AreEqual("12.5", 12.50.To<string>());
Assert.IsTrue("True".To<bool>());
}
/// <summary>
/// Tests for <see cref="ConvertibleExtensions.ToOrDefault{T}(System.IConvertible, System.IFormatProvider)"/>.
/// </summary>
[TestMethod]
public void ToOrDefault()
{
Assert.AreEqual(2, "2".ToOrDefault<int>());
Assert.AreEqual("12.5", 12.50.ToOrDefault<string>());
Assert.IsTrue("True".ToOrDefault<bool>());
Assert.ThrowsException<FormatException>(() => "Foo".ToOrDefault<double>());
Assert.IsTrue("1.5".ToOrDefault(out float f));
Assert.AreEqual(1.5f, f);
}
/// <summary>
/// Tests for <see cref="ConvertibleExtensions.ToOrOther{T}(System.IConvertible, T, System.IFormatProvider)"/>.
/// </summary>
[TestMethod]
public void ToOrOther()
{
Assert.AreEqual(2.0, "Foo".ToOrOther(2.0));
Assert.IsFalse("Foo".ToOrOther(out var d, 2.0));
Assert.AreEqual(2.0, d);
}
/// <summary>
/// Tests for <see cref="ConvertibleExtensions.ToOrNull{T}(System.IConvertible, System.IFormatProvider)"/>.
/// </summary>
[TestMethod]
public void ToOrNull()
{
Assert.IsFalse("foo".ToOrNull(out ConvertibleTests t));
Assert.IsNull(t);
}
}
}

View File

@ -0,0 +1,107 @@
namespace X10D.Tests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="DateTimeExtensions"/>.
/// </summary>
[TestClass]
public class DateTimeTests
{
/// <summary>
/// Tests for <see cref="DateTimeExtensions.Age(DateTime)"/>.
/// </summary>
[TestMethod]
public void Age()
{
// no choice but to create dynamic based on today's date.
// age varies with time
DateTime now = DateTime.Now;
var dt = new DateTime(now.Year - 18, 1, 1);
Assert.AreEqual(18, dt.Age());
}
/// <summary>
/// Tests for <see cref="DateTimeExtensions.First"/>.
/// </summary>
[TestMethod]
public void First()
{
var dt = new DateTime(2018, 6, 20);
Assert.AreEqual(4, dt.First(DayOfWeek.Monday).Day);
}
/// <summary>
/// Tests for <see cref="DateTimeExtensions.FirstDayOfMonth"/>.
/// </summary>
[TestMethod]
public void FirstDayOfMonth()
{
var dt = new DateTime(2018, 6, 20);
DateTime first = dt.FirstDayOfMonth();
Assert.AreEqual(dt.Year, first.Year);
Assert.AreEqual(dt.Month, first.Month);
Assert.AreEqual(1, first.Day);
}
/// <summary>
/// Tests for <see cref="DateTimeExtensions.Last"/>.
/// </summary>
[TestMethod]
public void Last()
{
{
var dt = new DateTime(2019, 12, 1);
DateTime last = dt.Last(DayOfWeek.Wednesday);
Assert.AreEqual(dt.Year, last.Year);
Assert.AreEqual(dt.Month, last.Month);
Assert.AreEqual(25, last.Day);
}
{
var dt = new DateTime(2020, 4, 14);
DateTime last = dt.Last(DayOfWeek.Friday);
Assert.AreEqual(dt.Year, last.Year);
Assert.AreEqual(dt.Month, last.Month);
Assert.AreEqual(24, last.Day);
last = dt.Last(DayOfWeek.Thursday);
Assert.AreEqual(dt.Year, last.Year);
Assert.AreEqual(dt.Month, last.Month);
Assert.AreEqual(30, last.Day);
}
}
/// <summary>
/// Tests for <see cref="DateTimeExtensions.LastDayOfMonth"/>.
/// </summary>
[TestMethod]
public void LastDayOfMonth()
{
var dt = new DateTime(2016, 2, 4);
DateTime last = dt.LastDayOfMonth();
Assert.AreEqual(dt.Year, last.Year);
Assert.AreEqual(dt.Month, last.Month);
Assert.AreEqual(29, last.Day); // 2016 is a leap year
}
/// <summary>
/// Tests for <see cref="DateTimeExtensions.ToUnixTimeStamp"/>.
/// </summary>
[TestMethod]
public void ToUnixTimestamp()
{
var dt = new DateTime(2015, 10, 21, 1, 0, 0, DateTimeKind.Utc);
var unix = dt.ToUnixTimeStamp();
Assert.AreEqual(1445389200L, unix);
}
}
}

View File

@ -0,0 +1,42 @@
namespace X10D.Tests
{
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="DictionaryExtensions"/>.
/// </summary>
[TestClass]
public class DictionaryTests
{
/// <summary>
/// Tests for <see cref="DictionaryExtensions.ToConnectionString{T1,T2}(IDictionary{T1,T2})"/>.
/// </summary>
[TestMethod]
public void ToConnectionString()
{
var dictionary = new Dictionary<string, object>
{
{ "username", "Foo" }, { "password", "Foo Bar" }, { "port", 3306 },
};
var connectionString = dictionary.ToConnectionString();
Assert.AreEqual("username=Foo;password=\"Foo Bar\";port=3306", connectionString);
}
/// <summary>
/// Tests for <see cref="DictionaryExtensions.ToGetParameters{T1,T2}(IDictionary{T1,T2})"/>.
/// </summary>
[TestMethod]
public void ToGetParameters()
{
var dictionary = new Dictionary<string, object>
{
{ "username", "Foo" }, { "password", "Foo Bar" }, { "port", 3306 },
};
var getParameterString = dictionary.ToGetParameters();
Assert.AreEqual("username=Foo&password=Foo+Bar&port=3306", getParameterString);
}
}
}

View File

@ -0,0 +1,84 @@
namespace X10D.Tests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="DoubleExtensions"/>.
/// </summary>
[TestClass]
public class DoubleTests
{
/// <summary>
/// Tests for <see cref="DoubleExtensions.Clamp"/>.
/// </summary>
[TestMethod]
public void Clamp()
{
Assert.AreEqual(2.0, 3.0.Clamp(1.0, 2.0));
Assert.AreEqual(1.0, (-3.0).Clamp(1.0, 2.0));
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.DegreesToRadians"/>.
/// </summary>
[TestMethod]
public void DegreesToRadians()
{
Assert.AreEqual(Math.PI, 180.0.DegreesToRadians());
Assert.AreEqual(Math.PI * 1.5, 270.0.DegreesToRadians());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.GetBytes"/>.
/// </summary>
[TestMethod]
public void GetBytes()
{
CollectionAssert.AreEqual(
new byte[] { 0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40 },
Math.PI.GetBytes());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.IsEven"/>.
/// </summary>
[TestMethod]
public void IsEven()
{
Assert.IsTrue(2.0.IsEven());
Assert.IsFalse(1.0.IsEven());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.IsOdd"/>.
/// </summary>
[TestMethod]
public void IsOdd()
{
Assert.IsFalse(2.0.IsOdd());
Assert.IsTrue(1.0.IsOdd());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.RadiansToDegrees"/>.
/// </summary>
[TestMethod]
public void RadiansToDegrees()
{
Assert.AreEqual(180.0, Math.PI.RadiansToDegrees());
Assert.AreEqual(360.0, (2.0 * Math.PI).RadiansToDegrees());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.Round"/>.
/// </summary>
[TestMethod]
public void Round()
{
Assert.AreEqual(5.0, 3.5.Round(5));
Assert.AreEqual(5.0, 7.0.Round(5));
Assert.AreEqual(10.0, 7.5.Round(5));
}
}
}

View File

@ -0,0 +1,55 @@
namespace X10D.Tests
{
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="EnumerableExtensions"/>.
/// </summary>
[TestClass]
public class EnumerableTests
{
/// <summary>
/// Tests for <see cref="EnumerableExtensions.Split{T}"/> using an array of <see cref="byte"/>.
/// </summary>
[TestMethod]
public void SplitByte()
{
byte[] foo = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
IEnumerable<IEnumerable<byte>> chunks = foo.Split(2).ToArray();
Assert.AreEqual(4, chunks.Count());
CollectionAssert.AreEqual(new byte[] { 0x01, 0x02 }, chunks.ElementAt(0).ToList());
CollectionAssert.AreEqual(new byte[] { 0x03, 0x04 }, chunks.ElementAt(1).ToList());
CollectionAssert.AreEqual(new byte[] { 0x05, 0x06 }, chunks.ElementAt(2).ToList());
CollectionAssert.AreEqual(new byte[] { 0x07, 0x08 }, chunks.ElementAt(3).ToList());
// test exceeding chunk size
chunks = foo.Split(foo.Length + 10).ToArray();
Assert.AreEqual(1, chunks.Count());
CollectionAssert.AreEqual(foo, chunks.SelectMany(c => c).ToList());
}
/// <summary>
/// Tests for <see cref="EnumerableExtensions.Split{T}"/> using an array of <see cref="int"/>.
/// </summary>
[TestMethod]
public void SplitInt32()
{
int[] foo = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
IEnumerable<IEnumerable<int>> chunks = foo.Split(2).ToArray();
Assert.AreEqual(4, chunks.Count());
CollectionAssert.AreEqual(new[] { 0x01, 0x02 }, chunks.ElementAt(0).ToList());
CollectionAssert.AreEqual(new[] { 0x03, 0x04 }, chunks.ElementAt(1).ToList());
CollectionAssert.AreEqual(new[] { 0x05, 0x06 }, chunks.ElementAt(2).ToList());
CollectionAssert.AreEqual(new[] { 0x07, 0x08 }, chunks.ElementAt(3).ToList());
// test exceeding chunk size
chunks = foo.Split(foo.Length + 10).ToArray();
Assert.AreEqual(1, chunks.Count());
CollectionAssert.AreEqual(foo, chunks.SelectMany(c => c).ToList());
}
}
}

View File

@ -0,0 +1,20 @@
namespace X10D.Tests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TimeSpanParserTests
{
[TestMethod]
public void TestParser()
{
Assert.AreEqual(TimeSpan.FromHours(3), "3h".ToTimeSpan());
Assert.AreEqual(TimeSpan.FromMinutes(2.5), "2.5m".ToTimeSpan());
Assert.AreEqual(TimeSpan.FromHours(1), "60m".ToTimeSpan());
Assert.AreEqual(TimeSpan.FromDays(1), "1d".ToTimeSpan());
Assert.AreEqual(TimeSpan.FromDays(8), "1w 1d".ToTimeSpan());
Assert.AreEqual(TimeSpan.FromDays(8), "1w1d".ToTimeSpan());
}
}
}