diff --git a/tooling/base/pt_types.h b/tooling/base/pt_types.h index 3535072ef28388d42ab9bed2ff4cd4f54ef6f436..3c6c4078e4c60c996d048095dccaee32f4b5c1b2 100644 --- a/tooling/base/pt_types.h +++ b/tooling/base/pt_types.h @@ -1060,6 +1060,28 @@ public: static std::unique_ptr Create(const PtJson ¶ms); std::unique_ptr ToJson() const override; + int32_t GetLine() const + { + return lineNumber_; + } + + SearchMatch &SetLine(int32_t line) + { + lineNumber_ = line; + return *this; + } + + std::string GetLineContent() const + { + return lineContent_; + } + + SearchMatch &SetLineContent(const std::string lineContent) + { + lineContent_ = lineContent; + return *this; + } + private: NO_COPY_SEMANTIC(SearchMatch); NO_MOVE_SEMANTIC(SearchMatch); diff --git a/tooling/test/BUILD.gn b/tooling/test/BUILD.gn index ca3ba59d40d3129774f981820db735c4de0a3716..1aefd4aad4d42aa93e587d6bb41b3b4d4d0bc214 100644 --- a/tooling/test/BUILD.gn +++ b/tooling/test/BUILD.gn @@ -186,6 +186,7 @@ host_unittest_action("DebuggerTest") { "pt_base64_test.cpp", "pt_json_test.cpp", "pt_params_test.cpp", + "pt_returns_test.cpp", "pt_types_test.cpp", "runtime_impl_test.cpp", "tracing_impl_test.cpp", diff --git a/tooling/test/pt_base64_test.cpp b/tooling/test/pt_base64_test.cpp index 27e3c85ee20d918a24467214751eca3a268a82c6..61c1fa29fe2a3848ac21b3592fe748e78fce8623 100644 --- a/tooling/test/pt_base64_test.cpp +++ b/tooling/test/pt_base64_test.cpp @@ -82,4 +82,36 @@ HWTEST_F_L0(PtBase64Test, ErrorTextTest) EXPECT_EQ(static_cast(len), 0); EXPECT_EQ(des, ""); } + +HWTEST_F_L0(PtBase64Test, EncodeTest) +{ + std::string src = "a"; + std::string des; + uint32_t len = PtBase64::Encode(src, des); + EXPECT_EQ(static_cast(len), 4); + + src = "aa"; + len = PtBase64::Encode(src, des); + EXPECT_EQ(static_cast(len), 4); + + src = "aaa"; + len = PtBase64::Encode(src, des); + EXPECT_EQ(static_cast(len), 4); +} + +HWTEST_F_L0(PtBase64Test, DecodeTest) +{ + std::string str = ""; + std::string des; + uint32_t len = PtBase64::Decode(str, des); + EXPECT_EQ(static_cast(len), 0); + + str = "Hello"; + len = PtBase64::Decode(str, des); + EXPECT_EQ(static_cast(len), 0); + + str = "HelloABC"; + len = PtBase64::Decode(str, des); + EXPECT_EQ(static_cast(len), 6); +} } \ No newline at end of file diff --git a/tooling/test/pt_returns_test.cpp b/tooling/test/pt_returns_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7cd7853588a4f893716f948695f8d6757a0fb38 --- /dev/null +++ b/tooling/test/pt_returns_test.cpp @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "base/pt_returns.h" +#include "ecmascript/tests/test_helper.h" +#include "tooling/protocol_handler.h" + +using namespace panda::ecmascript; +using namespace panda::ecmascript::tooling; + + +namespace panda::test { +class PtReturnsTest : public testing::Test { +public: + static void SetUpTestCase() + { + GTEST_LOG_(INFO) << "SetUpTestCase"; + } + + static void TearDownTestCase() + { + GTEST_LOG_(INFO) << "TearDownCase"; + } + + void SetUp() override + { + TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope); + } + + void TearDown() override + { + TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope); + } + +protected: + EcmaVM *ecmaVm {nullptr}; + EcmaHandleScope *scope {nullptr}; + JSThread *thread {nullptr}; +}; + +HWTEST_F_L0(PtReturnsTest, EvaluateOnCallFrameReturns) +{ + std::unique_ptr result = std::make_unique(); + result->SetType("idle"); + std::unique_ptr evaluateOnCallFrameReturns + = std::make_unique(std::move(result)); + ASSERT_NE(evaluateOnCallFrameReturns, nullptr); + + std::unique_ptr tmpJson; + ASSERT_EQ(evaluateOnCallFrameReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::NOT_EXIST); +} + +HWTEST_F_L0(PtReturnsTest, GetScriptSourceReturns) +{ + std::unique_ptr getScriptSourceReturns = std::make_unique + ("source_1"); + ASSERT_NE(getScriptSourceReturns, nullptr); + + std::unique_ptr tmpJson; + ASSERT_EQ(getScriptSourceReturns->ToJson()->GetObject("bytecode", &tmpJson), Result::NOT_EXIST); +} + +HWTEST_F_L0(PtReturnsTest, SearchInContentReturns) +{ + auto result = std::vector>(); + std::unique_ptr tmpResult = std::make_unique(); + tmpResult->SetLine(13); + tmpResult->SetLineContent("line-10"); + result.emplace_back(std::move(tmpResult)); + std::unique_ptr searchInContentReturns + = std::make_unique(std::move(result)); + ASSERT_NE(searchInContentReturns, nullptr); + + std::unique_ptr json; + ASSERT_EQ(searchInContentReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); + ASSERT_NE(json, nullptr); + EXPECT_EQ(json->GetSize(), 1); +} + +HWTEST_F_L0(PtReturnsTest, SetScriptSourceReturns) +{ + auto callFrames = std::vector>(); + std::unique_ptr callFrame = std::make_unique(); + std::unique_ptr location = std::make_unique(); + location->SetScriptId(3).SetLine(36); + std::unique_ptr res = std::make_unique(); + res->SetType("idle5"); + + callFrame->SetCallFrameId(33); + callFrame->SetLocation(std::move(location)); + callFrame->SetThis(std::move(res)); + callFrames.emplace_back(std::move(callFrame)); + + std::unique_ptr exceptionDetails = std::make_unique(); + exceptionDetails->SetExceptionId(12); + std::unique_ptr setScriptSourceReturns + = std::make_unique (std::move(callFrames), + true, std::move(exceptionDetails)); + ASSERT_NE(setScriptSourceReturns, nullptr); + + std::unique_ptr callFramesJson; + ASSERT_EQ(setScriptSourceReturns->ToJson()->GetArray("callFrames", &callFramesJson), Result::SUCCESS); + ASSERT_NE(callFramesJson, nullptr); + EXPECT_EQ(callFramesJson->GetSize(), 1); + bool tmpStackChanged; + ASSERT_EQ(setScriptSourceReturns->ToJson()->GetBool("stackChanged", &tmpStackChanged), Result::SUCCESS); + ASSERT_TRUE(tmpStackChanged); + std::unique_ptr exceptionDetailJson; + ASSERT_EQ(setScriptSourceReturns->ToJson()->GetObject("exceptionDetails", &exceptionDetailJson), Result::SUCCESS); + + std::unique_ptr setScriptSource = std::make_unique(std::nullopt); + ASSERT_NE(setScriptSource, nullptr); + + std::unique_ptr callJson; + ASSERT_EQ(setScriptSource->ToJson()->GetArray("callFrames", &callJson), Result::NOT_EXIST); + bool stack; + ASSERT_EQ(setScriptSource->ToJson()->GetBool("stackChanged", &stack), Result::NOT_EXIST); + std::unique_ptr exceptionJson; + ASSERT_EQ(setScriptSource->ToJson()->GetObject("exceptionDetails", &exceptionJson), Result::NOT_EXIST); +} + +HWTEST_F_L0(PtReturnsTest, GetPropertiesReturns) +{ + auto descriptor = std::vector>(); + std::unique_ptr propertyDescriptor + = std::make_unique(); + propertyDescriptor->SetName("filename1").SetConfigurable(true).SetEnumerable(true); + descriptor.emplace_back(std::move(propertyDescriptor)); + + auto internalPropertyDescripties = std::vector>(); + std::unique_ptr internalPropertyDescriptor + = std::make_unique(); + internalPropertyDescriptor->SetName("filename2"); + internalPropertyDescripties.emplace_back(std::move(internalPropertyDescriptor)); + + auto privateProperties = std::vector>(); + std::unique_ptr privatePropertyDescriptor + = std::make_unique(); + privatePropertyDescriptor->SetName("filename3"); + privateProperties.emplace_back(std::move(privatePropertyDescriptor)); + + std::unique_ptr exceptionDetails = std::make_unique(); + exceptionDetails->SetExceptionId(12); + std::unique_ptr getPropertiesReturns + = std::make_unique(std::move(descriptor), + std::move(internalPropertyDescripties), + std::move(privateProperties), + std::move(exceptionDetails)); + ASSERT_NE(getPropertiesReturns, nullptr); + + std::unique_ptr resultJson; + ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("result", &resultJson), Result::SUCCESS); + ASSERT_NE(resultJson, nullptr); + EXPECT_EQ(resultJson->GetSize(), 1); + + std::unique_ptr internalJson; + ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("internalProperties", &internalJson), Result::SUCCESS); + ASSERT_NE(internalJson, nullptr); + EXPECT_EQ(internalJson->GetSize(), 1); + + std::unique_ptr privateJson; + ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("privateProperties", &privateJson), Result::SUCCESS); + ASSERT_NE(privateJson, nullptr); + EXPECT_EQ(privateJson->GetSize(), 1); + + std::unique_ptr tmpJson; + ASSERT_EQ(getPropertiesReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::SUCCESS); +} + +HWTEST_F_L0(PtReturnsTest, GetBestEffortCoverageReturns) +{ + auto coverages = std::vector>(); + std::unique_ptr coverage = std::make_unique(); + coverage->SetStartOffset(0); + coverage->SetEndOffset(10); + coverage->SetCount(5); + coverages.emplace_back(std::move(coverage)); + + auto functions = std::vector>(); + std::unique_ptr functionCoverage = std::make_unique(); + functionCoverage->SetFunctionName("functionName1"); + functionCoverage->SetisBlockCoverage(true); + functionCoverage->SetFunctions(std::move(coverages)); + + auto result = std::vector>(); + std::unique_ptr scriptCoverage = std::make_unique(); + scriptCoverage->SetScriptId("13"); + scriptCoverage->SetUrl("url13"); + scriptCoverage->SetFunctions(std::move(functions)); + result.emplace_back(std::move(scriptCoverage)); + + std::unique_ptr getBestEffortCoverageReturns + = std::make_unique(std::move(result)); + + std::unique_ptr json; + ASSERT_EQ(getBestEffortCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); + ASSERT_NE(json, nullptr); + EXPECT_EQ(json->GetSize(), 1); +} + +HWTEST_F_L0(PtReturnsTest, TakePreciseCoverageReturns) +{ + auto coverages = std::vector>(); + std::unique_ptr coverage = std::make_unique(); + coverage->SetStartOffset(0); + coverage->SetEndOffset(10); + coverage->SetCount(5); + coverages.emplace_back(std::move(coverage)); + + auto functions = std::vector>(); + std::unique_ptr functionCoverage = std::make_unique(); + functionCoverage->SetFunctionName("functionName1"); + functionCoverage->SetisBlockCoverage(true); + functionCoverage->SetFunctions(std::move(coverages)); + + auto result = std::vector>(); + std::unique_ptr scriptCoverage = std::make_unique(); + scriptCoverage->SetScriptId("13"); + scriptCoverage->SetUrl("url13"); + scriptCoverage->SetFunctions(std::move(functions)); + result.emplace_back(std::move(scriptCoverage)); + + std::unique_ptr takePreciseCoverageReturns + = std::make_unique(std::move(result), 1001); + ASSERT_NE(takePreciseCoverageReturns, nullptr); + + std::unique_ptr json; + ASSERT_EQ(takePreciseCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); + ASSERT_NE(json, nullptr); + EXPECT_EQ(json->GetSize(), 1); +} + +HWTEST_F_L0(PtReturnsTest, TakeTypeProfileReturns) +{ + auto types = std::vector>(); + std::unique_ptr typeObject = std::make_unique(); + typeObject->SetName("object1"); + + auto entries = std::vector>(); + std::unique_ptr typeProfileEntry = std::make_unique(); + typeProfileEntry->SetOffset(4); + typeProfileEntry->SetTypes(std::move(types)); + + auto result = std::vector>(); + std::unique_ptr scriptTypeProfile = std::make_unique(); + scriptTypeProfile->SetUrl("url5"); + scriptTypeProfile->SetScriptId("id5"); + scriptTypeProfile->SetEntries(std::move(entries)); + result.emplace_back(std::move(scriptTypeProfile)); + + std::unique_ptr takeTypeProfileturns + = std::make_unique(std::move(result)); + + std::unique_ptr json; + ASSERT_EQ(takeTypeProfileturns->ToJson()->GetArray("result", &json), Result::SUCCESS); + ASSERT_NE(json, nullptr); + EXPECT_EQ(json->GetSize(), 1); +} + +HWTEST_F_L0(PtReturnsTest, GetCategoriesReturns) +{ + auto result = std::vector(); + std::string str = "getCategories"; + result.emplace_back(str); + std::unique_ptr getCategoriesReturns = std::make_unique + (std::move(result)); + + std::unique_ptr json; + ASSERT_EQ(getCategoriesReturns->ToJson()->GetArray("categories", &json), Result::SUCCESS); + ASSERT_NE(json, nullptr); + EXPECT_EQ(json->GetSize(), 1); +} +} \ No newline at end of file