File: Test.Case.ID.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (139 lines) | stat: -rw-r--r-- 5,329 bytes parent folder | download
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
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

extension Test.Case {
  /// The ID of a test case.
  ///
  /// Instances of this type are considered unique within the scope of a given
  /// parameterized test function. They are not necessarily unique across two
  /// different ``Test`` instances.
  @_spi(ForToolsIntegrationOnly)
  public struct ID: Sendable {
    /// The IDs of the arguments of this instance's associated ``Test/Case``, in
    /// the order they appear in ``Test/Case/arguments``.
    ///
    /// The value of this property is `nil` for the ID of the single test case
    /// associated with a non-parameterized test function.
    public var argumentIDs: [Argument.ID]?

    /// A number used to distinguish this test case from others associated with
    /// the same parameterized test function whose arguments have the same ID.
    ///
    /// The value of this property is `nil` for the ID of the single test case
    /// associated with a non-parameterized test function.
    ///
    /// ## See Also
    ///
    /// - ``Test/Case/discriminator``
    public var discriminator: Int?

    /// Whether or not this test case ID is considered stable across successive
    /// runs.
    public var isStable: Bool

    init(argumentIDs: [Argument.ID]?, discriminator: Int?, isStable: Bool) {
      precondition((argumentIDs == nil) == (discriminator == nil))

      self.argumentIDs = argumentIDs
      self.discriminator = discriminator
      self.isStable = isStable
    }
  }

  @_spi(ForToolsIntegrationOnly)
  public var id: ID {
    ID(argumentIDs: arguments.map { $0.map(\.id) }, discriminator: discriminator, isStable: isStable)
  }
}

// MARK: - CustomStringConvertible

extension Test.Case.ID: CustomStringConvertible {
  public var description: String {
    if let argumentIDs, let discriminator {
      "Parameterized test case ID: argumentIDs: \(argumentIDs), discriminator: \(discriminator), isStable: \(isStable)"
    } else {
      "Non-parameterized test case ID"
    }
  }
}

// MARK: - Codable

extension Test.Case.ID: Codable {
  private enum CodingKeys: String, CodingKey {
    /// A coding key for ``Test/Case/ID/argumentIDs``.
    ///
    /// This case's string value is non-standard because ``legacyArgumentIDs``
    /// already used "argumentIDs" and this needs to be different.
    case argumentIDs = "argIDs"

    /// A coding key for ``Test/Case/ID/discriminator``.
    case discriminator

    /// A coding key for ``Test/Case/ID/isStable``.
    case isStable

    /// A coding key for the legacy representation of ``Test/Case/ID/argumentIDs``.
    ///
    /// This case's string value is non-standard in order to maintain
    /// legacy compatibility with its original value.
    case legacyArgumentIDs = "argumentIDs"
  }

  public init(from decoder: any Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    if container.contains(.isStable) {
      // `isStable` is present, so we're decoding an instance encoded using the
      // newest style: every property can be decoded straightforwardly.
      try self.init(
        argumentIDs: container.decodeIfPresent([Test.Case.Argument.ID].self, forKey: .argumentIDs),
        discriminator: container.decodeIfPresent(Int.self, forKey: .discriminator),
        isStable: container.decode(Bool.self, forKey: .isStable)
      )
    } else if container.contains(.legacyArgumentIDs) {
      // `isStable` is absent, so we're decoding using the old style. Since the
      // legacy `argumentIDs` is present, the representation should be
      // considered stable.
      let decodedArgumentIDs = try container.decode([Test.Case.Argument.ID].self, forKey: .legacyArgumentIDs)
      let argumentIDs = decodedArgumentIDs.isEmpty ? nil : decodedArgumentIDs

      // Discriminator should be `nil` for the ID of a non-parameterized test
      // case, but can default to 0 for the ID of a parameterized test case.
      let discriminator = argumentIDs == nil ? nil : 0

      self.init(argumentIDs: argumentIDs, discriminator: discriminator, isStable: true)
    } else {
      // This is the old style, and since `argumentIDs` is absent, we know this
      // ID represents a parameterized test case which is non-stable.
      self.init(argumentIDs: [.init(bytes: [])], discriminator: 0, isStable: false)
    }
  }

  public func encode(to encoder: any Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    try container.encode(isStable, forKey: .isStable)
    try container.encodeIfPresent(discriminator, forKey: .discriminator)
    try container.encodeIfPresent(argumentIDs, forKey: .argumentIDs)

    // Encode the legacy representation of `argumentIDs`.
    if argumentIDs == nil {
      try container.encode([Test.Case.Argument.ID](), forKey: .legacyArgumentIDs)
    } else if isStable, let argumentIDs = argumentIDs {
      try container.encode(argumentIDs, forKey: .legacyArgumentIDs)
    }
  }
}

// MARK: - Equatable, Hashable

extension Test.Case.ID: Equatable, Hashable {}