[pygccxml-commit] SF.net SVN: pygccxml: [623] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-10-05 07:17:19
|
Revision: 623 http://svn.sourceforge.net/pygccxml/?rev=623&view=rev Author: roman_yakovenko Date: 2006-10-05 00:17:04 -0700 (Thu, 05 Oct 2006) Log Message: ----------- adding unit tests for convenience functions improving convenience functions error reporting Modified Paths: -------------- pyplusplus_dev/pyplusplus/code_repository/convenience.py pyplusplus_dev/unittests/test_all.py Added Paths: ----------- pyplusplus_dev/unittests/convenience_tester.py pyplusplus_dev/unittests/data/convenience_to_be_exported.hpp Modified: pyplusplus_dev/pyplusplus/code_repository/convenience.py =================================================================== --- pyplusplus_dev/pyplusplus/code_repository/convenience.py 2006-10-05 06:23:06 UTC (rev 622) +++ pyplusplus_dev/pyplusplus/code_repository/convenience.py 2006-10-05 07:17:04 UTC (rev 623) @@ -23,18 +23,16 @@ #include "boost/python.hpp" -//1 - dimension namespace pyplusplus{ namespace convenience{ -void raise_error( PyObject *exception, const char *message ){ - +inline void +raise_error( PyObject *exception, const char *message ){ PyErr_SetString(exception, message); boost::python::throw_error_already_set(); - } -void ensure_sequence( boost::python::object seq, Py_ssize_t expected_length=-1 ){ - +inline void +ensure_sequence( boost::python::object seq, Py_ssize_t expected_length=-1 ){ PyObject* seq_impl = seq.ptr(); if( !PySequence_Check( seq_impl ) ){ @@ -48,27 +46,32 @@ << "Actual sequence length is " << length << "."; raise_error( PyExc_ValueError, err.str().c_str() ); } - } template< class ExpectedType > void ensure_uniform_sequence( boost::python::object seq, Py_ssize_t expected_length=-1 ){ + ensure_sequence( seq, expected_length ); - ensure_sequence( seq, expected_length ); Py_ssize_t length = boost::python::len( seq ); for( Py_ssize_t index = 0; index < length; ++index ){ boost::python::object item = seq[index]; + boost::python::extract<ExpectedType> type_checker( item ); - if( type_checker.check() ){ - const boost::python::type_info expected_type_info( boost::python::type_id<ExpectedType>() ); - //TODO: How to extract type_info from PyObject? + if( !type_checker.check() ){ + std::string expected_type_name( boost::python::type_id<ExpectedType>().name() ); + + std::string item_type_name("different"); + PyObject* item_impl = item.ptr(); + if( item_impl && item_impl->ob_type && item_impl->ob_type->tp_name ){ + item_type_name = std::string( item_impl->ob_type->tp_name ); + } + std::stringstream err; - err << "Sequence should contain only items with type \"" << expected_type_info.name() << "\". " - << "Item at position " << index << " has different type."; + err << "Sequence should contain only items with type \\"" << expected_type_name << "\\". " + << "Item at position " << index << " has \\"" << item_type_name << "\\" type."; raise_error( PyExc_ValueError, err.str().c_str() ); } } - } } /*pyplusplus*/ } /*convenience*/ Added: pyplusplus_dev/unittests/convenience_tester.py =================================================================== --- pyplusplus_dev/unittests/convenience_tester.py (rev 0) +++ pyplusplus_dev/unittests/convenience_tester.py 2006-10-05 07:17:04 UTC (rev 623) @@ -0,0 +1,48 @@ +# Copyright 2004 Roman Yakovenko. +# Distributed under 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) + +import os +import sys +import unittest +import fundamental_tester_base + +from pyplusplus import code_repository + +class tester_t(fundamental_tester_base.fundamental_tester_base_t): + EXTENSION_NAME = 'convenience' + + def __init__( self, *args ): + fundamental_tester_base.fundamental_tester_base_t.__init__( + self + , tester_t.EXTENSION_NAME + , *args ) + + def customize(self, mb ): + mb.add_registration_code( 'bp::def( "ensure_sequence", &pyplusplus::convenience::ensure_sequence );' ) + mb.add_registration_code( 'bp::def( "ensure_int_sequence", &pyplusplus::convenience::ensure_uniform_sequence<int> );' ) + mb.build_code_creator( self.EXTENSION_NAME ) + + mb.code_creator.add_include( "__convenience.pypp.hpp" ) + mb.code_creator.add_system_header( code_repository.convenience.file_name ) + + def run_tests(self, module): + self.failIfRaisesAny( module.ensure_sequence, [1,2,3], -1 ) + self.failIfRaisesAny( module.ensure_sequence, [1,2,3], 3 ) + self.failIfNotRaisesAny( module.ensure_sequence, self, 1 ) + self.failIfRaisesAny( module.ensure_int_sequence, [1,2,3], -1 ) + self.failIfRaisesAny( module.ensure_int_sequence, [1,2,3], 3 ) + self.failIfNotRaisesAny( module.ensure_int_sequence, [self], 1 ) + self.failIfNotRaisesAny( module.ensure_int_sequence, [1,2.1,3] , 3 ) + +def create_suite(): + suite = unittest.TestSuite() + suite.addTest( unittest.makeSuite(tester_t)) + return suite + +def run_suite(): + unittest.TextTestRunner(verbosity=2).run( create_suite() ) + +if __name__ == "__main__": + run_suite() Added: pyplusplus_dev/unittests/data/convenience_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/convenience_to_be_exported.hpp (rev 0) +++ pyplusplus_dev/unittests/data/convenience_to_be_exported.hpp 2006-10-05 07:17:04 UTC (rev 623) @@ -0,0 +1,10 @@ +// Copyright 2004 Roman Yakovenko. +// Distributed under 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) + +#ifndef __convenience_to_be_exported_hpp__ +#define __convenience_to_be_exported_hpp__ + + +#endif//__convenience_to_be_exported_hpp__ Modified: pyplusplus_dev/unittests/test_all.py =================================================================== --- pyplusplus_dev/unittests/test_all.py 2006-10-05 06:23:06 UTC (rev 622) +++ pyplusplus_dev/unittests/test_all.py 2006-10-05 07:17:04 UTC (rev 623) @@ -64,6 +64,7 @@ import split_module_tester import properties_tester import arrays_bug_tester +import convenience_tester def create_suite(times): testers = [ @@ -124,6 +125,7 @@ , split_module_tester , properties_tester , arrays_bug_tester + , convenience_tester ] main_suite = unittest.TestSuite() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |