1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/strings/substitute.h"
18
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23
24 #include "utils/strings/stringpiece.h"
25
26 namespace libtextclassifier3 {
27 namespace {
28
TEST(SubstituteTest,Substitute)29 TEST(SubstituteTest, Substitute) {
30 EXPECT_EQ("Hello, world!",
31 strings::Substitute("$0, $1!", {"Hello", "world"}));
32
33 // Out of order.
34 EXPECT_EQ("world, Hello!",
35 strings::Substitute("$1, $0!", {"Hello", "world"}));
36 EXPECT_EQ("b, a, c, b",
37 strings::Substitute("$1, $0, $2, $1", {"a", "b", "c"}));
38
39 // Literal $
40 EXPECT_EQ("$", strings::Substitute("$$", {}));
41 EXPECT_EQ("$1", strings::Substitute("$$1", {}));
42
43 const char* null_cstring = nullptr;
44 EXPECT_EQ("Text: ''", strings::Substitute("Text: '$0'", {null_cstring}));
45 }
46
47 } // namespace
48 } // namespace libtextclassifier3
49