Basit Bir Test Framework Geliştirelim 3
2023-05-11
Assert
sınıfımıza daha fazla method ekleyelim.
using System;
namespace SimpleTestFramework
{
public class Assert
{
public static void Equal(object expected, object actual, string message = null)
{
if (!expected.Equals(actual))
{
throw new Exception(message ?? $"Expected {expected}, but got {actual}");
}
}
public static void True(bool condition, string message = null)
{
if (!condition)
{
throw new Exception(message ?? "Expected condition to be true, but was false.");
}
}
public static void False(bool condition, string message = null)
{
if (condition)
{
throw new Exception(message ?? "Expected condition to be false, but was true.");
}
}
public static void Equal<T>(T expected, T actual, string message = null)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
{
throw new Exception(message ?? $"Expected values to be equal, but they were not. Expected: {expected}, Actual: {actual}");
}
}
public static void NotEqual<T>(T expected, T actual, string message = null)
{
if (EqualityComparer<T>.Default.Equals(expected, actual))
{
throw new Exception(message ?? $"Expected values to not be equal, but they were. Value: {expected}");
}
}
public static void Contains<T>(T expectedItem, IEnumerable<T> collection, string message = null)
{
if (!collection.Contains(expectedItem))
{
throw new Exception(message ?? $"Expected collection to contain {expectedItem}, but it did not.");
}
}
public static void DoesNotContain<T>(T unexpectedItem, IEnumerable<T> collection, string message = null)
{
if (collection.Contains(unexpectedItem))
{
throw new Exception(message ?? $"Expected collection to not contain {unexpectedItem}, but it did.");
}
}
public static void IsNull<T>(T obj, string message = null) where T : class
{
if (obj != null)
{
throw new Exception(message ?? $"Expected object to be null, but it was not.");
}
}
public static void NotNull<T>(T obj, string message = null) where T : class
{
if (obj == null)
{
throw new Exception(message ?? $"Expected object to not be null, but it was.");
}
}
public static void IsNull(object obj, string message = null)
{
if (obj != null)
{
throw new Exception(message ?? "Expected null, but got a value");
}
}
public static void IsNotNull(object obj, string message = null)
{
if (obj == null)
{
throw new Exception(message ?? "Expected a value, but got null");
}
}
public static void ThrowsException<T>(Action action) where T : Exception
{
try
{
action();
throw new Exception($"Expected exception of type {typeof(T).Name}, but no exception was thrown");
}
catch (T)
{
// Test passed
}
catch
{
throw new Exception($"Expected exception of type {typeof(T).Name}, but a different exception was thrown");
}
}
public static void All<T>(IEnumerable<T> collection, Predicate<T> condition, string message = null)
{
if (collection.Any(item => !condition(item)))
{
throw new Exception(message ?? "Not all items in the collection satisfied the condition.");
}
}
public static void Any<T>(IEnumerable<T> collection, Predicate<T> condition, string message = null)
{
if (collection.All(item => !condition(item)))
{
throw new Exception(message ?? "No item in the collection satisfied the condition.");
}
}
public static void Eventually(Func<bool> condition, int timeoutMilliseconds = 1000, string message = null)
{
var stopTime = DateTime.Now.AddMilliseconds(timeoutMilliseconds);
while (DateTime.Now < stopTime)
{
if (condition())
{
return;
}
Thread.Sleep(50);
}
throw new Exception(message ?? $"Condition was not satisfied within {timeoutMilliseconds} milliseconds.");
}
}
}
Bu methodların her biri, testlerimizde belirli bir durumun doğru olup olmadığını kontrol eder. Örneğin, IsTrue
methodu, bir durumun doğru olduğunu kontrol eder. Eğer durum doğru değilse, bir hata fırlatır ve test başarısız olur. Benzer şekilde,
ThrowsException
methodu, belirli bir türde bir istisna fırlatılıp fırlatılmadığını kontrol eder. Bu, testlerimizde beklenen hataları kontrol etmek için kullanılabilir.