• 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 <filesystem>
17 #include <fstream>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include "mock/uv_fs_mock.h"
22 #include "read_lines_core.h"
23 
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28 
29 class ReadLinesCoreMockTest : public testing::Test {
30 public:
31     static filesystem::path tempFilePath;
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36     static inline shared_ptr<UvfsMock> uvMock = nullptr;
37 };
38 
39 filesystem::path ReadLinesCoreMockTest::tempFilePath;
40 
SetUpTestCase(void)41 void ReadLinesCoreMockTest::SetUpTestCase(void)
42 {
43     GTEST_LOG_(INFO) << "SetUpTestCase";
44     tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt";
45     ofstream(tempFilePath) << "Test content\n123\n456";
46     ofstream(tempFilePath).close();
47     uvMock = std::make_shared<UvfsMock>();
48     Uvfs::ins = uvMock;
49 }
50 
TearDownTestCase(void)51 void ReadLinesCoreMockTest::TearDownTestCase(void)
52 {
53     filesystem::remove(tempFilePath);
54     GTEST_LOG_(INFO) << "TearDownTestCase";
55     Uvfs::ins = nullptr;
56     uvMock = nullptr;
57 }
58 
SetUp(void)59 void ReadLinesCoreMockTest::SetUp(void)
60 {
61     GTEST_LOG_(INFO) << "SetUp";
62 }
63 
TearDown(void)64 void ReadLinesCoreMockTest::TearDown(void)
65 {
66     GTEST_LOG_(INFO) << "TearDown";
67 }
68 
69 /**
70  * @tc.name: ReadLinesCoreMockTest_DoReadLines_001
71  * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS.
72  * @tc.size: MEDIUM
73  * @tc.type: FUNC
74  * @tc.level Level 1
75  */
76 HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_001, testing::ext::TestSize.Level1)
77 {
78     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_001";
79 
80     string path = tempFilePath.string();
81     Options option;
82     option.encoding = "utf-8";
83 
84     EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1));
85     auto res = ReadLinesCore::DoReadLines(path, option);
86     EXPECT_EQ(res.IsSuccess(), true);
87 
88     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_001";
89 }
90 
91 /**
92  * @tc.name: ReadLinesCoreMockTest_DoReadLines_002
93  * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for SUCCESS.
94  * @tc.size: MEDIUM
95  * @tc.type: FUNC
96  * @tc.level Level 1
97  */
98 HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_002, testing::ext::TestSize.Level1)
99 {
100     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_002";
101 
102     string path = tempFilePath.string();
103 
104     EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1));
105     auto res = ReadLinesCore::DoReadLines(path);
106     EXPECT_EQ(res.IsSuccess(), true);
107 
108     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_002";
109 }
110 
111 /**
112  * @tc.name: ReadLinesCoreMockTest_DoReadLines_003
113  * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED.
114  * @tc.size: MEDIUM
115  * @tc.type: FUNC
116  * @tc.level Level 1
117  */
118 HWTEST_F(ReadLinesCoreMockTest, ReadLinesCoreMockTest_DoReadLines_003, testing::ext::TestSize.Level1)
119 {
120     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-begin ReadLinesCoreMockTest_DoReadLines_003";
121 
122     string path = tempFilePath.string();
123     Options option;
124     option.encoding = "utf-8";
125 
126     EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1));
127     auto res = ReadLinesCore::DoReadLines(path, option);
128     EXPECT_EQ(res.IsSuccess(), false);
129 
130     GTEST_LOG_(INFO) << "ReadLinesCoreMockTest-end ReadLinesCoreMockTest_DoReadLines_003";
131 }
132 
133 } // namespace OHOS::FileManagement::ModuleFileIO::Test