Skip to content

Commit 88451ec

Browse files
committed
Refactor test_formatting
- Make spelling of "ICU" consistently uppercase (except at start of names) - Move definitions related to existance of ICU together and sort by name
1 parent 087dfb6 commit 88451ec

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

test/formatting_common.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <limits>
1111
#include <sstream>
1212

13-
#include "../src/boost/locale/util/foreach_char.hpp"
1413
#include "boostLocale/test/tools.hpp"
1514
#include "boostLocale/test/unit_test.hpp"
1615

test/test_formatting.cpp

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3-
// Copyright (c) 2021-2022 Alexander Grund
3+
// Copyright (c) 2021-2024 Alexander Grund
44
//
55
// Distributed under the Boost Software License, Version 1.0.
66
// https://www.boost.org/LICENSE_1_0.txt
@@ -26,103 +26,109 @@
2626
const std::string test_locale_name = "en_US";
2727
std::string message_path = "./";
2828

29-
#ifdef BOOST_LOCALE_WITH_ICU
29+
#ifndef BOOST_LOCALE_WITH_ICU
30+
# define BOOST_LOCALE_ICU_VERSION 0
31+
# define BOOST_LOCALE_ICU_VERSION_EXACT 0
32+
#else
3033
# include <unicode/datefmt.h>
3134
# include <unicode/numfmt.h>
3235
# include <unicode/timezone.h>
3336
# include <unicode/uversion.h>
3437
# define BOOST_LOCALE_ICU_VERSION (U_ICU_VERSION_MAJOR_NUM * 100 + U_ICU_VERSION_MINOR_NUM)
3538
# define BOOST_LOCALE_ICU_VERSION_EXACT (BOOST_LOCALE_ICU_VERSION * 100 + U_ICU_VERSION_PATCHLEVEL_NUM)
39+
#endif
3640

