1 | //===- Protocol.cpp - LSP JSON protocol 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/Protocol.h" |
10 | |
11 | #include "gtest/gtest.h" |
12 | |
13 | using namespace mlir; |
14 | using namespace mlir::lsp; |
15 | using namespace testing; |
16 | |
17 | namespace { |
18 | |
19 | TEST(ProtocolTest, DiagnosticTagPresent) { |
20 | Diagnostic diagnostic; |
21 | diagnostic.tags.push_back(x: DiagnosticTag::Unnecessary); |
22 | |
23 | llvm::json::Value json = toJSON(diag: diagnostic); |
24 | const llvm::json::Object *o = json.getAsObject(); |
25 | const llvm::json::Array *v = o->get(K: "tags" )->getAsArray(); |
26 | EXPECT_EQ(*v, llvm::json::Array{1}); |
27 | |
28 | Diagnostic parsed; |
29 | llvm::json::Path::Root root = llvm::json::Path::Root(); |
30 | bool success = fromJSON(value: json, result&: parsed, path: llvm::json::Path(root)); |
31 | EXPECT_TRUE(success); |
32 | ASSERT_EQ(parsed.tags.size(), (size_t)1); |
33 | EXPECT_EQ(parsed.tags.at(0), DiagnosticTag::Unnecessary); |
34 | } |
35 | |
36 | TEST(ProtocolTest, DiagnosticTagNotPresent) { |
37 | Diagnostic diagnostic; |
38 | |
39 | llvm::json::Value json = toJSON(diag: diagnostic); |
40 | const llvm::json::Object *o = json.getAsObject(); |
41 | const llvm::json::Value *v = o->get(K: "tags" ); |
42 | EXPECT_EQ(v, nullptr); |
43 | |
44 | Diagnostic parsed; |
45 | llvm::json::Path::Root root = llvm::json::Path::Root(); |
46 | bool success = fromJSON(value: json, result&: parsed, path: llvm::json::Path(root)); |
47 | EXPECT_TRUE(success); |
48 | EXPECT_TRUE(parsed.tags.empty()); |
49 | } |
50 | |
51 | } // namespace |
52 | |