Skip to content

Commit 84bc21f

Browse files
authored
[mlir-lsp] Add transport unit tests (#89855)
Add unit tests for some aspects of the JSON transport and message handler. These will be expanded in future patches as behavior is modified.
1 parent d609029 commit 84bc21f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

mlir/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_subdirectory(Support)
2020
add_subdirectory(Rewrite)
2121
add_subdirectory(TableGen)
2222
add_subdirectory(Target)
23+
add_subdirectory(Tools)
2324
add_subdirectory(Transforms)
2425

2526
if(MLIR_ENABLE_EXECUTION_ENGINE)

mlir/unittests/Tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(lsp-server-support)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_mlir_unittest(MLIRLspServerSupportTests
2+
Transport.cpp
3+
)
4+
target_link_libraries(MLIRLspServerSupportTests
5+
PRIVATE
6+
MLIRLspServerSupportLib)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===- Transport.cpp - LSP JSON transport unit tests ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir/Tools/lsp-server-support/Transport.h"
10+
#include "llvm/ADT/ScopeExit.h"
11+
#include "llvm/Support/FileSystem.h"
12+
#include "gmock/gmock.h"
13+
#include "gtest/gtest.h"
14+
15+
using namespace mlir;
16+
using namespace mlir::lsp;
17+
using namespace testing;
18+
19+
namespace {
20+
21+
TEST(TransportTest, SendReply) {
22+
std::string out;
23+
llvm::raw_string_ostream os(out);
24+
JSONTransport transport(nullptr, os);
25+
MessageHandler handler(transport);
26+
27+
transport.reply(1989, nullptr);
28+
EXPECT_THAT(out, HasSubstr("\"id\":1989"));
29+
EXPECT_THAT(out, HasSubstr("\"result\":null"));
30+
}
31+
32+
TEST(TransportTest, MethodNotFound) {
33+
auto tempOr = llvm::sys::fs::TempFile::create("lsp-unittest-%%%%%%.json");
34+
ASSERT_TRUE((bool)tempOr);
35+
auto discardTemp =
36+
llvm::make_scope_exit([&]() { ASSERT_FALSE((bool)tempOr->discard()); });
37+
38+
{
39+
std::error_code ec;
40+
llvm::raw_fd_ostream os(tempOr->TmpName, ec);
41+
ASSERT_FALSE(ec);
42+
os << "{\"jsonrpc\":\"2.0\",\"id\":29,\"method\":\"ack\"}\n";
43+
os.close();
44+
}
45+
46+
std::string out;
47+
llvm::raw_string_ostream os(out);
48+
std::FILE *in = std::fopen(tempOr->TmpName.c_str(), "r");
49+
auto closeIn = llvm::make_scope_exit([&]() { std::fclose(in); });
50+
51+
JSONTransport transport(in, os, JSONStreamStyle::Delimited);
52+
MessageHandler handler(transport);
53+
54+
bool gotEOF = false;
55+
llvm::Error err = llvm::handleErrors(
56+
transport.run(handler), [&](const llvm::ECError &ecErr) {
57+
gotEOF = ecErr.convertToErrorCode() == std::errc::io_error;
58+
});
59+
llvm::consumeError(std::move(err));
60+
EXPECT_TRUE(gotEOF);
61+
EXPECT_THAT(out, HasSubstr("\"id\":29"));
62+
EXPECT_THAT(out, HasSubstr("\"error\""));
63+
EXPECT_THAT(out, HasSubstr("\"message\":\"method not found: ack\""));
64+
}
65+
} // namespace

0 commit comments

Comments
 (0)