TestConsole

This commit is contained in:
Cyberboss
2018-10-04 16:45:40 -04:00
parent 6b8291d9b5
commit 68ffe4baa6
4 changed files with 39 additions and 5 deletions
@@ -30,7 +30,7 @@ namespace Tgstation.Server.Host.Core
ConnectionString = connectionString
};
default:
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid database type ({0})!", databaseType));
throw new ArgumentOutOfRangeException(nameof(databaseType), databaseType, "Invalid DatabaseType!");
}
}
}
+8 -2
View File
@@ -12,7 +12,7 @@ namespace Tgstation.Server.Host.IO
public bool Available => Environment.UserInteractive;
/// <inheritdoc />
public Task PressAnyKeyAsync(CancellationToken cancellationToken) => Task.Factory.StartNew(() => System.Console.ReadKey(), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
public Task PressAnyKeyAsync(CancellationToken cancellationToken) => Task.Factory.StartNew(() => System.Console.Read(), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
/// <inheritdoc />
public Task<string> ReadLineAsync(bool usePasswordChar, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -50,7 +50,13 @@ namespace Tgstation.Server.Host.IO
/// <inheritdoc />
public Task WriteAsync(string text, bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
{
if (newLine)
if (text == null)
{
if (!newLine)
throw new InvalidOperationException("Cannot write null text without a new line!");
System.Console.WriteLine();
}
else if (newLine)
System.Console.WriteLine(text);
else
System.Console.Write(text);
@@ -7,14 +7,14 @@ using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Core.Tests
{
[TestClass]
public sealed class DBConnectionFactoryTests
public sealed class TestDBConnectionFactory
{
[TestMethod]
public void TestBadParameters()
{
var factory = new DBConnectionFactory();
Assert.ThrowsException<ArgumentNullException>(() => factory.CreateConnection(null, default));
Assert.ThrowsException<InvalidOperationException>(() => factory.CreateConnection(String.Empty, (DatabaseType)42));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => factory.CreateConnection(String.Empty, (DatabaseType)42));
}
[TestMethod]
@@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Tgstation.Server.Host.IO.Tests
{
[TestClass]
public sealed class TestConsole
{
[TestMethod]
public async Task TestWriteLine()
{
var console = new Console();
await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => console.WriteAsync(null, false, default)).ConfigureAwait(false);
await console.WriteAsync(null, true, default).ConfigureAwait(false);
await console.WriteAsync(String.Empty, false, default).ConfigureAwait(true);
}
[TestMethod]
public void TestUserInteractive()
{
var console = new Console();
Assert.AreEqual(Environment.UserInteractive, console.Available);
}
}
}