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 OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H 16 #define OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H 17 18 #include <cJSON.h> 19 #include <gmock/gmock.h> 20 21 CJSON_PUBLIC(cJSON *) CJSONParse(const char *value); 22 CJSON_PUBLIC(cJSON *) CJSONGetObjectItem(const cJSON *const object, const char *const string); 23 CJSON_PUBLIC(void) CJSONDelete(cJSON *item); 24 CJSON_PUBLIC(cJSON_bool) CJSONIsArray(const cJSON * const item); 25 CJSON_PUBLIC(void) CJSONFree(void *object); 26 27 namespace OHOS::FileManagement::Backup { 28 class CJson { 29 public: 30 CJson() = default; 31 virtual ~CJson() = default; 32 33 public: 34 virtual cJSON *cJSON_CreateArray() = 0; 35 virtual cJSON *cJSON_CreateObject() = 0; 36 virtual char *cJSON_Print(const cJSON *item) = 0; 37 virtual cJSON *cJSON_Parse(const char *value) = 0; 38 virtual cJSON *cJSON_GetObjectItem(const cJSON *const object, const char *const string) = 0; 39 virtual void cJSON_Delete(cJSON *item) = 0; 40 virtual cJSON_bool cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) = 0; 41 virtual int cJSON_GetArraySize(const cJSON *array) = 0; 42 virtual cJSON* cJSON_GetArrayItem(const cJSON* array, int index) = 0; 43 virtual void cJSON_free(void* object) = 0; 44 virtual cJSON_bool cJSON_AddItemToArray(cJSON *array, cJSON *item) = 0; 45 virtual cJSON *cJSON_AddStringToObject(cJSON *const object, const char *const name, const char *const string) = 0; 46 virtual cJSON_bool cJSON_IsArray(const cJSON * const item) = 0; 47 48 public: 49 static inline std::shared_ptr<CJson> cJsonPtr = nullptr; 50 }; 51 52 class CJsonMock : public CJson { 53 public: 54 MOCK_METHOD3(cJSON_AddItemToObject, cJSON_bool(cJSON *object, const char *string, cJSON *item)); 55 MOCK_METHOD1(cJSON_Delete, void(cJSON *item)); 56 MOCK_METHOD0(cJSON_CreateObject, cJSON *()); 57 MOCK_METHOD0(cJSON_CreateArray, cJSON *()); 58 MOCK_METHOD1(cJSON_Print, char *(const cJSON *item)); 59 MOCK_METHOD1(cJSON_Parse, cJSON *(const char *value)); 60 MOCK_METHOD2(cJSON_GetObjectItem, cJSON *(const cJSON *const object, const char *const string)); 61 MOCK_METHOD1(cJSON_GetArraySize, int(const cJSON *array)); 62 MOCK_METHOD2(cJSON_GetArrayItem, cJSON *(const cJSON* array, int index)); 63 MOCK_METHOD2(cJSON_AddItemToArray, cJSON_bool(cJSON *array, cJSON *item)); 64 MOCK_METHOD3(cJSON_AddStringToObject, 65 cJSON *(cJSON *const object, const char *const name, const char *const string)); 66 MOCK_METHOD1(cJSON_free, void(void* object)); 67 MOCK_METHOD1(cJSON_IsArray, cJSON_bool(const cJSON * const item)); 68 }; 69 } // namespace OHOS::FileManagement::Backup 70 #endif