Skip to content

Commit d51edfc

Browse files
authored
Add integration tests for Expand Macro actions (#1206)
* Add integration tests for Inline/Expand Macro actions - Validate the workflow of user calling Inline/Expand Macro actions on a swift project with macro - Add test fixture for swift macro - Verify inline macro by asserting on inlined value after calling the action - Verify expand macro by asserting expanded macro document contain the right macro Issue: #1205
1 parent cecfaed commit d51edfc

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

assets/test/swift-macro/Package.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// swift-tools-version:5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
import CompilerPluginSupport
6+
7+
let package = Package(
8+
name: "swift-macro",
9+
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
10+
products: [
11+
// Products define the executables and libraries a package produces, making them visible to other packages.
12+
.library(
13+
name: "swift-macro",
14+
targets: ["swift-macro"]
15+
),
16+
.executable(
17+
name: "swift-macroClient",
18+
targets: ["swift-macroClient"]
19+
),
20+
],
21+
dependencies: [
22+
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0-latest"),
23+
],
24+
targets: [
25+
// Targets are the basic building blocks of a package, defining a module or a test suite.
26+
// Targets can depend on other targets in this package and products from dependencies.
27+
// Macro implementation that performs the source transformation of a macro.
28+
.macro(
29+
name: "swift-macroMacros",
30+
dependencies: [
31+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
32+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
33+
]
34+
),
35+
36+
// Library that exposes a macro as part of its API, which is used in client programs.
37+
.target(name: "swift-macro", dependencies: ["swift-macroMacros"]),
38+
39+
// A client of the library, which is able to use the macro in its own code.
40+
.executableTarget(name: "swift-macroClient", dependencies: ["swift-macro"]),
41+
]
42+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
/// A macro that produces both a value and a string containing the
5+
/// source code that generated the value. For example,
6+
///
7+
/// #stringify(x + y)
8+
///
9+
/// produces a tuple `(x + y, "x + y")`.
10+
@freestanding(expression)
11+
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "swift_macroMacros", type: "StringifyMacro")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import swift_macro
2+
3+
let a = 17
4+
let b = 25
5+
6+
let (result, code) = #stringify(a + b)
7+
8+
print("The value \(result) was produced by the code \"\(code)\"")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import SwiftCompilerPlugin
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
import SwiftSyntaxMacros
5+
6+
/// Implementation of the `stringify` macro, which takes an expression
7+
/// of any type and produces a tuple containing the value of that expression
8+
/// and the source code that produced the value. For example
9+
///
10+
/// #stringify(x + y)
11+
///
12+
/// will expand to
13+
///
14+
/// (x + y, "x + y")
15+
public struct StringifyMacro: ExpressionMacro {
16+
public static func expansion(
17+
of node: some FreestandingMacroExpansionSyntax,
18+
in context: some MacroExpansionContext
19+
) -> ExprSyntax {
20+
guard let argument = node.arguments.first?.expression else {
21+
fatalError("compiler bug: the macro does not have any arguments")
22+
}
23+
24+
return "(\(argument), \(literal: argument.description))"
25+
}
26+
}
27+
28+
@main
29+
struct swift_macroPlugin: CompilerPlugin {
30+
let providingMacros: [Macro.Type] = [
31+
StringifyMacro.self,
32+
]
33+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 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 * as vscode from "vscode";
16+
import * as langclient from "vscode-languageclient/node";
17+
import { expect } from "chai";
18+
import { LanguageClientManager } from "../../../src/sourcekit-lsp/LanguageClientManager";
19+
import { WorkspaceContext } from "../../../src/WorkspaceContext";
20+
import { testAssetUri } from "../../fixtures";
21+
import { FolderContext } from "../../../src/FolderContext";
22+
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities";
23+
import { getBuildAllTask, SwiftTask } from "../../../src/tasks/SwiftTaskProvider";
24+
import { Version } from "../../../src/utilities/version";
25+
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";
26+
27+
async function waitForClientState(
28+
languageClientManager: LanguageClientManager,
29+
expectedState: langclient.State
30+
): Promise<langclient.State> {
31+
let clientState = undefined;
32+
while (clientState !== expectedState) {
33+
clientState = await languageClientManager.useLanguageClient(async client => client.state);
34+
console.warn("Language client is not ready yet. Retrying in 100 ms...");
35+
await new Promise(resolve => setTimeout(resolve, 100));
36+
}
37+
return clientState;
38+
}
39+
40+
suite("Integration, Macros Functionality Support with Sourcekit-lsp", function () {
41+
// Take around 60 seconds if running in isolation, longer than default timeout
42+
this.timeout(2 * 60 * 1000);
43+
44+
let clientManager: LanguageClientManager;
45+
let workspaceContext: WorkspaceContext;
46+
let folderContext: FolderContext;
47+
48+
activateExtensionForSuite({
49+
async setup(ctx) {
50+
workspaceContext = ctx;
51+
// Expand Macro support in Swift started from 6.1
52+
if (workspaceContext.swiftVersion.isLessThan(new Version(6, 1, 0))) {
53+
this.skip();
54+
}
55+
56+
// Wait for a clean starting point, and build all tasks for the fixture
57+
await waitForNoRunningTasks();
58+
folderContext = await folderInRootWorkspace("swift-macro", workspaceContext);
59+
await workspaceContext.focusFolder(folderContext);
60+
const tasks = (await getBuildAllTask(folderContext)) as SwiftTask;
61+
const { exitCode, output } = await executeTaskAndWaitForResult(tasks);
62+
expect(exitCode, `${output}`).to.equal(0);
63+
64+
// Ensure lsp client is ready
65+
clientManager = workspaceContext.languageClientManager;
66+
const clientState = await waitForClientState(clientManager, langclient.State.Running);
67+
expect(clientState).to.equals(langclient.State.Running);
68+
},
69+
});
70+
71+
test("Expand Macro", async function () {
72+
// Focus on the file of interest
73+
const uri = testAssetUri("swift-macro/Sources/swift-macroClient/main.swift");
74+
await vscode.window.showTextDocument(uri);
75+
76+
// Beginning of macro, #
77+
const position = new vscode.Position(5, 21);
78+
79+
// Create a range starting and ending at the specified position
80+
const range = new vscode.Range(position, position);
81+
82+
// Execute the code action provider command
83+
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
84+
"vscode.executeCodeActionProvider",
85+
uri,
86+
range
87+
);
88+
89+
const expectedMacro = '(a + b, "a + b")';
90+
91+
// Find the "expand.macro.command" action
92+
const expandMacroAction = codeActions.find(
93+
action => action.command?.command === "expand.macro.command"
94+
);
95+
96+
// Assert that the expand macro command is available
97+
expect(expandMacroAction).is.not.undefined;
98+
99+
// Set up a promise that resolves when the expected document is opened
100+
const expandedMacroUriPromise = new Promise<vscode.TextDocument>((resolve, reject) => {
101+
const disposable = vscode.workspace.onDidOpenTextDocument(openedDocument => {
102+
if (openedDocument.uri.scheme === "sourcekit-lsp") {
103+
disposable.dispose(); // Stop listening once we find the desired document
104+
resolve(openedDocument);
105+
}
106+
});
107+
108+
// Set a timeout to reject the promise if the document is not found
109+
setTimeout(() => {
110+
disposable.dispose();
111+
reject(new Error("Timed out waiting for sourcekit-lsp document to be opened."));
112+
}, 10000); // Wait up to 10 seconds for the document
113+
});
114+
115+
// Run expand macro action
116+
const command = expandMacroAction!.command!;
117+
expect(command.arguments).is.not.undefined;
118+
const commandArgs = command.arguments!;
119+
await vscode.commands.executeCommand(command.command, ...commandArgs);
120+
121+
// Wait for the expanded macro document to be opened
122+
const referenceDocument = await expandedMacroUriPromise;
123+
124+
// Verify that the reference document was successfully opened
125+
expect(referenceDocument).to.not.be.undefined;
126+
127+
// Assert that the content contains the expected result
128+
const content = referenceDocument.getText();
129+
expect(content).to.include(expectedMacro);
130+
});
131+
});

0 commit comments

Comments
 (0)