CounterNamedTest

Undocumented in source.
version(unittest)
class CounterNamedTest : NamedTest {}

Constructors

this
this()
Undocumented in source.

Members

Static variables

test_num
uint test_num;
Undocumented in source.

Inherited Members

From NamedTest

message
cstring message()

message that also uses this.name if present

test
void test(T ok, cstring msg, istring file, int line)

Same as enforceImpl!(TestException) but uses this.name for error message formatting.

test
void test(T1 a, T2 b, istring file, int line)

Same as enforceImpl!(op, TestException) but uses this.name for error message formatting.

Examples

Test for invalid pattern

auto pcre = new PCRE;
testThrown!(PCRE.PcreException)(pcre.preg_match("", "("));

Tests for simple boolean matching via the preg_match() method. (This unittest tests only the interface of this method. It does not test the full range of PCRE features as that is beyond its scope.)

void test ( scope bool delegate ( ) dg, bool match )
{
    auto t = new CounterNamedTest;
    t.test!("==")(match, dg());
}

auto pcre = new PCRE;

// Empty pattern (matches any string)
test({ return pcre.preg_match("Hello World", ""); }, true);

// Empty string and empty pattern (match)
test({ return pcre.preg_match("", ""); }, true);

// Empty string (no match)
test({ return pcre.preg_match("", "a"); }, false);

// Simple string match
test({ return pcre.preg_match("Hello World", "Hello"); }, true);

// Simple string match (fail)
test({ return pcre.preg_match("Hello World", "Hallo"); }, false);

// Case-sensitive match (fail)
test({ return pcre.preg_match("Hello World", "hello"); }, false);

// Case-insensitive match
test({ return pcre.preg_match("Hello World", "hello", false); }, true);

Tests for single substring matching via the CompiledRegex.findFirst() method.

auto pcre = new PCRE;
auto regex = pcre.new CompiledRegex;

void testFind ( cstring needle, cstring pre, cstring match, cstring post )
{
    regex.compile(needle);

    auto haystack = pre ~ match ~ post;
    auto res = regex.findFirst(haystack);

    auto t = new CounterNamedTest;
    t.test!("==")(res, haystack[pre.length .. pre.length + match.length]);
}

// Simple match
testFind(
    "a",
    "bbb",
    "a",
    "ccc"
);

// Match with a more complicated regex
testFind(
   "(firstparam=[^l]*liza.*(secondparam=mary|secondparam=lena|secondparam=john))|((secondparam=mary|secondparam=lena|secondparam=john).*firstparam=liza)",
    "http://example.org?",
    "firstparam=elizabeth&rand=84527497861&secondparam=mary",
    "&some=other&params=are&here=%20%21%22%12222"
);

// Single match returned when multiple matches are possible
testFind(
    "a",
    "bbb",
    "a",
    "cacac"
);

Tests for multiple substring matching via the CompiledRegex.findAll() method.

1 auto pcre = new PCRE;
2 auto regex = pcre.new CompiledRegex;
3 cstring[] matches_buffer;
4 
5 auto t = new NamedTest("PCRE findAll");
6 
7 {
8     istring str = "apa bepa cepa depa epa fepa gepa hepa";
9     regex.compile("a[ ]", false);
10 
11     matches_buffer.length = 0;
12     assumeSafeAppend(matches_buffer);
13 
14     foreach ( match; regex.findAll(str, matches_buffer) )
15     {
16         t.test!("==")(match, "a "[]);
17     }
18 
19     t.test!("==")(matches_buffer.length, 7);
20 }
21 
22 {
23     istring[3] exp = ["ast", "at", "ast"];
24     istring str = "en hast at en annan hast";
25     regex.compile("a[s]*t", false);
26 
27     matches_buffer = null;
28 
29 
30     foreach (i, match; regex.findAll(str, matches_buffer) )
31     {
32         t.test!("==")(match, exp[i]);
33     }
34     t.test!("==")(matches_buffer.length, 3);
35 }
36 
37 {
38     istring[3] exp = ["ta", "tb", "td"];
39     istring str = "tatb t c Tf td";
40     regex.compile("t[\\w]", true);
41     regex.study();
42 
43     matches_buffer = null;
44 
45     foreach (i, match; regex.findAll(str, matches_buffer) )
46     {
47         t.test!("==")(match, exp[i]);
48     }
49 
50     t.test!("==")(matches_buffer.length, 3);
51 }
52 
53 {
54     istring str = "en text";
55     regex.compile("zzz", false);
56     matches_buffer = null;
57     auto matches = regex.findAll(str, matches_buffer);
58 
59     t.test!("==")(matches_buffer.length, 0);
60 }
61 
62 {
63     istring str = "en text";
64     regex.compile("zzz", false);
65     matches_buffer = null;
66     auto matches = regex.findAll(str, matches_buffer);
67 
68     t.test!("==")(matches_buffer.length, 0);
69 }
70 
71 {
72     istring str ="aaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
73              ~ "aaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
74              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaa"
75              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaa"
76              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaa"
77              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaa"
78              ~ "aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbAbbbbbbbbbbbb"
79              ~ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbAbbbbbbb"
80              ~ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaAaaaaa"
81              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaa"
82              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaa"
83              ~ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
84              ~ "aaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
85              ~ "aacaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
86              ~ "aaaacaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
87 
88     regex.compile("a", true);
89     matches_buffer.length = 0;
90 
91     foreach (i, match; regex.findAll(str, matches_buffer) )
92     {
93         t.test!("==")(match, "a"[]);
94     }
95 
96     t.test!("==")(matches_buffer.length, 695);
97 }

Meta