-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add library method to validate and canonicalize locale ID
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "../test.h" | ||
|
||
#include "src/ecma402/locale.h" | ||
|
||
#include <unicode/uloc.h> | ||
|
||
#define TEST_SUITE ecma402LocaleValidation | ||
|
||
// NOLINTBEGIN(cert-err58-cpp, misc-const-correctness, | ||
// misc-use-anonymous-namespace) | ||
|
||
using string = std::basic_string<char, std::char_traits<char>, criterion::allocator<char>>; | ||
|
||
struct localeIdValidationTest { | ||
string localeId; | ||
string expected; | ||
|
||
localeIdValidationTest(string localeId, string expected) : localeId(localeId), expected(expected) {} | ||
}; | ||
|
||
ParameterizedTestParameters(TEST_SUITE, validateAndCanonicalizeUnicodeLocaleId) | ||
{ | ||
static criterion::parameters<struct localeIdValidationTest> tests; | ||
|
||
tests.emplace_back("en-US", "en-US"); | ||
tests.emplace_back("en_US", "en-US"); | ||
tests.emplace_back("en-Latn-US", "en-Latn-US"); | ||
tests.emplace_back("en-US-u-nu-latn-ca-gregory", "en-US-u-ca-gregory-nu-latn"); | ||
tests.emplace_back("foobar", "-1"); | ||
tests.emplace_back("zz-ZZ", "-1"); | ||
tests.emplace_back("und-u-va-posix", "-1"); | ||
tests.emplace_back("en-US-POSIX", "en-US-u-va-posix"); | ||
tests.emplace_back("en_US_POSIX", "en-US-u-va-posix"); | ||
|
||
return tests; | ||
} | ||
|
||
ParameterizedTest(struct localeIdValidationTest *test, TEST_SUITE, validateAndCanonicalizeUnicodeLocaleId) | ||
{ | ||
char *result; | ||
size_t resultLength; | ||
ecma402_errorStatus *status; | ||
|
||
status = ecma402_initErrorStatus(); | ||
result = (char *)malloc(sizeof(char) * ULOC_FULLNAME_CAPACITY); | ||
resultLength = ecma402_validateAndCanonicalizeUnicodeLocaleId(test->localeId.c_str(), result, status); | ||
|
||
cr_assert(eq(i8, ecma402_hasError(status), 0)); | ||
|
||
if (test->expected == "-1") { | ||
cr_expect(eq(i8, resultLength, -1)); | ||
} else { | ||
cr_expect(eq(str, result, test->expected.c_str()), | ||
"Expected canonicalized value of \"%s\" for language tag \"%s\"; got \"%s\" instead", | ||
test->expected.c_str(), test->localeId.c_str(), result); | ||
cr_expect(eq(i8, resultLength, test->expected.length())); | ||
} | ||
|
||
free(result); | ||
} | ||
|
||
// NOLINTEND(cert-err58-cpp, misc-const-correctness, | ||
// misc-use-anonymous-namespace) |