✅ testing - Test Framework

Unit Tests, Assertions, Mocking, Coverage

52
Fonksiyon
712
Satır
22 KB
Boyut

🚀 Hızlı Başlangıç

içe_aktar testing

test "toplama fonksiyonu" yap
    değişken sonuç = topla(2, 3)
    testing.assert_eq(sonuç, 5)
son

test "bölme sıfıra" yap
    testing.assert_panic(fonksiyon() yap
        değişken x = böl(10, 0)
    son)
son

// Test suite
test_grubu "Matematik İşlemleri" yap
    test "toplama" yap
        testing.assert_eq(topla(1, 1), 2)
        testing.assert_eq(topla(-1, 1), 0)
    son
    
    test "çıkarma" yap
        testing.assert_eq(çıkar(5, 3), 2)
        testing.assert_ne(çıkar(5, 3), 3)
    son
    
    test "çarpma" yap
        testing.assert_eq(çarp(2, 3), 6)
        testing.assert_eq(çarp(-2, 3), -6)
    son
son

📚 Assertions

💡 Örnekler

Comprehensive Test Suite

içe_aktar testing, collections

// Test edilen fonksiyonlar
fonksiyon stack_new() -> Stack yap
    dön yap içerik: [] son
son

fonksiyon stack_push(s: Stack, value: herhangi) yap
    s.içerik.ekle(value)
son

fonksiyon stack_pop(s: Stack) -> Seçenek yap
    eğer s.içerik.uzunluk() > 0 ise yap
        dön Bazı(s.içerik.pop())
    son değilse yap
        dön Hiç
    son
son

// Test suite
test_grubu "Stack Veri Yapısı" yap
    test "yeni stack boş olmalı" yap
        değişken s = stack_new()
        testing.assert_eq(s.içerik.uzunluk(), 0)
    son
    
    test "push eleman eklemeli" yap
        değişken s = stack_new()
        stack_push(s, 10)
        stack_push(s, 20)
        testing.assert_eq(s.içerik.uzunluk(), 2)
    son
    
    test "pop son elemanı çıkarmalı" yap
        değişken s = stack_new()
        stack_push(s, 10)
        stack_push(s, 20)
        
        değişken popped = stack_pop(s)
        testing.assert_some(popped)
        testing.assert_eq(popped.çıkar(), 20)
        testing.assert_eq(s.içerik.uzunluk(), 1)
    son
    
    test "boş stack'ten pop Hiç dönmeli" yap
        değişken s = stack_new()
        değişken popped = stack_pop(s)
        testing.assert_none(popped)
    son
    
    test "LIFO sırası korunmalı" yap
        değişken s = stack_new()
        stack_push(s, 1)
        stack_push(s, 2)
        stack_push(s, 3)
        
        testing.assert_eq(stack_pop(s).çıkar(), 3)
        testing.assert_eq(stack_pop(s).çıkar(), 2)
        testing.assert_eq(stack_pop(s).çıkar(), 1)
    son
son

// Tüm testleri çalıştır
testing.run_all()

Property-Based Testing

içe_aktar testing, random

// Property: reverse(reverse(list)) == list
test "reverse tersine çevirme idempotent" yap
    her i içinde aralık(0, 100) için yap
        // Random liste oluştur
        değişken uzunluk = random.aralık(0, 50)
        değişken liste = []
        
        her j içinde aralık(0, uzunluk) için yap
            liste.ekle(random.aralık(0, 1000))
        son
        
        // Property kontrolü
        değişken ters1 = liste.reverse()
        değişken ters2 = ters1.reverse()
        
        testing.assert_eq(liste, ters2)
    son
son

// Property: sort(list) sıralı olmalı
test "sort sıralama doğruluğu" yap
    her i içinde aralık(0, 100) için yap
        değişken uzunluk = random.aralık(1, 50)
        değişken liste = []
        
        her j içinde aralık(0, uzunluk) için yap
            liste.ekle(random.aralık(0, 1000))
        son
        
        değişken sıralı = liste.sort()
        
        // Sıralı mı kontrol
        her k içinde aralık(0, sıralı.uzunluk() - 1) için yap
            testing.assert_true(sıralı[k] <= sıralı[k + 1])
        son
    son
son

← Tüm Modüller