Add String Formatting Util
(Merged from http://go/wvgerrit/160042.) Since we don't have access to std::format yet, this patch adds a function to wvutil to format text into a std::string. Bug: 255466913 Test: x86-64 Test: raven Change-Id: I28043da76af5b4772a29fa7e7241343caf9b54a1
This commit is contained in:
committed by
John Bruce
parent
a3e67a9104
commit
ff73463d0b
64
libwvdrmengine/cdm/util/test/string_format_unittest.cpp
Normal file
64
libwvdrmengine/cdm/util/test/string_format_unittest.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright 2022 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "string_format.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
TEST(StringFormatTest, SignedInteger) {
|
||||
constexpr char kFormat[] = "Version %d";
|
||||
constexpr char kResult[] = "Version -123";
|
||||
std::string result;
|
||||
EXPECT_TRUE(FormatString(&result, kFormat, -123));
|
||||
EXPECT_EQ(result, kResult);
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, UnsignedInteger) {
|
||||
constexpr char kFormat[] = "Version %u";
|
||||
constexpr char kResult[] = "Version 27";
|
||||
std::string result;
|
||||
EXPECT_TRUE(FormatString(&result, kFormat, 27));
|
||||
EXPECT_EQ(result, kResult);
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, HexInteger) {
|
||||
constexpr char kFormat[] = "Version %X";
|
||||
constexpr char kResult[] = "Version FF";
|
||||
std::string result;
|
||||
EXPECT_TRUE(FormatString(&result, kFormat, 0xFF));
|
||||
EXPECT_EQ(result, kResult);
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, Strings) {
|
||||
constexpr char kFormat[] = "Hello, %s.";
|
||||
constexpr char kResult[] = "Hello, DRM.";
|
||||
std::string result;
|
||||
EXPECT_TRUE(FormatString(&result, kFormat, "DRM"));
|
||||
EXPECT_EQ(result, kResult);
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, Nothing) {
|
||||
constexpr char kString[] = "No format fields.";
|
||||
std::string result;
|
||||
EXPECT_TRUE(FormatString(&result, kString));
|
||||
EXPECT_EQ(result, kString);
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, NullOutput) {
|
||||
constexpr char kString[] = "This will never be referenced.";
|
||||
EXPECT_FALSE(FormatString(nullptr, kString));
|
||||
}
|
||||
|
||||
TEST(StringFormatTest, NullFormat) {
|
||||
std::string result;
|
||||
EXPECT_FALSE(FormatString(&result, nullptr));
|
||||
EXPECT_TRUE(result.empty());
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
Reference in New Issue
Block a user