37-
const icu::Locale& get_icu_test_locale()
41+
using format_style_t = std::ios_base&(std::ios_base&);
42+
43+
namespace {
44+
#ifndef BOOST_LOCALE_WITH_ICU
45+
const std::string icu_full_gmt_name;
46+
// clang-format off
47+
std::string get_ICU_currency_iso(...){ return ""; } // LCOV_EXCL_LINE
48+
std::string get_ICU_date(...){ return ""; } // LCOV_EXCL_LINE
49+
std::string get_ICU_datetime(...){ return ""; } // LCOV_EXCL_LINE
50+
std::string get_ICU_time(...){ return ""; } // LCOV_EXCL_LINE
51+
// clang-format on
52+
#else
53+
const icu::Locale& get_ICU_test_locale()
3854
{
3955
static icu::Locale locale = icu::Locale::createCanonical(test_locale_name.c_str());
4056
return locale;
4157
}
4258

43-
std::string from_icu_string(const icu::UnicodeString& str)
59+
std::string from_ICU_string(const icu::UnicodeString& str)
4460
{
4561
return boost::locale::conv::utf_to_utf<char>(str.getBuffer(), str.getBuffer() + str.length());
4662
}
47-
#else
48-
# define BOOST_LOCALE_ICU_VERSION 0
49-
# define BOOST_LOCALE_ICU_VERSION_EXACT 0
50-
#endif
5163

5264
// Currency style changes between ICU versions, so get "real" value from ICU
53-
#if BOOST_LOCALE_ICU_VERSION >= 402
65+
# if BOOST_LOCALE_ICU_VERSION >= 402
5466

55-
std::string get_icu_currency_iso(const double value)
67+
std::string get_ICU_currency_iso(const double value)
5668
{
57-
# if BOOST_LOCALE_ICU_VERSION >= 408
69+
# if BOOST_LOCALE_ICU_VERSION >= 408
5870
auto styleIso = UNUM_CURRENCY_ISO;
59-
# else
71+
# else
6072
auto styleIso = icu::NumberFormat::kIsoCurrencyStyle;
61-
# endif
73+
# endif
6274
UErrorCode err = U_ZERO_ERROR;
63-
std::unique_ptr<icu::NumberFormat> fmt(icu::NumberFormat::createInstance(get_icu_test_locale(), styleIso, err));
75+
std::unique_ptr<icu::NumberFormat> fmt(icu::NumberFormat::createInstance(get_ICU_test_locale(), styleIso, err));
6476
TEST_REQUIRE(U_SUCCESS(err) && fmt.get());
6577

6678
icu::UnicodeString tmp;
67-
return from_icu_string(fmt->format(value, tmp));
79+
return from_ICU_string(fmt->format(value, tmp));
6880
}
6981

70-
#endif
71-
72-
using format_style_t = std::ios_base&(std::ios_base&);
73-
74-
#ifdef BOOST_LOCALE_WITH_ICU
75-
std::string get_icu_gmt_name(icu::TimeZone::EDisplayType style)
82+
# endif
83+
std::string get_ICU_gmt_name(icu::TimeZone::EDisplayType style)
7684
{
7785
icu::UnicodeString tmp;
78-
return from_icu_string(icu::TimeZone::getGMT()->getDisplayName(false, style, get_icu_test_locale(), tmp));
86+
return from_ICU_string(icu::TimeZone::getGMT()->getDisplayName(false, style, get_ICU_test_locale(), tmp));
7987
}
8088

8189
// This changes between ICU versions, e.g. "GMT" or "Greenwich Mean Time"
82-
const std::string icu_full_gmt_name = get_icu_gmt_name(icu::TimeZone::EDisplayType::LONG);
90+
const std::string icu_full_gmt_name = get_ICU_gmt_name(icu::TimeZone::EDisplayType::LONG);
8391

84-
std::string get_ICU_time(format_style_t style, const time_t ts, const char* tz = nullptr)
92+
std::string get_ICU_date(format_style_t style, const time_t ts)
8593
{
8694
using icu::DateFormat;
8795
DateFormat::EStyle icu_style = DateFormat::kDefault;
8896
namespace as = boost::locale::as;
89-
if(style == as::time_short)
97+
if(style == as::date_short)
9098
icu_style = DateFormat::kShort;
91-
else if(style == as::time_medium)
99+
else if(style == as::date_medium)
92100
icu_style = DateFormat::kMedium;
93-
else if(style == as::time_long)
101+
else if(style == as::date_long)
94102
icu_style = DateFormat::kLong;
95-
else if(style == as::time_full)
103+
else if(style == as::date_full)
96104
icu_style = DateFormat::kFull;
97-
std::unique_ptr<icu::DateFormat> fmt(icu::DateFormat::createTimeInstance(icu_style, get_icu_test_locale()));
98-
if(!tz)
99-
fmt->setTimeZone(*icu::TimeZone::getGMT());
100-
else
101-
fmt->adoptTimeZone(icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(tz)));
105+
std::unique_ptr<icu::DateFormat> fmt(icu::DateFormat::createDateInstance(icu_style, get_ICU_test_locale()));
106+
fmt->setTimeZone(*icu::TimeZone::getGMT());
102107
icu::UnicodeString s;
103-
return from_icu_string(fmt->format(ts * 1000., s));
108+
return from_ICU_string(fmt->format(ts * 1000., s));
104109
}
105110

106-
std::string get_ICU_date(format_style_t style, const time_t ts)
111+
std::string get_ICU_datetime(format_style_t style, const time_t ts)
107112
{
108113
using icu::DateFormat;
109114
DateFormat::EStyle icu_style = DateFormat::kDefault;
110115
namespace as = boost::locale::as;
111-
if(style == as::date_short)
116+
if(style == as::time_short)
112117
icu_style = DateFormat::kShort;
113-
else if(style == as::date_medium)
118+
else if(style == as::time_medium)
114119
icu_style = DateFormat::kMedium;
115-
else if(style == as::date_long)
120+
else if(style == as::time_long)
116121
icu_style = DateFormat::kLong;
117-
else if(style == as::date_full)
122+
else if(style == as::time_full)
118123
icu_style = DateFormat::kFull;
119-
std::unique_ptr<icu::DateFormat> fmt(icu::DateFormat::createDateInstance(icu_style, get_icu_test_locale()));
124+
std::unique_ptr<icu::DateFormat> fmt(
125+
icu::DateFormat::createDateTimeInstance(icu_style, icu_style, get_ICU_test_locale()));
120126
fmt->setTimeZone(*icu::TimeZone::getGMT());
121127
icu::UnicodeString s;
122-
return from_icu_string(fmt->format(ts * 1000., s));
128+
return from_ICU_string(fmt->format(ts * 1000., s));
123129
}
124130

