|
1 | 1 | #include "io/bb/sanitizedstring.h" |
2 | 2 | #include <QString> |
| 3 | +#include <QRegularExpression> |
3 | 4 | #include <gtest/gtest.h> |
4 | 5 |
|
5 | 6 | namespace pboman3::io::test { |
6 | | - struct CtorParam { |
| 7 | + struct SanitizedStringTestParam { |
7 | 8 | const QString sourceText; |
8 | | - const QString expectedText; |
| 9 | + const QString expectedTextOrPattern; |
9 | 10 | }; |
10 | 11 |
|
11 | | - class CtorTest : public testing::TestWithParam<CtorParam> { |
| 12 | + class SanitizedStringRestrictedCharactersTest : public testing::TestWithParam<SanitizedStringTestParam> { |
12 | 13 | }; |
13 | 14 |
|
14 | | - TEST_P(CtorTest, Deals_With_Restricted_Characters) { |
| 15 | + TEST_P(SanitizedStringRestrictedCharactersTest, Deals_With_Restricted_Characters) { |
15 | 16 | SanitizedString ss(GetParam().sourceText); |
16 | | - ASSERT_EQ(static_cast<QString>(ss), GetParam().expectedText); |
| 17 | + ASSERT_EQ(static_cast<QString>(ss), GetParam().expectedTextOrPattern); |
17 | 18 | } |
18 | 19 |
|
19 | | - INSTANTIATE_TEST_SUITE_P(SanitizedStringTest, CtorTest, testing::Values( |
20 | | - CtorParam{"\t1\t", "%091%09"}, |
21 | | - CtorParam{"?1?", "%3F1%3F"}, |
22 | | - CtorParam{"*1*", "%2A1%2A"}, |
23 | | - CtorParam{"1///", "1%2F%2F%2F"}, |
24 | | - CtorParam{"\\2", "%5C2"} |
25 | | - )); |
| 20 | + INSTANTIATE_TEST_SUITE_P(TestSuite, SanitizedStringRestrictedCharactersTest, testing::Values( |
| 21 | + SanitizedStringTestParam{"\t1\t", "%91%9"}, |
| 22 | + SanitizedStringTestParam{"?1?", "%3f1%3f"}, |
| 23 | + SanitizedStringTestParam{"*1*", "%2a1%2a"}, |
| 24 | + SanitizedStringTestParam{"1///", "1%2f%2f%2f"}, |
| 25 | + SanitizedStringTestParam{"\\2", "%5c2"}, |
| 26 | + SanitizedStringTestParam{" ", "%20%20%20%20"}, |
| 27 | + SanitizedStringTestParam{"1111.", "1111%2e"}, |
| 28 | + SanitizedStringTestParam{"1111 ", "1111%20"} |
| 29 | + )); |
| 30 | + |
| 31 | + class SanitizedStringRestrictedKeywordsTest : public testing::TestWithParam<SanitizedStringTestParam> { |
| 32 | + }; |
| 33 | + |
| 34 | + TEST_P(SanitizedStringRestrictedKeywordsTest, Deals_With_Restricted_Keywords) { |
| 35 | + SanitizedString ss(GetParam().sourceText); |
| 36 | + QRegularExpression re(GetParam().expectedTextOrPattern); |
| 37 | + const QRegularExpressionMatch match = re.match(static_cast<QString>(ss)); |
| 38 | + ASSERT_TRUE(match.hasMatch()); |
| 39 | + } |
| 40 | + |
| 41 | + INSTANTIATE_TEST_SUITE_P(TestSuite, SanitizedStringRestrictedKeywordsTest, testing::Values( |
| 42 | + SanitizedStringTestParam{"COM1.c", "^COM1-\\d{1,4}.c"}, |
| 43 | + SanitizedStringTestParam{"COn", "^COn-\\d{1,4}"}, |
| 44 | + SanitizedStringTestParam{"COM1", "^COM1-\\d{1,4}"}, |
| 45 | + SanitizedStringTestParam{"lPt2", "^lPt2-\\d{1,4}"}, |
| 46 | + SanitizedStringTestParam{"NUL", "^NUL-\\d{1,4}"} |
| 47 | + )); |
| 48 | + |
| 49 | + class SanitizedStringLengthTest : public testing::TestWithParam<SanitizedStringTestParam> { |
| 50 | + }; |
| 51 | + |
| 52 | + TEST_P(SanitizedStringLengthTest, Deals_With_Long_Strings) { |
| 53 | + SanitizedString ss(GetParam().sourceText, 50); |
| 54 | + ASSERT_EQ(static_cast<QString>(ss), GetParam().expectedTextOrPattern); |
| 55 | + } |
| 56 | + |
| 57 | + INSTANTIATE_TEST_SUITE_P(TestSuite, SanitizedStringLengthTest, testing::Values( |
| 58 | + SanitizedStringTestParam{"123456789a123456789a123456789a123456789a123456789ab", |
| 59 | + "123456789a1234-d642eb4f7beba2ee9fda95f3ed39de8~37"} |
| 60 | + )); |
26 | 61 | } |
0 commit comments