• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "annotator/strip-unpaired-brackets.h"
18 
19 #include "gtest/gtest.h"
20 
21 namespace libtextclassifier3 {
22 namespace {
23 
24 class StripUnpairedBracketsTest : public ::testing::Test {
25  protected:
StripUnpairedBracketsTest()26   StripUnpairedBracketsTest() : INIT_UNILIB_FOR_TESTING(unilib_) {}
27   UniLib unilib_;
28 };
29 
TEST_F(StripUnpairedBracketsTest,StripUnpairedBrackets)30 TEST_F(StripUnpairedBracketsTest, StripUnpairedBrackets) {
31   // If the brackets match, nothing gets stripped.
32   EXPECT_EQ(StripUnpairedBrackets("call me (123) 456 today", {8, 17}, unilib_),
33             std::make_pair(8, 17));
34   EXPECT_EQ(StripUnpairedBrackets("call me (123 456) today", {8, 17}, unilib_),
35             std::make_pair(8, 17));
36 
37   // If the brackets don't match, they get stripped.
38   EXPECT_EQ(StripUnpairedBrackets("call me (123 456 today", {8, 16}, unilib_),
39             std::make_pair(9, 16));
40   EXPECT_EQ(StripUnpairedBrackets("call me )123 456 today", {8, 16}, unilib_),
41             std::make_pair(9, 16));
42   EXPECT_EQ(StripUnpairedBrackets("call me 123 456) today", {8, 16}, unilib_),
43             std::make_pair(8, 15));
44   EXPECT_EQ(StripUnpairedBrackets("call me 123 456( today", {8, 16}, unilib_),
45             std::make_pair(8, 15));
46 
47   // Strips brackets correctly from length-1 selections that consist of
48   // a bracket only.
49   EXPECT_EQ(StripUnpairedBrackets("call me at ) today", {11, 12}, unilib_),
50             std::make_pair(12, 12));
51   EXPECT_EQ(StripUnpairedBrackets("call me at ( today", {11, 12}, unilib_),
52             std::make_pair(12, 12));
53 
54   // Handles invalid spans gracefully.
55   EXPECT_EQ(StripUnpairedBrackets("call me at  today", {11, 11}, unilib_),
56             std::make_pair(11, 11));
57   EXPECT_EQ(StripUnpairedBrackets("hello world", {0, 0}, unilib_),
58             std::make_pair(0, 0));
59   EXPECT_EQ(StripUnpairedBrackets("hello world", {11, 11}, unilib_),
60             std::make_pair(11, 11));
61   EXPECT_EQ(StripUnpairedBrackets("hello world", {-1, -1}, unilib_),
62             std::make_pair(-1, -1));
63 }
64 
65 }  // namespace
66 }  // namespace libtextclassifier3
67