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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 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
//
//===----------------------------------------------------------------------===//
final class SubscriptExprTests: PrettyPrintTestCase {
func testBasicSubscriptGetters() {
let input =
"""
let a = myCollection[index]
let a = myCollection[label: index]
let a = myCollection[index, default: someDefaultValue]
"""
let expected =
"""
let a = myCollection[index]
let a = myCollection[label: index]
let a = myCollection[
index, default: someDefaultValue]
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
func testBasicSubscriptSetters() {
let input =
"""
myCollection[index] = someValue
myCollection[label: index] = someValue
myCollection[index, default: someDefaultValue] = someValue
"""
let expected =
"""
myCollection[index] = someValue
myCollection[label: index] = someValue
myCollection[
index, default: someDefaultValue] =
someValue
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
func testSubscriptGettersWithTrailingClosures() {
let input =
"""
let a = myCollection[index] { $0 < $1 }
let a = myCollection[label: index] { arg1, arg2 in foo() }
let a = myCollection[index, default: someDefaultValue] { arg1, arg2 in foo() }
"""
let expected =
"""
let a = myCollection[index] { $0 < $1 }
let a = myCollection[label: index] {
arg1, arg2 in foo()
}
let a = myCollection[
index, default: someDefaultValue
] { arg1, arg2 in foo() }
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
func testSubscriptSettersWithTrailingClosures() {
let input =
"""
myCollection[index] { $0 < $1 } = someValue
myCollection[label: index] { arg1, arg2 in foo() } = someValue
myCollection[index, default: someDefaultValue] { arg1, arg2 in foo() } = someValue
"""
let expected =
"""
myCollection[index] { $0 < $1 } = someValue
myCollection[label: index] { arg1, arg2 in
foo()
} = someValue
myCollection[
index, default: someDefaultValue
] { arg1, arg2 in foo() } = someValue
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
func testGroupsTrailingComma() {
let input =
"""
myCollection[
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
bar: bar]
"""
let expected =
"""
myCollection[
image: useLongName
? image(named: .longNameImage)
: image(named: .veryLongNameImageZ),
bar: bar]
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
}
func testDiscretionaryLineBreakBeforeTrailingClosure() {
let input =
"""
foo[a, b, c]
{
blah()
}
foo[
a, b, c
]
{
blah()
}
foo[arg1, arg2, arg3, arg4, arg5, arg6, arg7]
{
blah()
}
foo[ab, arg1, arg2] {
blah()
}
"""
let expected =
"""
foo[a, b, c] {
blah()
}
foo[
a, b, c
] {
blah()
}
foo[
arg1, arg2, arg3,
arg4, arg5, arg6,
arg7
] {
blah()
}
foo[ab, arg1, arg2]
{
blah()
}
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}
}
|