1 /* Copyright (C) 2012-2021 by László Nagy 2 This file is part of Bear. 3 4 Bear is a tool to generate compilation database for clang tooling. 5 6 Bear is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 Bear is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "gtest/gtest.h" 21 22 #include "report/libexec/Paths.h" 23 24 namespace { 25 TEST(PathIterator,works_on_empty)26 TEST(PathIterator, works_on_empty) 27 { 28 el::Paths paths(""); 29 for (auto path : paths) { 30 EXPECT_EQ(std::string_view("shall not match"), path); 31 } 32 } 33 TEST(PathIterator,works_on_single)34 TEST(PathIterator, works_on_single) 35 { 36 el::Paths paths("/bin"); 37 for (auto path : paths) { 38 EXPECT_EQ(path, std::string_view("/bin")); 39 } 40 } 41 TEST(PathIterator,works_on_multiple)42 TEST(PathIterator, works_on_multiple) 43 { 44 size_t count = 0; 45 46 el::Paths paths("/bin:/sbin:/usr/bin:/usr/sbin"); 47 for (auto path : paths) { 48 EXPECT_FALSE(path.empty()); 49 ++count; 50 } 51 EXPECT_EQ(4, count); 52 53 el::Paths::iterator it = paths.begin(); 54 el::Paths::iterator end = paths.end(); 55 EXPECT_NE(it, end); 56 EXPECT_EQ(std::string_view("/bin"), *(it++)); 57 EXPECT_NE(it, end); 58 EXPECT_EQ(std::string_view("/sbin"), *it); it++; 59 EXPECT_NE(it, end); 60 EXPECT_EQ(std::string_view("/usr/bin"), *it); ++it; 61 EXPECT_NE(it, end); 62 EXPECT_EQ(std::string_view("/usr/sbin"), *it); ++it; 63 EXPECT_EQ(it, end); 64 } 65 TEST(PathIterator,works_with_empty_values)66 TEST(PathIterator, works_with_empty_values) 67 { 68 size_t count = 0; 69 size_t empty = 0; 70 71 el::Paths paths("/bin::/sbin::"); 72 for (auto path : paths) { 73 ++count; 74 empty += path.empty() ? 1 : 0; 75 } 76 EXPECT_EQ(4, count); 77 EXPECT_EQ(2, empty); 78 } 79 }