1 // Copyright 2015, Tobias Hermann and the FunctionalPlus contributors. 2 // https://github.com/Dobiasd/FunctionalPlus 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <doctest/doctest.h> 8 #include <fplus/fplus.hpp> 9 #include <vector> 10 11 TEST_CASE("stringtools_test - trim") 12 { 13 using namespace fplus; 14 std::string untrimmed = " \n \t foo "; 15 REQUIRE_EQ(trim_whitespace_left(untrimmed), "foo "); 16 REQUIRE_EQ(trim_whitespace_right(untrimmed), " \n \t foo"); 17 REQUIRE_EQ(trim_whitespace(untrimmed), "foo"); 18 } 19 20 TEST_CASE("stringtools_test - split") 21 { 22 using namespace fplus; 23 std::string text = "Hi,\nI am a\r\n***strange***\n\rstring."; 24 std::vector<std::string> textAsLinesWithEmty = { 25 std::string("Hi,"), 26 std::string("I am a"), 27 std::string("***strange***"), 28 std::string(""), 29 std::string("string.") }; 30 std::vector<std::string> textAsLinesWithoutEmpty = { 31 std::string("Hi,"), 32 std::string("I am a"), 33 std::string("***strange***"), 34 std::string("string.") }; 35 std::vector<std::string> textAsWords = { 36 std::string("Hi"), 37 std::string("I"), 38 std::string("am"), 39 std::string("a"), 40 std::string("strange"), 41 std::string("string") }; 42 std::vector<std::string> textSplitBySpaceOnly = { 43 std::string("Hi,\nI"), 44 std::string("am"), 45 std::string("a\r\n***strange***\n\rstring.") }; 46 std::vector<std::string> textSplitBySpaceAndCommaAndLine = { 47 std::string("Hi"), 48 std::string("I"), 49 std::string("am"), 50 std::string("a"), 51 std::string("***strange***"), 52 std::string("string.") }; 53 54 REQUIRE_EQ(split_lines(true, text), textAsLinesWithEmty); 55 REQUIRE_EQ(split_lines(false, text), textAsLinesWithoutEmpty); 56 REQUIRE_EQ(split_words(false, text), textAsWords); 57 REQUIRE_EQ(split(' ', false, text), textSplitBySpaceOnly); 58 REQUIRE_EQ(split_one_of(std::string{ " ,\r\n" }, false, text), textSplitBySpaceAndCommaAndLine); 59 } 60 61 TEST_CASE("stringtools_test - to_string_filled") 62 { 63 using namespace fplus; 64 REQUIRE_EQ(to_string_fill_left('0', 5, 42), std::string("00042") ); 65 REQUIRE_EQ(to_string_fill_right(' ', 5, 42), std::string("42 ") ); 66 } 67 68 TEST_CASE("stringtools_test - to_lower_case") 69 { 70 using namespace fplus; 71 REQUIRE_EQ(to_lower_case(std::string("ChaRacTer&WorDs23")), std::string("character&words23")); 72 } 73 74 TEST_CASE("stringtools_test - to_upper_case") 75 { 76 using namespace fplus; 77 REQUIRE_EQ(to_upper_case(std::string("ChaRacTer&WorDs34")), std::string("CHARACTER&WORDS34")); 78 } 79