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 <gtest/gtest.h>
19
20 #include "read_lines_core.h"
21
22 namespace OHOS::FileManagement::ModuleFileIO::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26
27 class ReadLinesCoreTest : public testing::Test {
28 public:
29 static filesystem::path tempFilePath;
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
36 filesystem::path ReadLinesCoreTest::tempFilePath;
37
SetUpTestCase(void)38 void ReadLinesCoreTest::SetUpTestCase(void)
39 {
40 tempFilePath = filesystem::temp_directory_path() / "read_lines_test_file.txt";
41 ofstream(tempFilePath) << "Test content\n123\n456";
42 ofstream(tempFilePath).close();
43 GTEST_LOG_(INFO) << "SetUpTestCase";
44 }
45
TearDownTestCase(void)46 void ReadLinesCoreTest::TearDownTestCase(void)
47 {
48 filesystem::remove(tempFilePath);
49 GTEST_LOG_(INFO) << "TearDownTestCase";
50 }
51
SetUp(void)52 void ReadLinesCoreTest::SetUp(void)
53 {
54 GTEST_LOG_(INFO) << "SetUp";
55 }
56
TearDown(void)57 void ReadLinesCoreTest::TearDown(void)
58 {
59 GTEST_LOG_(INFO) << "TearDown";
60 }
61
62 /**
63 * @tc.name: ReadLinesCoreTest_DoReadLines_001
64 * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED.
65 * @tc.size: MEDIUM
66 * @tc.type: FUNC
67 * @tc.level Level 1
68 */
69 HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_001, testing::ext::TestSize.Level1)
70 {
71 GTEST_LOG_(INFO) << "ReadLinesCoreTest-begin ReadLinesCoreTest_DoReadLines_001";
72
73 string path = "ReadLinesCoreTest_DoReadLines_001";
74 Options option;
75 option.encoding = "utf-8";
76 auto res = ReadLinesCore::DoReadLines(path, option);
77 EXPECT_EQ(res.IsSuccess(), false);
78
79 GTEST_LOG_(INFO) << "ReadLinesCoreTest-end ReadLinesCoreTest_DoReadLines_001";
80 }
81
82 /**
83 * @tc.name: ReadLinesCoreTest_DoReadLines_002
84 * @tc.desc: Test function of ReadLinesCore::DoReadLines interface for FAILED.
85 * @tc.size: MEDIUM
86 * @tc.type: FUNC
87 * @tc.level Level 1
88 */
89 HWTEST_F(ReadLinesCoreTest, ReadLinesCoreTest_DoReadLines_002, testing::ext::TestSize.Level1)
90 {
91 GTEST_LOG_(INFO) << "ReadLinesCoreTest-begin ReadLinesCoreTest_DoReadLines_002";
92
93 string path = "ReadLinesCoreTest_DoReadLines_002";
94 Options option;
95 option.encoding = "utf-16";
96 auto res = ReadLinesCore::DoReadLines(path, option);
97 EXPECT_EQ(res.IsSuccess(), false);
98
99 GTEST_LOG_(INFO) << "ReadLinesCoreTest-end ReadLinesCoreTest_DoReadLines_002";
100 }
101
102 } // namespace OHOS::FileManagement::ModuleFileIO::Test