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#include "ecmascript/tests/test_helper.h" 17#include "tooling/base/pt_json.h" 18 19using namespace panda::ecmascript::tooling; 20 21namespace panda::test { 22class PtJsonTest : public testing::Test { 23public: 24 static void SetUpTestCase() 25 { 26 GTEST_LOG_(INFO) << "SetUpTestCase"; 27 } 28 29 static void TearDownTestCase() 30 { 31 GTEST_LOG_(INFO) << "TearDownCase"; 32 } 33 34 void SetUp() override 35 { 36 } 37 38 void TearDown() override 39 { 40 } 41}; 42 43HWTEST_F_L0(PtJsonTest, FalseTest) 44{ 45 std::string str = "false"; 46 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 47 ASSERT_TRUE(json->IsBool()); 48 EXPECT_FALSE(json->GetBool()); 49 EXPECT_EQ(json->Stringify(), str); 50 json->ReleaseRoot(); 51} 52 53HWTEST_F_L0(PtJsonTest, TrueTest) 54{ 55 std::string str = "true"; 56 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 57 ASSERT_TRUE(json->IsBool()); 58 EXPECT_TRUE(json->GetBool()); 59 EXPECT_EQ(json->Stringify(), str); 60 json->ReleaseRoot(); 61} 62 63HWTEST_F_L0(PtJsonTest, IntTest) 64{ 65 std::string str = "100"; 66 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 67 ASSERT_TRUE(json->IsNumber()); 68 EXPECT_EQ(json->GetInt(), 100); 69 EXPECT_EQ(json->Stringify(), str); 70 json->ReleaseRoot(); 71} 72 73HWTEST_F_L0(PtJsonTest, Int64Test) 74{ 75 std::string str = "123456789012345"; 76 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 77 ASSERT_TRUE(json->IsNumber()); 78 EXPECT_EQ(json->GetInt64(), 123456789012345); 79 EXPECT_EQ(json->Stringify(), str); 80 json->ReleaseRoot(); 81} 82 83HWTEST_F_L0(PtJsonTest, DoubleTest) 84{ 85 std::string str = "12345.6789"; 86 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 87 ASSERT_TRUE(json->IsNumber()); 88 EXPECT_EQ(json->GetDouble(), 12345.6789); 89 EXPECT_EQ(json->Stringify(), str); 90 json->ReleaseRoot(); 91} 92 93HWTEST_F_L0(PtJsonTest, StringTest) 94{ 95 std::string str = "\"abcdefg\""; 96 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 97 ASSERT_TRUE(json->IsString()); 98 EXPECT_EQ(json->GetString(), "abcdefg"); 99 EXPECT_EQ(json->Stringify(), str); 100 json->ReleaseRoot(); 101} 102 103HWTEST_F_L0(PtJsonTest, ArrayTest1) 104{ 105 std::string str = "[\"a\",\"b\",200]"; 106 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 107 ASSERT_TRUE(json->IsArray()); 108 EXPECT_EQ(json->GetSize(), 3); 109 EXPECT_EQ(json->Get(0)->GetString(), "a"); 110 EXPECT_EQ(json->Get(1)->GetString(), "b"); 111 EXPECT_EQ(json->Get(2)->GetInt(), 200); 112 EXPECT_EQ(json->Stringify(), str); 113 json->ReleaseRoot(); 114} 115 116HWTEST_F_L0(PtJsonTest, ArrayTest2) 117{ 118 std::string str = "[\"a\",\"b\",200,10.5,{}]"; 119 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 120 ASSERT_TRUE(json->IsArray()); 121 EXPECT_EQ(json->GetSize(), 5); 122 EXPECT_EQ(json->Get(0)->GetString(), "a"); 123 EXPECT_EQ(json->Get(1)->GetString(), "b"); 124 EXPECT_EQ(json->Get(2)->GetInt(), 200); 125 EXPECT_EQ(json->Get(3)->GetDouble(), 10.5); 126 EXPECT_TRUE(json->Get(4)->IsObject()); 127 EXPECT_EQ(json->Stringify(), str); 128 json->ReleaseRoot(); 129} 130 131HWTEST_F_L0(PtJsonTest, ObjectTest) 132{ 133 auto child1 = PtJson::CreateObject(); 134 child1->Add("ch", "child_1"); 135 ASSERT_TRUE(child1->Contains("ch")); 136 137 auto child2 = PtJson::CreateObject(); 138 child2->Add("ch", "child_2"); 139 ASSERT_TRUE(child2->Contains("ch")); 140 141 auto arr = PtJson::CreateArray(); 142 arr->Push(100); 143 EXPECT_EQ(arr->GetSize(), 1); 144 145 auto root = PtJson::CreateObject(); 146 root->Add("a", false); 147 root->Add("b", 100); 148 root->Add("c", 100.2); 149 root->Add("d", static_cast<int64_t>(200)); 150 root->Add("e", "abc"); 151 root->Add("f", child2); 152 root->Add("g", arr); 153 154 bool b; 155 int32_t i32; 156 int64_t i64; 157 double d; 158 std::string str; 159 std::unique_ptr<PtJson> json; 160 ASSERT_EQ(root->GetBool("a", &b), Result::SUCCESS); 161 EXPECT_FALSE(b); 162 ASSERT_EQ(root->GetInt("b", &i32), Result::SUCCESS); 163 EXPECT_EQ(i32, 100); 164 ASSERT_EQ(root->GetDouble("c", &d), Result::SUCCESS); 165 EXPECT_EQ(d, 100.2); 166 ASSERT_EQ(root->GetInt64("d", &i64), Result::SUCCESS); 167 EXPECT_EQ(i64, static_cast<int64_t>(200)); 168 ASSERT_EQ(root->GetString("e", &str), Result::SUCCESS); 169 EXPECT_EQ(str, "abc"); 170 ASSERT_EQ(root->GetObject("f", &json), Result::SUCCESS); 171 ASSERT_EQ(json->GetString("ch", &str), Result::SUCCESS); 172 EXPECT_EQ(str, "child_2"); 173 ASSERT_EQ(root->GetArray("g", &json), Result::SUCCESS); 174 ASSERT_TRUE(json->IsArray()); 175 EXPECT_EQ(json->Get(0)->GetInt(), 100); 176 177 EXPECT_EQ(root->Stringify(), 178 "{\"a\":false,\"b\":100,\"c\":100.2,\"d\":200,\"e\":\"abc\",\"f\":{\"ch\":\"child_2\"},\"g\":[100]}"); 179 root->ReleaseRoot(); 180} 181 182HWTEST_F_L0(PtJsonTest, StringifyTest) 183{ 184 PtJson ptJson; 185 std::string result = ptJson.Stringify(); 186 ASSERT_EQ(result, ""); 187} 188 189HWTEST_F_L0(PtJsonTest, GetKeyTest) 190{ 191 PtJson ptJson; 192 std::string result = ptJson.GetKey(); 193 ASSERT_EQ(result, ""); 194 cJSON temp; 195 char test[] = "test"; 196 temp.string = test; 197 PtJson ptJson1(&temp); 198 result = ptJson1.GetKey(); 199 ASSERT_EQ(result, "test"); 200 cJSON temp1; 201 temp1.string = nullptr; 202 PtJson ptJson2(&temp1); 203 result = ptJson2.GetKey(); 204 ASSERT_EQ(result, ""); 205 std::unique_ptr<PtJson> ptr = std::make_unique<PtJson>(); 206 Result result1 = ptJson.GetAny(test, &ptr); 207 ASSERT_EQ(result1, Result::NOT_EXIST); 208 cJSON json; 209 cJSON son; 210 son.string = test; 211 json.string = test; 212 json.child = &son; 213 PtJson ptJson3(&json); 214 std::unique_ptr<PtJson> ptr1 = std::make_unique<PtJson>(); 215 result1 = ptJson3.GetAny(test, &ptr1); 216 ASSERT_EQ(result1, Result::SUCCESS); 217} 218 219HWTEST_F_L0(PtJsonTest, GetTypeTest) 220{ 221 cJSON *node = cJSON_CreateNumber(0); 222 PtJson num(node); 223 cJSON *node1 = cJSON_CreateString("test"); 224 PtJson str(node1); 225 bool result = str.GetBool(false); 226 ASSERT_EQ(result, false); 227 int32_t result1 = str.GetInt(int32_t(0)); 228 ASSERT_EQ(result1, 0); 229 int64_t result2 = str.GetInt64(int64_t(0)); 230 ASSERT_EQ(result2, 0); 231 double result3 = str.GetDouble(0.1); // 0.1:num 232 ASSERT_EQ(result3, 0.1); // 0.1:num 233 std::string result4 = num.GetString(); 234 ASSERT_EQ(result4, ""); 235} 236 237HWTEST_F_L0(PtJsonTest, PushTest) 238{ 239 char *ptr = nullptr; 240 PtJson ptJson; 241 bool result = ptJson.Push(ptr); 242 ASSERT_EQ(result, false); 243 char cPtr[] = "value"; 244 result = ptJson.Push(cPtr); 245 ASSERT_EQ(result, false); 246 bool value = true; 247 result = ptJson.Push(value); 248 ASSERT_EQ(result, false); 249 double value1 = 0.1; // 0.1:value 250 result = ptJson.Push(value1); 251 ASSERT_EQ(result, false); 252 std::unique_ptr<PtJson> ptr1; 253 result = ptJson.Push(std::move(ptr1)); 254 ASSERT_EQ(result, false); 255 std::unique_ptr<PtJson> ptr2 = std::make_unique<PtJson>(); 256 result = ptJson.Push(std::move(ptr2)); 257 ASSERT_EQ(result, false); 258 cJSON json; 259 std::unique_ptr<PtJson> ptr3 = std::make_unique<PtJson>(&json); 260 result = ptJson.Push(std::move(ptr3)); 261 ASSERT_EQ(result, false); 262} 263 264HWTEST_F_L0(PtJsonTest, AddTest) 265{ 266 char key[] = "key"; 267 double value = 0.1; // 0.1:value 268 PtJson ptJson; 269 bool result = ptJson.Add(key, value); 270 ASSERT_EQ(result, false); 271 char value1[] = "value"; 272 result = ptJson.Add(key, value1); 273 ASSERT_EQ(result, false); 274 std::unique_ptr<PtJson> ptr = std::make_unique<PtJson>(); 275 result = ptJson.Add(key, std::move(ptr)); 276 ASSERT_EQ(result, false); 277 cJSON json; 278 std::unique_ptr<PtJson> ptr1 = std::make_unique<PtJson>(&json); 279 result = ptJson.Add(key, std::move(ptr1)); 280 ASSERT_EQ(result, false); 281 bool value2 = true; 282 result = ptJson.Add(key, value2); 283 ASSERT_EQ(result, false); 284} 285 286HWTEST_F_L0(PtJsonTest, UIntTest) 287{ 288 std::string str = "UIntTest"; 289 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 290 EXPECT_EQ(json->GetUInt(), 0); 291 str = "12345"; 292 json = PtJson::Parse(str.c_str()); 293 EXPECT_EQ(json->GetUInt(), 12345); 294} 295 296HWTEST_F_L0(PtJsonTest, UInt64Test) 297{ 298 std::string str = "UInt64Test"; 299 std::unique_ptr<PtJson> json = PtJson::Parse(str.c_str()); 300 EXPECT_EQ(json->GetUInt64(), 0); 301 str = "123456789012345"; 302 json = PtJson::Parse(str.c_str()); 303 EXPECT_EQ(json->GetUInt64(), 123456789012345); 304} 305 306HWTEST_F_L0(PtJsonTest, ResultUInt64Test) 307{ 308 auto test = PtJson::CreateObject(); 309 test->Add("a", "ResultUInt64Test"); 310 test->Add("b", 100); 311 uint64_t ui64; 312 ASSERT_EQ(test->GetUInt64("a", &ui64), Result::TYPE_ERROR); 313 ASSERT_EQ(test->GetUInt64("b", &ui64), Result::SUCCESS); 314 EXPECT_EQ(ui64, static_cast<uint64_t>(100)); 315} 316}