1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include <cinttypes>
15
16 #include "pw_log/log.h"
17 #include "pw_log/tokenized_args.h"
18 #include "pw_tokenizer/enum.h"
19 #include "pw_tokenizer/tokenize.h"
20 #include "pw_unit_test/constexpr.h"
21 #include "pw_unit_test/framework.h"
22
23 namespace this_is_a_test {
24 namespace {
25
26 enum Thing { kAlpha, kBravo, kCharlie };
27
28 // Tokenize the enum! Adding a new entry above but not here is a compiler error.
29 PW_TOKENIZE_ENUM(::this_is_a_test::Thing, kAlpha, kBravo, kCharlie);
30
31 enum Thing2 { kDelta, kEcho, kFoxtrot };
32
33 // Tokenize the enum with custom strings! Adding a new entry above but not here
34 // is a compiler error.
35 PW_TOKENIZE_ENUM_CUSTOM(::this_is_a_test::Thing2,
36 (kDelta, "DELTA"),
37 (kEcho, "ECHO"),
38 (kFoxtrot, "FOXTROT"));
39
40 // pw_log backends that use pw_tokenizer and want to support nested tokenization
41 // define this file under their public_overrides/ directory to activate the
42 // PW_LOG_TOKEN aliases. If this file does not exist in the log backend,
43 // arguments behave as basic strings (const char*).
44 #if __has_include("pw_log_backend/log_backend_uses_pw_tokenizer.h")
45
TEST(TokenizedArgs,EmptyString_TokenizingBackend)46 TEST(TokenizedArgs, EmptyString_TokenizingBackend) {
47 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN("");
48 EXPECT_EQ(0u, token);
49 }
50
TEST(TokenizedArgs,ExprMatchesStringExpr_TokenizingBackend)51 TEST(TokenizedArgs, ExprMatchesStringExpr_TokenizingBackend) {
52 EXPECT_EQ(pw::tokenizer::Hash("[:-)"), PW_LOG_TOKEN_EXPR("[:-)"));
53 }
54
55 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenFmt_TokenizingBackend, {
56 constexpr const char* nested_token = PW_LOG_TOKEN_FMT();
57 PW_TEST_EXPECT_STREQ("$#%08" PRIx32, nested_token);
58 });
59
60 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenEnumFmt_TokenizingBackend, {
61 constexpr char nested_token[] = PW_LOG_ENUM_FMT(::this_is_a_test::Thing);
62 PW_TEST_EXPECT_STREQ("${::this_is_a_test::Thing}#%08" PRIx32, nested_token);
63 });
64
65 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenOrString_TokenizingBackend, {
66 PW_TEST_EXPECT_EQ(static_cast<uint32_t>(kAlpha), PW_LOG_ENUM(kAlpha));
67 });
68
69 PW_CONSTEXPR_TEST(TokenizedArgs, NestedTokenFmt1_TokenizingBackend, {
70 constexpr char nested_token[] = PW_LOG_NESTED_TOKEN_FMT();
71 PW_TEST_EXPECT_STREQ("${$#%" PRIx32 "}#%08" PRIx32, nested_token);
72 });
73
74 PW_CONSTEXPR_TEST(TokenizedArgs, NestedTokenFmt2_TokenizingBackend, {
75 constexpr char nested_token[] = PW_LOG_NESTED_TOKEN_FMT("enum_domain");
76 PW_TEST_EXPECT_STREQ("${${enum_domain}#%" PRIx32 "}#%08" PRIx32,
77 nested_token);
78 });
79
80 #else
81
82 PW_CONSTEXPR_TEST(TokenizedArgs, EmptyString_NonTokenizingBackend, {
83 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN("");
84 PW_TEST_EXPECT_STREQ("", token);
85 });
86
87 PW_CONSTEXPR_TEST(TokenizedArgs, ExprMatchesStringExpr_NonTokenizingBackend, {
88 constexpr PW_LOG_TOKEN_TYPE token1 = PW_LOG_TOKEN("[:-)");
89 constexpr PW_LOG_TOKEN_TYPE token2 = PW_LOG_TOKEN_EXPR("[:-)");
90 PW_TEST_EXPECT_STREQ(token1, token2);
91 });
92
93 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenFmt_NonTokenizingBackend, {
94 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN_FMT();
95 PW_TEST_EXPECT_STREQ("%s", token);
96 });
97
98 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenEnumFmt_NonTokenizingBackend, {
99 constexpr PW_LOG_TOKEN_TYPE nested_token =
100 PW_LOG_ENUM_FMT(::this_is_a_test::Thing);
101 PW_TEST_EXPECT_STREQ("%s", nested_token);
102 });
103
104 PW_CONSTEXPR_TEST(TokenizedArgs, LogTokenOrString_NonTokenizingBackend, {
105 constexpr PW_LOG_TOKEN_TYPE nested_token = PW_LOG_ENUM(kAlpha);
106 PW_TEST_EXPECT_STREQ("kAlpha", nested_token);
107 });
108
109 PW_CONSTEXPR_TEST(TokenizedArgs, NestedTokenFmt1_NonTokenizingBackend, {
110 constexpr char nested_token[] = PW_LOG_NESTED_TOKEN_FMT();
111 PW_TEST_EXPECT_STREQ("%s::%s", nested_token);
112 });
113
114 PW_CONSTEXPR_TEST(TokenizedArgs, NestedTokenFmt2_NonTokenizingBackend, {
115 constexpr char nested_token[] = PW_LOG_NESTED_TOKEN_FMT(kAlpha);
116 PW_TEST_EXPECT_STREQ("%s::%s", nested_token);
117 });
118
119 #endif //__has_include("log_backend/log_backend_uses_pw_tokenizer.h")
120
121 } // namespace
122 } // namespace this_is_a_test
123