summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/regex/regex.cpp
blob: d1b94daa7ba514faad6f2a9054f164ae010834c8 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/vespalib/util/regexp.h>
#include <vespa/vespalib/util/exception.h>
#include <regex>

using namespace vespalib;

TEST("require that prefix detection works") {
    EXPECT_EQUAL("", Regexp::get_prefix(""));
    EXPECT_EQUAL("", Regexp::get_prefix("foo"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo"));
    EXPECT_EQUAL("", Regexp::get_prefix("^foo|bar"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo$"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo[a-z]"));
    EXPECT_EQUAL("fo", Regexp::get_prefix("^foo{0,1}"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo."));
    EXPECT_EQUAL("fo", Regexp::get_prefix("^foo*"));
    EXPECT_EQUAL("fo", Regexp::get_prefix("^foo?"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo+"));
}

TEST("require that prefix detection sometimes underestimates the prefix size") {
    EXPECT_EQUAL("", Regexp::get_prefix("^^foo"));
    EXPECT_EQUAL("", Regexp::get_prefix("^foo(bar|baz)"));
    EXPECT_EQUAL("fo", Regexp::get_prefix("^foo{1,2}"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo\\."));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo(bar)"));
    EXPECT_EQUAL("", Regexp::get_prefix("(^foo)"));
    EXPECT_EQUAL("", Regexp::get_prefix("^(foo)"));
    EXPECT_EQUAL("foo", Regexp::get_prefix("^foo[a]"));
    EXPECT_EQUAL("", Regexp::get_prefix("^foo|^foobar"));
}

const vespalib::string special("^|()[]{}.*?+\\$");

struct ExprFixture {
    std::vector<vespalib::string> expressions;
    ExprFixture() {
        expressions.push_back(special);
        for (char c: special) {
            expressions.push_back(vespalib::string(&c, 1));
        }
        expressions.push_back("abc");
        expressions.push_back("[:digit:]");
    }
};

TEST_F("require that regexp can be made from suffix string", ExprFixture()) {
    for (vespalib::string str: f1.expressions) {
        std::regex re(std::string(Regexp::make_from_suffix(str)));
        EXPECT_TRUE(std::regex_search(std::string(str), re));
        EXPECT_FALSE(std::regex_search(std::string(str + "foo"), re));
        EXPECT_TRUE(std::regex_search(std::string("foo" + str), re));
        EXPECT_FALSE(std::regex_search(std::string("foo" + str + "bar"), re));
    }
}

TEST_F("require that regexp can be made from substring string", ExprFixture()) {
    for (vespalib::string str: f1.expressions) {
        std::regex re(std::string(Regexp::make_from_substring(str)));
        EXPECT_TRUE(std::regex_search(std::string(str), re));
        EXPECT_TRUE(std::regex_search(std::string(str + "foo"), re));
        EXPECT_TRUE(std::regex_search(std::string("foo" + str), re));
        EXPECT_TRUE(std::regex_search(std::string("foo" + str + "bar"), re));
    }
}

TEST_MAIN() { TEST_RUN_ALL(); }