[pygccxml-commit] SF.net SVN: pygccxml: [530] pyplusplus_dev/unittests
                
                Brought to you by:
                
                    mbaas,
                    
                
                    roman_yakovenko
                    
                
            
            
        
        
        
    | 
      
      
      From: <mb...@us...> - 2006-09-06 14:02:51
      
     | 
| Revision: 530
          http://svn.sourceforge.net/pygccxml/?rev=530&view=rev
Author:   mbaas
Date:     2006-09-06 07:02:43 -0700 (Wed, 06 Sep 2006)
Log Message:
-----------
Added more tests (which do work now).
Modified Paths:
--------------
    pyplusplus_dev/unittests/data/function_transformations_to_be_exported.hpp
    pyplusplus_dev/unittests/function_transformations_tester.py
Modified: pyplusplus_dev/unittests/data/function_transformations_to_be_exported.hpp
===================================================================
--- pyplusplus_dev/unittests/data/function_transformations_to_be_exported.hpp	2006-09-06 14:01:52 UTC (rev 529)
+++ pyplusplus_dev/unittests/data/function_transformations_to_be_exported.hpp	2006-09-06 14:02:43 UTC (rev 530)
@@ -10,7 +10,7 @@
 
 struct image_t{
 
-    image_t( unsigned int& h, unsigned int& w )
+    image_t( unsigned int h, unsigned int w )
     : m_height( h )
       , m_width( w )
     {}
@@ -22,16 +22,53 @@
         w = m_width;
     }
 
+    // Return only one value
+    virtual void get_one_value(unsigned int& h) {
+        h = m_height;
+    }
+
+    // Like get_size() but with a return type and an additional argument
+    // that has to be kept in the signature
+    virtual int get_size2( unsigned int& h, unsigned int& w, int noref=0 ){
+        h = m_height;
+        w = m_width;
+	return noref;
+    }
+
+    // A method with an input argument
+    virtual int input_arg(int& in){
+      return in;
+    }
+
+    // A method taking an input array of fixed size
+    virtual int fixed_input_array(int v[3]) {
+      return v[0]+v[1]+v[2];
+    }
+
     unsigned int m_width;
     unsigned int m_height;
 
 };
 
+// Provide an instance created in C++ (i.e. this is not a wrapper instance)
+image_t cpp_instance(12,13);
+image_t& get_cpp_instance() {
+  return cpp_instance;
+}
+
 inline void
 get_image_size( image_t& img, unsigned int& h, unsigned int& w ){
     img.get_size( h, w );
 }
 
+// This is used for calling img.get_one_value() on an instance passed
+// in by Python.
+unsigned int get_image_one_value( image_t& img ) {
+  unsigned int v;
+  img.get_one_value(v);
+  return v;
 }
 
+}
+
 #endif//__function_transformations_to_be_exported_hpp__
Modified: pyplusplus_dev/unittests/function_transformations_tester.py
===================================================================
--- pyplusplus_dev/unittests/function_transformations_tester.py	2006-09-06 14:01:52 UTC (rev 529)
+++ pyplusplus_dev/unittests/function_transformations_tester.py	2006-09-06 14:02:43 UTC (rev 530)
@@ -8,6 +8,7 @@
 import unittest
 import fundamental_tester_base
 from pyplusplus.function_transformers.arg_policies import *
+from pyplusplus.decl_wrappers import *
 
 class tester_t(fundamental_tester_base.fundamental_tester_base_t):
     EXTENSION_NAME = 'function_transformations'
@@ -20,13 +21,99 @@
 
     def customize( self, mb ):
         image = mb.class_( "image_t" )
-        image.member_function( "get_size" ).function_transformers.extend([Output(1), Output(2)])
+        image.member_function( "get_size" ).function_transformers.extend([output_t(1), output_t(2)])
+        image.member_function( "get_one_value" ).function_transformers.extend([output_t(1)])
+        image.member_function( "get_size2" ).function_transformers.extend([output_t(1), output_t(2)])
+        image.member_function( "input_arg" ).function_transformers.extend([input_t(1)])
+        image.member_function( "fixed_input_array" ).function_transformers.extend([input_array_t(1,3)])
+        mb.free_function("get_cpp_instance").call_policies = return_value_policy(reference_existing_object)
+        mb.variable( "cpp_instance" ).exclude()
+        mb.decls(lambda decl: decl.name[0]=="_").exclude()
 
     def run_tests(self, module):
+        """Run the actual unit tests.
+        """
+        
+        ####### Do the tests directly on the wrapper C++ class ########
+        
         img = module.image_t( 2, 6)
-        h,w = img.get_size()
-        self.failUnless( h == 2 and w == 6 )
 
+        # Check a method that returns two values by reference
+        self.assertEqual(img.get_size(), (2,6))
+
+        # Check a method that only returns one value by reference
+        self.assertEqual(img.get_one_value(), 2)
+
+        # Check if the C++ class can also be passed back to C++
+        self.assertEqual(module.get_image_one_value(img), 2)
+
+        # Check get_size2()
+        self.assertEqual(img.get_size2(), (0,2,6))
+        self.assertEqual(img.get_size2(1), (1,2,6))
+
+        # Check the input_arg method
+        self.assertEqual(img.input_arg(5), 5)
+
+        # Check the fixed_input_array method
+        self.assertEqual(img.fixed_input_array([1,2,3]), 6)
+        self.assertEqual(img.fixed_input_array((1,2,3)), 6)
+        self.assertRaises(ValueError, lambda : img.fixed_input_array([1,2,3,4]))
+        self.assertRaises(ValueError, lambda : img.fixed_input_array([1,2]))
+        self.assertRaises(ValueError, lambda : img.fixed_input_array(1))
+        self.assertRaises(ValueError, lambda : img.fixed_input_array(None))
+
+        ####### Do the tests on a class derived in Python ########
+        
+        class py_image1_t(module.image_t):
+            def __init__(self, h, w):
+                module.image_t.__init__(self, h, w)
+
+            # Override a virtual method
+            def get_one_value(self):
+                return self.m_height+1
+
+        pyimg1 = py_image1_t(3,7)
+        
+        # Check a method that returns two values by reference
+        self.assertEqual(pyimg1.get_size(), (3,7))
+
+        # Check a method that only returns one value by reference
+        self.assertEqual(pyimg1.get_one_value(), 4)
+
+        # Check if the Python class can also be passed back to C++
+        self.assertEqual(module.get_image_one_value(pyimg1), 4)
+
+        class py_image2_t(module.image_t):
+            def __init__(self, h, w):
+                module.image_t.__init__(self, h, w)
+
+            # Override a virtual method and invoke the inherited method
+            def get_one_value(self):
+                return module.image_t.get_one_value(self)+2
+        
+        pyimg2 = py_image2_t(4,8)
+
+        # Check the derived get_one_value() method
+        self.assertEqual(pyimg2.get_one_value(), 6)
+
+        # Check if the Python class can also be passed back to C++
+        self.assertEqual(module.get_image_one_value(pyimg2), 6)
+
+        ####### Do the tests on a class instantiated in C++ ########
+
+        cppimg = module.get_cpp_instance()
+
+        # Check a method that returns two values by reference
+        self.assertEqual(cppimg.get_size(), (12,13))
+
+        # Check a method that only returns one value by reference
+        self.assertEqual(cppimg.get_one_value(), 12)
+
+        # Check if the C++ class can also be passed back to C++
+        self.assertEqual(module.get_image_one_value(cppimg), 12)
+
+
+
 def create_suite():
     suite = unittest.TestSuite()
     suite.addTest( unittest.makeSuite(tester_t))
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |