1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#include "song/StringFilter.hxx"
#include "lib/icu/Init.hxx"
#include "config.h"
#include <gtest/gtest.h>
class StringFilterTest : public ::testing::Test {
protected:
void SetUp() override {
IcuInit();
}
void TearDown() override {
IcuFinish();
}
};
TEST_F(StringFilterTest, ASCII)
{
const StringFilter f{"needle", false, StringFilter::Position::FULL, false};
EXPECT_TRUE(f.Match("needle"));
EXPECT_FALSE(f.Match("nëedle"));
EXPECT_FALSE(f.Match("néedle"));
EXPECT_FALSE(f.Match("nèedle"));
EXPECT_FALSE(f.Match("nêedle"));
EXPECT_FALSE(f.Match("Needle"));
EXPECT_FALSE(f.Match("NEEDLE"));
EXPECT_FALSE(f.Match(""));
EXPECT_FALSE(f.Match("foo"));
EXPECT_FALSE(f.Match("needleBAR"));
EXPECT_FALSE(f.Match("FOOneedleBAR"));
}
TEST_F(StringFilterTest, Negated)
{
const StringFilter f{"needle", false, StringFilter::Position::FULL, true};
EXPECT_FALSE(f.Match("needle"));
EXPECT_TRUE(f.Match("Needle"));
EXPECT_TRUE(f.Match("NEEDLE"));
EXPECT_TRUE(f.Match(""));
EXPECT_TRUE(f.Match("foo"));
EXPECT_TRUE(f.Match("needleBAR"));
EXPECT_TRUE(f.Match("FOOneedleBAR"));
}
TEST_F(StringFilterTest, StartsWith)
{
const StringFilter f{"needle", false, StringFilter::Position::PREFIX, false};
EXPECT_TRUE(f.Match("needle"));
EXPECT_FALSE(f.Match("Needle"));
EXPECT_FALSE(f.Match("NEEDLE"));
EXPECT_FALSE(f.Match(""));
EXPECT_FALSE(f.Match("foo"));
EXPECT_TRUE(f.Match("needleBAR"));
EXPECT_FALSE(f.Match("NeedleBAR"));
EXPECT_FALSE(f.Match("FOOneedleBAR"));
}
TEST_F(StringFilterTest, IsIn)
{
const StringFilter f{"needle", false, StringFilter::Position::ANYWHERE, false};
EXPECT_TRUE(f.Match("needle"));
EXPECT_FALSE(f.Match("Needle"));
EXPECT_FALSE(f.Match("NEEDLE"));
EXPECT_FALSE(f.Match(""));
EXPECT_FALSE(f.Match("foo"));
EXPECT_TRUE(f.Match("needleBAR"));
EXPECT_FALSE(f.Match("NeedleBAR"));
EXPECT_TRUE(f.Match("FOOneedleBAR"));
}
TEST_F(StringFilterTest, Latin)
{
const StringFilter f{"nëedlé", false, StringFilter::Position::FULL, false};
EXPECT_TRUE(f.Match("nëedlé"));
#if defined(HAVE_ICU) || defined(_WIN32)
EXPECT_TRUE(f.Match("nëedl\u00e9"));
// TODO EXPECT_TRUE(f.Match("nëedl\u0065\u0301"));
#endif
EXPECT_FALSE(f.Match("NËEDLÉ"));
EXPECT_FALSE(f.Match("needlé"));
EXPECT_FALSE(f.Match("néedlé"));
EXPECT_FALSE(f.Match("nèedlé"));
EXPECT_FALSE(f.Match("nêedlé"));
EXPECT_FALSE(f.Match("Needlé"));
EXPECT_FALSE(f.Match("NEEDLÉ"));
EXPECT_FALSE(f.Match(""));
EXPECT_FALSE(f.Match("foo"));
EXPECT_FALSE(f.Match("FOOnëedleBAR"));
}
#if defined(HAVE_ICU) || defined(_WIN32)
TEST_F(StringFilterTest, Normalize)
{
const StringFilter f{"1①H", true, StringFilter::Position::FULL, false};
EXPECT_TRUE(f.Match("1①H"));
#ifndef _WIN32
// fails with Windows CompareStringEx()
EXPECT_TRUE(f.Match("¹₁H"));
EXPECT_TRUE(f.Match("①1ℌ"));
EXPECT_TRUE(f.Match("①1ℍ"));
#endif
EXPECT_FALSE(f.Match("21H"));
EXPECT_TRUE(StringFilter("dž", true, StringFilter::Position::FULL, false).Match("dž"));
EXPECT_TRUE(StringFilter("\u212b", true, StringFilter::Position::FULL, false).Match("\u0041\u030a"));
EXPECT_TRUE(StringFilter("\u212b", true, StringFilter::Position::FULL, false).Match("\u00c5"));
EXPECT_TRUE(StringFilter("\u1e69", true, StringFilter::Position::FULL, false).Match("\u0073\u0323\u0307"));
EXPECT_TRUE(StringFilter("\u1e69", true, StringFilter::Position::FULL, false).Match("\u0073\u0307\u0323"));
}
#endif
#ifdef HAVE_ICU
TEST_F(StringFilterTest, Transliterate)
{
const StringFilter f{"'", true, StringFilter::Position::FULL, false};
EXPECT_TRUE(f.Match("’"));
EXPECT_FALSE(f.Match("\""));
}
#endif
TEST_F(StringFilterTest, FoldCase)
{
const StringFilter f{"nëedlé", true, StringFilter::Position::FULL, false};
EXPECT_TRUE(f.Match("nëedlé"));
#if defined(HAVE_ICU) || defined(_WIN32)
EXPECT_TRUE(f.Match("nëedl\u00e9"));
EXPECT_TRUE(f.Match("nëedl\u0065\u0301"));
EXPECT_TRUE(f.Match("NËEDLÉ"));
EXPECT_TRUE(f.Match("NËEDL\u00c9"));
EXPECT_TRUE(f.Match("NËEDL\u0045\u0301"));
#endif
EXPECT_FALSE(f.Match("needlé"));
EXPECT_FALSE(f.Match("néedlé"));
EXPECT_FALSE(f.Match("nèedlé"));
EXPECT_FALSE(f.Match("nêedlé"));
EXPECT_FALSE(f.Match("Needlé"));
EXPECT_FALSE(f.Match("NEEDLÉ"));
EXPECT_FALSE(f.Match(""));
EXPECT_FALSE(f.Match("foo"));
EXPECT_FALSE(f.Match("FOOnëedleBAR"));
}
|