• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef __JSVMTEST_H__
16 #define __JSVMTEST_H__
17 
18 #include <sstream>
19 
20 #if defined(OHOS_JSVM_HAP) && !defined(OHOS_JSVM_XTS)
21 #include "ark_runtime/jsvm.h"
22 #else
23 #include "jsvm.h"
24 #endif
25 
26 #include <cstdarg>
27 #include <cstdint>
28 #include <cstdio>
29 #include <cstring>
30 #include <iostream>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include "jsvm_utils.h"
36 #include "securec.h"
37 #include "test_entry.h"
38 
39 class TestRunner {
40 public:
GetTestVec()41     static std::vector<TestRunner *> &GetTestVec()
42     {
43         static std::vector<TestRunner *> testVec;
44         return testVec;
45     };
GetTestMap()46     static std::unordered_map<std::string, TestRunner *> &GetTestMap()
47     {
48         static std::unordered_map<std::string, TestRunner *> testMap;
49         return testMap;
50     }
51     static bool globalInited;
Register(const char * name,TestRunner * test)52     static size_t Register(const char *name, TestRunner *test)
53     {
54         GetTestVec().push_back(test);
55         GetTestMap().emplace(name, test);
56         return GetTestVec().size();
57     }
58 
GetName()59     virtual const char *GetName() const
60     {
61         CHECK_FATAL(false, "should not be here");
62         return NULL;
63     }
64 
IsFromScratch()65     virtual bool IsFromScratch() const
66     {
67         return false;
68     }
69 
TestBegin()70     void TestBegin() const
71     {
72         std::stringstream ss;
73         ss << "[ RUN      ] " << GetName();
74         Print(ss.str().c_str());
75     }
76 
TestEnd()77     void TestEnd() const
78     {
79         std::stringstream ss;
80         ss << "[       OK ] " << GetName();
81         Print(ss.str().c_str());
82     }
83 
84     void Run(size_t index = 0)
85     {
86         if (IsFromScratch()) {
87             TestBegin();
88             DoRun();
89             TestEnd();
90             return;
91         }
92         if (!globalInited) {
93             JSVM_InitOptions init_options{};
94             memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options));
95             OH_JSVM_Init(&init_options);
96             globalInited = true;
97         }
98         OH_JSVM_CreateVM(nullptr, &vm);
99         JSVM_VMScope vm_scope;
100         OH_JSVM_OpenVMScope(vm, &vm_scope);
101 
102         OH_JSVM_CreateEnv(vm, 0, NULL, &env);
103         jsvm_env = env;
104         JSVM_EnvScope env_scope;
105         OH_JSVM_OpenEnvScope(env, &env_scope);
106 
107         {
108             jsvm::HandleScope handleScope(env);
109             TestBegin();
110             DoRun();
111             TestEnd();
112         }
113 
114         OH_JSVM_CloseEnvScope(env, env_scope);
115         OH_JSVM_DestroyEnv(env);
116         OH_JSVM_CloseVMScope(vm, vm_scope);
117         OH_JSVM_DestroyVM(vm);
118     }
119 
DoRun()120     virtual void DoRun()
121     {
122         CHECK_FATAL(false, "should not be here");
123     }
124 
125 protected:
126     JSVM_VM vm;
127     JSVM_Env env;
128 };
129 
130 // Any fields defined in TestRunner can be accessd in TEST() {}, such as `vm`, `env`
131 // GetName returned string must be same with register name
132 #ifndef TEST
133 #define TEST(Name)                                                                                 \
134     class Test##Name : public TestRunner {                                                         \
135     public:                                                                                        \
136         const char *GetName() const override                                                       \
137         {                                                                                          \
138             return __FILE_NAME__ "/" #Name;                                                        \
139         }                                                                                          \
140         void DoRun() override;                                                                     \
141     };                                                                                             \
142     static size_t testId_##Name = TestRunner::Register(__FILE_NAME__ "/" #Name, new Test##Name()); \
143     inline void Test##Name::DoRun()
144 #endif
145 
146 #ifndef TEST_FROM_SCRATCH
147 #define TEST_FROM_SCRATCH(Name)                                                                    \
148     class Test##Name : public TestRunner {                                                         \
149     public:                                                                                        \
150         const char *GetName() const override                                                       \
151         {                                                                                          \
152             return __FILE_NAME__ "/" #Name;                                                        \
153         }                                                                                          \
154         bool IsFromScratch() const override                                                        \
155         {                                                                                          \
156             return true;                                                                           \
157         }                                                                                          \
158         void DoRun() override;                                                                     \
159     };                                                                                             \
160     static size_t testId_##Name = TestRunner::Register(__FILE_NAME__ "/" #Name, new Test##Name()); \
161     inline void Test##Name::DoRun()
162 #endif
163 
164 #ifndef TEST_DISABLE
165 #define TEST_DISABLE(Name) inline void TestDisable##Name(JSVM_Env env)
166 #endif
167 
168 #endif  // __JSVMTEST_H__
169