|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import { expect } from "chai"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { |
| 18 | + mockObject, |
| 19 | + mockGlobalObject, |
| 20 | + mockGlobalModule, |
| 21 | + MockedObject, |
| 22 | + mockFn, |
| 23 | + instance, |
| 24 | +} from "../../MockUtils"; |
| 25 | +import { |
| 26 | + DarwinCompatibleTarget, |
| 27 | + SwiftToolchain, |
| 28 | + getDarwinTargetTriple, |
| 29 | +} from "../../../src/toolchain/toolchain"; |
| 30 | +import { WorkspaceContext } from "../../../src/WorkspaceContext"; |
| 31 | +import { switchPlatform } from "../../../src/commands/switchPlatform"; |
| 32 | +import { StatusItem } from "../../../src/ui/StatusItem"; |
| 33 | +import configuration from "../../../src/configuration"; |
| 34 | + |
| 35 | +suite("Switch Target Platform Unit Tests", () => { |
| 36 | + const mockedConfiguration = mockGlobalModule(configuration); |
| 37 | + const windowMock = mockGlobalObject(vscode, "window"); |
| 38 | + const mockSwiftToolchain = mockGlobalModule(SwiftToolchain); |
| 39 | + let mockContext: MockedObject<WorkspaceContext>; |
| 40 | + let mockedStatusItem: MockedObject<StatusItem>; |
| 41 | + |
| 42 | + setup(() => { |
| 43 | + mockedStatusItem = mockObject<StatusItem>({ |
| 44 | + start: mockFn(), |
| 45 | + end: mockFn(), |
| 46 | + }); |
| 47 | + mockContext = mockObject<WorkspaceContext>({ |
| 48 | + statusItem: instance(mockedStatusItem), |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test("Call Switch Platform and switch to iOS", async () => { |
| 53 | + const selectedItem = { value: DarwinCompatibleTarget.iOS, label: "iOS" }; |
| 54 | + windowMock.showQuickPick.resolves(selectedItem); |
| 55 | + mockSwiftToolchain.getSDKForTarget.resolves(""); |
| 56 | + expect(mockedConfiguration.swiftSDK).to.equal(""); |
| 57 | + |
| 58 | + await switchPlatform(instance(mockContext)); |
| 59 | + |
| 60 | + expect(windowMock.showQuickPick).to.have.been.calledOnce; |
| 61 | + expect(windowMock.showWarningMessage).to.have.been.calledOnceWithExactly( |
| 62 | + "Selecting the iOS target platform will provide code editing support, but compiling with a iOS SDK will have undefined results." |
| 63 | + ); |
| 64 | + expect(mockedStatusItem.start).to.have.been.called; |
| 65 | + expect(mockedStatusItem.end).to.have.been.called; |
| 66 | + expect(mockedConfiguration.swiftSDK).to.equal( |
| 67 | + getDarwinTargetTriple(DarwinCompatibleTarget.iOS) |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + test("Call Switch Platform and switch to macOS", async () => { |
| 72 | + const selectedItem = { value: undefined, label: "macOS" }; |
| 73 | + windowMock.showQuickPick.resolves(selectedItem); |
| 74 | + mockSwiftToolchain.getSDKForTarget.resolves(""); |
| 75 | + expect(mockedConfiguration.swiftSDK).to.equal(""); |
| 76 | + |
| 77 | + await switchPlatform(instance(mockContext)); |
| 78 | + |
| 79 | + expect(windowMock.showQuickPick).to.have.been.calledOnce; |
| 80 | + expect(windowMock.showWarningMessage).to.not.have.been.called; |
| 81 | + expect(mockedStatusItem.start).to.have.been.called; |
| 82 | + expect(mockedStatusItem.end).to.have.been.called; |
| 83 | + expect(mockedConfiguration.swiftSDK).to.equal(""); |
| 84 | + }); |
| 85 | +}); |
0 commit comments