1.0.0 release
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// PatternClosureTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("Pattern.closure")
|
||||
struct PatternClosureTests {
|
||||
|
||||
@Test func closureValuesAreYielded() {
|
||||
var counter: Int = 0
|
||||
let pat: Pattern<Int> = Pattern.closure {
|
||||
counter += 1
|
||||
return counter < 4 ? counter : nil
|
||||
}
|
||||
#expect(pat.next() == 1)
|
||||
#expect(pat.next() == 2)
|
||||
#expect(pat.next() == 3)
|
||||
#expect(pat.next() == nil)
|
||||
#expect(pat.isExhausted)
|
||||
}
|
||||
|
||||
@Test func nilLatchesExhaustion() {
|
||||
var callCount: Int = 0
|
||||
let pat: Pattern<Int> = Pattern.closure {
|
||||
callCount += 1
|
||||
return nil
|
||||
}
|
||||
#expect(pat.next() == nil)
|
||||
#expect(pat.next() == nil) // does not invoke body again
|
||||
#expect(pat.next() == nil)
|
||||
#expect(callCount == 1, "Body should be called once; then latched.")
|
||||
}
|
||||
|
||||
@Test func resetClearsLatchAndAllowsRetry() {
|
||||
var counter: Int = 0
|
||||
let pat: Pattern<Int> = Pattern.closure {
|
||||
counter += 1
|
||||
return counter < 2 ? counter : nil
|
||||
}
|
||||
#expect(pat.next() == 1)
|
||||
#expect(pat.next() == nil)
|
||||
#expect(pat.isExhausted)
|
||||
pat.reset()
|
||||
#expect(!pat.isExhausted)
|
||||
// Closure state itself is not reset — counter is now 2, so body returns nil
|
||||
// → latch fires again immediately. This is the documented behaviour.
|
||||
#expect(pat.next() == nil)
|
||||
#expect(pat.isExhausted)
|
||||
}
|
||||
|
||||
@Test func capturedStateSurvivesBetweenCalls() {
|
||||
var fib: (Int, Int) = (0, 1)
|
||||
let pat: Pattern<Int> = Pattern.closure {
|
||||
let v: Int = fib.0
|
||||
fib = (fib.1, fib.0 + fib.1)
|
||||
return v
|
||||
}
|
||||
#expect(pat.next() == 0)
|
||||
#expect(pat.next() == 1)
|
||||
#expect(pat.next() == 1)
|
||||
#expect(pat.next() == 2)
|
||||
#expect(pat.next() == 3)
|
||||
#expect(pat.next() == 5)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user