Skip to content

Commit 3c60270

Browse files
[tests] Add regression tests for SR-15038
1 parent da234d0 commit 3c60270

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/Constraints/casts.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,67 @@ extension ChangeType where T == String? {
549549
var foo: String? { return self.delta?.previous as? String } // OK
550550
var bar: String? { self.delta?.next }
551551
}
552+
553+
// SR-15038
554+
protocol ExperimentDeserializable {
555+
static func deserializeExperiment(_ value: Any) -> Self?
556+
}
557+
558+
extension String: ExperimentDeserializable {
559+
static func deserializeExperiment(_ value: Any) -> String? { value as? String }
560+
}
561+
562+
extension Int: ExperimentDeserializable {
563+
static func deserializeExperiment(_ value: Any) -> Int? { value as? Int }
564+
}
565+
566+
class Constant<T> {
567+
private init(getUnderlyingValue: @escaping () -> T) {
568+
print(getUnderlyingValue())
569+
}
570+
}
571+
572+
struct Thing {
573+
let storage: [String: Any]
574+
}
575+
576+
extension Constant where T: Sequence, T.Element: ExperimentDeserializable {
577+
static func foo<U>(thing: Thing, defaultValue: T) -> T where T == [U] {
578+
guard let array = thing.storage["foo"] as? [Any] else {
579+
fatalError()
580+
}
581+
582+
let value = array.map(T.Element.deserializeExperiment) as? [T.Element] ?? defaultValue // OK
583+
return value
584+
}
585+
}
586+
587+
// Array
588+
func decodeStringOrInt<T: FixedWidthInteger>() -> [T] {
589+
let stringWrapped = [String]()
590+
if let values = stringWrapped.map({ $0.isEmpty ? 0 : T($0) }) as? [T] { // OK
591+
return values
592+
} else {
593+
fatalError()
594+
}
595+
}
596+
597+
// Set
598+
func decodeStringOrIntSet<T: FixedWidthInteger>() -> Set<T> {
599+
let stringWrapped = [String]()
600+
if let values = Set(stringWrapped.map({ $0.isEmpty ? 0 : T($0) })) as? Set<T> { // OK
601+
return values
602+
} else {
603+
fatalError()
604+
}
605+
}
606+
607+
// Dictionary
608+
func decodeStringOrIntDictionary<T: FixedWidthInteger>() -> [Int: T] {
609+
let stringWrapped = [String]()
610+
if let values = Dictionary(uniqueKeysWithValues: stringWrapped.map({ $0.isEmpty ? (0, 0) : (0, T($0)) })) as? [Int: T] { // OK
611+
return values
612+
} else {
613+
fatalError()
614+
}
615+
}

0 commit comments

Comments
 (0)