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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
final class TernaryExprTests: PrettyPrintTestCase {
func testTernaryExprs() {
let input =
"""
let x = a ? b : c
let y = a ?b:c
let z = a ? b: c
let reallyLongName = a ? longTruePart : longFalsePart
let reallyLongName = reallyLongCondition ? reallyLongTruePart : reallyLongFalsePart
let reallyLongName = reallyLongCondition ? reallyReallyReallyLongTruePart : reallyLongFalsePart
let reallyLongName = someCondition ? firstFunc(foo: arg) : secondFunc(bar: arg)
"""
let expected =
"""
let x = a ? b : c
let y = a ? b : c
let z = a ? b : c
let reallyLongName =
a ? longTruePart : longFalsePart
let reallyLongName =
reallyLongCondition
? reallyLongTruePart : reallyLongFalsePart
let reallyLongName =
reallyLongCondition
? reallyReallyReallyLongTruePart
: reallyLongFalsePart
let reallyLongName =
someCondition
? firstFunc(foo: arg)
: secondFunc(bar: arg)
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
func testTernaryExprsWithMultiplePartChoices() {
let input =
"""
let someLocalizedText =
shouldUseTheFirstOption ? stringFunc(name: "Name1", comment: "short comment") :
stringFunc(name: "Name2", comment: "Some very, extremely long comment", details: "Another comment")
"""
let expected =
"""
let someLocalizedText =
shouldUseTheFirstOption
? stringFunc(name: "Name1", comment: "short comment")
: stringFunc(
name: "Name2", comment: "Some very, extremely long comment",
details: "Another comment")
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
}
func testTernaryWithWrappingExpressions() {
let input =
"""
foo = firstTerm + secondTerm + thirdTerm ? firstTerm + secondTerm + thirdTerm : firstTerm + secondTerm + thirdTerm
let foo = firstTerm + secondTerm + thirdTerm ? firstTerm + secondTerm + thirdTerm : firstTerm + secondTerm + thirdTerm
foo = firstTerm || secondTerm && thirdTerm ? firstTerm + secondTerm + thirdTerm : firstTerm + secondTerm + thirdTerm
let foo = firstTerm || secondTerm && thirdTerm ? firstTerm + secondTerm + thirdTerm : firstTerm + secondTerm + thirdTerm
"""
let expected =
"""
foo =
firstTerm
+ secondTerm
+ thirdTerm
? firstTerm
+ secondTerm
+ thirdTerm
: firstTerm
+ secondTerm
+ thirdTerm
let foo =
firstTerm
+ secondTerm
+ thirdTerm
? firstTerm
+ secondTerm
+ thirdTerm
: firstTerm
+ secondTerm
+ thirdTerm
foo =
firstTerm
|| secondTerm
&& thirdTerm
? firstTerm
+ secondTerm
+ thirdTerm
: firstTerm
+ secondTerm
+ thirdTerm
let foo =
firstTerm
|| secondTerm
&& thirdTerm
? firstTerm
+ secondTerm
+ thirdTerm
: firstTerm
+ secondTerm
+ thirdTerm
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 15)
}
func testNestedTernaries() {
let input =
"""
a = b ? c : d ? e : f
let a = b ? c : d ? e : f
a = b ? c0 + c1 : d ? e : f
let a = b ? c0 + c1 : d ? e : f
a = b ? c0 + c1 + c2 + c3 : d ? e : f
let a = b ? c0 + c1 + c2 + c3 : d ? e : f
foo = testA ? testB ? testC : testD : testE ? testF : testG
let foo = testA ? testB ? testC : testD : testE ? testF : testG
"""
let expected =
"""
a =
b
? c
: d ? e : f
let a =
b
? c
: d ? e : f
a =
b
? c0 + c1
: d ? e : f
let a =
b
? c0 + c1
: d ? e : f
a =
b
? c0 + c1
+ c2 + c3
: d ? e : f
let a =
b
? c0 + c1
+ c2 + c3
: d ? e : f
foo =
testA
? testB
? testC
: testD
: testE
? testF
: testG
let foo =
testA
? testB
? testC
: testD
: testE
? testF
: testG
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 15)
}
func testExpressionStartsWithTernary() {
// When the ternary itself doesn't already start on a continuation line, we don't have a way
// to indent the continuation of the condition differently from the first and second choices,
// because we don't want to double-indent the condition's continuation lines, and we don't want
// to keep put the choices at the same indentation level as the condition (because that would
// be the start of the statement). Neither of these choices is really ideal, unfortunately.
let input =
"""
condition ? callSomething() : callSomethingElse()
condition && otherCondition ? callSomething() : callSomethingElse()
(condition && otherCondition) ? callSomething() : callSomethingElse()
"""
let expected =
"""
condition
? callSomething()
: callSomethingElse()
condition
&& otherCondition
? callSomething()
: callSomethingElse()
(condition
&& otherCondition)
? callSomething()
: callSomethingElse()
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
}
func testParenthesizedTernary() {
let input =
"""
let a = (
foo ?
bar : baz
)
a = (
foo ?
bar : baz
)
b = foo ? (
bar
) : (
baz
)
c = foo ?
(
foo2 ? nestedBar : nestedBaz
) : (baz)
"""
let expected =
"""
let a = (foo ? bar : baz)
a = (foo ? bar : baz)
b = foo ? (bar) : (baz)
c = foo ? (foo2 ? nestedBar : nestedBaz) : (baz)
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
}
}
|