|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SourceKitD |
| 14 | +import SKCore |
| 15 | +import TSCBasic |
| 16 | +import TSCUtility |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class SourceKitDTests: XCTestCase { |
| 20 | + static var sourcekitdPath: AbsolutePath! = nil |
| 21 | + static var sdkpath: String? = nil |
| 22 | + |
| 23 | + override class func setUp() { |
| 24 | + sourcekitdPath = ToolchainRegistry.shared.default!.sourcekitd! |
| 25 | + if case .darwin? = Platform.currentPlatform, |
| 26 | + let str = try? Process.checkNonZeroExit( |
| 27 | + args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx") |
| 28 | + { |
| 29 | + sdkpath = str.spm_chomp() |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + func testMultipleNotificationHandlers() { |
| 34 | + let sourcekitd = try! SourceKitDImpl.getOrCreate(dylibPath: SourceKitDTests.sourcekitdPath) |
| 35 | + let keys = sourcekitd.keys |
| 36 | + |
| 37 | + let expectation1 = expectation(description: "handler 1") |
| 38 | + let handler1 = ClosureNotificationHandler { response in |
| 39 | + XCTAssertEqual(response.value?[keys.notification], sourcekitd.values.notification_documentupdate) |
| 40 | + expectation1.fulfill() |
| 41 | + } |
| 42 | + sourcekitd.addNotificationHandler(handler1) |
| 43 | + |
| 44 | + let expectation2 = expectation(description: "handler 2") |
| 45 | + let handler2 = ClosureNotificationHandler { response in |
| 46 | + XCTAssertEqual(response.value?[keys.notification], sourcekitd.values.notification_documentupdate) |
| 47 | + expectation2.fulfill() |
| 48 | + } |
| 49 | + sourcekitd.addNotificationHandler(handler2) |
| 50 | + |
| 51 | + let req = SKDRequestDictionary(sourcekitd: sourcekitd) |
| 52 | + req[keys.request] = sourcekitd.requests.editor_open |
| 53 | + let path: String = #file |
| 54 | + req[keys.name] = path |
| 55 | + req[keys.sourcetext] = """ |
| 56 | + func foo() {} |
| 57 | + """ |
| 58 | + let args = SKDRequestArray(sourcekitd: sourcekitd) |
| 59 | + if let sdkpath = SourceKitDTests.sdkpath { |
| 60 | + args.append("-sdk") |
| 61 | + args.append(sdkpath) |
| 62 | + } |
| 63 | + args.append(path) |
| 64 | + req[keys.compilerargs] = args |
| 65 | + |
| 66 | + _ = try! sourcekitd.sendSync(req) |
| 67 | + |
| 68 | + waitForExpectations(timeout: 15) |
| 69 | + |
| 70 | + let close = SKDRequestDictionary(sourcekitd: sourcekitd) |
| 71 | + close[keys.request] = sourcekitd.requests.editor_close |
| 72 | + close[keys.name] = path |
| 73 | + _ = try! sourcekitd.sendSync(close) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +private class ClosureNotificationHandler: SKDNotificationHandler { |
| 78 | + let f: (SKDResponse) -> Void |
| 79 | + |
| 80 | + init(_ f: @escaping (SKDResponse) -> Void) { |
| 81 | + self.f = f |
| 82 | + } |
| 83 | + |
| 84 | + func notification(_ response: SKDResponse) { |
| 85 | + f(response) |
| 86 | + } |
| 87 | +} |
0 commit comments