2017-02-24 22:48:06 +01:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <tuple>
|
2017-12-23 13:10:48 +01:00
|
|
|
|
#include <utility>
|
2017-02-24 22:48:06 +01:00
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
#include <openrct2/core/String.hpp>
|
2017-02-25 17:07:08 +01:00
|
|
|
|
#include "AssertHelpers.hpp"
|
2018-04-15 17:43:28 +02:00
|
|
|
|
#include "helpers/StringHelpers.hpp"
|
2017-02-24 22:48:06 +01:00
|
|
|
|
|
2017-12-23 13:10:48 +01:00
|
|
|
|
using TCase = std::tuple<std::string, std::string, std::string>;
|
2017-02-24 22:48:06 +01:00
|
|
|
|
|
|
|
|
|
class StringTest : public testing::TestWithParam<TCase>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-15 15:40:22 +02:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Tests for String::Trim
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2017-02-24 22:48:06 +01:00
|
|
|
|
INSTANTIATE_TEST_CASE_P(TrimData, StringTest, testing::Values(
|
2017-12-23 13:10:48 +01:00
|
|
|
|
// input after Trim after TrimStart
|
|
|
|
|
TCase("string", "string", "string"),
|
|
|
|
|
TCase(" string", "string", "string"),
|
|
|
|
|
TCase("string ", "string", "string "),
|
|
|
|
|
TCase(" some string ", "some string", "some string "),
|
|
|
|
|
TCase(" ", "", ""),
|
|
|
|
|
TCase(" ストリング", "ストリング", "ストリング"),
|
|
|
|
|
TCase("ストリング ", "ストリング", "ストリング "),
|
|
|
|
|
TCase(" ストリング ", "ストリング", "ストリング "),
|
|
|
|
|
TCase(" ", "", ""),
|
|
|
|
|
TCase("", "", ""),
|
|
|
|
|
TCase("\n", "", ""),
|
|
|
|
|
TCase("\n\n\n\r\n", "", ""),
|
|
|
|
|
TCase("\n\n\n\r\nstring\n\n", "string", "string\n\n")
|
2017-02-24 22:48:06 +01:00
|
|
|
|
));
|
|
|
|
|
TEST_P(StringTest, Trim)
|
|
|
|
|
{
|
|
|
|
|
auto testCase = GetParam();
|
|
|
|
|
std::string input = std::get<0>(testCase);
|
|
|
|
|
std::string expected = std::get<1>(testCase);
|
|
|
|
|
std::string actual = String::Trim(input);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
2017-02-25 17:07:08 +01:00
|
|
|
|
|
2017-12-23 13:10:48 +01:00
|
|
|
|
TEST_P(StringTest, TrimStart)
|
|
|
|
|
{
|
|
|
|
|
auto testCase = GetParam();
|
|
|
|
|
std::string input = std::get<0>(testCase);
|
|
|
|
|
std::string expected = std::get<2>(testCase);
|
|
|
|
|
std::string actual = String::TrimStart(input);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 15:40:22 +02:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Tests for String::Split
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2017-02-25 17:07:08 +01:00
|
|
|
|
TEST_F(StringTest, Split_ByComma)
|
|
|
|
|
{
|
|
|
|
|
auto actual = String::Split("a,bb,ccc,dd", ",");
|
|
|
|
|
AssertVector<std::string>(actual, { "a", "bb", "ccc", "dd" });
|
|
|
|
|
}
|
|
|
|
|
TEST_F(StringTest, Split_ByColonColon)
|
|
|
|
|
{
|
|
|
|
|
auto actual = String::Split("a::bb:ccc:::::dd", "::");
|
|
|
|
|
AssertVector<std::string>(actual, { "a", "bb:ccc", "", ":dd" });
|
|
|
|
|
}
|
|
|
|
|
TEST_F(StringTest, Split_Empty)
|
|
|
|
|
{
|
|
|
|
|
auto actual = String::Split("", ".");
|
|
|
|
|
AssertVector<std::string>(actual, { });
|
|
|
|
|
}
|
|
|
|
|
TEST_F(StringTest, Split_ByEmpty)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_THROW(String::Split("string", ""), std::invalid_argument);
|
|
|
|
|
}
|
2018-04-15 15:40:22 +02:00
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Tests for String::Convert
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// TODO Remove when String::Convert is implemented for non-Windows platforms
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
|
TEST_F(StringTest, Convert_950_to_UTF8)
|
|
|
|
|
{
|
2018-04-15 17:43:28 +02:00
|
|
|
|
auto input = StringFromHex("a7d6b374aabab4c4a6e2aab0af57");
|
2018-04-15 15:40:22 +02:00
|
|
|
|
auto expected = u8"快速的棕色狐狸";
|
|
|
|
|
auto actual = String::Convert(input, CODE_PAGE::CP_950, CODE_PAGE::CP_UTF8);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(StringTest, Convert_UTF8_to_932)
|
|
|
|
|
{
|
|
|
|
|
auto input = u8"ファストブラウンフォックス";
|
2018-04-15 17:43:28 +02:00
|
|
|
|
auto expected = StringFromHex("83748340835883678375838983458393837483488362834e8358");
|
2018-04-15 15:40:22 +02:00
|
|
|
|
auto actual = String::Convert(input, CODE_PAGE::CP_UTF8, CODE_PAGE::CP_932);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(StringTest, Convert_UTF8_to_UTF8)
|
|
|
|
|
{
|
|
|
|
|
auto input = u8"سريع|brown|ثعلب";
|
|
|
|
|
auto expected = input;
|
|
|
|
|
auto actual = String::Convert(input, CODE_PAGE::CP_UTF8, CODE_PAGE::CP_UTF8);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(StringTest, Convert_Empty)
|
|
|
|
|
{
|
|
|
|
|
auto input = "";
|
|
|
|
|
auto expected = input;
|
|
|
|
|
auto actual = String::Convert(input, CODE_PAGE::CP_1252, CODE_PAGE::CP_UTF8);
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|