|
| 1 | +public class SubscriptDeclTests: PrettyPrintTestCase { |
| 2 | + public func testBasicSubscriptDeclarations() { |
| 3 | + let input = |
| 4 | + """ |
| 5 | + struct MyStruct { |
| 6 | + subscript(index: Int) -> Int { |
| 7 | + return self.values[index] |
| 8 | + } |
| 9 | + subscript(row: Int, col: Int) -> Int { |
| 10 | + return self.values[row][col] |
| 11 | + } |
| 12 | + subscript(index: Int) -> Int { |
| 13 | + get { return self.value[index] } |
| 14 | + set(newValue) { self.value[index] = newValue } |
| 15 | + } |
| 16 | + } |
| 17 | + """ |
| 18 | + |
| 19 | + let expected = |
| 20 | + """ |
| 21 | + struct MyStruct { |
| 22 | + subscript(index: Int) -> Int { |
| 23 | + return self.values[index] |
| 24 | + } |
| 25 | + subscript(row: Int, col: Int) -> Int { |
| 26 | + return self.values[row][col] |
| 27 | + } |
| 28 | + subscript(index: Int) -> Int { |
| 29 | + get { return self.value[index] } |
| 30 | + set(newValue) { self.value[index] = newValue } |
| 31 | + } |
| 32 | + } |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 50) |
| 37 | + } |
| 38 | + |
| 39 | + public func testSubscriptGenerics() { |
| 40 | + let input = |
| 41 | + """ |
| 42 | + struct MyStruct { |
| 43 | + subscript<T>(index: T) -> Double { |
| 44 | + return 1.23 |
| 45 | + } |
| 46 | + subscript<S, T>(row: S, col: T) -> Double { |
| 47 | + return self.values[row][col] |
| 48 | + } |
| 49 | + subscript<LongTypeName1, LongTypeName2, LongTypeName3>( |
| 50 | + var1: LongTypeName1, |
| 51 | + var2: LongTypeName2, |
| 52 | + var3: LongTypeName3 |
| 53 | + ) -> Int { |
| 54 | + return self.values[var1][var2][var3] |
| 55 | + } |
| 56 | + } |
| 57 | + """ |
| 58 | + |
| 59 | + let expected = |
| 60 | + """ |
| 61 | + struct MyStruct { |
| 62 | + subscript<T>(index: T) -> Double { return 1.23 } |
| 63 | + subscript<S, T>(row: S, col: T) -> Double { |
| 64 | + return self.values[row][col] |
| 65 | + } |
| 66 | + subscript< |
| 67 | + LongTypeName1, |
| 68 | + LongTypeName2, |
| 69 | + LongTypeName3 |
| 70 | + >( |
| 71 | + var1: LongTypeName1, |
| 72 | + var2: LongTypeName2, |
| 73 | + var3: LongTypeName3 |
| 74 | + ) -> Int { |
| 75 | + return self.values[var1][var2][var3] |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + """ |
| 80 | + |
| 81 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 50) |
| 82 | + } |
| 83 | + |
| 84 | + public func testSubscriptGenericWhere() { |
| 85 | + let input = |
| 86 | + """ |
| 87 | + struct MyStruct { |
| 88 | + subscript<Elements: Collection, Element>( |
| 89 | + var1: Element, |
| 90 | + var2: Elements |
| 91 | + ) -> Double where Elements.Element == Element { |
| 92 | + return 1.23 |
| 93 | + } |
| 94 | + subscript<Elements: Collection, Element>( |
| 95 | + var1: Element, |
| 96 | + var2: Elements |
| 97 | + ) -> Double |
| 98 | + where |
| 99 | + Elements.Element == Element, |
| 100 | + Element: Equatable |
| 101 | + { return 1.23 } |
| 102 | + } |
| 103 | + """ |
| 104 | + |
| 105 | + let expected = |
| 106 | + """ |
| 107 | + struct MyStruct { |
| 108 | + subscript<Elements: Collection, Element>( |
| 109 | + var1: Element, |
| 110 | + var2: Elements |
| 111 | + ) -> Double where Elements.Element == Element { |
| 112 | + return 1.23 |
| 113 | + } |
| 114 | + subscript<Elements: Collection, Element>( |
| 115 | + var1: Element, |
| 116 | + var2: Elements |
| 117 | + ) -> Double |
| 118 | + where |
| 119 | + Elements.Element == Element, |
| 120 | + Element: Equatable |
| 121 | + { return 1.23 } |
| 122 | + } |
| 123 | +
|
| 124 | + """ |
| 125 | + |
| 126 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 50) |
| 127 | + } |
| 128 | + |
| 129 | + public func testSubscriptAttributes() { |
| 130 | + let input = |
| 131 | + """ |
| 132 | + struct MyStruct { |
| 133 | + @discardableResult subscript(index: Int) -> Int { |
| 134 | + let a = 123 |
| 135 | + return a |
| 136 | + } |
| 137 | + @discardableResult @objc subscript(index: Int) -> Int { |
| 138 | + let a = 123 |
| 139 | + return a |
| 140 | + } |
| 141 | + @discardableResult @objc @inlinable subscript(index: Int) -> Int { |
| 142 | + let a = 123 |
| 143 | + return a |
| 144 | + } |
| 145 | + @discardableResult |
| 146 | + @objc |
| 147 | + @inlinable |
| 148 | + @available(swift 4.0) |
| 149 | + subscript(index: Int) -> Int { |
| 150 | + let a = 123 |
| 151 | + return a |
| 152 | + } |
| 153 | + } |
| 154 | + """ |
| 155 | + |
| 156 | + let expected = |
| 157 | + """ |
| 158 | + struct MyStruct { |
| 159 | + @discardableResult subscript(index: Int) -> Int { |
| 160 | + let a = 123 |
| 161 | + return a |
| 162 | + } |
| 163 | + @discardableResult @objc subscript(index: Int) -> Int { |
| 164 | + let a = 123 |
| 165 | + return a |
| 166 | + } |
| 167 | + @discardableResult @objc @inlinable subscript(index: Int) -> Int { |
| 168 | + let a = 123 |
| 169 | + return a |
| 170 | + } |
| 171 | + @discardableResult |
| 172 | + @objc |
| 173 | + @inlinable |
| 174 | + @available(swift 4.0) |
| 175 | + subscript(index: Int) -> Int { |
| 176 | + let a = 123 |
| 177 | + return a |
| 178 | + } |
| 179 | + } |
| 180 | +
|
| 181 | + """ |
| 182 | + |
| 183 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 70) |
| 184 | + } |
| 185 | + |
| 186 | + public func testSubscriptFullWrap() { |
| 187 | + let input = |
| 188 | + """ |
| 189 | + struct MyStruct { |
| 190 | + @discardableResult @objc |
| 191 | + subscript<ManyElements: Collection, Element>(var1: Element, var2: ManyElements) -> ManyElements.Index? where ManyElements.Element == Element, Element: Equatable { |
| 192 | + get { |
| 193 | + let out = vals[var1][var2] |
| 194 | + return out |
| 195 | + } |
| 196 | + set(newValue) { |
| 197 | + let tmp = compute(newValue) |
| 198 | + vals[var1][var2] = tmp |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + """ |
| 203 | + |
| 204 | + let expected = |
| 205 | + """ |
| 206 | + struct MyStruct { |
| 207 | + @discardableResult |
| 208 | + @objc |
| 209 | + subscript< |
| 210 | + ManyElements: Collection, |
| 211 | + Element |
| 212 | + >( |
| 213 | + var1: Element, |
| 214 | + var2: ManyElements |
| 215 | + ) -> ManyElements.Index? |
| 216 | + where |
| 217 | + ManyElements.Element == |
| 218 | + Element, |
| 219 | + Element: Equatable |
| 220 | + { |
| 221 | + get { |
| 222 | + let out = vals[var1][var2] |
| 223 | + return out |
| 224 | + } |
| 225 | + set(newValue) { |
| 226 | + let tmp = compute(newValue) |
| 227 | + vals[var1][var2] = tmp |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +
|
| 232 | + """ |
| 233 | + |
| 234 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 34) |
| 235 | + } |
| 236 | +} |
0 commit comments