| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | using namespace System; |
| 6 | using namespace Xunit; |
| 7 | using namespace WixInternal::TestSupport; |
| 8 | |
| 9 | namespace DutilTests |
| 10 | { |
| 11 | public ref class StrUtil |
| 12 | { |
| 13 | public: |
| 14 | [Fact] |
| 15 | void StrUtilFormattedTest() |
| 16 | { |
| 17 | HRESULT hr = S_OK; |
| 18 | LPWSTR sczText = NULL; |
| 19 | |
| 20 | try |
| 21 | { |
| 22 | hr = StrAllocFormatted(&sczText, L"%hs - %ls - %u", "ansi string", L"unicode string", 1234); |
| 23 | NativeAssert::Succeeded(hr, "Failed to format string."); |
| 24 | NativeAssert::StringEqual(L"ansi string - unicode string - 1234", sczText); |
| 25 | |
| 26 | ReleaseNullStr(sczText); |
| 27 | |
| 28 | hr = StrAllocString(&sczText, L"repeat", 0); |
| 29 | NativeAssert::Succeeded(hr, "Failed to allocate string."); |
| 30 | |
| 31 | hr = StrAllocFormatted(&sczText, L"%ls and %ls", sczText, sczText); |
| 32 | NativeAssert::Succeeded(hr, "Failed to format string unto itself."); |
| 33 | NativeAssert::StringEqual(L"repeat and repeat", sczText); |
| 34 | } |
| 35 | finally |
| 36 | { |
| 37 | ReleaseStr(sczText); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | [Fact] |
| 42 | void StrUtilTrimTest() |
| 43 | { |
| 44 | TestTrim(L"", L""); |
| 45 | TestTrim(L"Blah", L"Blah"); |
| 46 | TestTrim(L"\t\t\tBlah", L"Blah"); |
| 47 | TestTrim(L"\t Blah ", L"Blah"); |
| 48 | TestTrim(L"Blah ", L"Blah"); |
| 49 | TestTrim(L"\t Spaces \t Between \t", L"Spaces \t Between"); |
| 50 | TestTrim(L" \t\t\t ", L""); |
| 51 | |
| 52 | TestTrimAnsi("", ""); |
| 53 | TestTrimAnsi("Blah", "Blah"); |
| 54 | TestTrimAnsi("\t\t\tBlah", "Blah"); |
| 55 | TestTrimAnsi(" Blah ", "Blah"); |
| 56 | TestTrimAnsi("Blah ", "Blah"); |
| 57 | TestTrimAnsi("\t Spaces \t Between \t", "Spaces \t Between"); |
| 58 | TestTrimAnsi(" \t\t\t ", ""); |
| 59 | } |
| 60 | |
| 61 | [Fact] |
| 62 | void StrUtilConvertTest() |
| 63 | { |
| 64 | char a[] = { 'a', 'b', 'C', 'd', '\0', '\0' }; |
| 65 | |
| 66 | TestStrAllocStringAnsi(a, 5, L"abCd"); |
| 67 | TestStrAllocStringAnsi(a, 4, L"abCd"); |
| 68 | TestStrAllocStringAnsi(a, 3, L"abC"); |
| 69 | TestStrAllocStringAnsi(a, 2, L"ab"); |
| 70 | TestStrAllocStringAnsi(a, 1, L"a"); |
| 71 | TestStrAllocStringAnsi(a, 0, L"abCd"); |
| 72 | |
| 73 | wchar_t b[] = { L'a', L'b', L'C', L'd', L'\0', L'\0' }; |
| 74 | |
| 75 | TestStrAnsiAllocString(b, 5, "abCd"); |
| 76 | TestStrAnsiAllocString(b, 4, "abCd"); |
| 77 | TestStrAnsiAllocString(b, 3, "abC"); |
| 78 | TestStrAnsiAllocString(b, 2, "ab"); |
| 79 | TestStrAnsiAllocString(b, 1, "a"); |
| 80 | TestStrAnsiAllocString(b, 0, "abCd"); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | void TestTrim(LPCWSTR wzInput, LPCWSTR wzExpectedResult) |
| 85 | { |
| 86 | HRESULT hr = S_OK; |
| 87 | LPWSTR sczOutput = NULL; |
| 88 | |
| 89 | DutilInitialize(&DutilTestTraceError); |
| 90 | |
| 91 | try |
| 92 | { |
| 93 | hr = StrTrimWhitespace(&sczOutput, wzInput); |
| 94 | NativeAssert::Succeeded(hr, "Failed to trim whitespace from string: {0}", wzInput); |
| 95 | |
| 96 | if (0 != wcscmp(wzExpectedResult, sczOutput)) |
| 97 | { |
| 98 | hr = E_FAIL; |
| 99 | ExitOnFailure(hr, "Trimmed string \"%ls\", expected result \"%ls\", actual result \"%ls\"", wzInput, wzExpectedResult, sczOutput); |
| 100 | } |
| 101 | } |
| 102 | finally |
| 103 | { |
| 104 | ReleaseStr(sczOutput); |
| 105 | } |
| 106 | |
| 107 | LExit: |
| 108 | DutilUninitialize(); |
| 109 | } |
| 110 | |
| 111 | void TestTrimAnsi(LPCSTR szInput, LPCSTR szExpectedResult) |
| 112 | { |
| 113 | HRESULT hr = S_OK; |
| 114 | LPSTR sczOutput = NULL; |
| 115 | |
| 116 | DutilInitialize(&DutilTestTraceError); |
| 117 | |
| 118 | try |
| 119 | { |
| 120 | hr = StrAnsiTrimWhitespace(&sczOutput, szInput); |
| 121 | NativeAssert::Succeeded(hr, "Failed to trim whitespace from string: \"{0}\"", szInput); |
| 122 | |
| 123 | if (0 != strcmp(szExpectedResult, sczOutput)) |
| 124 | { |
| 125 | hr = E_FAIL; |
| 126 | ExitOnFailure(hr, "Trimmed string \"%hs\", expected result \"%hs\", actual result \"%hs\"", szInput, szExpectedResult, sczOutput); |
| 127 | } |
| 128 | } |
| 129 | finally |
| 130 | { |
| 131 | ReleaseStr(sczOutput); |
| 132 | } |
| 133 | |
| 134 | LExit: |
| 135 | DutilUninitialize(); |
| 136 | } |
| 137 | |
| 138 | void TestStrAllocStringAnsi(LPCSTR szSource, DWORD cchSource, LPCWSTR wzExpectedResult) |
| 139 | { |
| 140 | HRESULT hr = S_OK; |
| 141 | LPWSTR sczOutput = NULL; |
| 142 | |
| 143 | DutilInitialize(&DutilTestTraceError); |
| 144 | |
| 145 | try |
| 146 | { |
| 147 | hr = StrAllocStringAnsi(&sczOutput, szSource, cchSource, CP_UTF8); |
| 148 | NativeAssert::Succeeded(hr, "Failed to call StrAllocStringAnsi on string: \"{0}\"", szSource); |
| 149 | |
| 150 | if (0 != wcscmp(sczOutput, wzExpectedResult)) |
| 151 | { |
| 152 | hr = E_FAIL; |
| 153 | ExitOnFailure(hr, "String doesn't match, expected result \"%ls\", actual result \"%ls\"", wzExpectedResult, sczOutput); |
| 154 | } |
| 155 | } |
| 156 | finally |
| 157 | { |
| 158 | ReleaseStr(sczOutput); |
| 159 | } |
| 160 | |
| 161 | LExit: |
| 162 | DutilUninitialize(); |
| 163 | } |
| 164 | |
| 165 | void TestStrAnsiAllocString(LPWSTR wzSource, DWORD cchSource, LPCSTR szExpectedResult) |
| 166 | { |
| 167 | HRESULT hr = S_OK; |
| 168 | LPSTR sczOutput = NULL; |
| 169 | |
| 170 | DutilInitialize(&DutilTestTraceError); |
| 171 | |
| 172 | try |
| 173 | { |
| 174 | hr = StrAnsiAllocString(&sczOutput, wzSource, cchSource, CP_UTF8); |
| 175 | NativeAssert::Succeeded(hr, "Failed to call StrAllocStringAnsi on string: \"{0}\"", wzSource); |
| 176 | |
| 177 | if (0 != strcmp(sczOutput, szExpectedResult)) |
| 178 | { |
| 179 | hr = E_FAIL; |
| 180 | ExitOnFailure(hr, "String doesn't match, expected result \"%hs\", actual result \"%hs\"", szExpectedResult, sczOutput); |
| 181 | } |
| 182 | } |
| 183 | finally |
| 184 | { |
| 185 | ReleaseStr(sczOutput); |
| 186 | } |
| 187 | |
| 188 | LExit: |
| 189 | DutilUninitialize(); |
| 190 | } |
| 191 | }; |
| 192 | } |