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.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 