forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastTraps.swift.gyb
80 lines (60 loc) · 1.87 KB
/
CastTraps.swift.gyb
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
// -*- swift -*-
// RUN: rm -rf %t ; mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/CastTraps.swift
// RUN: %S/../../utils/line-directive %t/CastTraps.swift -- %target-build-swift %t/CastTraps.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/CastTraps.swift -- %target-run %t/a.out
// FIXME: Casting.cpp has dozens of places to fail a cast. This test does not
// attempt to enumerate them all.
import StdlibUnittest
import Foundation
% types = []
% objectTypes = []
% protocolTypes = []
% types.append(['a.Class1', 'a.Class2'])
% objectTypes.append(['a.Class1', 'a.Class2'])
class Class1 { }
class Class2 { }
% types.append(['a.Struct1', 'a.Struct2'])
struct Struct1 { }
struct Struct2 { }
% types.append(['a.ObjCClass1', 'a.ObjCClass2'])
% objectTypes.append(['a.ObjCClass1', 'a.ObjCClass2'])
class ObjCClass1 : NSObject { }
class ObjCClass2 : NSObject { }
% types.append(['NSDateFormatter', 'NSNumberFormatter'])
% objectTypes.append(['NSDateFormatter', 'NSNumberFormatter'])
// non-Swift Objective-C class
% protocolTypes.append('NSURLSessionDelegate')
// non-Swift Objective-C protocol
var CastTrapsTestSuite = TestSuite("CastTraps")
// Test `(T1() as Any) as T2` cast failure
// for all type pairs.
% for (t1, _) in types:
% for (_, t2) in types:
CastTrapsTestSuite.test("${t1}__${t2}")
.crashOutputMatches("Could not cast value of type '${t1}' ")
.crashOutputMatches(" to '${t2}'")
.code
{
let o = ${t1}() as Any
expectCrashLater()
o as! ${t2}
}
% end
% end
// Test `(T1() as AnyObject) as T2` cast failure
// for object type to protocol type pairs
% for (t1, _) in objectTypes:
% for (t2) in protocolTypes:
CastTrapsTestSuite.test("${t1}__${t2}")
.crashOutputMatches("Could not cast value of type '${t1}' ")
.crashOutputMatches(" to '${t2}'")
.code
{
let o = ${t1}() as AnyObject
expectCrashLater()
o as! ${t2}
}
% end
% end
runAllTests()