43 lines
1.2 KiB
Swift
43 lines
1.2 KiB
Swift
// PatternFilterTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Pattern.filter")
|
|
struct PatternFilterTests {
|
|
|
|
@Test func keepsValuesPassingPredicate() {
|
|
let pat: Pattern<Int> = Pattern.filter(
|
|
Pattern.sequence([1, 2, 3, 4, 5, 6], repeats: 1),
|
|
where: { $0 % 2 == 0 }
|
|
)
|
|
var collected: [Int] = []
|
|
while let v: Int = pat.next() { collected.append(v) }
|
|
#expect(collected == [2, 4, 6])
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func emptyResultWhenPredicateNeverMatches() {
|
|
let pat: Pattern<Int> = Pattern.filter(
|
|
Pattern.sequence([1, 3, 5], repeats: 1),
|
|
where: { $0 % 2 == 0 }
|
|
)
|
|
#expect(pat.next() == nil)
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func passThroughWhenPredicateAlwaysTrue() {
|
|
let pat: Pattern<Int> = Pattern.filter(
|
|
Pattern.sequence([7, 8, 9], repeats: 1),
|
|
where: { _ in true }
|
|
)
|
|
var collected: [Int] = []
|
|
while let v: Int = pat.next() { collected.append(v) }
|
|
#expect(collected == [7, 8, 9])
|
|
}
|
|
}
|