1 /** 2 * Copyright (c) 2022 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 #ifndef PANDA_TOOLING_INSPECTOR_TEST_INSPECTOR_TEST_BASE_H 17 #define PANDA_TOOLING_INSPECTOR_TEST_INSPECTOR_TEST_BASE_H 18 19 #include "../inspector.h" 20 #include "client.h" 21 #include "combined_event_loop.h" 22 #include "test_debugger.h" 23 #include "test_logger.h" 24 #include "test_server.h" 25 26 #include "gtest/gtest.h" 27 #include "gmock/gmock.h" 28 29 #include <functional> 30 #include <string> 31 #include <type_traits> 32 #include <utility> 33 34 namespace panda { 35 class Class; 36 class JsonObject; 37 class JsonObjectBuilder; 38 } // namespace panda 39 40 namespace panda::tooling::inspector::test { 41 class InspectorTestBase : public testing::Test { 42 protected: AttachDebugger()43 virtual bool AttachDebugger() const 44 { 45 return true; 46 } 47 48 void SetUp() override; 49 void TearDown() override; 50 51 Class *LoadSourceFile(const std::string &source); 52 53 template <typename Function, typename Result = std::invoke_result_t<Function, const JsonObject &>, 54 typename = std::enable_if_t<std::is_void_v<Result>>> CallSync(const char * method,std::function<void (JsonObjectBuilder &)> && params,Function && handler)55 void CallSync(const char *method, std::function<void(JsonObjectBuilder &)> &¶ms, Function &&handler) 56 { 57 client_.Call(method, std::move(params), 58 [&](auto &object) { std::invoke(std::forward<Function>(handler), object); }); 59 (server_ + client_).Poll(); 60 } 61 CallSync(const char * method,std::function<void (JsonObjectBuilder &)> && params)62 void CallSync(const char *method, std::function<void(JsonObjectBuilder &)> &¶ms) 63 { 64 CallSync(method, std::move(params), [](auto & /* result */) {}); 65 } 66 67 template <typename Function, typename Result = std::invoke_result_t<Function, const JsonObject &>, 68 typename = std::enable_if_t<!std::is_void_v<Result>>> CallSync(const char * method,std::function<void (JsonObjectBuilder &)> && params,Function && handler)69 Result CallSync(const char *method, std::function<void(JsonObjectBuilder &)> &¶ms, Function &&handler) 70 { 71 Result result; 72 CallSync(method, std::move(params), 73 [&](auto &object) { result = std::invoke(std::forward<Function>(handler), object); }); 74 return result; 75 } 76 77 TestLogger logger_ {TestLogger::FAIL_ON_ERROR | TestLogger::OUTPUT_ON_FAIL}; 78 testing::StrictMock<TestDebugger> debugger_; 79 Client client_ {"client", logger_}; 80 TestServer server_ {"server", logger_}; 81 Inspector inspector_ {server_, &debugger_}; 82 83 private: SetUpSourceFiles()84 virtual void SetUpSourceFiles() {} 85 }; 86 } // namespace panda::tooling::inspector::test 87 88 #endif // PANDA_TOOLING_INSPECTOR_TEST_INSPECTOR_TEST_BASE_H 89