• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "lowering_test.h"
17 
18 namespace ark::es2panda {
19 
TEST_F(LoweringTest,TestConvertPrimitiveCastMethodCall)20 TEST_F(LoweringTest, TestConvertPrimitiveCastMethodCall)
21 {
22     char const *text = R"(
23         let d: double = 3.14
24         let b: byte = d.toByte()
25         let s: short = d.toShort()
26         let i: int = d.toInt()
27         let l: long = d.toLong()
28         let f: float= d.toFloat()
29     )";
30 
31     static std::array<std::string_view, 7U> castMethods {{
32         "toChar",
33         "toByte",
34         "toShort",
35         "toInt",
36         "toLong",
37         "toFloat",
38         "toDouble",
39     }};
40 
41     CONTEXT(ES2PANDA_STATE_LOWERED, text)
42     {
43         const auto *const ast = GetAst();
44         ASSERT_FALSE(ast->IsAnyChild([](ir::AstNode *const node) {
45             if (!node->IsCallExpression()) {
46                 return false;
47             }
48             auto call = node->AsCallExpression();
49             if (!call->Callee()->IsMemberExpression()) {
50                 return false;
51             }
52             auto me = node->AsCallExpression()->Callee()->AsMemberExpression();
53             auto prop = me->Property();
54             if (!prop->IsIdentifier()) {
55                 return false;
56             }
57             auto m = prop->AsIdentifier()->Name().Utf8();
58             return std::find(castMethods.begin(), castMethods.end(), m) != castMethods.end();
59         }));
60     }
61 }
62 
63 }  // namespace ark::es2panda
64