|
10 | 10 |
|
11 | 11 | #include <executorch/runtime/platform/log.h> |
12 | 12 | #include <executorch/runtime/platform/runtime.h> |
| 13 | +#include <executorch/runtime/platform/test/pal_spy.h> |
| 14 | +#include <executorch/runtime/platform/test/stub_platform.h> |
13 | 15 |
|
14 | 16 | using namespace executorch::runtime; |
15 | 17 |
|
16 | | -class LoggingTest : public ::testing::Test { |
17 | | - public: |
18 | | - static void SetUpTestSuite() { |
19 | | - // Initialize runtime. |
20 | | - runtime_init(); |
21 | | - } |
22 | | -}; |
| 18 | +class LoggingTest : public ::testing::Test {}; |
23 | 19 |
|
24 | 20 | TEST_F(LoggingTest, LogLevels) { |
| 21 | + PalSpy spy; |
| 22 | + InterceptWith iw(spy); |
| 23 | + |
25 | 24 | ET_LOG(Debug, "Debug log."); |
| 25 | + EXPECT_EQ(spy.last_log_message_args.message, "Debug log."); |
| 26 | + |
26 | 27 | ET_LOG(Info, "Info log."); |
| 28 | + EXPECT_EQ(spy.last_log_message_args.message, "Info log."); |
| 29 | + |
27 | 30 | ET_LOG(Error, "Error log."); |
| 31 | + EXPECT_EQ(spy.last_log_message_args.message, "Error log."); |
| 32 | + |
28 | 33 | ET_LOG(Fatal, "Fatal log."); |
| 34 | + EXPECT_EQ(spy.last_log_message_args.message, "Fatal log."); |
29 | 35 | } |
30 | 36 |
|
31 | 37 | TEST_F(LoggingTest, LogFormatting) { |
| 38 | + PalSpy spy; |
| 39 | + InterceptWith iw(spy); |
| 40 | + |
32 | 41 | ET_LOG(Info, "Sample log with integer: %u", 100); |
| 42 | + EXPECT_EQ(spy.last_log_message_args.message, "Sample log with integer: 100"); |
| 43 | +} |
| 44 | + |
| 45 | +static std::string get_prefix(std::size_t length, bool use_multibyte) { |
| 46 | + if (!use_multibyte) { |
| 47 | + return std::string(length, 'A'); |
| 48 | + } |
| 49 | + std::ostringstream result; |
| 50 | + result << std::string(length % 4, 'A'); |
| 51 | + std::size_t remaining = length - (length % 4); |
| 52 | + while (remaining > 0) { |
| 53 | + result << "\xF0\x9F\x91\x8D"; |
| 54 | + remaining -= 4; |
| 55 | + } |
| 56 | + return result.str(); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(LoggingTest, Utf8Truncation) { |
| 60 | + PalSpy spy; |
| 61 | + InterceptWith iw(spy); |
| 62 | + |
| 63 | + const char euro[] = "\xE2\x82\xAC"; |
| 64 | + const char thumbs_up[] = "\xF0\x9F\x91\x8D"; |
| 65 | + const char e_acute[] = "\xC3\xA9"; |
| 66 | + const char capital_a_tilde[] = "\xC3\x83"; |
| 67 | + |
| 68 | + struct TruncCase { |
| 69 | + size_t prefix_length; |
| 70 | + const char* codepoint; |
| 71 | + }; |
| 72 | + const TruncCase cases[] = { |
| 73 | + {253, euro}, |
| 74 | + {252, thumbs_up}, |
| 75 | + {254, e_acute}, |
| 76 | + {254, capital_a_tilde}, |
| 77 | + }; |
| 78 | + for (bool use_multibyte_prefix : {false, true}) { |
| 79 | + for (const auto& c : cases) { |
| 80 | + const std::string prefix = |
| 81 | + get_prefix(c.prefix_length, use_multibyte_prefix); |
| 82 | + const std::string suffix = "_SHOULD_BE_CUT"; |
| 83 | + ET_LOG(Info, "%s%s%s", prefix.c_str(), c.codepoint, suffix.c_str()); |
| 84 | + EXPECT_EQ(spy.last_log_message_args.message, prefix); |
| 85 | + EXPECT_EQ(spy.last_log_message_args.length, prefix.size()); |
| 86 | + } |
| 87 | + } |
33 | 88 | } |
0 commit comments