✅ testing - Test Framework
Unit Tests, Assertions, Mocking, Coverage
52
function
712
lines
22 KB
Boyut
🚀 Quick Start
import testing
test "toplama functionu" do
let Result = topla(2, 3)
testing.assert_eq(Result, 5)
end
test "bölme sıfıra" do
testing.assert_panic(function() do
let x = böl(10, 0)
end)
end
// Test suite
test_grubu "Matematik İşlemleri" do
test "toplama" do
testing.assert_eq(topla(1, 1), 2)
testing.assert_eq(topla(-1, 1), 0)
end
test "çıkarma" do
testing.assert_eq(çıkar(5, 3), 2)
testing.assert_ne(çıkar(5, 3), 3)
end
test "çarpma" do
testing.assert_eq(çarp(2, 3), 6)
testing.assert_eq(çarp(-2, 3), -6)
end
end
📚 Assertions
- assert_eq: Eşitlik kontrolü
- assert_ne: Eşit değil
- assert_true/false: Boolean kontrol
- assert_close: Yaklaşık eşitlik (float)
- assert_panic: Error fırlatma kontrolü
- assert_ok/err: Result type kontrolü
💡 Exampleler
Comprehensive Test Suite
import testing, collections
// Test edilen functionlar
function stack_new() -> Stack do
return do içerik: [] end
end
function stack_push(s: Stack, value: herhangi) do
s.içerik.ekle(value)
end
function stack_pop(s: Stack) -> Option do
if s.içerik.uzunluk() > 0 do
return Some(s.içerik.pop())
end else do
return Hiç
end
end
// Test suite
test_grubu "Stack Veri Yapısı" do
test "yeni stack boş olmalı" do
let s = stack_new()
testing.assert_eq(s.içerik.uzunluk(), 0)
end
test "push eleman eklemeli" do
let s = stack_new()
stack_push(s, 10)
stack_push(s, 20)
testing.assert_eq(s.içerik.uzunluk(), 2)
end
test "pop end elemanı çıkarmalı" do
let s = stack_new()
stack_push(s, 10)
stack_push(s, 20)
let popped = stack_pop(s)
testing.assert_some(popped)
testing.assert_eq(popped.çıkar(), 20)
testing.assert_eq(s.içerik.uzunluk(), 1)
end
test "boş stack'ten pop Hiç dönmeli" do
let s = stack_new()
let popped = stack_pop(s)
testing.assert_none(popped)
end
test "LIFO sırası korunmalı" do
let 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)
end
end
// Tüm testleri çalıştır
testing.run_all()
Property-Based Testing
import testing, random
// Property: reverse(reverse(list)) == list
test "reverse tersine çevirme idempotent" do
each i in aralık(0, 100) for do
// Random liste create
let uzunluk = random.aralık(0, 50)
let liste = []
each j in aralık(0, uzunluk) for do
liste.ekle(random.aralık(0, 1000))
end
// Property kontrolü
let ters1 = liste.reverse()
let ters2 = ters1.reverse()
testing.assert_eq(liste, ters2)
end
end
// Property: sort(list) sıralı olmalı
test "sort sıralama doğruluğu" do
each i in aralık(0, 100) for do
let uzunluk = random.aralık(1, 50)
let liste = []
each j in aralık(0, uzunluk) for do
liste.ekle(random.aralık(0, 1000))
end
let sıralı = liste.sort()
// Sıralı mı kontrol
each k in aralık(0, sıralı.uzunluk() - 1) for do
testing.assert_true(sıralı[k] <= sıralı[k + 1])
end
end
end