1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This test file has been translated from swift/test/Parse/foreach_async.swift
import XCTest
final class ForeachAsyncTests: ParserTestCase {
func testForeachAsync1() {
assertParse(
"""
import _Concurrency
"""
)
}
func testForeachAsync2() {
assertParse(
"""
struct AsyncRange<Bound: Comparable & Strideable>: AsyncSequence, AsyncIteratorProtocol where Bound.Stride : SignedInteger {
var range: Range<Bound>.Iterator
typealias Element = Bound
mutating func next() async -> Element? { return range.next() }
func cancel() { }
func makeAsyncIterator() -> Self { return self }
}
"""
)
}
func testForeachAsync3() {
assertParse(
"""
struct AsyncIntRange<Int> : AsyncSequence, AsyncIteratorProtocol {
typealias Element = (Int, Int)
func next() async -> (Int, Int)? {}
func cancel() { }
typealias AsyncIterator = AsyncIntRange<Int>
func makeAsyncIterator() -> AsyncIntRange<Int> { return self }
}
"""
)
}
func testForeachAsync4() {
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
// Simple foreach loop, using the variable in the body
for await i in r {
sum = sum + i
}
}
"""
)
}
// Check scoping of variable introduced with foreach loop
// For-each loops with two variables and varying degrees of typedness
func testForeachAsync5() {
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await (i, j) in iir {
sum = sum + i + j
}
}
"""
)
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await (i, j) in iir {
sum = sum + i + j
}
}
"""
)
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await (i, j) : (Int, Int) in iir {
sum = sum + i + j
}
}
"""
)
}
// Parse errors
func testForeachAsync6() {
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await i 1️⃣r {
}
}
""",
diagnostics: [
DiagnosticSpec(message: "expected 'in' in 'for' statement", fixIts: ["insert 'in'"])
],
fixedSource: """
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await i in r {
}
}
"""
)
assertParse(
"""
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await i in r 1️⃣sum = sum + i;2️⃣
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '{' in 'for' statement", fixIts: ["insert '{'"]),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '}' to end 'for' statement", fixIts: ["insert '}'"]),
],
fixedSource: """
func for_each(r: AsyncRange<Int>, iir: AsyncIntRange<Int>) async {
var sum = 0
for await i in r { sum = sum + i;
}
}
"""
)
}
}
|