-
Notifications
You must be signed in to change notification settings - Fork 31
/
SwiftInitializerGeneratorTests.swift
159 lines (147 loc) · 4.28 KB
/
SwiftInitializerGeneratorTests.swift
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
//
// SwiftInitializerGeneratorTests.swift
// SwiftInitializerGeneratorTests
//
// Created by Bouke Haarsma on 21-12-16.
// Copyright © 2016 Bouke Haarsma. All rights reserved.
//
import XCTest
class SwiftInitializerGeneratorTests: XCTestCase {
func assert(input: [String], output: [String], file: StaticString = #file, line: UInt = #line) {
do {
let lines = try generate(selection: input, indentation: " ", leadingIndent: "")
if(lines != output) {
XCTFail("Output is not correct; expected:\n\(output.joined(separator: "\n"))\n\ngot:\n\(lines.joined(separator: "\n"))", file: file, line: line)
}
} catch {
XCTFail("Could not generate initializer: \(error)", file: file, line: line)
}
}
func testNoAccessModifiers() {
assert(
input: [
"let a: Int",
"let b: Int"
],
output: [
"public init(a: Int, b: Int) {",
" self.a = a",
" self.b = b",
"}"
])
}
func testNoProperties() {
assert(
input: [
"",
""
],
output: [
"public init() {",
"}"
])
}
func testEmptyLineInBetween() {
assert(
input: [
"let a: Int",
"",
"let b: Int"
],
output: [
"public init(a: Int, b: Int) {",
" self.a = a",
" self.b = b",
"}"
])
}
func testSingleAccessModifier() {
assert(
input: [
"internal let a: Int",
"private let b: Int"
],
output: [
"public init(a: Int, b: Int) {",
" self.a = a",
" self.b = b",
"}"
])
}
func testDoubleAccessModifier() {
assert(
input: [
"public internal(set) let a: Int",
"public private(set) let b: Int"
],
output: [
"public init(a: Int, b: Int) {",
" self.a = a",
" self.b = b",
"}"
])
}
func testCommentLine() {
assert(
input: [
"/// a very important property",
"let a: Int",
"// this one, not so much",
"let b: Int",
"/*",
" * pay attention to this one",
" */",
"let c: IBOutlet!"
],
output: [
"public init(a: Int, b: Int, c: IBOutlet!) {",
" self.a = a",
" self.b = b",
" self.c = c",
"}"
])
}
func testDynamicVar() {
assert(
input: ["dynamic var hello: String",
"dynamic var a: Int?",
"var b: Float"],
output: [
"public init(hello: String, a: Int?, b: Float) {",
" self.hello = hello",
" self.a = a",
" self.b = b",
"}"
])
}
func testEscapingClosure() {
assert(
input: [
"let a: (String) -> Int?",
"let b: () -> () -> Void",
"let c: ((String, Int))->()",
],
output: [
"public init(a: @escaping (String) -> Int?, b: @escaping () -> () -> Void, c: @escaping ((String, Int))->()) {",
" self.a = a",
" self.b = b",
" self.c = c",
"}"
])
}
func testNoEscapingAttribute() {
assert(
input: [
"let a: (() -> Void)?",
"let b: [() -> Void]",
"let c: (()->())!"
],
output: [
"public init(a: (() -> Void)?, b: [() -> Void], c: (()->())!) {",
" self.a = a",
" self.b = b",
" self.c = c",
"}"
])
}
}