Skip to content

Commit af1cafb

Browse files
phlptpagalanin-at-nvidia
authored andcommitted
Add a test for a global locale that changes the digit separator
1 parent 04500d8 commit af1cafb

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set(CLI11_TESTS
4949
StringParseTest
5050
ComplexTypeTest
5151
TrueFalseTest
52+
localeTest
5253
OptionGroupTest
5354
EncodingTest)
5455

tests/localeTest.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
2+
// under NSF AWARD 1414736 and by the respective contributors.
3+
// All rights reserved.
4+
//
5+
// SPDX-License-Identifier: BSD-3-Clause
6+
7+
#include "app_helper.hpp"
8+
9+
#include <complex>
10+
#include <cstdint>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <locale>
14+
#include <numeric>
15+
#include <string>
16+
#include <utility>
17+
#include <vector>
18+
19+
// Custom facet for thousands separator
20+
class CustomThousandsSeparator : public std::numpunct<char> {
21+
protected:
22+
char do_thousands_sep() const override { return '|'; } // Space separator
23+
std::string do_grouping() const override { return "\2"; } // Group digits in sets of 2
24+
};
25+
26+
// derived from https://github.com/CLIUtils/CLI11/pull/1160
27+
TEST_CASE_METHOD(TApp, "locale", "[separators]") {
28+
std::locale customLocale(std::locale::classic(), new CustomThousandsSeparator);
29+
std::locale::global(customLocale); // Set as the default system-wide locale
30+
31+
// Ensure standard streams use the custom locale automatically
32+
std::cout.imbue(std::locale());
33+
std::int64_t foo;
34+
std::uint64_t bar;
35+
float qux;
36+
37+
app.add_option("FOO", foo, "Foo option")->default_val(1234567)->force_callback();
38+
app.add_option("BAR", bar, "Bar option")->default_val(2345678)->force_callback();
39+
app.add_option("QUX", qux, "QUX option")->default_val(3456.78)->force_callback();
40+
41+
CHECK_NOTHROW(run());
42+
CHECK(foo == 1234567);
43+
CHECK(bar == 2345678);
44+
CHECK_THAT(qux, Catch::Matchers::WithinAbs(13456.78, 0.01));
45+
}

tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ testnames = [
5757
['StringParseTest', {}],
5858
['ComplexTypeTest', {}],
5959
['TrueFalseTest', {}],
60+
['localeTest', {}],
6061
['OptionGroupTest', {}],
6162
['EncodingTest', {}],
6263
# multi-only

0 commit comments

Comments
 (0)