Home | History | Annotate | Download | only in libtextclassifier
      1 /*
      2  * Copyright (C) 2017 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 "strip-unpaired-brackets.h"
     18 
     19 #include "gtest/gtest.h"
     20 
     21 namespace libtextclassifier2 {
     22 namespace {
     23 
     24 TEST(StripUnpairedBracketsTest, StripUnpairedBrackets) {
     25   CREATE_UNILIB_FOR_TESTING
     26   // If the brackets match, nothing gets stripped.
     27   EXPECT_EQ(StripUnpairedBrackets("call me (123) 456 today", {8, 17}, unilib),
     28             std::make_pair(8, 17));
     29   EXPECT_EQ(StripUnpairedBrackets("call me (123 456) today", {8, 17}, unilib),
     30             std::make_pair(8, 17));
     31 
     32   // If the brackets don't match, they get stripped.
     33   EXPECT_EQ(StripUnpairedBrackets("call me (123 456 today", {8, 16}, unilib),
     34             std::make_pair(9, 16));
     35   EXPECT_EQ(StripUnpairedBrackets("call me )123 456 today", {8, 16}, unilib),
     36             std::make_pair(9, 16));
     37   EXPECT_EQ(StripUnpairedBrackets("call me 123 456) today", {8, 16}, unilib),
     38             std::make_pair(8, 15));
     39   EXPECT_EQ(StripUnpairedBrackets("call me 123 456( today", {8, 16}, unilib),
     40             std::make_pair(8, 15));
     41 
     42   // Strips brackets correctly from length-1 selections that consist of
     43   // a bracket only.
     44   EXPECT_EQ(StripUnpairedBrackets("call me at ) today", {11, 12}, unilib),
     45             std::make_pair(12, 12));
     46   EXPECT_EQ(StripUnpairedBrackets("call me at ( today", {11, 12}, unilib),
     47             std::make_pair(12, 12));
     48 
     49   // Handles invalid spans gracefully.
     50   EXPECT_EQ(StripUnpairedBrackets("call me at  today", {11, 11}, unilib),
     51             std::make_pair(11, 11));
     52   EXPECT_EQ(StripUnpairedBrackets("hello world", {0, 0}, unilib),
     53             std::make_pair(0, 0));
     54   EXPECT_EQ(StripUnpairedBrackets("hello world", {11, 11}, unilib),
     55             std::make_pair(11, 11));
     56   EXPECT_EQ(StripUnpairedBrackets("hello world", {-1, -1}, unilib),
     57             std::make_pair(-1, -1));
     58 }
     59 
     60 }  // namespace
     61 }  // namespace libtextclassifier2
     62