• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2004
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         test_not_regex.hpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Declares tests for invalid regexes.
17   */
18 
19 
20 #ifndef BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
21 #define BOOST_REGEX_REGRESS_TEST_NOT_REGEX_HPP
22 #include "info.hpp"
23 //
24 // this file implements a test for a regular expression that should not compile:
25 //
26 struct test_invalid_regex_tag{};
27 
28 template<class charT, class traits>
test_empty(boost::basic_regex<charT,traits> & r)29 void test_empty(boost::basic_regex<charT, traits>& r)
30 {
31    if(!r.empty())
32    {
33       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::empty().", charT);
34    }
35    if(r.size())
36    {
37       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::size().", charT);
38    }
39    if(r.str().size())
40    {
41       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::str().", charT);
42    }
43    if(r.begin() != r.end())
44    {
45       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
46    }
47    if(r.status() == 0)
48    {
49       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::status().", charT);
50    }
51    if(r.begin() != r.end())
52    {
53       BOOST_REGEX_TEST_ERROR("Invalid value returned from basic_regex<>::begin().", charT);
54    }
55 }
56 
57 template<class charT, class traits>
test(boost::basic_regex<charT,traits> & r,const test_invalid_regex_tag &)58 void test(boost::basic_regex<charT, traits>& r, const test_invalid_regex_tag&)
59 {
60    const std::basic_string<charT>& expression = test_info<charT>::expression();
61    boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
62    //
63    // try it with exceptions disabled first:
64    //
65 #ifndef BOOST_NO_EXCEPTIONS
66    try
67 #endif
68    {
69       if(0 == r.assign(expression, syntax_options | boost::regex_constants::no_except).status())
70       {
71          BOOST_REGEX_TEST_ERROR("Expression compiled when it should not have done so.", charT);
72       }
73       test_empty(r);
74    }
75 #ifndef BOOST_NO_EXCEPTIONS
76    catch(...)
77    {
78       BOOST_REGEX_TEST_ERROR("Unexpected exception thrown.", charT);
79    }
80 #endif
81    //
82    // now try again with exceptions:
83    //
84    bool have_catch = false;
85 #ifndef BOOST_NO_EXCEPTIONS
86    try
87 #endif
88    {
89       r.assign(expression, syntax_options);
90 #ifdef BOOST_NO_EXCEPTIONS
91       if(r.status())
92          have_catch = true;
93 #endif
94    }
95 #ifndef BOOST_NO_EXCEPTIONS
96    catch(const boost::bad_expression&)
97    {
98       have_catch = true;
99       test_empty(r);
100    }
101    catch(const std::runtime_error& e)
102    {
103       have_catch = true;
104       BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << e.what(), charT);
105    }
106    catch(const std::exception& e)
107    {
108       have_catch = true;
109       BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << e.what(), charT);
110    }
111    catch(...)
112    {
113       have_catch = true;
114       BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", charT);
115    }
116 #endif
117    if(!have_catch)
118    {
119       // oops expected exception was not thrown:
120       BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", charT);
121    }
122 
123 }
124 
125 
126 
127 #endif
128