summaryrefslogtreecommitdiff
path: root/libcutl/tests/re/driver.cxx
blob: 4b070dde52af3b431730560ee3638ce356fffd45 (plain)
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
// file      : tests/re/driver.cxx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <string>
#include <cassert>
#include <iostream>

#include <cutl/re.hxx>

using namespace cutl::re;

int
main ()
{
  // empty() and str().
  //
  {
    regex r;
    assert (r.empty ());
    r = "['`]foo([^ ]*)bar['`]";
    assert (!r.empty ());
    assert (r.str () == "['`]foo([^ ]*)bar['`]");
  }

  // Error handling.
  //
  try
  {
    regex r ("['`]foo([^ ]*bar['`]");
    assert (false);
  }
  catch (format const& e)
  {
    assert (e.regex () == "['`]foo([^ ]*bar['`]");
    assert (!e.description ().empty ());
    //std::cerr << e.description () << std::endl;
  }

  // match(), search(), and replace().
  //
  {
    regex r ("['`]foo([^ ]*)bar['`]");

    assert (r.match ("'foofoxbar'"));
    assert (!r.match ("'foof xbar'"));

    assert (r.search ("prefix 'foofoxbar' suffix"));
    assert (!r.search ("prefix 'foof xbar' suffix"));

    assert (r.replace ("'foofoxbar'", "\\u$1") == "Fox");
  }

  // regexsub
  //
  {
    regexsub r ("/['`]foo([^ ]*)bar['`]/\\u$1/");

    assert (r.replace ("'foofoxbar'") == "Fox");
  }

  // regexsub escaping
  //
  {
    regexsub r ("#a\\#\\\\#a?\\\\#");

    assert (r.replace ("a#\\") == "a?\\");
  }

  // regexsub error handling.
  //
  try
  {
    regexsub r ("/['`]foo([^ ]*)bar['`]#\\u$1/");
    assert (false);
  }
  catch (format const& e)
  {
    assert (e.regex () == "/['`]foo([^ ]*)bar['`]#\\u$1/");
    assert (!e.description ().empty ());
    //std::cerr << e.description () << std::endl;
  }
}