• 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 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "fs_reader_iterator.h"
23 #include "read_lines_core.h"
24 #include "uv_fs_mock.h"
25 
26 namespace OHOS::FileManagement::ModuleFileIO::Test {
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 
31 class FsReaderIteratorMockTest : public testing::Test {
32 public:
33     static filesystem::path tempFilePath;
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38     static inline shared_ptr<UvfsMock> uvMock = nullptr;
39 };
40 
41 filesystem::path FsReaderIteratorMockTest::tempFilePath;
42 
SetUpTestCase(void)43 void FsReaderIteratorMockTest::SetUpTestCase(void)
44 {
45     GTEST_LOG_(INFO) << "SetUpTestCase";
46     tempFilePath = filesystem::temp_directory_path() / "FsReaderIteratorMockTest_test_file.txt";
47     ofstream tempfile(tempFilePath);
48     tempfile << "";
49     tempfile.close();
50     uvMock = make_shared<UvfsMock>();
51     Uvfs::ins = uvMock;
52 }
53 
TearDownTestCase(void)54 void FsReaderIteratorMockTest::TearDownTestCase(void)
55 {
56     GTEST_LOG_(INFO) << "TearDownTestCase";
57     filesystem::remove(tempFilePath);
58     Uvfs::ins = nullptr;
59     uvMock = nullptr;
60 }
61 
SetUp(void)62 void FsReaderIteratorMockTest::SetUp(void)
63 {
64     GTEST_LOG_(INFO) << "SetUp";
65 }
66 
TearDown(void)67 void FsReaderIteratorMockTest::TearDown(void)
68 {
69     GTEST_LOG_(INFO) << "TearDown";
70 }
71 
72 /**
73  * @tc.name: FsReaderIteratorMockTest_Next_001
74  * @tc.desc: Test FsReaderIterator::Next for success case
75  * @tc.size: SMALL
76  * @tc.type: FUNC
77  * @tc.level Level 1
78  */
79 HWTEST_F(FsReaderIteratorMockTest, FsReaderIteratorMockTest_Next_001, testing::ext::TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-begin FsReaderIteratorMockTest_Next_001";
82 
83     string path = tempFilePath.string();
84     optional<Options> option = nullopt;
85 
86     EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1));
87 
88     auto result = ReadLinesCore::DoReadLines(path, option);
89     EXPECT_TRUE(result.IsSuccess());
90 
91     auto iterator = result.GetData().value();
92     ASSERT_NE(iterator, nullptr);
93 
94     auto nextResult = iterator->Next();
95     EXPECT_TRUE(nextResult.IsSuccess());
96     EXPECT_TRUE(nextResult.GetData().value().done);
97     EXPECT_EQ(nextResult.GetData().value().value, "");
98 
99     GTEST_LOG_(INFO) << "FsReaderIteratorMockTest-end FsReaderIteratorMockTest_Next_001";
100 }
101 
102 }