Debian Codegraph @

tests/unit/tst_utils.cpp

raw
1 /*
2 * Copyright (C) 2021 UBports foundation.
3 * Author(s): Marius Gripsgard <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 3, as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 #include <gtest/gtest.h>
20
21 #include "utils.h"
22
23 TEST(UtilsTest, testSplitString)
24 {
25 auto spl = utils::string::split("1:2:3dfdf3:5", ':');
26
27 EXPECT_EQ(spl, (std::vector<std::string>{
28 "1",
29 "2",
30 "3dfdf3",
31 "5"
32 }));
33 }
34
35 TEST(UtilsTest, testEndsWith)
36 {
37 EXPECT_TRUE(utils::string::endsWith("this end with", "with"));
38 EXPECT_FALSE(utils::string::endsWith("this end with not", "with"));
39 }
40
41 TEST(UtilsTest, testStartsWith)
42 {
43 EXPECT_TRUE(utils::string::startsWith("this end with", "this"));
44 EXPECT_FALSE(utils::string::startsWith("this end with not", "with"));
45 }
46
47 TEST(UtilsTest, testCompareInsensitive)
48 {
49 // Test for match
50 EXPECT_TRUE(utils::string::compareInsensitive("this should be equal", "ThIs ShOuLd BE eQuAl"));
51
52 // Test for diff size
53 EXPECT_FALSE(utils::string::compareInsensitive("this", "not equal"));
54
55 // Test no match same size
56 EXPECT_FALSE(utils::string::compareInsensitive("t", "q"));
57
58 // Test for odd chars (Å is not a valid insensetive match)
59 EXPECT_TRUE(utils::string::compareInsensitive(":#*&(%(*#)_(^%()@#(_*#* ÅFHJF", ":#*&(%(*#)_(^%()@#(_*#* Åfhjf"));
60 }
61
62 TEST(UtilsTest, testJoin)
63 {
64 auto str = utils::path::join("this", "that");
65
66 EXPECT_EQ(str, "this/that");
67 }
68
69 TEST(UtilsTest, testJoinSuffix)
70 {
71 auto str = utils::path::join("this", "that", ".end");
72
73 EXPECT_EQ(str, "this/that.end");
74 }
75
76 TEST(UtilsTest, testJoinFileSuffix)
77 {
78 auto str = utils::path::join("this", "that", "file", ".end");
79
80 EXPECT_EQ(str, "this/that/file.end");
81 }
82
83 TEST(UtilsTest, testEnvGet)
84 {
85 utils::env::SetUnsetAfter setUnset("DEVICEINFO_TESTING_ENV", "test_this");
86
87 EXPECT_EQ(utils::env::get("DEVICEINFO_TESTING_ENV"), "test_this");
88 }
89
90 TEST(UtilsTest, testEnvSetAndUnset)
91 {
92 EXPECT_TRUE(utils::env::set("DEVICEINFO_TESTING_ENV", "test_this"));
93 EXPECT_EQ(utils::env::get("DEVICEINFO_TESTING_ENV"), "test_this");
94
95 EXPECT_TRUE(utils::env::unset("DEVICEINFO_TESTING_ENV"));
96 EXPECT_EQ(utils::env::get("DEVICEINFO_TESTING_ENV"), "");
97 }
98
99 TEST(UtilsTest, testEnvGetNoVal)
100 {
101 EXPECT_EQ(utils::env::get("DEVICEINFO_TESTING_ENV", "test_no_val"), "test_no_val");
102 }