|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import XCTest |
| 12 | +import Foundation |
| 13 | +@testable import SymbolKit |
| 14 | + |
| 15 | +class GraphCollectorTests: XCTestCase { |
| 16 | + /// Verify that extension graphs are merged with the correct main graph depending on |
| 17 | + /// the chosen strategy. |
| 18 | + func testExtensionGraphMergingStrategies() throws { |
| 19 | + let a = SymbolGraph(metadata: .init(formatVersion: .init(major: 1, minor: 0, patch: 0), generator: "unit-test"), |
| 20 | + module: .init(name: "A", platform: .init()), |
| 21 | + symbols: [ |
| 22 | + .init(identifier: .init(precise: "s:AA", interfaceLanguage: "swift"), |
| 23 | + names: .init(title: "A", navigator: nil, subHeading: nil, prose: nil), |
| 24 | + pathComponents: ["A"], |
| 25 | + docComment: nil, |
| 26 | + accessLevel: .init(rawValue: "public"), |
| 27 | + kind: .init(parsedIdentifier: .class, displayName: "Class"), |
| 28 | + mixins: [:]) |
| 29 | + ], |
| 30 | + relationships: []) |
| 31 | + |
| 32 | + let b = SymbolGraph(metadata: .init(formatVersion: .init(major: 1, minor: 0, patch: 0), generator: "unit-test"), |
| 33 | + module: .init(name: "B", platform: .init()), |
| 34 | + symbols: [ |
| 35 | + .init(identifier: .init(precise: "s:BB", interfaceLanguage: "swift"), |
| 36 | + names: .init(title: "B", navigator: nil, subHeading: nil, prose: nil), |
| 37 | + pathComponents: ["B"], |
| 38 | + docComment: nil, |
| 39 | + accessLevel: .init(rawValue: "public"), |
| 40 | + kind: .init(parsedIdentifier: .class, displayName: "Class"), |
| 41 | + mixins: [:]) |
| 42 | + ], |
| 43 | + relationships: []) |
| 44 | + |
| 45 | + let a_At_B = SymbolGraph(metadata: .init(formatVersion: .init(major: 1, minor: 0, patch: 0), generator: "unit-test"), |
| 46 | + module: .init(name: "A", platform: .init()), |
| 47 | + symbols: [ |
| 48 | + .init(identifier: .init(precise: "s:BBAatB", interfaceLanguage: "swift"), |
| 49 | + names: .init(title: "AatB", navigator: nil, subHeading: nil, prose: nil), |
| 50 | + pathComponents: ["B", "AatB"], |
| 51 | + docComment: nil, |
| 52 | + accessLevel: .init(rawValue: "public"), |
| 53 | + kind: .init(parsedIdentifier: .class, displayName: "Class"), |
| 54 | + mixins: [:]) |
| 55 | + ], |
| 56 | + relationships: []) |
| 57 | + |
| 58 | + |
| 59 | + // test with default strategy (extension graphs get attached to extend**ed** graph |
| 60 | + var collector = GraphCollector() |
| 61 | + |
| 62 | + collector.mergeSymbolGraph(a, at: .init(fileURLWithPath: "A.symbols.json")) |
| 63 | + collector.mergeSymbolGraph(b, at: .init(fileURLWithPath: "B.symbols.json")) |
| 64 | + collector .mergeSymbolGraph(a_At_B , at : .init (fileURLWithPath : "[email protected]")) |
| 65 | + |
| 66 | + var (unifiedGraphs, _) = collector.finishLoading() |
| 67 | + |
| 68 | + XCTAssertEqual(unifiedGraphs.count, 2) |
| 69 | + |
| 70 | + var graphA = try XCTUnwrap(unifiedGraphs["A"]) |
| 71 | + XCTAssertEqual(graphA.symbols.count, 1) |
| 72 | + XCTAssert(graphA.symbols.keys.contains("s:AA")) |
| 73 | + var graphB = try XCTUnwrap(unifiedGraphs["B"]) |
| 74 | + XCTAssertEqual(graphB.symbols.count, 2) |
| 75 | + XCTAssert(graphB.symbols.keys.contains("s:BB")) |
| 76 | + XCTAssert(graphB.symbols.keys.contains("s:BBAatB")) |
| 77 | + |
| 78 | + // test with extendingGraph association strategy (extension graphs get attached to extend**ing** graph |
| 79 | + collector = GraphCollector(extensionGraphAssociationStrategy: .extendingGraph) |
| 80 | + |
| 81 | + collector.mergeSymbolGraph(a, at: .init(fileURLWithPath: "A.symbols.json")) |
| 82 | + collector.mergeSymbolGraph(b, at: .init(fileURLWithPath: "B.symbols.json")) |
| 83 | + collector .mergeSymbolGraph(a_At_B , at : .init (fileURLWithPath : "[email protected]")) |
| 84 | + |
| 85 | + (unifiedGraphs, _) = collector.finishLoading() |
| 86 | + |
| 87 | + XCTAssertEqual(unifiedGraphs.count, 2) |
| 88 | + |
| 89 | + graphA = try XCTUnwrap(unifiedGraphs["A"]) |
| 90 | + XCTAssertEqual(graphA.symbols.count, 2) |
| 91 | + XCTAssert(graphA.symbols.keys.contains("s:AA")) |
| 92 | + XCTAssert(graphA.symbols.keys.contains("s:BBAatB")) |
| 93 | + graphB = try XCTUnwrap(unifiedGraphs["B"]) |
| 94 | + XCTAssertEqual(graphB.symbols.count, 1) |
| 95 | + XCTAssert(graphB.symbols.keys.contains("s:BB")) |
| 96 | + } |
| 97 | +} |
0 commit comments