Buy Me A Coffee

Basit Bir Test Framework Geliştirelim 5

2023-05-11

Bu sefer, ‘Assert’ sınıfına ‘LessThan’, ‘LessThanOrEqual’, ‘GreaterThan’, ve ‘GreaterThanOrEqual’ işlevleri ekleyelim. Bu işlevler sayesinde, iki değerin birbirine göre sıralamasını kontrol edebiliriz. Ayrıca, her biri için özel hata türleri oluşturalım.

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

// ... önceki exception sınıfları ...

public class AssertLessThanFailedException : AssertFailedException
{
    public AssertLessThanFailedException(object value, object boundary)
        : base($"Expected {value} to be less than {boundary}, but it was not.") { }
}

public class AssertLessThanOrEqualFailedException : AssertFailedException
{
    public AssertLessThanOrEqualFailedException(object value, object boundary)
        : base($"Expected {value} to be less than or equal to {boundary}, but it was not.") { }
}

public class AssertGreaterThanFailedException : AssertFailedException
{
    public AssertGreaterThanFailedException(object value, object boundary)
        : base($"Expected {value} to be greater than {boundary}, but it was not.") { }
}

public class AssertGreaterThanOrEqualFailedException : AssertFailedException
{
    public AssertGreaterThanOrEqualFailedException(object value, object boundary)
        : base($"Expected {value} to be greater than or equal to {boundary}, but it was not.") { }
}

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

    public static void LessThan<T>(T value, T boundary) where T : IComparable
    {
        if (value.CompareTo(boundary) >= 0)
        {
            throw new AssertLessThanFailedException(value, boundary);
        }
    }

    public static void LessThanOrEqual<T>(T value, T boundary) where T : IComparable
    {
        if (value.CompareTo(boundary) > 0)
        {
            throw new AssertLessThanOrEqualFailedException(value, boundary);
        }
    }

    public static void GreaterThan<T>(T value, T boundary) where T : IComparable
    {
        if (value.CompareTo(boundary) <= 0)
        {
            throw new AssertGreaterThanFailedException(value, boundary);
        }
    }

    public static void GreaterThanOrEqual<T>(T value, T boundary) where T : IComparable
    {
        if (value.CompareTo(boundary) < 0)
        {
            throw new AssertGreaterThanOrEqualFailedException(value, boundary);
        }
    }

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

Bu yeni metodlar ve hata türleri, birim testlerinizin daha geniş bir yelpazede kontrol sağlamasına olanak sağlar. LessThan, LessThanOrEqual, GreaterThan ve GreaterThanOrEqual metodları sayesinde, bir değerin belirli bir sınırın altında veya üstünde olup olmadığını kontrol edebilirsiniz. Her bir hata türü, belirli bir karşılaştırma işleminin neden başarısız olduğunu net bir şekilde belirtir.



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 