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