125-
std::string get_ICU_datetime(format_style_t style, const time_t ts)
131+
std::string get_ICU_time(format_style_t style, const time_t ts, const char* tz = nullptr)
126132
{
127133
using icu::DateFormat;
128134
DateFormat::EStyle icu_style = DateFormat::kDefault;
@@ -135,21 +141,16 @@ std::string get_ICU_datetime(format_style_t style, const time_t ts)
135141
icu_style = DateFormat::kLong;
136142
else if(style == as::time_full)
137143
icu_style = DateFormat::kFull;
138-
std::unique_ptr<icu::DateFormat> fmt(
139-
icu::DateFormat::createDateTimeInstance(icu_style, icu_style, get_icu_test_locale()));
140-
fmt->setTimeZone(*icu::TimeZone::getGMT());
144+
std::unique_ptr<icu::DateFormat> fmt(icu::DateFormat::createTimeInstance(icu_style, get_ICU_test_locale()));
145+
if(!tz)
146+
fmt->setTimeZone(*icu::TimeZone::getGMT());
147+
else
148+
fmt->adoptTimeZone(icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(tz)));
141149
icu::UnicodeString s;
142-
return from_icu_string(fmt->format(ts * 1000., s));
150+
return from_ICU_string(fmt->format(ts * 1000., s));
143151
}
144-
145-
#else
146-
const std::string icu_full_gmt_name;
147-
// clang-format off
148-
std::string get_ICU_time(...){ return ""; } // LCOV_EXCL_LINE
149-
std::string get_ICU_datetime(...){ return ""; } // LCOV_EXCL_LINE
150-
std::string get_ICU_date(...){ return ""; } // LCOV_EXCL_LINE
151-
// clang-format on
152152
#endif
153+
} // namespace
153154

154155
using namespace boost::locale;
155156

@@ -427,8 +428,8 @@ void test_manip(std::string e_charset = "UTF-8")
427428
#if BOOST_LOCALE_ICU_VERSION >= 402
428429
TEST_FMT_PARSE_2(as::currency, as::currency_national, 1345, "$1,345.00");
429430
TEST_FMT_PARSE_2(as::currency, as::currency_national, 1345.34, "$1,345.34");
430-
TEST_FMT_PARSE_2(as::currency, as::currency_iso, 1345, get_icu_currency_iso(1345));
431-
TEST_FMT_PARSE_2(as::currency, as::currency_iso, 1345.34, get_icu_currency_iso(1345.34));
431+
TEST_FMT_PARSE_2(as::currency, as::currency_iso, 1345, get_ICU_currency_iso(1345));
432+
TEST_FMT_PARSE_2(as::currency, as::currency_iso, 1345.34, get_ICU_currency_iso(1345.34));
432433
#endif
433434
TEST_FMT_PARSE_1(as::spellout, 10, "ten");
434435
#if 402 <= BOOST_LOCALE_ICU_VERSION && BOOST_LOCALE_ICU_VERSION < 408
@@ -800,7 +801,7 @@ void test_format_class(std::string charset = "UTF-8")
800801
#if BOOST_LOCALE_ICU_VERSION >= 402
801802
TEST_FORMAT_CLS("{1,cur=nat}", 1234, "$1,234.00");
802803
TEST_FORMAT_CLS("{1,cur=national}", 1234, "$1,234.00");
803-
TEST_FORMAT_CLS("{1,cur=iso}", 1234, get_icu_currency_iso(1234));
804+
TEST_FORMAT_CLS("{1,cur=iso}", 1234, get_ICU_currency_iso(1234));
804805
#endif
805806
TEST_FORMAT_CLS("{1,spell}", 10, "ten");
806807
TEST_FORMAT_CLS("{1,spellout}", 10, "ten");

0 commit comments

Comments
 (0)