• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/pt_json.h"
18 
19 using namespace panda::ecmascript::tooling;
20 
21 namespace panda::test {
22 class PtJsonTest : public testing::Test {
23 public:
SetUpTestCase()24     static void SetUpTestCase()
25     {
26         GTEST_LOG_(INFO) << "SetUpTestCase";
27     }
28 
TearDownTestCase()29     static void TearDownTestCase()
30     {
31         GTEST_LOG_(INFO) << "TearDownCase";
32     }
33 
SetUp()34     void SetUp() override
35     {
36     }
37 
TearDown()38     void TearDown() override
39     {
40     }
41 };
42 
HWTEST_F_L0(PtJsonTest,FalseTest)43 HWTEST_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 
HWTEST_F_L0(PtJsonTest,TrueTest)53 HWTEST_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 
HWTEST_F_L0(PtJsonTest,IntTest)63 HWTEST_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 
HWTEST_F_L0(PtJsonTest,Int64Test)73 HWTEST_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 
HWTEST_F_L0(PtJsonTest,DoubleTest)83 HWTEST_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 
HWTEST_F_L0(PtJsonTest,StringTest)93 HWTEST_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 
HWTEST_F_L0(PtJsonTest,ArrayTest1)103 HWTEST_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 
HWTEST_F_L0(PtJsonTest,ArrayTest2)116 HWTEST_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 
HWTEST_F_L0(PtJsonTest,ObjectTest)131 HWTEST_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 }