1 /**
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "lsp/include/cancellation_token.h"
17 #include <gtest/gtest.h>
18 #include "lsp_api_test.h"
19
20 const int DEFAULT_THROTTLE = 20;
21 const int NO_THROTTLE = 0;
22
23 class LspCancellationTokenTests : public LSPAPITests {};
24
TEST_F(LspCancellationTokenTests,CancellationTokenHostNull)25 TEST_F(LspCancellationTokenTests, CancellationTokenHostNull)
26 {
27 ark::es2panda::lsp::CancellationToken cancellationToken(DEFAULT_THROTTLE, nullptr);
28 ASSERT_EQ(false, cancellationToken.IsCancellationRequested());
29 }
30
TEST_F(LspCancellationTokenTests,CancellationTokenHostNotNull)31 TEST_F(LspCancellationTokenTests, CancellationTokenHostNotNull)
32 {
33 class LanguageHost : public ark::es2panda::lsp::HostCancellationToken {
34 public:
35 bool IsCancellationRequested() override
36 {
37 return true;
38 }
39 };
40
41 LanguageHost host;
42 ark::es2panda::lsp::CancellationToken cancellationToken(DEFAULT_THROTTLE, &host);
43
44 ASSERT_EQ(true, cancellationToken.IsCancellationRequested());
45 }
46
TEST_F(LspCancellationTokenTests,CancellationTokenThrottledCancellationCheck)47 TEST_F(LspCancellationTokenTests, CancellationTokenThrottledCancellationCheck)
48 {
49 class LanguageHost : public ark::es2panda::lsp::HostCancellationToken {
50 public:
51 bool IsCancellationRequested() override
52 {
53 return true;
54 }
55 };
56
57 LanguageHost host;
58 ark::es2panda::lsp::CancellationToken cancellationToken(DEFAULT_THROTTLE, &host);
59
60 ASSERT_EQ(true, cancellationToken.IsCancellationRequested());
61 ASSERT_EQ(false, cancellationToken.ThrottledCancellationCheck());
62 }
63
TEST_F(LspCancellationTokenTests,CancellationTokenThrottledCancellationCheck2)64 TEST_F(LspCancellationTokenTests, CancellationTokenThrottledCancellationCheck2)
65 {
66 class LanguageHost : public ark::es2panda::lsp::HostCancellationToken {
67 public:
68 bool IsCancellationRequested() override
69 {
70 return true;
71 }
72 };
73
74 LanguageHost host;
75 ark::es2panda::lsp::CancellationToken cancellationToken(NO_THROTTLE, &host);
76
77 ASSERT_EQ(true, cancellationToken.IsCancellationRequested());
78 ASSERT_EQ(true, cancellationToken.ThrottledCancellationCheck());
79 }
80