|
| 1 | +import SwiftFormatConfiguration |
| 2 | + |
1 | 3 | public class AttributeTests: PrettyPrintTestCase {
|
2 | 4 | public func testAttributeParamSpacing() {
|
3 | 5 | let input =
|
| 6 | + """ |
| 7 | + @available( iOS 9.0,* ) |
| 8 | + func f() {} |
| 9 | + @available(*, unavailable ,renamed:"MyRenamedProtocol") |
| 10 | + func f() {} |
| 11 | + @available(iOS 10.0, macOS 10.12, *) |
| 12 | + func f() {} |
| 13 | + """ |
| 14 | + |
| 15 | + let expected = |
4 | 16 | """
|
5 | 17 | @available(iOS 9.0, *)
|
6 | 18 | func f() {}
|
7 | 19 | @available(*, unavailable, renamed: "MyRenamedProtocol")
|
8 | 20 | func f() {}
|
9 | 21 | @available(iOS 10.0, macOS 10.12, *)
|
10 | 22 | func f() {}
|
| 23 | +
|
| 24 | + """ |
| 25 | + |
| 26 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 60) |
| 27 | + } |
| 28 | + |
| 29 | + public func testAttributeBinPackedWrapping() { |
| 30 | + let input = |
| 31 | + """ |
| 32 | + @available(iOS 9.0, *) |
| 33 | + func f() {} |
| 34 | + @available(*,unavailable, renamed:"MyRenamedProtocol") |
| 35 | + func f() {} |
| 36 | + @available(iOS 10.0, macOS 10.12, *) |
| 37 | + func f() {} |
11 | 38 | """
|
12 | 39 |
|
13 | 40 | let expected =
|
14 | 41 | """
|
15 | 42 | @available(iOS 9.0, *)
|
16 | 43 | func f() {}
|
17 |
| - @available(*, unavailable, renamed: "MyRenamedProtocol") |
| 44 | + @available( |
| 45 | + *, unavailable, |
| 46 | + renamed: "MyRenamedProtocol" |
| 47 | + ) |
| 48 | + func f() {} |
| 49 | + @available( |
| 50 | + iOS 10.0, macOS 10.12, * |
| 51 | + ) |
| 52 | + func f() {} |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + // Attributes should wrap to avoid overflowing the line length, using the following priorities: |
| 57 | + // 1. Keep the entire attribute together, on 1 line. |
| 58 | + // 2. Otherwise, try to keep the entire attribute argument list together on 1 line. |
| 59 | + // 3. Otherwise, use argument list consistency (default: inconsistent) for the arguments. |
| 60 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 32) |
| 61 | + } |
| 62 | + |
| 63 | + public func testAttributeArgumentPerLineWrapping() { |
| 64 | + let input = |
| 65 | + """ |
| 66 | + @available(iOS 9.0, *) |
| 67 | + func f() {} |
| 68 | + @available(*,unavailable, renamed:"MyRenamedProtocol") |
18 | 69 | func f() {}
|
19 | 70 | @available(iOS 10.0, macOS 10.12, *)
|
20 | 71 | func f() {}
|
| 72 | + """ |
| 73 | + |
| 74 | + let expected = |
| 75 | + """ |
| 76 | + @available(iOS 9.0, *) |
| 77 | + func f() {} |
| 78 | + @available( |
| 79 | + *, |
| 80 | + unavailable, |
| 81 | + renamed: "MyRenamedProtocol" |
| 82 | + ) |
| 83 | + func f() {} |
| 84 | + @available( |
| 85 | + iOS 10.0, |
| 86 | + macOS 10.12, |
| 87 | + * |
| 88 | + ) |
| 89 | + func f() {} |
| 90 | +
|
| 91 | + """ |
| 92 | + |
| 93 | + let configuration = Configuration() |
| 94 | + configuration.lineBreakBeforeEachArgument = true |
| 95 | + assertPrettyPrintEqual( |
| 96 | + input: input, expected: expected, linelength: 32, configuration: configuration) |
| 97 | + } |
| 98 | + |
| 99 | + public func testAttributeFormattingRespectsDiscretionaryLineBreaks() { |
| 100 | + let input = |
| 101 | + """ |
| 102 | + @available( |
| 103 | + iOSApplicationExtension, |
| 104 | + introduced: 10.0, |
| 105 | + deprecated: 11.0, |
| 106 | + message: |
| 107 | + "Use something else because this is definitely deprecated.") |
| 108 | + func f2() {} |
| 109 | + """ |
| 110 | + |
| 111 | + let expected = |
| 112 | + """ |
| 113 | + @available( |
| 114 | + iOSApplicationExtension, |
| 115 | + introduced: 10.0, |
| 116 | + deprecated: 11.0, |
| 117 | + message: |
| 118 | + "Use something else because this is definitely deprecated." |
| 119 | + ) |
| 120 | + func f2() {} |
| 121 | +
|
| 122 | + """ |
| 123 | + |
| 124 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 40) |
| 125 | + } |
| 126 | + |
| 127 | + public func testAttributeInterArgumentBinPackedLineBreaking() { |
| 128 | + let input = |
| 129 | + """ |
| 130 | + @available(iOSApplicationExtension, introduced: 10.0, deprecated: 11.0, message: "Use something else because this is definitely deprecated.") |
| 131 | + func f2() {} |
| 132 | + """ |
| 133 | + |
| 134 | + let expected = |
| 135 | + """ |
| 136 | + @available( |
| 137 | + iOSApplicationExtension, |
| 138 | + introduced: 10.0, deprecated: 11.0, |
| 139 | + message: |
| 140 | + "Use something else because this is definitely deprecated." |
| 141 | + ) |
| 142 | + func f2() {} |
| 143 | +
|
| 144 | + """ |
| 145 | + |
| 146 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 40) |
| 147 | + } |
| 148 | + |
| 149 | + public func testAttributArgumentPerLineBreaking() { |
| 150 | + let input = |
| 151 | + """ |
| 152 | + @available(iOSApplicationExtension, introduced: 10.0, deprecated: 11.0, message: "Use something else because this is definitely deprecated.") |
| 153 | + func f2() {} |
| 154 | + """ |
| 155 | + |
| 156 | + let expected = |
| 157 | + """ |
| 158 | + @available( |
| 159 | + iOSApplicationExtension, |
| 160 | + introduced: 10.0, |
| 161 | + deprecated: 11.0, |
| 162 | + message: |
| 163 | + "Use something else because this is definitely deprecated." |
| 164 | + ) |
| 165 | + func f2() {} |
| 166 | +
|
| 167 | + """ |
| 168 | + |
| 169 | + let configuration = Configuration() |
| 170 | + configuration.lineBreakBeforeEachArgument = true |
| 171 | + assertPrettyPrintEqual( |
| 172 | + input: input, expected: expected, linelength: 40, configuration: configuration) |
| 173 | + } |
| 174 | + |
| 175 | + public func testObjCAttributes() { |
| 176 | + let input = |
| 177 | + """ |
| 178 | + @objc func f() {} |
| 179 | + @objc(foo:bar:baz) |
| 180 | + func f() {} |
| 181 | + @objc(thisMethodHasAVeryLongName:andThisArgumentHasANameToo:soDoesThisOne:bar:) |
| 182 | + func f() {} |
| 183 | + """ |
| 184 | + |
| 185 | + let expected = |
| 186 | + """ |
| 187 | + @objc func f() {} |
| 188 | + @objc(foo:bar:baz) |
| 189 | + func f() {} |
| 190 | + @objc( |
| 191 | + thisMethodHasAVeryLongName:andThisArgumentHasANameToo:soDoesThisOne:bar: |
| 192 | + ) |
| 193 | + func f() {} |
21 | 194 |
|
22 | 195 | """
|
23 | 196 |
|
24 |
| - // Attributes should not wrap. |
25 |
| - assertPrettyPrintEqual(input: input, expected: expected, linelength: 15) |
| 197 | + // TODO: Support line breaks between argument names in Objective-C selectors. |
| 198 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 40) |
26 | 199 | }
|
27 | 200 | }
|
0 commit comments