File: test_regexranges_main.cpp

package info (click to toggle)
source-highlight 3.1.8-1.2~deb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,224 kB
  • sloc: sh: 11,709; cpp: 10,226; ansic: 9,521; makefile: 1,902; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ada: 36; ml: 29; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 7; sed: 4
file content (91 lines) | stat: -rw-r--r-- 2,835 bytes parent folder | download | duplicates (5)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>
#include <iterator>

#include "asserttestexit.h"
#include "srchilite/regexranges.h"
#include <iostream>

using namespace std;
using namespace srchilite;

RegexRanges ranges;

void check_range_regex(const string &s, bool expectedTrue = true) {
    cout << "checking " << s << endl;
    if (expectedTrue)
        assertTrue(ranges.addRegexRange(s));
    else
        assertFalse(ranges.addRegexRange(s));
}

void check_match(const string &line, const string &expected = "") {
    cout << "searching inside " << line;
    const boost::regex *matched = 0;
    if (expected != "") {
        matched = ranges.matches(line);
        assertTrue(matched != 0);
        assertEquals(expected, matched->str());
        cout << " found " << *matched << endl;
    } else {
        assertTrue(ranges.matches(line) == 0);
        cout << " not found" << endl;
    }
}

void check_in_range(const string &s, bool expectedTrue = true) {
    cout << "checking " << s << "... ";
    if (expectedTrue) {
        assertTrue(ranges.isInRange(s));
    } else {
        assertFalse(ranges.isInRange(s));
    }
    cout << expectedTrue << endl;
}

int main() {
    cout << boolalpha;

    check_range_regex("simple regex");
    check_range_regex("[[:alpha:]]+");
    // test with a wrong regular expression
    // this is accepted now
    // https://savannah.gnu.org/bugs/index.php?41786
    check_range_regex("{notclosed");

    // reset regular expressions
    ranges.clear();

    check_range_regex("/// foo");
    check_range_regex("/// bar");

    check_match(" not contains the searched string...", "");
    check_match(" does contain /// foo the searched string...", "/// foo");
    check_match(" does contain /// bar the searched string...", "/// bar");

    check_in_range("not in range", false);
    check_in_range("not in range, /// bar but the range starts", false);
    check_in_range("now in range", true);
    check_in_range("still in range, and /// foo is not considered now", true);
    check_in_range("end of range /// bar", false);
    check_in_range("not in range", false);
    check_in_range("not in range, /// foo but the range starts", false);
    check_in_range("now in range", true);
    check_in_range("now in range", true);
    check_in_range("end of range /// foo", false);
    check_in_range("not in range", false);

    // now try with a more involved regular expression
    check_range_regex("/// [[:digit:]]{2}");
    check_in_range("not in range", false);
    check_in_range("not in range, /// 25 but the range starts", false);
    check_in_range("now in range", true);
    check_in_range("still in range, and /// foo is not considered now", true);
    check_in_range("end of range /// 27", false);
    check_in_range("not in range", false);

    return 0;
}