File: TestListing.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 (100 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (2)
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//
//  TestListing.swift
//  Implementation of the mode for printing the list of tests.
//

internal struct TestListing {
    private let testSuite: XCTestSuite

    init(testSuite: XCTestSuite) {
        self.testSuite = testSuite
    }

    /// Prints a flat list of the tests in the suite, in the format used to
    /// specify a test by name when running tests.
    func printTestList() {
        let list = testSuite.list()
        let tests = list.count == 1 ? "test" : "tests"
        let bundleName = testSuite.findBundleTestSuite()?.name ?? "<<unknown bundle>>"

        print("Listing \(list.count) \(tests) in \(bundleName):\n")
        for entry in testSuite.list() {
            print(entry)
        }
    }

    /// Prints a JSON representation of the tests in the suite, mirring the internal
    /// tree representation of test suites and test cases. This output is intended
    /// to be consumed by other tools.
    func printTestJSON() {
        let json = try! JSONSerialization.data(withJSONObject: testSuite.dictionaryRepresentation())
        print(String(data: json, encoding: .utf8)!)
    }
}

protocol Listable {
    func list() -> [String]
    func dictionaryRepresentation() -> NSDictionary
}

private func moduleName(value: Any) -> String {
    let moduleAndType = String(reflecting: type(of: value))
    return String(moduleAndType.split(separator: ".").first!)
}

extension XCTestSuite: Listable {
    private var listables: [Listable] {
        return tests
            .compactMap({ ($0 as? Listable) })
    }

    private var listingName: String {
        if let childTestCase = tests.first as? XCTestCase, name == String(describing: type(of: childTestCase)) {
            return "\(moduleName(value: childTestCase)).\(name)"
        } else {
            return name
        }
    }

    func list() -> [String] {
        return listables.flatMap({ $0.list() })
    }

    func dictionaryRepresentation() -> NSDictionary {
        let listedTests = NSArray(array: tests.compactMap({ ($0 as? Listable)?.dictionaryRepresentation() }))
        return NSDictionary(objects: [NSString(string: listingName),
                                      listedTests],
                            forKeys: [NSString(string: "name"),
                                      NSString(string: "tests")])
    }

    func findBundleTestSuite() -> XCTestSuite? {
        if name.hasSuffix(".xctest") {
            return self
        } else {
            return tests.compactMap({ ($0 as? XCTestSuite)?.findBundleTestSuite() }).first
        }
    }
}

extension XCTestCase: Listable {
    func list() -> [String] {
        let adjustedName = name.split(separator: ".")
            .map(String.init)
            .joined(separator: "/")
        return ["\(moduleName(value: self)).\(adjustedName)"]
    }

    func dictionaryRepresentation() -> NSDictionary {
        let methodName = String(name.split(separator: ".").last!)
        return NSDictionary(object: NSString(string: methodName), forKey: NSString(string: "name"))
    }
}