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
|
// RUN: %empty-directory(%t)
// RUN: cp %s %t/main.swift
// RUN: %target-build-swift %t/main.swift %S/Inputs/CodableMultifileOther.swift -module-name main -o %t/main
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main
// REQUIRES: executable_test
// FIXME: This test could run on Linux too, if we could either use
// corelibs-foundation, or implement a mock Encoder for testing.
// REQUIRES: objc_interop
import StdlibUnittest
import Foundation
var CodableMultifileTestSuite = TestSuite("CodableMultifile")
// The first test doesn't synthesize encode(to:) at all.
CodableMultifileTestSuite.test("1") {
let derived = DerivedFirst()
// Make sure the vtable offset matches between this translation unit and
// the other file, which requires us to force the encode(to:) member when
// type checking this translation unit.
expectEqual(false, derived.derivedMember)
}
// The second test synthesizes init(from:) before encode(to:).
// We define a wrapper so that the virtual method BaseSecond.encode(to:) method
// is called from outside its module. If we use the conformance of BaseSecond
// to Codable, we don't expose the bug because the virtual method call is
// encapsulated in the conformance, which is emitted in the same translation unit
// as BaseSecond.
struct WrapperSecond : Encodable {
let base: BaseSecond
func encode(to encoder: Encoder) throws {
try base.encode(to: encoder)
}
}
CodableMultifileTestSuite.test("2") {
// Make sure we synthesize the init(from:) member before encode(to:) in
// this translation unit.
_ = BaseSecond.init(from:)
let base = WrapperSecond(base: BaseSecond())
let encoder = JSONEncoder()
expectEqual(
"{\"baseMember\":2}",
String(data: try! encoder.encode(base), encoding: .utf8)!)
}
// The third test synthesizes encode(to:) before init(from:).
// See above.
struct WrapperThird : Encodable {
let base: BaseThird
func encode(to encoder: Encoder) throws {
try base.encode(to: encoder)
}
}
CodableMultifileTestSuite.test("3") {
// Make sure we synthesize the encode(to:) member before init(from:) in
// this translation unit.
_ = BaseThird.encode(to:)
let base = WrapperThird(base: BaseThird())
let encoder = JSONEncoder()
expectEqual(
"{\"baseMember\":3}",
String(data: try! encoder.encode(base), encoding: .utf8)!)
}
runAllTests()
|