File: localeTest.cpp

package info (click to toggle)
cli11 2.6.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,520 kB
  • sloc: cpp: 26,743; python: 129; sh: 64; makefile: 11
file content (47 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

#include "app_helper.hpp"

#include <complex>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <locale>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

// Custom facet for thousands separator
class CustomThousandsSeparator : public std::numpunct<char> {
  protected:
    char do_thousands_sep() const override { return '|'; }     // Space separator
    std::string do_grouping() const override { return "\2"; }  // Group digits in sets of 2
};

#if CLI11_HAS_RTTI != 0
// derived from https://github.com/CLIUtils/CLI11/pull/1160
TEST_CASE_METHOD(TApp, "locale", "[separators]") {
    std::locale customLocale(std::locale::classic(), new CustomThousandsSeparator);
    std::locale::global(customLocale);  // Set as the default system-wide locale

    // Ensure standard streams use the custom locale automatically
    std::cout.imbue(std::locale());
    std::int64_t foo{0};
    std::uint64_t bar{0};
    float qux{0};

    app.add_option("FOO", foo, "Foo option")->default_val(1234567)->force_callback();
    app.add_option("BAR", bar, "Bar option")->default_val(2345678)->force_callback();
    app.add_option("QUX", qux, "QUX option")->default_val(3456.78)->force_callback();

    CHECK_NOTHROW(run());
    CHECK(foo == 1234567);
    CHECK(bar == 2345678);
    CHECK_THAT(qux, Catch::Matchers::WithinAbs(3456.78, 0.01));
}
#endif