File: MetadataAvailabilityTests.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 (185 lines) | stat: -rw-r--r-- 7,878 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
176
177
178
179
180
181
182
183
184
185
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2022-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
*/

import Foundation
import XCTest
import Markdown

@testable import SwiftDocC

class MetadataAvailabilityTests: XCTestCase {
    func testInvalidWithNoArguments() throws {
        let source = "@Available"
        
        try assertDirective(Metadata.Availability.self, source: source) { directive, problems in
            XCTAssertNil(directive)
            
            XCTAssertEqual(2, problems.count)
            let diagnosticIdentifiers = Set(problems.map { $0.diagnostic.identifier })
            let diagnosticExplanations = Set(problems.map { $0.diagnostic.explanation })
            XCTAssertEqual(diagnosticIdentifiers, ["org.swift.docc.HasArgument.unlabeled", "org.swift.docc.HasArgument.introduced"])
            XCTAssertEqual(diagnosticExplanations, [
                "Available expects an argument for the \'introduced\' parameter that\'s convertible to a semantic version number (\'[0-9]+(.[0-9]+)?(.[0-9]+)?\')",
                "Available expects an argument for an unnamed parameter that\'s convertible to \'Platform\'"
            ])
        }
    }

    func testInvalidDuplicateIntroduced() throws {
        for platform in Metadata.Availability.Platform.defaultCases {
            let source = """
            @Metadata {
                @Available(\(platform.rawValue), introduced: \"1.0\")
                @Available(\(platform.rawValue), introduced: \"2.0\")
            }
            """
            try assertDirective(Metadata.self, source: source) { directive, problems in
                XCTAssertEqual(2, problems.count)
                let diagnosticIdentifiers = Set(problems.map { $0.diagnostic.identifier })
                XCTAssertEqual(diagnosticIdentifiers, ["org.swift.docc.\(Metadata.Availability.self).DuplicateIntroduced"])
            }
        }
    }
    
    func testInvalidIntroducedFormat() throws {
        let source = """
        @Metadata {
            @TechnologyRoot
            @Available(Package, introduced: \"\")
            @Available(Package, introduced: \".\")
            @Available(Package, introduced: \"1.\")
            @Available(Package, introduced: \".1\")
            @Available(Package, introduced: \"test\")
            @Available(Package, introduced: \"test.1.2\")
            @Available(Package, introduced: \"2.1.test\")
            @Available(Package, introduced: \"test.test.test\")
        }
        """

        try assertDirective(Metadata.self, source: source) { directive, problems in
            XCTAssertEqual(8, problems.count)
            let diagnosticIdentifiers = Set(problems.map { $0.diagnostic.identifier })
            let diagnosticExplanations = Set(problems.map { $0.diagnostic.explanation })
            XCTAssertEqual(diagnosticIdentifiers, ["org.swift.docc.HasArgument.introduced.ConversionFailed"])
            XCTAssertEqual(diagnosticExplanations, [
                "Available expects an argument for the \'introduced\' parameter that\'s convertible to a semantic version number (\'[0-9]+(.[0-9]+)?(.[0-9]+)?\')",
            ])
        }
    }
    
    func testValidSemanticVersionFormat() throws {
        let source = """
        @Metadata {
            @Available(iOS, introduced: \"3.5.2\", deprecated: \"5.6.7\")
            @Available(macOS, introduced: \"3.5\", deprecated: \"5.6\")
            @Available(Package, introduced: \"3\", deprecated: \"5\")
        }
        """

        try assertDirective(Metadata.self, source: source) { directive, problems in
            XCTAssertEqual(0, problems.count)

            let directive = try XCTUnwrap(directive)
            XCTAssertEqual(3, directive.availability.count)

            let platforms = directive.availability.map { $0.platform }
            XCTAssertEqual(platforms, [
                .iOS,
                .macOS,
                .other("Package")
            ])
            
            let introducedVersions = directive.availability.map { $0.introduced }
            XCTAssertEqual(introducedVersions, [
                SemanticVersion(major: 3, minor: 5, patch: 2),
                SemanticVersion(major: 3, minor: 5, patch: 0),
                SemanticVersion(major: 3, minor: 0, patch: 0)
            ])
                        
            let deprecatedVersions = directive.availability.map { $0.deprecated }
            XCTAssertEqual(deprecatedVersions, [
                SemanticVersion(major: 5, minor: 6, patch: 7),
                SemanticVersion(major: 5, minor: 6, patch: 0),
                SemanticVersion(major: 5, minor: 0, patch: 0)
            ])

        }
    }

    func testValidIntroducedDirective() throws {
        // Assemble all the combinations of arguments you could give
        let validArguments: [String] = [
          "deprecated: \"1.0\"",
        ]
        // separate those that give a version so we can test the `*` platform separately
        var validArgumentsWithVersion = ["introduced: \"1.0\""]
        for arg in validArguments {
            validArgumentsWithVersion.append("introduced: \"1.0\", \(arg)")
            validArgumentsWithVersion.append("\(arg), introduced: \"1.0\"")
        }

        var checkPlatforms = Metadata.Availability.Platform.defaultCases.map({ $0.rawValue })
        checkPlatforms += [
            "Package",
            "\"My Package\"", // Also check a platform with spaces in the name
            // FIXME: Test validArguments with the `*` platform once that's introduced (https://github.com/apple/swift-docc/issues/969)
//            "*",
        ]
        
        for platform in checkPlatforms {
            for args in validArgumentsWithVersion {
                try assertValidAvailability(source: "@Available(\(platform), \(args))")
            }
        }
    }
        
    /// Basic validity test for giving several directives.
    func testMultipleAvailabilityDirectives() throws {
        let source = """
        @Metadata {
            @Available(macOS, introduced: "11.0")
            @Available(iOS, introduced: "15.0")
            @Available(watchOS, introduced: "7.0", deprecated: "9.0")
            @Available("My Package", introduced: "0.1", deprecated: "1.0")
        }
        """
        try assertValidMetadata(source: source)
    }
    
    func assertDirective<Directive: AutomaticDirectiveConvertible>(_ type: Directive.Type, source: String, assertion assert: (Directive?, [Problem]) throws -> Void) throws {
        let document = Document(parsing: source, options: .parseBlockDirectives)
        let directive = document.child(at: 0) as? BlockDirective
        XCTAssertNotNil(directive)

        let (bundle, context) = try testBundleAndContext(named: "AvailabilityBundle")

        try directive.map { directive in
            var problems = [Problem]()
            XCTAssertEqual(Directive.directiveName, directive.name)
            let converted = Directive(from: directive, source: nil, for: bundle, in: context, problems: &problems)
            try assert(converted, problems)
        }
    }

    func assertValidDirective<Directive: AutomaticDirectiveConvertible>(_ type: Directive.Type, source: String) throws {
        try assertDirective(type, source: source) { directive, problems in
            XCTAssertNotNil(directive)
            XCTAssert(problems.isEmpty)
        }
    }

    func assertValidAvailability(source: String) throws {
        try assertValidDirective(Metadata.Availability.self, source: source)
    }

    func assertValidMetadata(source: String) throws {
        try assertValidDirective(Metadata.self, source: source)
    }
}