tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestTextDirective.cpp (8336B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "TextDirectiveUtil.h"
      8 #include "gtest/gtest.h"
      9 
     10 using namespace mozilla::dom;
     11 
     12 TEST(TestTextDirective, ComputeWordBoundaryDistancesLTR)
     13 {
     14  nsString text(u"Hello, world! This is a test.");
     15  nsTArray<uint32_t> wordEndDistances =
     16      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     17          text);
     18  EXPECT_EQ(wordEndDistances.Length(), 7u);
     19  EXPECT_EQ(wordEndDistances[0], 5u);   // "Hello"
     20  EXPECT_EQ(wordEndDistances[1], 12u);  // "Hello, world"
     21  EXPECT_EQ(wordEndDistances[2], 18u);  // "Hello, world! This"
     22  EXPECT_EQ(wordEndDistances[3], 21u);  // "Hello, world! This is"
     23  EXPECT_EQ(wordEndDistances[4], 23u);  // "Hello, world! This is a"
     24  EXPECT_EQ(wordEndDistances[5], 28u);  // "Hello, world! This is a test"
     25  EXPECT_EQ(wordEndDistances[6], 29u);  // "Hello, world! This is a test."
     26 }
     27 
     28 TEST(TestTextDirective, ComputeWordBoundaryDistancesRTL)
     29 {
     30  nsString text(u"Hello, world! This is a test.");
     31  nsTArray<uint32_t> wordBeginDistances =
     32      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Left>(
     33          text);
     34  EXPECT_EQ(wordBeginDistances.Length(), 6u);
     35  EXPECT_EQ(wordBeginDistances[0], 5u);   // "test."
     36  EXPECT_EQ(wordBeginDistances[1], 7u);   // "a test."
     37  EXPECT_EQ(wordBeginDistances[2], 10u);  // "is a test."
     38  EXPECT_EQ(wordBeginDistances[3], 15u);  // "This is a test."
     39  EXPECT_EQ(wordBeginDistances[4], 22u);  // "world! This is a test."
     40  EXPECT_EQ(wordBeginDistances[5], 29u);  // "Hello, world! This is a test."
     41 }
     42 
     43 TEST(TestTextDirective, ComputeWordBoundaryDistancesPunctuationOnly)
     44 {
     45  nsString text(u": , .");
     46  nsTArray<uint32_t> wordEndDistances =
     47      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     48          text);
     49  EXPECT_EQ(wordEndDistances.Length(), 1u);
     50  EXPECT_EQ(wordEndDistances[0], 5u);
     51 }
     52 
     53 TEST(TestTextDirective,
     54     ComputeWordBoundaryDistancesWithPunctuationSeparatedByWhitespace)
     55 {
     56  nsString text(u"foo ... bar");
     57  nsTArray<uint32_t> wordEndDistances =
     58      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     59          text);
     60  EXPECT_EQ(wordEndDistances.Length(), 2u);
     61  EXPECT_EQ(wordEndDistances[0], 3u);   // "foo"
     62  EXPECT_EQ(wordEndDistances[1], 11u);  // "foo ... bar"
     63 }
     64 
     65 TEST(TestTextDirective, ComputeWordBoundaryDistancesEndingInPunctuation)
     66 {
     67  nsString text(u"foo ...");
     68  nsTArray<uint32_t> wordEndDistances =
     69      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     70          text);
     71  EXPECT_EQ(wordEndDistances.Length(), 2u);
     72  EXPECT_EQ(wordEndDistances[0], 3u);  // "foo"
     73  EXPECT_EQ(wordEndDistances[1], 7u);  // "foo ..."
     74 }
     75 
     76 TEST(TestTextDirective, ComputeWordBoundaryDistancesWithEmptyString)
     77 {
     78  nsString text(u"");
     79  nsTArray<uint32_t> wordEndDistances =
     80      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     81          text);
     82  EXPECT_EQ(wordEndDistances.Length(), 1u);
     83  EXPECT_EQ(wordEndDistances[0], 0u);
     84 }
     85 
     86 TEST(TestTextDirective,
     87     RemoveFirstWordFromStringAndDistanceArrayPunctuationAfterFirstWordLTR)
     88 {
     89  nsString text(u"Hello, world! This is a test.");
     90  nsTArray<uint32_t> wordEndDistances =
     91      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
     92          text);
     93  EXPECT_EQ(wordEndDistances.Length(), 7u);
     94  uint32_t firstWordLength =
     95      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
     96          TextScanDirection::Right>(text, wordEndDistances);
     97  EXPECT_EQ(firstWordLength, 7u);  // "Hello, "
     98  EXPECT_EQ(text, u"world! This is a test.");
     99  EXPECT_EQ(wordEndDistances.Length(), 6u);
    100  EXPECT_EQ(wordEndDistances[0], 5u);   // "world"
    101  EXPECT_EQ(wordEndDistances[1], 11u);  // "world! This"
    102  EXPECT_EQ(wordEndDistances[2], 14u);  // "world! This is"
    103  EXPECT_EQ(wordEndDistances[3], 16u);  // "world! This is a"
    104  EXPECT_EQ(wordEndDistances[4], 21u);  // "world! This is a test"
    105  EXPECT_EQ(wordEndDistances[5], 22u);  // "world! This is a test."
    106 }
    107 
    108 TEST(TestTextDirective, RemoveFirstWordFromStringInParenthesisLTR)
    109 {
    110  nsString text(u"(Hello) world");
    111  nsTArray<uint32_t> wordEndDistances =
    112      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
    113          text);
    114  EXPECT_EQ(wordEndDistances.Length(), 2u);
    115  uint32_t firstWordLength =
    116      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    117          TextScanDirection::Right>(text, wordEndDistances);
    118  EXPECT_EQ(firstWordLength, 8u);  // "(Hello) "
    119  EXPECT_EQ(text, u"world");
    120  EXPECT_EQ(wordEndDistances.Length(), 1u);
    121  EXPECT_EQ(wordEndDistances[0], 5u);  // "world"
    122 }
    123 
    124 TEST(TestTextDirective, RemoveFirstWordFromStringAndDistanceArrayRTL)
    125 {
    126  nsString text(u"Hello, world! This is a test.");
    127  nsTArray<uint32_t> wordEndDistances =
    128      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Left>(
    129          text);
    130  EXPECT_EQ(wordEndDistances.Length(), 6u);
    131  uint32_t firstWordLength =
    132      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    133          TextScanDirection::Left>(text, wordEndDistances);
    134  EXPECT_EQ(firstWordLength, 6u);  // " test."
    135  EXPECT_EQ(text, u"Hello, world! This is a");
    136  EXPECT_EQ(wordEndDistances.Length(), 5u);
    137  EXPECT_EQ(wordEndDistances[0], 1u);   // "a"
    138  EXPECT_EQ(wordEndDistances[1], 4u);   // "is a"
    139  EXPECT_EQ(wordEndDistances[2], 9u);   // "This is a"
    140  EXPECT_EQ(wordEndDistances[3], 16u);  // "world! This is a"
    141  EXPECT_EQ(wordEndDistances[4], 23u);  // "Hello, world! This is a"
    142 }
    143 
    144 TEST(TestTextDirective, RemoveFirstWordFromStringInParenthesisRTL)
    145 {
    146  nsString text(u"Hello (world)");
    147  nsTArray<uint32_t> wordEndDistances =
    148      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Left>(
    149          text);
    150  EXPECT_EQ(wordEndDistances.Length(), 2u);
    151  uint32_t firstWordLength =
    152      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    153          TextScanDirection::Left>(text, wordEndDistances);
    154  EXPECT_EQ(firstWordLength, 8u);  // " (world)"
    155  EXPECT_EQ(text, u"Hello");
    156  EXPECT_EQ(wordEndDistances.Length(), 1u);
    157  EXPECT_EQ(wordEndDistances[0], 5u);  // "Hello"
    158 }
    159 
    160 TEST(TestTextDirective,
    161     RemoveFirstWordFromStringAndDistanceArrayMultiplePunctuationLTR)
    162 {
    163  nsString text(u"...foo!!! bar?");
    164  nsTArray<uint32_t> wordEndDistances =
    165      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
    166          text);
    167  EXPECT_EQ(wordEndDistances.Length(), 3u);
    168  uint32_t firstWordLength =
    169      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    170          TextScanDirection::Right>(text, wordEndDistances);
    171  EXPECT_EQ(firstWordLength, 10u);  // "...foo!!! "
    172  EXPECT_EQ(text, u"bar?");
    173  EXPECT_EQ(wordEndDistances.Length(), 2u);
    174  EXPECT_EQ(wordEndDistances[0], 3u);  // "bar"
    175  EXPECT_EQ(wordEndDistances[1], 4u);  // "bar?"
    176 }
    177 
    178 TEST(TestTextDirective,
    179     RemoveFirstWordFromStringAndDistanceArrayMultiplePunctuationRTL)
    180 {
    181  nsString text(u"foo!!! ...bar");
    182  nsTArray<uint32_t> wordEndDistances =
    183      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Left>(
    184          text);
    185  EXPECT_EQ(wordEndDistances.Length(), 2u);
    186  uint32_t firstWordLength =
    187      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    188          TextScanDirection::Left>(text, wordEndDistances);
    189  EXPECT_EQ(firstWordLength, 10u);  // "!!! ...bar"
    190  EXPECT_EQ(text, u"foo");
    191  EXPECT_EQ(wordEndDistances.Length(), 1u);
    192  EXPECT_EQ(wordEndDistances[0], 3u);  // "foo"
    193 }
    194 
    195 TEST(TestTextDirective,
    196     RemoveFirstWordFromStringAndDistanceArrayEndsInPunctuationLTR)
    197 {
    198  nsString text(u"foo ...");
    199  nsTArray<uint32_t> wordEndDistances =
    200      TextDirectiveUtil::ComputeWordBoundaryDistances<TextScanDirection::Right>(
    201          text);
    202  EXPECT_EQ(wordEndDistances.Length(), 2u);
    203  uint32_t firstWordLength =
    204      TextDirectiveUtil::RemoveFirstWordFromStringAndDistanceArray<
    205          TextScanDirection::Right>(text, wordEndDistances);
    206  EXPECT_EQ(firstWordLength, 7u);  // "foo ..."
    207  EXPECT_EQ(text, u"foo ...");
    208  EXPECT_EQ(wordEndDistances.Length(), 0u);
    209 }