xref: /llvm-project/mlir/unittests/Tools/lsp-server-support/Protocol.cpp (revision 878c141adcd3a1ea47c4cc8429af5c8522678536)
1*878c141aSLily Brown //===- Protocol.cpp - LSP JSON protocol unit tests ------------------------===//
2*878c141aSLily Brown //
3*878c141aSLily Brown // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*878c141aSLily Brown // See https://llvm.org/LICENSE.txt for license information.
5*878c141aSLily Brown // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*878c141aSLily Brown //
7*878c141aSLily Brown //===----------------------------------------------------------------------===//
8*878c141aSLily Brown 
9*878c141aSLily Brown #include "mlir/Tools/lsp-server-support/Protocol.h"
10*878c141aSLily Brown 
11*878c141aSLily Brown #include "gtest/gtest.h"
12*878c141aSLily Brown 
13*878c141aSLily Brown using namespace mlir;
14*878c141aSLily Brown using namespace mlir::lsp;
15*878c141aSLily Brown using namespace testing;
16*878c141aSLily Brown 
17*878c141aSLily Brown namespace {
18*878c141aSLily Brown 
TEST(ProtocolTest,DiagnosticTagPresent)19*878c141aSLily Brown TEST(ProtocolTest, DiagnosticTagPresent) {
20*878c141aSLily Brown   Diagnostic diagnostic;
21*878c141aSLily Brown   diagnostic.tags.push_back(DiagnosticTag::Unnecessary);
22*878c141aSLily Brown 
23*878c141aSLily Brown   llvm::json::Value json = toJSON(diagnostic);
24*878c141aSLily Brown   const llvm::json::Object *o = json.getAsObject();
25*878c141aSLily Brown   const llvm::json::Array *v = o->get("tags")->getAsArray();
26*878c141aSLily Brown   EXPECT_EQ(*v, llvm::json::Array{1});
27*878c141aSLily Brown 
28*878c141aSLily Brown   Diagnostic parsed;
29*878c141aSLily Brown   llvm::json::Path::Root root = llvm::json::Path::Root();
30*878c141aSLily Brown   bool success = fromJSON(json, parsed, llvm::json::Path(root));
31*878c141aSLily Brown   EXPECT_TRUE(success);
32*878c141aSLily Brown   ASSERT_EQ(parsed.tags.size(), (size_t)1);
33*878c141aSLily Brown   EXPECT_EQ(parsed.tags.at(0), DiagnosticTag::Unnecessary);
34*878c141aSLily Brown }
35*878c141aSLily Brown 
TEST(ProtocolTest,DiagnosticTagNotPresent)36*878c141aSLily Brown TEST(ProtocolTest, DiagnosticTagNotPresent) {
37*878c141aSLily Brown   Diagnostic diagnostic;
38*878c141aSLily Brown 
39*878c141aSLily Brown   llvm::json::Value json = toJSON(diagnostic);
40*878c141aSLily Brown   const llvm::json::Object *o = json.getAsObject();
41*878c141aSLily Brown   const llvm::json::Value *v = o->get("tags");
42*878c141aSLily Brown   EXPECT_EQ(v, nullptr);
43*878c141aSLily Brown 
44*878c141aSLily Brown   Diagnostic parsed;
45*878c141aSLily Brown   llvm::json::Path::Root root = llvm::json::Path::Root();
46*878c141aSLily Brown   bool success = fromJSON(json, parsed, llvm::json::Path(root));
47*878c141aSLily Brown   EXPECT_TRUE(success);
48*878c141aSLily Brown   EXPECT_TRUE(parsed.tags.empty());
49*878c141aSLily Brown }
50*878c141aSLily Brown 
51*878c141aSLily Brown } // namespace
52