From 0c726a3fbe1920d40df1446ac66353655d99225c Mon Sep 17 00:00:00 2001 From: hbristow Date: Wed, 19 Jun 2013 16:37:57 +1000 Subject: [PATCH] Improved exception handling and unit tests --- .../templates/template_class_base.cpp | 10 +++++++ .../templates/template_function_base.cpp | 5 +++- modules/matlab/test/OpenCVTest.m | 30 ++++++++++++++++++- modules/matlab/test/testsuite.m | 3 ++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/modules/matlab/generator/templates/template_class_base.cpp b/modules/matlab/generator/templates/template_class_base.cpp index 3f8d2516f..1e59f3f63 100644 --- a/modules/matlab/generator/templates/template_class_base.cpp +++ b/modules/matlab/generator/templates/template_class_base.cpp @@ -27,6 +27,16 @@ std::vector {{function.name}}({{clss.name}}& inst, const std::vector #include +#include #include {% block includes %} {% endblock %} @@ -42,7 +43,9 @@ void mexFunction(int nlhs, mxArray* plhs[], try { {{fun.name}}(); } catch(cv::Exception& e) { - mexErrMsgTxt(std::string("OpenCV exception caught: ").append(e.what()).c_str()); + mexErrMsgTxt(std::string("cv::exception caught: ").append(e.what()).c_str()); + } catch(std::exception& e) { + mexErrMsgTxt(std::string("std::exception caught: ").append(e.what()).c_str()); } catch(...) { mexErrMsgTxt("Uncaught exception occurred in {{fun.name}}"); } diff --git a/modules/matlab/test/OpenCVTest.m b/modules/matlab/test/OpenCVTest.m index df97a6c5e..c78f9898c 100644 --- a/modules/matlab/test/OpenCVTest.m +++ b/modules/matlab/test/OpenCVTest.m @@ -5,7 +5,7 @@ classdef OpenCVTest < matlab.unittest.TestCase methods(Test) % check if the autogenerated functions can be found - function randExists(testcase) + function functionsExist(testcase) try cv.rand(); catch @@ -13,5 +13,33 @@ classdef OpenCVTest < matlab.unittest.TestCase end testcase.verifyTrue(true); end + + % check that std exception is thrown + function stdException(testcase) + try + std_exception(); + testcase.verifyFail(); + catch + % TODO: Catch more specific exception + testcase.verifyTrue(true); + end + end + + % check that OpenCV exceptions are correctly caught + function cvException(testcase) + testcase.verifyFail(); + end + + % check that all exceptions are caught + function allException(testcase) + try + exception(); + testcase.verifyFail(); + catch + % TODO: Catch more specific exception + testcase.verifyTrue(true); + end + end + end end diff --git a/modules/matlab/test/testsuite.m b/modules/matlab/test/testsuite.m index 3fc274ef2..2f31911ac 100644 --- a/modules/matlab/test/testsuite.m +++ b/modules/matlab/test/testsuite.m @@ -3,3 +3,6 @@ opencv_tests = OpenCVTest(); %run the tests result = run(opencv_tests); + +% shutdown +exit();