exception_test.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2019, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 #include <gtest/gtest.h>
00035 #include <nav_core2/exceptions.h>
00036 #include <string>
00037 
00038 TEST(Exceptions, direct_code_access)
00039 {
00040   // Make sure the caught exceptions have the same types as the thrown exception
00041   // (This version does not create any copies of the exception)
00042   try
00043   {
00044     throw nav_core2::GlobalPlannerTimeoutException("test case");
00045   }
00046   catch (nav_core2::GlobalPlannerTimeoutException& x)
00047   {
00048     EXPECT_EQ(x.getResultCode(), 12);
00049   }
00050 
00051   try
00052   {
00053     throw nav_core2::GlobalPlannerTimeoutException("test case");
00054   }
00055   catch (nav_core2::PlannerException& x)
00056   {
00057     EXPECT_EQ(x.getResultCode(), 12);
00058   }
00059 }
00060 
00061 TEST(Exceptions, indirect_code_access)
00062 {
00063   // Make sure the caught exceptions have the same types as the thrown exception
00064   // (This version copies the exception to a new object with the parent type)
00065   nav_core2::PlannerException e("");
00066   try
00067   {
00068     throw nav_core2::GlobalPlannerTimeoutException("test case");
00069   }
00070   catch (nav_core2::GlobalPlannerTimeoutException& x)
00071   {
00072     e = x;
00073   }
00074   EXPECT_EQ(e.getResultCode(), 12);
00075 }
00076 
00077 TEST(Exceptions, rethrow)
00078 {
00079   // This version tests the ability to catch specific exceptions when rethrown
00080   // A copy of the exception is made and rethrown, with the goal being able to catch the specific type
00081   // and not the parent type.
00082   nav_core2::NavCore2ExceptionPtr e;
00083   try
00084   {
00085     throw nav_core2::GlobalPlannerTimeoutException("test case");
00086   }
00087   catch (nav_core2::GlobalPlannerTimeoutException& x)
00088   {
00089     e = std::current_exception();
00090   }
00091 
00092   EXPECT_EQ(nav_core2::getResultCode(e), 12);
00093 
00094   try
00095   {
00096     std::rethrow_exception(e);
00097   }
00098   catch (nav_core2::GlobalPlannerTimeoutException& x)
00099   {
00100     EXPECT_EQ(x.getResultCode(), 12);
00101     SUCCEED();
00102   }
00103   catch (nav_core2::PlannerException& x)
00104   {
00105     FAIL() << "PlannerException caught instead of specific exception";
00106   }
00107 }
00108 
00109 TEST(Exceptions, weird_exception)
00110 {
00111   nav_core2::NavCore2ExceptionPtr e;
00112 
00113   // Check what happens with no exception
00114   EXPECT_EQ(nav_core2::getResultCode(e), -1);
00115 
00116   // Check what happens with a non NavCore2Exception
00117   try
00118   {
00119     std::string().at(1);  // this generates an std::out_of_range
00120   }
00121   catch (...)
00122   {
00123     e = std::current_exception();
00124   }
00125 
00126   EXPECT_EQ(nav_core2::getResultCode(e), -1);
00127 }
00128 
00129 int main(int argc, char **argv)
00130 {
00131   testing::InitGoogleTest(&argc, argv);
00132   return RUN_ALL_TESTS();
00133 }


nav_core2
Author(s):
autogenerated on Wed Jun 26 2019 20:09:31