Basit Bir Test Framework Geliştirelim 4

2023-05-11

Assert sınıfını daha da geliştirelim. Şimdi, özel hata mesajları içeren Assert hata türleri ekleyelim. Bu, hataları daha iyi yönetmeyi ve test hatalarını daha iyi tanımlamayı sağlayacaktır.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

public class AssertFailedException : Exception
{
    public AssertFailedException(string message) : base(message) { }
}

public class AssertEqualFailedException : AssertFailedException
{
    public AssertEqualFailedException(object expected, object actual)
        : base($"Expected: {expected}, Actual: {actual}") { }
}

public class AssertNotEqualFailedException : AssertFailedException
{
    public AssertNotEqualFailedException(object value)
        : base($"Unexpected value: {value}") { }
}

public class AssertTrueFailedException : AssertFailedException
{
    public AssertTrueFailedException() : base("Expected condition to be true, but was false.") { }
}

public class AssertFalseFailedException : AssertFailedException
{
    public AssertFalseFailedException() : base("Expected condition to be false, but was true.") { }
}

public class AssertContainsFailedException : AssertFailedException
{
    public AssertContainsFailedException(object item)
        : base($"Expected collection to contain {item}, but it did not.") { }
}

public class AssertDoesNotContainFailedException : AssertFailedException
{
    public AssertDoesNotContainFailedException(object item)
        : base($"Expected collection to not contain {item}, but it did.") { }
}

public class AssertAllFailedException : AssertFailedException
{
    public AssertAllFailedException() : base("Not all items in the collection satisfied the condition.") { }
}

public class AssertAnyFailedException : AssertFailedException
{
    public AssertAnyFailedException() : base("No item in the collection satisfied the condition.") { }
}

public class AssertEventuallyFailedException : AssertFailedException
{
    public AssertEventuallyFailedException(int timeoutMilliseconds)
        : base($"Condition was not satisfied within {timeoutMilliseconds} milliseconds.") { }
}

public static class Assert
{
    // ... metodlar ...

    public static void True(bool condition)
    {
        if (!condition)
        {
            throw new AssertTrueFailedException();
        }
    }

    public static void False(bool condition)
    {
        if (condition)
        {
            throw new AssertFalseFailedException();
        }
    }

    // ... diğer metodlar ...
}

Bu kodda, her bir Assert metodu için özel bir hata türü oluşturduk. Her hata türü, belirli bir Assert metodunun başarısız olmasının özelleştirilmiş bir açıklamasını içerir. Bu, hata mesajlarını daha anlamlı hale getirir ve belirli bir Assert metodunun neden başarısız olduğunu daha kolay anlamamızı sağlar.



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 