File: test_not_regex.hpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (127 lines) | stat: -rw-r--r-- 3,417 bytes parent folder | download | duplicates (14)
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
/*
 *
 * Copyright (c) 2004
 * John Maddock
 *
 * Use, modification and distribution are subject to the 
 * Boost Software License, Version 1.0. (See accompanying file 
 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 *
 */

 /*
  *   LOCATION:    see http://www.boost.org for most recent version.
  *   FILE         test_not_regex.hpp
  *   VERSION      see <boost/version.hpp>
  *   DESCRIPTION: Declares tests for invalid regexes.
  */


#ifndef BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
#define BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
#include "info.hpp"
//
// this file implements a test for a regular expression that should not compile:
//
struct test_invalid_regex_tag{};

template<class charT, class traits>
void test_empty(boost::basic_regex<charT, traits>& r)
{
   if(!r.empty())
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::empty().", charT);
   }
   if(r.size())
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::size().", charT);
   }
   if(r.str().size())
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::str().", charT);
   }
   if(r.begin() != r.end())
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
   }
   if(r.status() == 0)
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::status().", charT);
   }
   if(r.begin() != r.end())
   {
      BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
   }
}

template<class charT, class traits>
void test(boost::basic_regex<charT, traits>& r, const test_invalid_regex_tag&)
{
   const std::basic_string<charT>& expression = test_info<charT>::expression();
   boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
   //
   // try it with exceptions disabled first:
   //
#ifndef BOOST_NO_EXCEPTIONS
   try
#endif
   {
      if(0 == r.assign(expression, syntax_options | boost::regex_constants::no_except).status())
      {
         BOOST_REGEX_TEST_ERROR("Expression compiled when it should not have done so.", charT);
      }
      test_empty(r);
   }
#ifndef BOOST_NO_EXCEPTIONS
   catch(...)
   {
      BOOST_REGEX_TEST_ERROR("Unexpected exception thrown.", charT);
   }
#endif
   //
   // now try again with exceptions:
   //
   bool have_catch = false;
#ifndef BOOST_NO_EXCEPTIONS
   try
#endif
   {
      r.assign(expression, syntax_options);
#ifdef BOOST_NO_EXCEPTIONS
      if(r.status())
         have_catch = true;
#endif
   }
#ifndef BOOST_NO_EXCEPTIONS
   catch(const boost::bad_expression&)
   {
      have_catch = true;
      test_empty(r);
   }
   catch(const std::runtime_error& e)
   {
      have_catch = true;
      BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << e.what(), charT);
   }
   catch(const std::exception& e)
   {
      have_catch = true;
      BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << e.what(), charT);
   }
   catch(...)
   {
      have_catch = true;
      BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", charT);
   }
#endif
   if(!have_catch)
   {
      // oops expected exception was not thrown:
      BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", charT);
   }

}



#endif