File: Test.Case.ArgumentTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (175 lines) | stat: -rw-r--r-- 6,081 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
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
//
// 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
//

@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing

@Suite("Test.Case.Argument Tests")
struct Test_Case_ArgumentTests {
  @Test("One parameter")
  func oneParameter() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 1)

      let argument = testCase.arguments[0]
      #expect(argument.value as? String == "value")
      #expect(argument.parameter.index == 0)
      #expect(argument.parameter.firstName == "x")
    }

    await runTestFunction(named: "oneParameter(x:)", in: ParameterizedTests.self, configuration: configuration)
  }

  @Test("Two parameters")
  func twoParameters() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 2)

      do {
        let argument = testCase.arguments[0]
        #expect(argument.value as? String == "value")
        #expect(argument.parameter.index == 0)
        #expect(argument.parameter.firstName == "x")
      }
      do {
        let argument = testCase.arguments[1]
        #expect(argument.value as? Int == 123)
        #expect(argument.parameter.index == 1)
        #expect(argument.parameter.firstName == "y")
      }
    }

    await runTestFunction(named: "twoParameters(x:y:)", in: ParameterizedTests.self, configuration: configuration)
  }

  @Test("One 1-tuple parameter")
  func one1TupleParameter() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 1)

      let argument = testCase.arguments[0]
      #expect(argument.value as? (String) == ("value"))
      #expect(argument.parameter.index == 0)
      #expect(argument.parameter.firstName == "x")
    }

    await runTestFunction(named: "one1TupleParameter(x:)", in: ParameterizedTests.self, configuration: configuration)
  }

  @Test("One 2-tuple parameter")
  func one2TupleParameter() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 1)

      let argument = testCase.arguments[0]
      let value = try #require(argument.value as? (String, Int))
      #expect(value.0 == "value")
      #expect(value.1 == 123)
      #expect(argument.parameter.index == 0)
      #expect(argument.parameter.firstName == "x")
    }

    await runTestFunction(named: "one2TupleParameter(x:)", in: ParameterizedTests.self, configuration: configuration)
  }

  @Test("Two Dictionary element (key, value) parameters")
  func DictionaryElementParameters() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 2)

      do {
        let argument = testCase.arguments[0]
        #expect(argument.value as? String == "value")
        #expect(argument.parameter.index == 0)
        #expect(argument.parameter.firstName == "x")
      }
      do {
        let argument = testCase.arguments[1]
        #expect(argument.value as? Int == 123)
        #expect(argument.parameter.index == 1)
        #expect(argument.parameter.firstName == "y")
      }
    }

    await runTestFunction(named: "twoDictionaryElementParameters(x:y:)", in: ParameterizedTests.self, configuration: configuration)
  }

  @Test("One Dictionary element tuple (key, value) parameter")
  func oneDictionaryElementTupleParameter() async {
    var configuration = Configuration()
    configuration.setEventHandler { event, context in
      guard case .testCaseStarted = event.kind else {
        return
      }
      let testCase = try #require(context.testCase)
      try #require(testCase.arguments.count == 1)

      let argument = testCase.arguments[0]
      let value = try #require(argument.value as? (String, Int))
      #expect(value.0 == "value")
      #expect(value.1 == 123)
      #expect(argument.parameter.index == 0)
      #expect(argument.parameter.firstName == "x")
      #expect(argument.parameter.typeInfo.fullyQualifiedName == "(key: Swift.String, value: Swift.Int)")
      #expect(argument.parameter.typeInfo.unqualifiedName == "(key: String, value: Int)")
    }

    await runTestFunction(named: "oneDictionaryElementTupleParameter(x:)", in: ParameterizedTests.self, configuration: configuration)
  }
}

// MARK: - Fixture tests

@Suite(.hidden)
struct ParameterizedTests {
  @Test(.hidden, arguments: ["value"])
  func oneParameter(x: String) {}

  @Test(.hidden, arguments: ["value"], [123])
  func twoParameters(x: String, y: Int) {}

  @Test(.hidden, arguments: [("value")])
  func one1TupleParameter(x: (String)) {}

  @Test(.hidden, arguments: [("value", 123)])
  func one2TupleParameter(x: (String, Int)) {}

  @Test(.hidden, arguments: ["value": 123])
  func twoDictionaryElementParameters(x: String, y: Int) {}

  @Test(.hidden, arguments: ["value": 123])
  func oneDictionaryElementTupleParameter(x: (key: String, value: Int)) {}

  @Test(.disabled(), arguments: [1, 2, 3]) func disabled(x: Int) {}
}