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 "pw_log/tokenized_args.h"
15
16 #include <cinttypes>
17 #include <cstring>
18
19 #include "pw_log_tokenized/log_tokenized.h"
20 #include "pw_tokenizer/enum.h"
21 #include "pw_tokenizer/hash.h"
22 #include "pw_tokenizer/tokenize.h"
23 #include "pw_unit_test/framework.h"
24
25 namespace this_is_a_test {
26 namespace {
27
28 enum Thing { kAlpha, kBravo, kCharlie };
29
30 // Tokenize the enum! Adding a new entry above but not here is a compiler error.
31 PW_TOKENIZE_ENUM(::this_is_a_test::Thing, kAlpha, kBravo, kCharlie);
32
33 enum Thing2 { kDelta, kEcho, kFoxtrot };
34
35 // Tokenize the enum with custom strings! Adding a new entry above but not here
36 // is a compiler error.
37 PW_TOKENIZE_ENUM_CUSTOM(::this_is_a_test::Thing2,
38 (kDelta, "DELTA"),
39 (kEcho, "ECHO"),
40 (kFoxtrot, "FOXTROT"));
41
42 // pw_log backends that use pw_tokenizer and want to support nested tokenization
43 // define this file under their public_overrides/ directory to activate the
44 // PW_LOG_TOKEN aliases. If this file does not exist in the log backend,
45 // arguments behave as basic strings (const char*).
46 #if __has_include("pw_log_backend/log_backend_uses_pw_tokenizer.h")
47
TEST(TokenizedArgs,EmptyString_TokenizingBackend)48 TEST(TokenizedArgs, EmptyString_TokenizingBackend) {
49 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN("");
50 EXPECT_EQ(0u, token);
51 }
52
TEST(TokenizedArgs,ExprMatchesStringExpr_TokenizingBackend)53 TEST(TokenizedArgs, ExprMatchesStringExpr_TokenizingBackend) {
54 EXPECT_EQ(::pw::tokenizer::Hash("[:-)"), PW_LOG_TOKEN_EXPR("[:-)"));
55 }
56
TEST(TokenizedArgs,LogTokenFmt_TokenizingBackend)57 TEST(TokenizedArgs, LogTokenFmt_TokenizingBackend) {
58 constexpr const char* nested_token = PW_LOG_TOKEN_FMT();
59 EXPECT_STREQ("$#%08" PRIx32, nested_token);
60 }
61
TEST(TokenizedArgs,LogTokenEnumFmt_TokenizingBackend)62 TEST(TokenizedArgs, LogTokenEnumFmt_TokenizingBackend) {
63 constexpr char nested_token[] = PW_LOG_ENUM_FMT(::this_is_a_test::Thing);
64 EXPECT_STREQ("${::this_is_a_test::Thing}#%08" PRIx32, nested_token);
65 }
66
TEST(TokenizedArgs,LogTokenOrString_TokenizingBackend)67 TEST(TokenizedArgs, LogTokenOrString_TokenizingBackend) {
68 EXPECT_EQ(static_cast<uint32_t>(kAlpha), PW_LOG_ENUM(kAlpha));
69 }
70
TEST(TokenizedArgs,LogTokenEnumFmt2_TokenizingBackend)71 TEST(TokenizedArgs, LogTokenEnumFmt2_TokenizingBackend) {
72 constexpr char nested_token[] = PW_LOG_ENUM_FMT(::this_is_a_test::Thing2);
73 EXPECT_STREQ("${::this_is_a_test::Thing2}#%08" PRIx32, nested_token);
74 }
75
TEST(TokenizedArgs,LogTokenOrString2_TokenizingBackend)76 TEST(TokenizedArgs, LogTokenOrString2_TokenizingBackend) {
77 EXPECT_EQ(static_cast<uint32_t>(kDelta), PW_LOG_ENUM(kDelta));
78 }
79
80 #else
81
TEST(TokenizedArgs,EmptyString_NonTokenizingBackend)82 TEST(TokenizedArgs, EmptyString_NonTokenizingBackend) {
83 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN("");
84 EXPECT_STREQ("", token);
85 }
86
TEST(TokenizedArgs,ExprMatchesStringExpr_NonTokenizingBackend)87 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 EXPECT_STREQ(token1, token2);
91 }
92
TEST(TokenizedArgs,LogTokenFmt_NonTokenizingBackend)93 TEST(TokenizedArgs, LogTokenFmt_NonTokenizingBackend) {
94 constexpr PW_LOG_TOKEN_TYPE token = PW_LOG_TOKEN_FMT();
95 EXPECT_STREQ("%s", token);
96 }
97
TEST(TokenizedArgs,LogTokenEnumFmt_NonTokenizingBackend)98 TEST(TokenizedArgs, LogTokenEnumFmt_NonTokenizingBackend) {
99 constexpr PW_LOG_TOKEN_TYPE nested_token =
100 PW_LOG_ENUM_FMT(::this_is_a_test::Thing);
101 EXPECT_STREQ("%s", nested_token);
102 }
103
TEST(TokenizedArgs,LogTokenOrString_NonTokenizingBackend)104 TEST(TokenizedArgs, LogTokenOrString_NonTokenizingBackend) {
105 constexpr PW_LOG_TOKEN_TYPE nested_token = PW_LOG_ENUM(kAlpha);
106 EXPECT_STREQ("kAlpha", nested_token);
107 }
108
TEST(TokenizedArgs,LogTokenEnumFmt2_NonTokenizingBackend)109 TEST(TokenizedArgs, LogTokenEnumFmt2_NonTokenizingBackend) {
110 constexpr PW_LOG_TOKEN_TYPE nested_token =
111 PW_LOG_ENUM_FMT(::this_is_a_test::Thing2);
112 EXPECT_STREQ("%s", nested_token);
113 }
114
TEST(TokenizedArgs,LogTokenOrString2_NonTokenizingBackend)115 TEST(TokenizedArgs, LogTokenOrString2_NonTokenizingBackend) {
116 constexpr PW_LOG_TOKEN_TYPE nested_token = PW_LOG_ENUM(kDelta);
117 EXPECT_STREQ("DELTA", nested_token);
118 }
119
120 #endif //__has_include("log_backend/log_backend_uses_pw_tokenizer.h")
121
122 } // namespace
123 } // namespace this_is_a_test
124