1 /**
2 * Copyright (c) 2024-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 "ast_verifier_test.h"
17 #include "checker/ETSchecker.h"
18 #include "ir/astNode.h"
19 #include "util/diagnosticEngine.h"
20 #include <gtest/gtest.h>
21
22 using ark::es2panda::checker::ETSChecker;
23 using ark::es2panda::compiler::ast_verifier::GetterSetterValidation;
24 using ark::es2panda::util::DiagnosticEngine;
25
26 namespace {
TEST_F(ASTVerifierTest,ValidateCorrectGetterSetter)27 TEST_F(ASTVerifierTest, ValidateCorrectGetterSetter)
28 {
29 DiagnosticEngine de {};
30 ETSChecker checker {de};
31
32 char const *text =
33 R"(
34 class A {
35 private _value: number = 0;
36 set value(v: number) {
37 this._value = v
38 }
39
40 get value(): number {
41 return this._value
42 }
43 }
44 )";
45
46 CONTEXT(ES2PANDA_STATE_LOWERED, text)
47 {
48 EXPECT_TRUE(Verify<GetterSetterValidation>());
49 }
50 }
51
TEST_F(ASTVerifierTest,ValidateAbstractGettersSetters)52 TEST_F(ASTVerifierTest, ValidateAbstractGettersSetters)
53 {
54 DiagnosticEngine de {};
55 ETSChecker checker {de};
56
57 char const *text =
58 R"(
59 abstract class AbstractClass {
60 private _value: int;
61 abstract set value(v: int);
62 abstract get value(): int;
63 }
64 )";
65
66 CONTEXT(ES2PANDA_STATE_LOWERED, text)
67 {
68 EXPECT_TRUE(Verify<GetterSetterValidation>());
69 }
70 }
71
TEST_F(ASTVerifierTest,ValidateAmbientGettersSetters)72 TEST_F(ASTVerifierTest, ValidateAmbientGettersSetters)
73 {
74 DiagnosticEngine de {};
75 ETSChecker checker {de};
76
77 char const *text =
78 R"(
79 declare class A {
80 get foo(): int
81 set foo(x: int)
82 }
83 )";
84
85 CONTEXT(ES2PANDA_STATE_LOWERED, text)
86 {
87 EXPECT_TRUE(Verify<GetterSetterValidation>());
88 }
89 }
90 } // anonymous namespace
91