-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.swift
470 lines (374 loc) · 17.4 KB
/
main.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import WinSDK
import XCTest
import test_component
import Ctest_component
import Foundation
class SwiftWinRTTests : XCTestCase {
public func testBlittableStruct() throws {
let simple = Simple()
var blittableStruct = try simple.returnBlittableStruct()
print("first:", blittableStruct.first)
print("second: ", blittableStruct.second)
XCTAssertEqual(blittableStruct.first, 123, "not copied correctly")
XCTAssertEqual(blittableStruct.second, 456, "not copied correctly")
// TakeBlittableStruct requires the struct to have these values
let putBlittableStruct = BlittableStruct(first: 654, second: 321)
try simple.takeBlittableStruct(putBlittableStruct)
blittableStruct = simple.blittableStructProperty
print("first:", blittableStruct.first)
print("second: ", blittableStruct.second)
XCTAssertEqual(blittableStruct.first, 0, "not copied correctly")
XCTAssertEqual(blittableStruct.second, 0, "not copied correctly")
simple.blittableStructProperty = putBlittableStruct
blittableStruct = simple.blittableStructProperty
print("first:", blittableStruct.first)
print("second: ", blittableStruct.second)
XCTAssertEqual(blittableStruct.first, 654, "not copied correctly")
XCTAssertEqual(blittableStruct.second, 321, "not copied correctly")
}
public func testNonBlittableStruct() throws {
let simple = Simple()
var nonBlittableStruct = try simple.returnNonBlittableStruct()
print("first:", nonBlittableStruct.first)
print("second: ", nonBlittableStruct.second)
XCTAssertEqual(nonBlittableStruct.first, "Hello", "not copied correctly")
XCTAssertEqual(nonBlittableStruct.second, "World", "not copied correctly")
// TakeBlittableStruct requires the struct to have these values
let putNonBlittableStruct = NonBlittableStruct(first: "From", second: "Swift!", third: 3, fourth: "Yay!")
try simple.takeNonBlittableStruct(putNonBlittableStruct)
nonBlittableStruct = simple.nonBlittableStructProperty
print("first:", nonBlittableStruct.first)
print("second: ", nonBlittableStruct.second)
XCTAssertEqual(nonBlittableStruct.first, "", "not copied correctly")
XCTAssertEqual(nonBlittableStruct.second, "", "not copied correctly")
simple.nonBlittableStructProperty = putNonBlittableStruct
nonBlittableStruct = simple.nonBlittableStructProperty
print("first:", nonBlittableStruct.first)
print("second: ", nonBlittableStruct.second)
print("third:", nonBlittableStruct.third)
print("fourth: ", nonBlittableStruct.fourth)
XCTAssertEqual(nonBlittableStruct.first, "From", "not copied correctly")
XCTAssertEqual(nonBlittableStruct.second, "Swift!", "not copied correctly")
XCTAssertEqual(nonBlittableStruct.third, 3, "not copied correctly")
XCTAssertEqual(nonBlittableStruct.fourth, "Yay!", "not copied correctly")
}
public func testStructWithIReference() throws {
let simple = Simple()
var structWithIReference = try simple.returnStructWithReference()
XCTAssertEqual(structWithIReference.value1, 4, "not copied correctly")
XCTAssertEqual(structWithIReference.value2, 2, "not copied correctly")
// takeStructWithReference requires the struct to have these values
try simple.takeStructWithReference(structWithIReference)
structWithIReference = simple.structWithReferenceProperty
XCTAssertNil(structWithIReference.value1, "not copied correctly")
XCTAssertNil(structWithIReference.value2, "not copied correctly")
simple.structWithReferenceProperty = StructWithIReference(value1: 4, value2: nil)
structWithIReference = simple.structWithReferenceProperty
XCTAssertEqual(structWithIReference.value1, 4, "not copied correctly")
XCTAssertEqual(structWithIReference.value2, nil, "not copied correctly")
simple.structWithReferenceProperty = StructWithIReference(value1: 4, value2: 2)
structWithIReference = simple.structWithReferenceProperty
XCTAssertEqual(structWithIReference.value1, 4, "not copied correctly")
XCTAssertEqual(structWithIReference.value2, 2, "not copied correctly")
}
public func testEnums() throws {
let classy = Class()
var enumVal = Signed.second
let returned = try classy.inEnum(enumVal);
XCTAssertEqual(returned, "Second", "improper value returned")
enumVal = try classy.returnEnum()
XCTAssertEqual(enumVal, Signed.first, "improper value returned")
var enumProp = classy.enumProperty
print("class has: ", enumProp)
XCTAssertEqual(enumProp, Fruit.banana, "fruit should be b-a-n-a-n-a-s")
print("setting enum to Apple")
classy.enumProperty = .apple
enumProp = classy.enumProperty
print("an", enumProp, "a day keeps the bugs away")
XCTAssertEqual(enumProp, Fruit.apple, "fruit should be apple")
}
public func testCustomConstructors() throws {
var classy = Class("hello")
XCTAssertEqual(classy.enumProperty, Fruit.banana, "fruit should be bananas")
classy = Class("world", Fruit.pineapple)
XCTAssertEqual(classy.enumProperty, Fruit.pineapple, "fruit should be Pineapple")
}
public func testStaticMethods() throws {
try Class.staticTest()
var result = try Class.staticTestReturn()
print("result: ", result);
XCTAssertEqual(result, 42);
result = Class.staticProperty
print("result: ", result);
XCTAssertEqual(result, 18);
print("calling static class methods")
let output = try StaticClass.inEnum(Signed.first)
print("result: ", output)
XCTAssertEqual(output, "First")
let fruit = try StaticClass.enumProperty
print("result: ", fruit)
XCTAssertEqual(fruit, Fruit.orange, "expected Orange!")
StaticClass.enumProperty = Fruit.banana
XCTAssertEqual(StaticClass.enumProperty, Fruit.banana, "expected Banana!")
let putNonBlittableStruct = NonBlittableStruct(first: "From", second: "Swift!", third: 1, fourth: "Yay!")
let putStructResult = try StaticClass.inNonBlittableStruct(putNonBlittableStruct)
print("result: ", putStructResult)
XCTAssertEqual(putStructResult, "FromSwift!Yay!")
}
public func testOutParams() throws {
let classy = Class()
print(">> testing OutInt32")
var outInt: Int32 = 0
try classy.outInt32(&outInt)
print(" result: ", outInt)
XCTAssertEqual(outInt, 123)
print(">> testing OutString")
var outString: String = ""
try classy.outString(&outString)
print(" result: ", outString)
XCTAssertEqual(outString, "123")
print(">> testing OutEnum")
var outEnum: Signed = .second
try classy.outEnum(&outEnum)
print(" result: ", outEnum)
XCTAssertEqual(outEnum, .first)
print(">> testing OutBlittableStruct")
var outBlittableStruct = BlittableStruct()
try classy.outBlittableStruct(&outBlittableStruct)
print(" result: ", outBlittableStruct)
XCTAssertEqual(outBlittableStruct.first, 867)
XCTAssertEqual(outBlittableStruct.second, 5309)
print(">> testing OutNonBlittableStruct")
var outNonBlittableStruct: NonBlittableStruct = .init()
try classy.outNonBlittableStruct(&outNonBlittableStruct)
print(" result: ", outNonBlittableStruct)
XCTAssertEqual(outNonBlittableStruct.first, "please")
XCTAssertEqual(outNonBlittableStruct.second, "vote")
XCTAssertEqual(outNonBlittableStruct.third, 4)
XCTAssertEqual(outNonBlittableStruct.fourth, "pedro")
}
public func testDelegate() throws {
var classy = Class()
do
{
print("TESTING ISimpleDelegate")
let mySwiftDelegate = MySimpleDelegate()
try classy.setDelegate(mySwiftDelegate)
XCTAssertEqual(mySwiftDelegate.getThisCount(), 1)
XCTAssertEqual(mySwiftDelegate.getThat(), 3)
mySwiftDelegate.doThis()
mySwiftDelegate.doThat(6)
XCTAssertEqual(mySwiftDelegate.getThisCount(), 2)
XCTAssertEqual(mySwiftDelegate.getThat(), 6)
// Get the delegate back and make sure it works
// TODO: WIN-78 retrieving the delegate causes a memory
// leak. We should verify the delegate is fully cleaned up
var retrievedDelegate = try classy.getDelegate()!
try retrievedDelegate.doThis()
try retrievedDelegate.doThat(9)
XCTAssertEqual(mySwiftDelegate.getThisCount(), 3)
XCTAssertEqual(mySwiftDelegate.getThat(), 9)
// Null out the delegate from the C++ and retrieve a new one. Note that
// we're initializing the swift object with `nil`, which is similar to
// how in C++/WinRT this works.
try classy.setDelegate(nil)
// This will hand us a new C++ class which implements the interface.
retrievedDelegate = try classy.getDelegate()!
try retrievedDelegate.doThis()
try retrievedDelegate.doThat(15)
XCTAssertEqual(mySwiftDelegate.getThisCount(), 3)
XCTAssertEqual(mySwiftDelegate.getThat(), 9)
try classy.setDelegate(retrievedDelegate)
}
do
{
print("TESTING IIAmImplementable")
let impl = MyImplementableDelegate()
classy = Class("with delegate", .orange, impl)
var enumVal = Signed.second
let returned = try classy.inEnum(enumVal);
XCTAssertEqual(returned, impl.inEnum(enumVal), "improper value returned")
enumVal = try classy.returnEnum()
XCTAssertEqual(enumVal, impl.returnEnum(), "improper value returned")
var enumProp = classy.enumProperty
print("class has: ", enumProp)
XCTAssertEqual(enumProp, impl.enumProperty, "i'd be apple'd if this was wrong")
print("setting enum to Pineapple")
classy.enumProperty = .pineapple
enumProp = classy.enumProperty
print("a", enumProp, "is prickly")
XCTAssertEqual(enumProp, impl.enumProperty, "fruit should be prickly")
print(">> testing OutInt32")
var outInt: Int32 = 0
try classy.outInt32(&outInt)
print(" result: ", outInt)
var expectedOutInt: Int32 = 0
impl.outInt32(&expectedOutInt)
XCTAssertEqual(outInt, expectedOutInt)
print(">> testing OutString")
var outString: String = ""
try classy.outString(&outString)
var expectedOutString: String = ""
impl.outString(&expectedOutString)
print(" result: ", outString)
XCTAssertEqual(outString, expectedOutString)
print(">> testing OutEnum")
var outEnum: Signed = .second
try classy.outEnum(&outEnum)
print(" result: ", outEnum)
var expectedOutEnum: Signed = .second
impl.outEnum(&expectedOutEnum)
XCTAssertEqual(outEnum, expectedOutEnum)
print(">> testing OutBlittableStruct")
var outBlittableStruct = BlittableStruct()
try classy.outBlittableStruct(&outBlittableStruct)
print(" result: ", outBlittableStruct)
var outBlittableStructExpected = BlittableStruct()
impl.outBlittableStruct(&outBlittableStructExpected)
// TODO: Implement equatable on structs
//XCTAssertEqual(outBlittableStruct, outBlittableStructExpected)
XCTAssertEqual(outBlittableStruct.first, outBlittableStructExpected.first)
XCTAssertEqual(outBlittableStruct.second, outBlittableStructExpected.second)
print(">> testing OutNonBlittableStruct")
var outNonBlittableStruct: NonBlittableStruct = .init()
try classy.outNonBlittableStruct(&outNonBlittableStruct)
print(" result: ", outNonBlittableStruct)
var outNonBlittableStructExected = NonBlittableStruct()
impl.outNonBlittableStruct(&outNonBlittableStructExected)
//XCTAssertEqual(outNonBlittableStruct, outNonBlittableStructExected)
XCTAssertEqual(outNonBlittableStruct.first, outNonBlittableStructExected.first)
XCTAssertEqual(outNonBlittableStruct.second, outNonBlittableStructExected.second)
XCTAssertEqual(outNonBlittableStruct.third, outNonBlittableStructExected.third)
XCTAssertEqual(outNonBlittableStruct.fourth, outNonBlittableStructExected.fourth)
}
}
public func testNonDefaultMethods() throws {
Class.staticPropertyFloat = 4.0
XCTAssertEqual(Class.staticPropertyFloat, 4.0)
XCTAssertEqual(try Class.staticTestReturnFloat(), 42.24)
let classy = Class()
classy.method()
}
public func testChar() throws {
let classy = Class()
print("classy ReturnChar: ", try classy.returnChar())
XCTAssertEqual(try classy.returnChar(), "d")
let result = try classy.inChar("x")
print("classy InChar: ", result)
XCTAssertEqual(result, "x")
var out: Character = "_"
try classy.outChar(&out)
print("classy OutChar: ", out)
XCTAssertEqual(out, "z")
}
class DoTheNewBase : IBasic {
func method() {
print("it's done")
}
}
public func testDoubleDelegate() throws {
let classy = Class()
let newBase = DoTheNewBase()
classy.implementation = newBase
classy.method()
var implReturn = classy.implementation!
implReturn.method()
XCTAssertIdentical(implReturn, newBase, "incorrect swift object returned")
classy.implementation = nil
implReturn = classy.implementation // Will create a new implementation
XCTAssertNotIdentical(implReturn, newBase, "incorrect swift object returned")
let double = DoubleDelegate()
classy.implementation = double
try classy.setDelegate(double)
classy.method()
let delegate = try classy.getDelegate()!
try delegate.doThis()
try delegate.doThat(19)
XCTAssertIdentical(double, delegate)
XCTAssertIdentical(double, classy.implementation)
}
public func testIReference() throws {
let classy = Class()
XCTAssertEqual(classy.startValue, nil)
classy.startValue = 23
print("value: ", classy.startValue ?? "N/A")
XCTAssertEqual(classy.startValue, 23)
let uuidString = "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"
let id: Foundation.UUID? = .init(uuidString: uuidString)
classy.id = id
let unwrappedID = try XCTUnwrap(classy.id)
XCTAssertEqual("\(unwrappedID)", uuidString)
XCTAssertEqual(classy.id, id)
}
public func testUnicode() throws {
let classy = Class()
_ = try classy.inString("\u{E909}")
}
public func testNullValues() throws {
XCTAssertTrue(try NullValues.isObjectNull(nil))
XCTAssertTrue(try NullValues.isInterfaceNull(nil))
XCTAssertTrue(try NullValues.isGenericInterfaceNull(nil))
XCTAssertTrue(try NullValues.isClassNull(nil))
XCTAssertTrue(try NullValues.isDelegateNull(nil))
XCTAssertFalse(try NullValues.isObjectNull(NoopClosable()))
XCTAssertFalse(try NullValues.isInterfaceNull(NoopClosable()))
XCTAssertFalse(try NullValues.isGenericInterfaceNull([""].toVector()))
XCTAssertFalse(try NullValues.isClassNull(NoopClosable()))
XCTAssertFalse(try NullValues.isDelegateNull({}))
XCTAssertNil(try NullValues.getNullObject())
XCTAssertNil(try NullValues.getNullInterface())
XCTAssertNil(try NullValues.getNullGenericInterface())
XCTAssertNil(try NullValues.getNullClass())
XCTAssertNil(try NullValues.getNullDelegate())
}
public func testCasing() {
// Don't change the casing of these names! This test is to make sure the
// casing is as expected. It doesn't need to run, just compile.
print("\(SwiftifiableNames.camelCase)")
print("\(SwiftifiableNames.pascalCase)")
print("\(SwiftifiableNames.esingleLetterPrefixed)")
print("\(SwiftifiableNames.leadingCaps)")
print("\(SwiftifiableNames.r8g8b8a8Typeless)")
print("\(SwiftifiableNames.uuid)")
}
public func testErrorInfo() {
let message = "You are doing a bad thing"
do {
let classy = Class()
try classy.fail(message)
} catch {
XCTAssertEqual("\(error)", message)
}
}
public func testNoExcept() throws {
let classy = Class()
classy.noexceptVoid()
classy.method()
XCTAssertEqual(classy.noexceptInt32(), 123)
XCTAssertEqual(classy.noexceptString(), "123")
}
}
var tests: [XCTestCaseEntry] = [
testCase([
("testBlittableStruct", SwiftWinRTTests.testBlittableStruct),
("testChar", SwiftWinRTTests.testChar),
("testCustomConstructors", SwiftWinRTTests.testCustomConstructors),
("testDelegate", SwiftWinRTTests.testDelegate),
("testDoubleDelegate", SwiftWinRTTests.testDoubleDelegate),
("testEnums", SwiftWinRTTests.testEnums),
("testIReference", SwiftWinRTTests.testIReference),
("testNonBlittableStruct", SwiftWinRTTests.testNonBlittableStruct),
("testNonDefaultMethods", SwiftWinRTTests.testNonDefaultMethods),
("testNullValues", SwiftWinRTTests.testNullValues),
("testOutParams", SwiftWinRTTests.testOutParams),
("testStaticMethods", SwiftWinRTTests.testStaticMethods),
("testStructWithIReference", SwiftWinRTTests.testStructWithIReference),
("testUnicode", SwiftWinRTTests.testUnicode),
("testErrorInfo", SwiftWinRTTests.testErrorInfo),
])
] + valueBoxingTests + eventTests + collectionTests + aggregationTests + asyncTests + memoryManagementTests + bufferTests + weakReferenceTests
// Have to start adding tests in different lines, otherwise we get the following error:
// error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
tests += arrayTests
RoInitialize(RO_INIT_MULTITHREADED)
XCTMain(tests)