Skip to content

Commit 8c40106

Browse files
committed
[BuilderTransform] NFC: Add more pattern matching tests for result builders
1 parent 7a854ee commit 8c40106

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/Constraints/result_builder.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,64 @@ func getE(_ i: Int) -> E {
513513
}
514514
}
515515

516+
func test_labeled_splats() {
517+
enum E {
518+
case multi(a: String, b: String)
519+
case tuple((a: Int, b: Int))
520+
case single(result: Int)
521+
case single_multi(result: (a: Int, q: String))
522+
}
523+
524+
func test_answer(_: String) -> Int { 42 }
525+
func test_question(_: Int) -> String { "ultimate question" }
526+
527+
let e: E = E.single(result: 42)
528+
529+
tuplify(true) { _ in
530+
switch e {
531+
case .single(let result):
532+
test_question(result)
533+
case let .single_multi(result):
534+
test_answer(result.q)
535+
test_question(result.a)
536+
case let .multi(value): // tuple splat preserves labels
537+
test_answer(value.a)
538+
test_answer(value.b)
539+
case let .tuple(a: a, b: b): // un-splat preserves labels too
540+
test_question(a)
541+
test_question(b)
542+
}
543+
544+
// compound names still work with and without splat
545+
switch e {
546+
case .single(_): 42
547+
case .single_multi(result: (let a, let q)):
548+
test_answer(q)
549+
test_question(a)
550+
case let .multi(a: a, b: b):
551+
test_answer(a)
552+
test_answer(b)
553+
case let .tuple((a: a, b: b)):
554+
test_question(a)
555+
test_question(b)
556+
}
557+
558+
// no labels, no problem regardless of splatting
559+
switch e {
560+
case .single(_): 42
561+
case let .single_multi(result: (a, q)):
562+
test_question(a)
563+
test_answer(q)
564+
case let .multi(a, b):
565+
test_answer(a)
566+
test_answer(b)
567+
case let .tuple((a, b)):
568+
test_question(a)
569+
test_question(b)
570+
}
571+
}
572+
}
573+
516574
tuplify(true) { c in
517575
"testIfLetMatching"
518576
if let theValue = getOptionalInt(c) {

0 commit comments

Comments
 (0)