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
|
final class VariableDeclarationTests: PrettyPrintTestCase {
func testBasicVariableDecl() {
let input =
"""
let x = firstVariable + secondVariable / thirdVariable + fourthVariable
let y: Int = anotherVar + moreVar
let (w, z, s): (Int, Double, Bool) = firstTuple + secondTuple
"""
let expected =
"""
let x =
firstVariable
+ secondVariable
/ thirdVariable
+ fourthVariable
let y: Int =
anotherVar + moreVar
let (w, z, s):
(Int, Double, Bool) =
firstTuple + secondTuple
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
}
func testVariableDeclWithAttributes() {
let input =
"""
@NSCopying let a: Int = 123
@NSCopying @NSManaged let a: Int = 123
@NSCopying let areallylongvarname: Int = 123
@NSCopying @NSManaged let areallylongvarname: Int = 123
"""
let expected =
"""
@NSCopying let a: Int = 123
@NSCopying @NSManaged let a: Int = 123
@NSCopying let areallylongvarname: Int =
123
@NSCopying @NSManaged
let areallylongvarname: Int = 123
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
}
func testMultipleBindings() {
let input =
"""
let a = 100, b = 200, c = 300, d = 400, e = 500, f = 600
let a = 5, anotherReallyLongVariableName = something, longVariableName = longFunctionCall()
let a = letsForceTheFirstOneToWrapAsWell, longVariableName = longFunctionCall()
let a = firstThing + secondThing + thirdThing, b = firstThing + secondThing + thirdThing
"""
let expected =
"""
let a = 100, b = 200, c = 300, d = 400,
e = 500, f = 600
let a = 5,
anotherReallyLongVariableName =
something,
longVariableName = longFunctionCall()
let
a = letsForceTheFirstOneToWrapAsWell,
longVariableName = longFunctionCall()
let
a =
firstThing + secondThing
+ thirdThing,
b =
firstThing + secondThing
+ thirdThing
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
}
func testMultipleBindingsWithTypeAnnotations() {
let input =
"""
let a: Int = 100, b: ReallyLongTypeName = 200, c: (AnotherLongTypeName, AnotherOne) = 300
"""
let expected =
"""
let a: Int = 100,
b: ReallyLongTypeName = 200,
c: (AnotherLongTypeName, AnotherOne) =
300
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
}
func testAsyncLetBindings() {
let input =
"""
async let a = fetch("1.jpg")
async let b: Image = fetch("2.jpg")
async let secondPhotoToFetch = fetch("3.jpg")
async let theVeryLastPhotoWeWant = fetch("4.jpg")
"""
let expected =
"""
async let a = fetch("1.jpg")
async let b: Image = fetch(
"2.jpg")
async let secondPhotoToFetch =
fetch("3.jpg")
async let theVeryLastPhotoWeWant =
fetch("4.jpg")
"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 29)
}
}
|