blob: aeb1ad503f98ce5018d48ff2a75e8bc0fcb897bd [file] [log] [blame]
Austin Schuh9d823002019-04-14 12:53:17 -07001// Copyright (c) 2019, offa
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#include "seasocks/StringUtil.h"
27#include <catch2/catch.hpp>
28
29using namespace seasocks;
30
31TEST_CASE("case insensitive string comparison", "[ToStringTests]") {
32 CHECK(caseInsensitiveSame("abc def 123", "abc def 123") == true);
33 CHECK(caseInsensitiveSame("abc def 123", "abc ghi 123") == false);
34 CHECK(caseInsensitiveSame("abc", "aBc") == true);
35 CHECK(caseInsensitiveSame("", "") == true);
36 CHECK(caseInsensitiveSame("123", "123") == true);
37 CHECK(caseInsensitiveSame(" ", " ") == true);
38}
39
40TEST_CASE("replace replaces match if found", "[ToStringTests]") {
41 std::string str = "x-?-z";
42 replace(str, "-?-", "y");
43 CHECK(str == "xyz");
44}
45
46TEST_CASE("replace replaces all matches", "[ToStringTests]") {
47 std::string str = "1xx2 xx34xxxx";
48 replace(str, "xx", "---");
49 CHECK(str == "1---2 ---34------");
50}
51
52TEST_CASE("replace does nothing if no match", "[ToStringTests]") {
53 std::string str = "no match in here";
54 replace(str, "xx", "no!");
55 CHECK(str == "no match in here");
56}
57
58TEST_CASE("replace is safe to empty strings", "[ToStringTests]") {
59 std::string empty{};
60 replace(empty, "a", "b");
61 CHECK(empty == "");
62
63 std::string str = "input text";
64 replace(str, "", "aaa");
65 CHECK(str == "input text");
66
67 replace(str, " text", "");
68 CHECK(str == "input");
69}
70
71TEST_CASE("split splits input if delimiter found", "[ToStringTests]") {
72 using Catch::Matchers::Equals;
73
74 const auto result = split("123-456-789-0", '-');
75 CHECK_THAT(result, Equals<std::string>({"123", "456", "789", "0"}));
76}
77
78TEST_CASE("split returns input if delimiter not found", "[ToStringTests]") {
79 using Catch::Matchers::Equals;
80
81 const auto result = split("1-2 3-4", 'x');
82 CHECK_THAT(result, Equals<std::string>({"1-2 3-4"}));
83}
84
85TEST_CASE("split is safe to empty strings", "[ToStringTests]") {
86 using Catch::Matchers::Equals;
87
88 const auto result = split("", '-');
89 CHECK_THAT(result, Equals<std::string>({}));
90}
91
92TEST_CASE("split returns empty strings if input is delimiter", "[ToStringTests]") {
93 using Catch::Matchers::Equals;
94
95 const auto result = split("-", '-');
96 CHECK_THAT(result, Equals<std::string>({"", ""}));
97}
98
99TEST_CASE("trim whitespace removes whitespace on both ends", "[ToStringTests]") {
100 const auto result = trimWhitespace(" abc def ");
101 CHECK(result == "abc def");
102}
103
104TEST_CASE("trim whitespace removes whitespace escape sequences", "[ToStringTests]") {
105 const auto result = trimWhitespace("\t xyz\n");
106 CHECK(result == "xyz");
107}
108
109TEST_CASE("trim whitespace is safe to empty string", "[ToStringTests]") {
110 const auto result = trimWhitespace("");
111 CHECK(result == "");
112}
113
114TEST_CASE("trim whitespace returns empty string if only whitespace", "[ToStringTests]") {
115 const auto result = trimWhitespace(" \n \t ");
116 CHECK(result == "");
117}