Skip to content

Commit 1916333

Browse files
committed
Add tests
1 parent f524bb1 commit 1916333

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed

src/commands/runPlayground.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ export interface PlaygroundItem {
2424
}
2525

2626
export interface DocumentPlaygroundItem extends PlaygroundItem {
27-
id: string;
28-
label?: string;
2927
range: Range;
3028
}
3129

3230
export interface WorkspacePlaygroundItem extends PlaygroundItem {
33-
id: string;
34-
label?: string;
3531
location: Location;
3632
}
3733

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 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+
import { expect } from "chai";
15+
import * as vscode from "vscode";
16+
17+
import { FolderContext } from "@src/FolderContext";
18+
import { WorkspaceContext } from "@src/WorkspaceContext";
19+
import { Commands } from "@src/commands";
20+
import { SwiftTask } from "@src/tasks/SwiftTaskProvider";
21+
22+
import { mockGlobalObject } from "../../MockUtils";
23+
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";
24+
25+
suite("Run Playground Command", function () {
26+
let folderContext: FolderContext;
27+
let workspaceContext: WorkspaceContext;
28+
29+
const mockTasks = mockGlobalObject(vscode, "tasks");
30+
31+
activateExtensionForSuite({
32+
async setup(ctx) {
33+
workspaceContext = ctx;
34+
folderContext = await folderInRootWorkspace("defaultPackage", workspaceContext);
35+
},
36+
});
37+
38+
setup(async () => {
39+
await workspaceContext.focusFolder(folderContext);
40+
});
41+
42+
test("No playground item provided", async () => {
43+
expect(await vscode.commands.executeCommand(Commands.PLAY), undefined).to.be.false;
44+
expect(mockTasks.executeTask).to.not.have.been.called;
45+
});
46+
47+
test("No folder focussed", async () => {
48+
await workspaceContext.focusFolder(null);
49+
expect(
50+
await vscode.commands.executeCommand(Commands.PLAY, {
51+
id: "PackageLib/PackageLib.swift:3",
52+
})
53+
).to.be.false;
54+
expect(mockTasks.executeTask).to.not.have.been.called;
55+
});
56+
57+
test('Runs "swift play" on "id"', async () => {
58+
expect(
59+
await vscode.commands.executeCommand(Commands.PLAY, {
60+
id: "PackageLib/PackageLib.swift:3",
61+
})
62+
).to.be.true;
63+
expect(mockTasks.executeTask).to.have.been.calledOnce;
64+
65+
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
66+
expect(task.execution.args).to.deep.equal(["play", "PackageLib/PackageLib.swift:3"]);
67+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
68+
});
69+
70+
test('Runs "swift play" on "id" with space in path', async () => {
71+
expect(
72+
await vscode.commands.executeCommand(Commands.PLAY, {
73+
id: "PackageLib/Package Lib.swift:3",
74+
})
75+
).to.be.true;
76+
expect(mockTasks.executeTask).to.have.been.calledOnce;
77+
78+
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
79+
expect(task.execution.args).to.deep.equal(["play", "PackageLib/Package Lib.swift:3"]);
80+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
81+
});
82+
83+
test('Runs "swift play" on "label"', async () => {
84+
expect(
85+
await vscode.commands.executeCommand(Commands.PLAY, {
86+
id: "PackageLib/PackageLib.swift:3",
87+
label: "bar",
88+
})
89+
).to.be.true;
90+
expect(mockTasks.executeTask).to.have.been.calledOnce;
91+
92+
const task = mockTasks.executeTask.args[0][0] as SwiftTask;
93+
expect(task.execution.args).to.deep.equal(["play", "bar"]);
94+
expect(task.execution.options.cwd).to.equal(folderContext.folder.fsPath);
95+
});
96+
});

test/unit-tests/sourcekit-lsp/LanguageClientManager.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ suite("LanguageClientManager Suite", () => {
547547
},
548548
isResolved: true,
549549
},
550+
{
551+
range: new vscode.Range(0, 0, 0, 0),
552+
command: {
553+
title: 'Play "bar"',
554+
command: "swift.play",
555+
},
556+
isResolved: true,
557+
},
550558
{
551559
range: new vscode.Range(0, 0, 0, 0),
552560
command: {
@@ -588,6 +596,14 @@ suite("LanguageClientManager Suite", () => {
588596
},
589597
isResolved: true,
590598
},
599+
{
600+
range: new vscode.Range(0, 0, 0, 0),
601+
command: {
602+
title: '$(play)\u00A0Play "bar"',
603+
command: "swift.play",
604+
},
605+
isResolved: true,
606+
},
591607
{
592608
range: new vscode.Range(0, 0, 0, 0),
593609
command: {

0 commit comments

Comments
 (0)