diff options
Diffstat (limited to 'libs/python/pyste/tests')
70 files changed, 2246 insertions, 0 deletions
diff --git a/libs/python/pyste/tests/GCCXMLParserUT.py b/libs/python/pyste/tests/GCCXMLParserUT.py new file mode 100644 index 000000000..7175c9c68 --- /dev/null +++ b/libs/python/pyste/tests/GCCXMLParserUT.py @@ -0,0 +1,341 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 sys +sys.path.append('../src') +import unittest +import tempfile +import os.path +from Pyste import GCCXMLParser +from Pyste.declarations import * + + +class Tester(unittest.TestCase): + + def TestConstructor(self, class_, method, visib): + self.assert_(isinstance(method, Constructor)) + self.assertEqual(method.FullName(), class_.FullName() + '::' + method.name) + self.assertEqual(method.result, None) + self.assertEqual(method.visibility, visib) + self.assert_(not method.virtual) + self.assert_(not method.abstract) + self.assert_(not method.static) + + def TestDefaultConstructor(self, class_, method, visib): + self.TestConstructor(class_, method, visib) + self.assert_(method.IsDefault()) + + def TestCopyConstructor(self, class_, method, visib): + self.TestConstructor(class_, method, visib) + self.assertEqual(len(method.parameters), 1) + param = method.parameters[0] + self.TestType( + param, + ReferenceType, + class_.FullName(), + 'const %s&' % class_.FullName(), + True) + self.assert_(method.IsCopy()) + + + def TestType(self, type_, classtype_, name, fullname, const): + self.assert_(isinstance(type_, classtype_)) + self.assertEqual(type_.name, name) + self.assertEqual(type_.namespace, None) + self.assertEqual(type_.FullName(), fullname) + self.assertEqual(type_.const, const) + + +class ClassBaseTest(Tester): + + def setUp(self): + self.base = GetDecl('Base') + + def testClass(self): + 'test the properties of the class Base' + self.assert_(isinstance(self.base, Class)) + self.assert_(self.base.abstract) + + + def testFoo(self): + 'test function foo in class Base' + foo = GetMember(self.base, 'foo') + self.assert_(isinstance(foo, Method)) + self.assertEqual(foo.visibility, Scope.public) + self.assert_(foo.virtual) + self.assert_(foo.abstract) + self.failIf(foo.static) + self.assertEqual(foo.class_, 'test::Base') + self.failIf(foo.const) + self.assertEqual(foo.FullName(), 'test::Base::foo') + self.assertEqual(foo.result.name, 'void') + self.assertEqual(len(foo.parameters), 1) + param = foo.parameters[0] + self.TestType(param, FundamentalType, 'int', 'int', False) + self.assertEqual(foo.namespace, None) + self.assertEqual( + foo.PointerDeclaration(1), '(void (test::Base::*)(int) )&test::Base::foo') + + def testX(self): + 'test the member x in class Base' + x = GetMember(self.base, 'x') + self.assertEqual(x.class_, 'test::Base') + self.assertEqual(x.FullName(), 'test::Base::x') + self.assertEqual(x.namespace, None) + self.assertEqual(x.visibility, Scope.private) + self.TestType(x.type, FundamentalType, 'int', 'int', False) + self.assertEqual(x.static, False) + + def testConstructors(self): + 'test constructors in class Base' + constructors = GetMembers(self.base, 'Base') + for cons in constructors: + if len(cons.parameters) == 0: + self.TestDefaultConstructor(self.base, cons, Scope.public) + elif len(cons.parameters) == 1: # copy constructor + self.TestCopyConstructor(self.base, cons, Scope.public) + elif len(cons.parameters) == 2: # other constructor + intp, floatp = cons.parameters + self.TestType(intp, FundamentalType, 'int', 'int', False) + self.TestType(floatp, FundamentalType, 'float', 'float', False) + + def testSimple(self): + 'test function simple in class Base' + simple = GetMember(self.base, 'simple') + self.assert_(isinstance(simple, Method)) + self.assertEqual(simple.visibility, Scope.protected) + self.assertEqual(simple.FullName(), 'test::Base::simple') + self.assertEqual(len(simple.parameters), 1) + param = simple.parameters[0] + self.TestType(param, ReferenceType, 'std::string', 'const std::string&', True) + self.TestType(simple.result, FundamentalType, 'bool', 'bool', False) + self.assertEqual( + simple.PointerDeclaration(1), + '(bool (test::Base::*)(const std::string&) )&test::Base::simple') + + + def testZ(self): + z = GetMember(self.base, 'z') + self.assert_(isinstance(z, Variable)) + self.assertEqual(z.visibility, Scope.public) + self.assertEqual(z.FullName(), 'test::Base::z') + self.assertEqual(z.type.name, 'int') + self.assertEqual(z.type.const, False) + self.assert_(z.static) + + +class ClassTemplateTest(Tester): + + def setUp(self): + self.template = GetDecl('Template<int>') + + def testClass(self): + 'test the properties of the Template<int> class' + self.assert_(isinstance(self.template, Class)) + self.assert_(not self.template.abstract) + self.assertEqual(self.template.FullName(), 'Template<int>') + self.assertEqual(self.template.namespace, '') + self.assertEqual(self.template.name, 'Template<int>') + + def testConstructors(self): + 'test the automatic constructors of the class Template<int>' + constructors = GetMembers(self.template, 'Template') + for cons in constructors: + if len(cons.parameters) == 0: + self.TestDefaultConstructor(self.template, cons, Scope.public) + elif len(cons.parameters) == 1: + self.TestCopyConstructor(self.template, cons, Scope.public) + + + def testValue(self): + 'test the class variable value' + value = GetMember(self.template, 'value') + self.assert_(isinstance(value, ClassVariable)) + self.assert_(value.name, 'value') + self.TestType(value.type, FundamentalType, 'int', 'int', False) + self.assert_(not value.static) + self.assertEqual(value.visibility, Scope.public) + self.assertEqual(value.class_, 'Template<int>') + self.assertEqual(value.FullName(), 'Template<int>::value') + + def testBase(self): + 'test the superclasses of Template<int>' + bases = self.template.bases + self.assertEqual(len(bases), 1) + base = bases[0] + self.assert_(isinstance(base, Base)) + self.assertEqual(base.name, 'test::Base') + self.assertEqual(base.visibility, Scope.protected) + + + +class FreeFuncTest(Tester): + + def setUp(self): + self.func = GetDecl('FreeFunc') + + def testFunc(self): + 'test attributes of FreeFunc' + self.assert_(isinstance(self.func, Function)) + self.assertEqual(self.func.name, 'FreeFunc') + self.assertEqual(self.func.FullName(), 'test::FreeFunc') + self.assertEqual(self.func.namespace, 'test') + self.assertEqual( + self.func.PointerDeclaration(1), + '(const test::Base& (*)(const std::string&, int))&test::FreeFunc') + + + def testResult(self): + 'test the return value of FreeFunc' + res = self.func.result + self.TestType(res, ReferenceType, 'test::Base', 'const test::Base&', True) + + def testParameters(self): + 'test the parameters of FreeFunc' + self.assertEqual(len(self.func.parameters), 2) + strp, intp = self.func.parameters + self.TestType(strp, ReferenceType, 'std::string', 'const std::string&', True) + self.assertEqual(strp.default, None) + self.TestType(intp, FundamentalType, 'int', 'int', False) + self.assertEqual(intp.default, '10') + + + +class testFunctionPointers(Tester): + + def testMethodPointer(self): + 'test declaration of a pointer-to-method' + meth = GetDecl('MethodTester') + param = meth.parameters[0] + fullname = 'void (test::Base::*)(int)' + self.TestType(param, PointerType, fullname, fullname, False) + + def testFunctionPointer(self): + 'test declaration of a pointer-to-function' + func = GetDecl('FunctionTester') + param = func.parameters[0] + fullname = 'void (*)(int)' + self.TestType(param, PointerType, fullname, fullname, False) + + + +# ============================================================================= +# Support routines +# ============================================================================= + +cppcode = ''' +namespace std { + class string; +} +namespace test { +class Base +{ +public: + Base(); + Base(const Base&); + Base(int, float); + + virtual void foo(int = 0.0) = 0; + static int z; +protected: + bool simple(const std::string&); +private: + int x; +}; + +void MethodTester( void (Base::*)(int) ); +void FunctionTester( void (*)(int) ); + + +const Base & FreeFunc(const std::string&, int=10); + +} + +template <class T> +struct Template: protected test::Base +{ + T value; + virtual void foo(int); +}; + +Template<int> __aTemplateInt; +''' + +def GetXMLFile(): + '''Generates an gccxml file using the code from the global cppcode. + Returns the xml's filename.''' + # write the code to a header file + tmpfile = tempfile.mktemp() + '.h' + f = file(tmpfile, 'w') + f.write(cppcode) + f.close() + # run gccxml + outfile = tmpfile + '.xml' + if os.system('gccxml "%s" "-fxml=%s"' % (tmpfile, outfile)) != 0: + raise RuntimeError, 'Error executing GCCXML.' + # read the output file into the xmlcode + f = file(outfile) + xmlcode = f.read() + #print xmlcode + f.close() + # remove the header + os.remove(tmpfile) + return outfile + + + +def GetDeclarations(): + 'Uses the GCCXMLParser module to get the declarations.' + xmlfile = GetXMLFile() + declarations = GCCXMLParser.ParseDeclarations(xmlfile) + os.remove(xmlfile) + return declarations + +# the declarations to be analysed +declarations = GetDeclarations() + + +def GetDecl(name): + 'returns one of the top declarations given its name' + for decl in declarations: + if decl.name == name: + return decl + else: + raise RuntimeError, 'Declaration not found: %s' % name + + +def GetMember(class_, name): + 'gets the member of the given class by its name' + + res = None + multipleFound = False + for member in class_: + if member.name == name: + if res is not None: + multipleFound = True + break + res = member + if res is None or multipleFound: + raise RuntimeError, \ + 'No member or more than one member found in class %s: %s' \ + % (class_.name, name) + return res + + +def GetMembers(class_, name): + 'gets the members of the given class by its name' + res = [] + for member in class_: + if member.name == name: + res.append(member) + if len(res) in (0, 1): + raise RuntimeError, \ + 'GetMembers: 0 or 1 members found in class %s: %s' \ + % (class_.name, name) + return res + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/SmartFileUT.py b/libs/python/pyste/tests/SmartFileUT.py new file mode 100644 index 000000000..9e4e99883 --- /dev/null +++ b/libs/python/pyste/tests/SmartFileUT.py @@ -0,0 +1,84 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 sys +sys.path.append('../src') +from SmartFile import * +import unittest +import tempfile +import os +import time + + +class SmartFileTest(unittest.TestCase): + + FILENAME = tempfile.mktemp() + + def setUp(self): + self._Clean() + + def tearDown(self): + self._Clean() + + def _Clean(self): + try: + os.remove(self.FILENAME) + except OSError: pass + + + def testNonExistant(self): + "Must override the file, as there's no file in the disk yet" + self.assert_(not os.path.isfile(self.FILENAME)) + f = SmartFile(self.FILENAME, 'w') + f.write('Testing 123\nTesting again.') + f.close() + self.assert_(os.path.isfile(self.FILENAME)) + + + def testOverride(self): + "Must override the file, because the contents are different" + contents = 'Contents!\nContents!' + # create the file normally first + f = file(self.FILENAME, 'w') + f.write(contents) + f.close() + file_time = os.path.getmtime(self.FILENAME) + self.assert_(os.path.isfile(self.FILENAME)) + time.sleep(2) + f = SmartFile(self.FILENAME, 'w') + f.write(contents + '_') + f.close() + new_file_time = os.path.getmtime(self.FILENAME) + self.assert_(new_file_time != file_time) + + + def testNoOverride(self): + "Must not override the file, because the contents are the same" + contents = 'Contents!\nContents!' + # create the file normally first + f = file(self.FILENAME, 'w') + f.write(contents) + f.close() + file_time = os.path.getmtime(self.FILENAME) + self.assert_(os.path.isfile(self.FILENAME)) + time.sleep(2) + f = SmartFile(self.FILENAME, 'w') + f.write(contents) + f.close() + new_file_time = os.path.getmtime(self.FILENAME) + self.assert_(new_file_time == file_time) + + + def testAutoClose(self): + "Must be closed when garbage-collected" + def foo(): + f = SmartFile(self.FILENAME) + f.write('testing') + self.assert_(not os.path.isfile(self.FILENAME)) + foo() + self.assert_(os.path.isfile(self.FILENAME)) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/abstract_test.h b/libs/python/pyste/tests/abstract_test.h new file mode 100644 index 000000000..e0fba2b24 --- /dev/null +++ b/libs/python/pyste/tests/abstract_test.h @@ -0,0 +1,22 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include <vector> +#include <string> + +namespace abstract { + +struct A { + virtual ~A() {} + virtual std::string f()=0; +}; + +struct B: A { + std::string f() { return "B::f"; } +}; + +std::string call(A* a) { return a->f(); } + +} diff --git a/libs/python/pyste/tests/abstract_test.pyste b/libs/python/pyste/tests/abstract_test.pyste new file mode 100644 index 000000000..c65bb3ad6 --- /dev/null +++ b/libs/python/pyste/tests/abstract_test.pyste @@ -0,0 +1,3 @@ +Class('abstract::A', 'abstract_test.h') +Class('abstract::B', 'abstract_test.h') +Function('abstract::call', 'abstract_test.h') diff --git a/libs/python/pyste/tests/abstract_testUT.py b/libs/python/pyste/tests/abstract_testUT.py new file mode 100644 index 000000000..4dc61a262 --- /dev/null +++ b/libs/python/pyste/tests/abstract_testUT.py @@ -0,0 +1,26 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _abstract_test import * + +class AbstractTest(unittest.TestCase): + + def testIt(self): + class C(A): + def f(self): + return 'C::f' + + a = A() + b = B() + c = C() + self.assertRaises(RuntimeError, a.f) + self.assertEqual(b.f(), 'B::f') + self.assertEqual(call(b), 'B::f') + self.assertEqual(c.f(), 'C::f') + self.assertEqual(call(c), 'C::f') + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/add_test.h b/libs/python/pyste/tests/add_test.h new file mode 100644 index 000000000..447e8814c --- /dev/null +++ b/libs/python/pyste/tests/add_test.h @@ -0,0 +1,18 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace add_test { + +struct C +{ + int x; +}; + +const int get_x(C& c) +{ + return c.x; +} + +} diff --git a/libs/python/pyste/tests/add_test.pyste b/libs/python/pyste/tests/add_test.pyste new file mode 100644 index 000000000..cc7faa9ed --- /dev/null +++ b/libs/python/pyste/tests/add_test.pyste @@ -0,0 +1,2 @@ +C = Class('add_test::C', 'add_test.h') +add_method(C, 'add_test::get_x') diff --git a/libs/python/pyste/tests/add_testUT.py b/libs/python/pyste/tests/add_testUT.py new file mode 100644 index 000000000..16f57a320 --- /dev/null +++ b/libs/python/pyste/tests/add_testUT.py @@ -0,0 +1,16 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _add_test import * + +class AddMethodTest(unittest.TestCase): + + def testIt(self): + c = C() + c.x = 10 + self.assertEqual(c.get_x(), 10) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/basic.cpp b/libs/python/pyste/tests/basic.cpp new file mode 100644 index 000000000..d07b6da62 --- /dev/null +++ b/libs/python/pyste/tests/basic.cpp @@ -0,0 +1,13 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include "basic.h" + +namespace basic { + + int C::static_value = 3; + const int C::const_static_value = 100; + +} diff --git a/libs/python/pyste/tests/basic.h b/libs/python/pyste/tests/basic.h new file mode 100644 index 000000000..690fed2d3 --- /dev/null +++ b/libs/python/pyste/tests/basic.h @@ -0,0 +1,69 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 BASIC_H +#define BASIC_H + + +#include <string> + +namespace basic { + +struct C +{ + // test virtuallity + C(): value(1), const_value(0) {} + virtual int f(int x = 10) + { + return x*2; + } + + int foo(int x=1){ + return x+1; + } + + const std::string& get_name() { return name; } + void set_name(const std::string& name) { this->name = name; } +private: + std::string name; + +public: + // test data members + static int static_value; + static const int const_static_value; + + int value; + const int const_value; + + // test static functions + static int mul(int x, int y) { return x*y; } + static double mul(double x, double y) { return x*y; } + + static int square(int x=2) { return x*x; } +}; + +inline int call_f(C& c) +{ + return c.f(); +} + +inline int call_f(C& c, int x) +{ + return c.f(x); +} + +inline int get_static() +{ + return C::static_value; +} + +inline int get_value(C& c) +{ + return c.value; +} + +} + +#endif diff --git a/libs/python/pyste/tests/basic.pyste b/libs/python/pyste/tests/basic.pyste new file mode 100644 index 000000000..4fe0b5b31 --- /dev/null +++ b/libs/python/pyste/tests/basic.pyste @@ -0,0 +1,5 @@ +Class('basic::C', 'basic.h') +Function('basic::call_f', 'basic.h') +Function('basic::get_static', 'basic.h') +Function('basic::get_value', 'basic.h') + diff --git a/libs/python/pyste/tests/basicUT.py b/libs/python/pyste/tests/basicUT.py new file mode 100644 index 000000000..9a18bcbf3 --- /dev/null +++ b/libs/python/pyste/tests/basicUT.py @@ -0,0 +1,73 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _basic import * + +class BasicExampleTest(unittest.TestCase): + + def testIt(self): + + # test virtual functions + class D(C): + def f(self, x=10): + return x+1 + + d = D() + c = C() + + self.assertEqual(c.f(), 20) + self.assertEqual(c.f(3), 6) + self.assertEqual(d.f(), 11) + self.assertEqual(d.f(3), 4) + self.assertEqual(call_f(c), 20) + self.assertEqual(call_f(c, 4), 8) + self.assertEqual(call_f(d), 11) + self.assertEqual(call_f(d, 3), 4) + + # test data members + def testValue(value): + self.assertEqual(c.value, value) + self.assertEqual(d.value, value) + self.assertEqual(get_value(c), value) + self.assertEqual(get_value(d), value) + testValue(1) + c.value = 30 + d.value = 30 + testValue(30) + self.assertEqual(c.const_value, 0) + self.assertEqual(d.const_value, 0) + def set_const_value(): + c.const_value = 12 + self.assertRaises(AttributeError, set_const_value) + + # test static data-members + def testStatic(value): + self.assertEqual(C.static_value, value) + self.assertEqual(c.static_value, value) + self.assertEqual(D.static_value, value) + self.assertEqual(d.static_value, value) + self.assertEqual(get_static(), value) + testStatic(3) + C.static_value = 10 + testStatic(10) + self.assertEqual(C.const_static_value, 100) + def set_const_static(): + C.const_static_value = 1 + self.assertRaises(AttributeError, set_const_static) + + # test static function + def test_mul(result, *args): + self.assertEqual(C.mul(*args), result) + self.assertEqual(c.mul(*args), result) + test_mul(16, 8, 2) + test_mul(6.0, 2.0, 3.0) + self.assertEqual(C.square(), 4) + self.assertEqual(c.square(), 4) + self.assertEqual(C.square(3), 9) + self.assertEqual(c.square(3), 9) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/code_test.h b/libs/python/pyste/tests/code_test.h new file mode 100644 index 000000000..0a31205a3 --- /dev/null +++ b/libs/python/pyste/tests/code_test.h @@ -0,0 +1,8 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +struct A { + int x; +}; diff --git a/libs/python/pyste/tests/code_test.pyste b/libs/python/pyste/tests/code_test.pyste new file mode 100644 index 000000000..467996fdb --- /dev/null +++ b/libs/python/pyste/tests/code_test.pyste @@ -0,0 +1,9 @@ +Class('A', 'code_test.h') +Include('string') +declaration_code(''' +int get(A& a) { return a.x; } + +std::string foo() { return "Hello!"; } +''') +module_code(' def("get", &get);\n') +module_code(' def("foo", &foo);\n') diff --git a/libs/python/pyste/tests/code_testUT.py b/libs/python/pyste/tests/code_testUT.py new file mode 100644 index 000000000..1059570aa --- /dev/null +++ b/libs/python/pyste/tests/code_testUT.py @@ -0,0 +1,18 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _code_test import * + +class CodeTest(unittest.TestCase): + + def testIt(self): + a = A() + a.x = 12 + self.assertEqual(get(a), 12) + self.assertEqual(foo(), "Hello!") + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/enums.h b/libs/python/pyste/tests/enums.h new file mode 100644 index 000000000..afe33ca48 --- /dev/null +++ b/libs/python/pyste/tests/enums.h @@ -0,0 +1,34 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 ENUMS_H +#define ENUMS_H + +namespace enums { + +enum color { red, blue }; + +struct X +{ + enum choices + { + good = 1, + bad = 2 + }; + + int set(choices c) + { + return (int)c; + } +}; + +enum { + x = 0, + y = 1 +}; + +} + +#endif diff --git a/libs/python/pyste/tests/enums.pyste b/libs/python/pyste/tests/enums.pyste new file mode 100644 index 000000000..c18a1244f --- /dev/null +++ b/libs/python/pyste/tests/enums.pyste @@ -0,0 +1,8 @@ +h = AllFromHeader('enums.h') +rename(h.color.red, 'Red') +rename(h.color.blue, 'Blue') +export_values(h.color) +rename(h.X.choices.bad, 'Bad') +rename(h.X.choices.good, 'Good') +rename(h.X.choices, 'Choices') + diff --git a/libs/python/pyste/tests/enumsUT.py b/libs/python/pyste/tests/enumsUT.py new file mode 100644 index 000000000..7c7720dcb --- /dev/null +++ b/libs/python/pyste/tests/enumsUT.py @@ -0,0 +1,24 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _enums import * + +class EnumsTest(unittest.TestCase): + + def testIt(self): + self.assertEqual(int(Red), 0) + self.assertEqual(int(Blue), 1) + + self.assertEqual(int(X.Choices.Good), 1) + self.assertEqual(int(X.Choices.Bad), 2) + a = X() + self.assertEqual(a.set(a.Choices.Good), 1) + self.assertEqual(a.set(a.Choices.Bad), 2) + self.assertEqual(x, 0) + self.assertEqual(y, 1) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/header_test.h b/libs/python/pyste/tests/header_test.h new file mode 100644 index 000000000..030d0d26c --- /dev/null +++ b/libs/python/pyste/tests/header_test.h @@ -0,0 +1,43 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 HEADER_TEST_H +#define HEADER_TEST_H + +#include <map> +#include <string> + +namespace header_test { + +enum choice { red, blue }; + +inline std::string choice_str(choice c) +{ + std::map<choice, std::string> choice_map; + choice_map[red] = "red"; + choice_map[blue] = "blue"; + return choice_map[c]; +} + +struct C +{ + choice c; + + std::string get() + { + return choice_str(c); + } +}; + +// test the exclusion of the following + +struct ForwardDeclared; // should be excluded automatically +struct A {}; +void foo(); +enum bar { value }; + +} + +#endif diff --git a/libs/python/pyste/tests/header_test.pyste b/libs/python/pyste/tests/header_test.pyste new file mode 100644 index 000000000..3bd55501c --- /dev/null +++ b/libs/python/pyste/tests/header_test.pyste @@ -0,0 +1,4 @@ +h = AllFromHeader('header_test.h') +exclude(h.A) +exclude(h.foo) +exclude(h.bar) diff --git a/libs/python/pyste/tests/header_testUT.py b/libs/python/pyste/tests/header_testUT.py new file mode 100644 index 000000000..aa0d4a16f --- /dev/null +++ b/libs/python/pyste/tests/header_testUT.py @@ -0,0 +1,27 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _header_test import * + +class HeaderTest(unittest.TestCase): + + def testIt(self): + self.assertEqual(choice.red, 0) + self.assertEqual(choice.blue, 1) + self.assertEqual(choice_str(choice.blue), 'blue') + self.assertEqual(choice_str(choice.red), 'red') + c = C() + c.c = choice.blue + self.assertEqual(c.get(), 'blue') + c.c = choice.red + self.assertEqual(c.get(), 'red') + # the following classes/functions should not have being exported + self.assertRaises(NameError, lambda: A()) + self.assertRaises(NameError, lambda: foo()) + self.assertRaises(NameError, lambda: bar.value) + self.assertRaises(NameError, lambda: ForwardDeclared()) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/infosUT.py b/libs/python/pyste/tests/infosUT.py new file mode 100644 index 000000000..93769f34e --- /dev/null +++ b/libs/python/pyste/tests/infosUT.py @@ -0,0 +1,55 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 sys +from Pyste.infos import * +from Pyste.policies import * +from Pyste.exporterutils import * +import unittest + +#================================================================================ +# InfosTest +#================================================================================ +class InfosTest(unittest.TestCase): + + def testFunctionInfo(self): + info = FunctionInfo('test::foo', 'foo.h') + rename(info, 'hello') + set_policy(info, return_internal_reference()) + set_wrapper(info, FunctionWrapper('foo_wrapper')) + + info = InfoWrapper(info) + + self.assertEqual(info.rename, 'hello') + self.assertEqual(info.policy.Code(), 'return_internal_reference< 1 >') + self.assertEqual(info.wrapper.name, 'foo_wrapper') + + + def testClassInfo(self): + info = ClassInfo('test::IFoo', 'foo.h') + rename(info.name, 'Name') + rename(info.exclude, 'Exclude') + rename(info, 'Foo') + rename(info.Bar, 'bar') + set_policy(info.Baz, return_internal_reference()) + rename(info.operator['>>'], 'from_string') + exclude(info.Bar) + set_wrapper(info.Baz, FunctionWrapper('baz_wrapper')) + + info = InfoWrapper(info) + + self.assertEqual(info.rename, 'Foo') + self.assertEqual(info['Bar'].rename, 'bar') + self.assertEqual(info['name'].rename, 'Name') + self.assertEqual(info['exclude'].rename, 'Exclude') + self.assertEqual(info['Bar'].exclude, True) + self.assertEqual(info['Baz'].policy.Code(), 'return_internal_reference< 1 >') + self.assertEqual(info['Baz'].wrapper.name, 'baz_wrapper') + self.assertEqual(info['operator']['>>'].rename, 'from_string') + + + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/inherit.cpp b/libs/python/pyste/tests/inherit.cpp new file mode 100644 index 000000000..a75e83891 --- /dev/null +++ b/libs/python/pyste/tests/inherit.cpp @@ -0,0 +1,8 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include "inherit.h" + +int inherit::C::s = 1; diff --git a/libs/python/pyste/tests/inherit.h b/libs/python/pyste/tests/inherit.h new file mode 100644 index 000000000..8f903f4fd --- /dev/null +++ b/libs/python/pyste/tests/inherit.h @@ -0,0 +1,43 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace inherit { + +template<typename T> +class A +{ +public: + void set(T v) { mData = v; } + + T get() const { return mData; } + +private: + T mData; +}; + + +class B : public A<int> +{ +public: + int go() { return get(); } +}; + +struct C : B +{ + enum ab { a = 1, b = 2 }; + int f1() { return 1; } + int x; + static int s; +}; + +struct D : C +{ + int f2() { return 2; } + int y; +}; + +struct X {}; +struct E: X, D {}; +} diff --git a/libs/python/pyste/tests/inherit.pyste b/libs/python/pyste/tests/inherit.pyste new file mode 100644 index 000000000..0dc029989 --- /dev/null +++ b/libs/python/pyste/tests/inherit.pyste @@ -0,0 +1,8 @@ +A = Template('inherit::A', 'inherit.h') +A_int = A('int', 'A_int') + +Class('inherit::B', 'inherit.h') +Class('inherit::D', 'inherit.h') +E = Class('inherit::E', 'inherit.h') +exclude(E.s) +exclude(E.ab) diff --git a/libs/python/pyste/tests/inherit2.h b/libs/python/pyste/tests/inherit2.h new file mode 100644 index 000000000..af9387bd0 --- /dev/null +++ b/libs/python/pyste/tests/inherit2.h @@ -0,0 +1,35 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace inherit2 { + +struct A +{ + int x; + int getx() { return x; } + int foo() { return 0; } + int foo(int x) { return x; } +}; + +struct B : A +{ + int y; + int gety() { return y; } + int foo() { return 1; } +}; + +struct C : B +{ + int z; + int getz() { return z; } +}; + +struct D : C +{ + int w; + int getw() { return w; } +}; + +} diff --git a/libs/python/pyste/tests/inherit2.pyste b/libs/python/pyste/tests/inherit2.pyste new file mode 100644 index 000000000..380821398 --- /dev/null +++ b/libs/python/pyste/tests/inherit2.pyste @@ -0,0 +1,2 @@ +Class('inherit2::B', 'inherit2.h') +Class('inherit2::D', 'inherit2.h') diff --git a/libs/python/pyste/tests/inherit2UT.py b/libs/python/pyste/tests/inherit2UT.py new file mode 100644 index 000000000..85afce617 --- /dev/null +++ b/libs/python/pyste/tests/inherit2UT.py @@ -0,0 +1,31 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _inherit2 import * + +class InheritExampleTest(unittest.TestCase): + + def testIt(self): + b = B() + d = D() + + self.assert_(issubclass(D, B)) + b.x, b.y = 10, 5 + self.assertEqual(b.getx(), 10) + self.assertEqual(b.gety(), 5) + d.x, d.y, d.z, d.w = 20, 15, 10, 5 + self.assertEqual(d.getx(), 20) + self.assertEqual(d.gety(), 15) + self.assertEqual(d.getz(), 10) + self.assertEqual(d.getw(), 5) + self.assertEqual(b.foo(), 1) + self.assertEqual(b.foo(3), 3) + + def wrong(): + return b.getw() + self.assertRaises(AttributeError, wrong) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/inherit3.h b/libs/python/pyste/tests/inherit3.h new file mode 100644 index 000000000..1945fb514 --- /dev/null +++ b/libs/python/pyste/tests/inherit3.h @@ -0,0 +1,46 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ + +namespace inherit3 { + +struct A +{ + A() { x = 0; } + struct X { int y; }; + int x; + virtual int foo() { return 0; } + virtual int foo(int x) { return x; } + A operator+(A o) const + { + A r; + r.x = o.x + x; + return r; + } + enum E { i, j }; + +}; + +struct B: A +{ + B() { x = 0; } + struct X { int y; }; + int x; + int foo() { return 1; } + A operator+(A o) const + { + A r; + r.x = o.x + x; + return r; + } + enum E { i, j }; + +}; + +struct C: A +{ +}; + +} diff --git a/libs/python/pyste/tests/inherit3.pyste b/libs/python/pyste/tests/inherit3.pyste new file mode 100644 index 000000000..f95c06054 --- /dev/null +++ b/libs/python/pyste/tests/inherit3.pyste @@ -0,0 +1,2 @@ +Class('inherit3::B', 'inherit3.h') +Class('inherit3::C', 'inherit3.h') diff --git a/libs/python/pyste/tests/inherit3UT.py b/libs/python/pyste/tests/inherit3UT.py new file mode 100644 index 000000000..b7dba1e93 --- /dev/null +++ b/libs/python/pyste/tests/inherit3UT.py @@ -0,0 +1,27 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _inherit3 import * + +class testInherit3(unittest.TestCase): + + def testIt(self): + def testInst(c): + self.assertEqual(c.x, 0) + self.assertEqual(c.foo(3), 3) + x = c.X() + self.assertEqual(x.y, 0) + self.assertEqual(c.E.i, 0) + self.assertEqual(c.E.j, 1) + b = B() + c = C() + testInst(b) + testInst(c) + self.assertEqual(b.foo(), 1) + self.assertEqual(c.foo(), 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/inherit4.h b/libs/python/pyste/tests/inherit4.h new file mode 100644 index 000000000..a1cecfbc8 --- /dev/null +++ b/libs/python/pyste/tests/inherit4.h @@ -0,0 +1,23 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace inherit4 { + +struct A +{ + int x; +}; + +struct B: A +{ + int y; +}; + +struct C: B +{ + int z; +}; + +} diff --git a/libs/python/pyste/tests/inherit4.pyste b/libs/python/pyste/tests/inherit4.pyste new file mode 100644 index 000000000..4809e022f --- /dev/null +++ b/libs/python/pyste/tests/inherit4.pyste @@ -0,0 +1,3 @@ +Class('inherit4::A', 'inherit4.h') +Class('inherit4::B', 'inherit4.h') +Class('inherit4::C', 'inherit4.h') diff --git a/libs/python/pyste/tests/inherit4UT.py b/libs/python/pyste/tests/inherit4UT.py new file mode 100644 index 000000000..f2c75c553 --- /dev/null +++ b/libs/python/pyste/tests/inherit4UT.py @@ -0,0 +1,31 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _inherit4 import * + +class TestInherit4(unittest.TestCase): + + def testIt(self): + self.assert_(issubclass(B, A)) + self.assert_(issubclass(C, A)) + self.assert_(issubclass(C, B)) + a = A() + a.x = 1 + b = B() + b.x = 10 + b.y = 20 + c = C() + c.x = 100 + c.y = 200 + c.z = 300 + self.assertEqual(a.x, 1) + self.assertEqual(b.x, 10) + self.assertEqual(b.y, 20) + self.assertEqual(c.x, 100) + self.assertEqual(c.y, 200) + self.assertEqual(c.z, 300) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/inheritUT.py b/libs/python/pyste/tests/inheritUT.py new file mode 100644 index 000000000..f3e7b4062 --- /dev/null +++ b/libs/python/pyste/tests/inheritUT.py @@ -0,0 +1,33 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _inherit import * + +class InheritExampleTest(unittest.TestCase): + + def testIt(self): + a = A_int() + b = B() + self.assert_(isinstance(b, A_int)) + self.assert_(issubclass(B, A_int)) + a.set(10) + self.assertEqual(a.get(), 10) + b.set(1) + self.assertEqual(b.go(), 1) + self.assertEqual(b.get(), 1) + + d = D() + self.assert_(issubclass(D, B)) + self.assertEqual(d.x, 0) + self.assertEqual(d.y, 0) + self.assertEqual(d.s, 1) + self.assertEqual(D.s, 1) + self.assertEqual(d.f1(), 1) + self.assertEqual(d.f2(), 2) + + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/nested.cpp b/libs/python/pyste/tests/nested.cpp new file mode 100644 index 000000000..6e167ab06 --- /dev/null +++ b/libs/python/pyste/tests/nested.cpp @@ -0,0 +1,9 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include "nested.h" + +int nested::X::staticXValue = 10; +int nested::X::Y::staticYValue = 20; diff --git a/libs/python/pyste/tests/nested.h b/libs/python/pyste/tests/nested.h new file mode 100644 index 000000000..13ed60856 --- /dev/null +++ b/libs/python/pyste/tests/nested.h @@ -0,0 +1,32 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 NESTED_H +#define NESTED_H + +namespace nested { + +struct X +{ + struct Y + { + int valueY; + static int staticYValue; + struct Z + { + int valueZ; + }; + }; + + static int staticXValue; + int valueX; +}; + +typedef X Root; + +} + +#endif diff --git a/libs/python/pyste/tests/nested.pyste b/libs/python/pyste/tests/nested.pyste new file mode 100644 index 000000000..48bb26b55 --- /dev/null +++ b/libs/python/pyste/tests/nested.pyste @@ -0,0 +1 @@ +Class('nested::Root', 'nested.h') diff --git a/libs/python/pyste/tests/nestedUT.py b/libs/python/pyste/tests/nestedUT.py new file mode 100644 index 000000000..06c1c7beb --- /dev/null +++ b/libs/python/pyste/tests/nestedUT.py @@ -0,0 +1,19 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _nested import * + +class NestedTest(unittest.TestCase): + + def testIt(self): + self.assertEqual(Root.staticXValue, 10) + self.assertEqual(Root.Y.staticYValue, 20) + z = Root.Y.Z() + z.valueZ = 3 + self.assertEqual(z.valueZ, 3) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/opaque.h b/libs/python/pyste/tests/opaque.h new file mode 100644 index 000000000..1947830ea --- /dev/null +++ b/libs/python/pyste/tests/opaque.h @@ -0,0 +1,57 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 OPAQUE_H +#define OPAQUE_H + +#include <iostream> + +namespace opaque { + + +struct C { + C(int v): value(v) {} + int value; +}; + + +inline C* new_C() +{ + return new C(10); +} + +inline C* new_C_zero() +{ + return new C(0); +} + +inline int get(C* c) +{ + return c->value; +} + +struct D { + D(double v): value(v) {} + double value; +}; + +struct A +{ + D* new_handle() + { + return new D(3.0); + } + + double get(D* d) + { + return d->value; + } + + int f(int x=0) { return x; } +}; + +} + +#endif diff --git a/libs/python/pyste/tests/opaque.pyste b/libs/python/pyste/tests/opaque.pyste new file mode 100644 index 000000000..8180d251c --- /dev/null +++ b/libs/python/pyste/tests/opaque.pyste @@ -0,0 +1,7 @@ +foo = Function('opaque::new_C', 'opaque.h') +set_policy(foo, return_value_policy(return_opaque_pointer)) +foo = Function('opaque::new_C_zero', 'opaque.h') +set_policy(foo, return_value_policy(return_opaque_pointer)) +Function('opaque::get', 'opaque.h' ) +A = Class('opaque::A', 'opaque.h') +set_policy(A.new_handle, return_value_policy(return_opaque_pointer)) diff --git a/libs/python/pyste/tests/opaqueUT.py b/libs/python/pyste/tests/opaqueUT.py new file mode 100644 index 000000000..0f3e1e073 --- /dev/null +++ b/libs/python/pyste/tests/opaqueUT.py @@ -0,0 +1,24 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _opaque import * + +class OpaqueTest(unittest.TestCase): + + def testIt(self): + + c = new_C() + self.assertEqual(get(c), 10) + c = new_C_zero() + self.assertEqual(get(c), 0) + a = A() + d = a.new_handle() + self.assertEqual(a.get(d), 3.0) + self.assertEqual(a.f(), 0) + self.assertEqual(a.f(3), 3) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/operators.cpp b/libs/python/pyste/tests/operators.cpp new file mode 100644 index 000000000..cecdaca0d --- /dev/null +++ b/libs/python/pyste/tests/operators.cpp @@ -0,0 +1,8 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include "operators.h" + +double operators::C::x = 10; diff --git a/libs/python/pyste/tests/operators.h b/libs/python/pyste/tests/operators.h new file mode 100644 index 000000000..5d3944216 --- /dev/null +++ b/libs/python/pyste/tests/operators.h @@ -0,0 +1,52 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 OPERATORS_H +#define OPERATORS_H + + +namespace operators { + +struct C +{ + static double x; + double value; + + const C operator+(const C other) const + { + C c; + c.value = value + other.value; + return c; + } + operator int() const + { + return (int)value; + } + + double operator()() + { + return C::x; + } + + double operator()(double other) + { + return C::x + other; + } + + operator const char*() { return "C"; } +}; + +inline const C operator*(const C& lhs, const C& rhs) +{ + C c; + c.value = lhs.value * rhs.value; + return c; +} + + +} + + +#endif diff --git a/libs/python/pyste/tests/operators.pyste b/libs/python/pyste/tests/operators.pyste new file mode 100644 index 000000000..4ab7a3709 --- /dev/null +++ b/libs/python/pyste/tests/operators.pyste @@ -0,0 +1,2 @@ +C = Class('operators::C', 'operators.h') +#exclude(C.operator['+']) diff --git a/libs/python/pyste/tests/operatorsUT.py b/libs/python/pyste/tests/operatorsUT.py new file mode 100644 index 000000000..beb193173 --- /dev/null +++ b/libs/python/pyste/tests/operatorsUT.py @@ -0,0 +1,30 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _operators import * + +class OperatorTest(unittest.TestCase): + + def testIt(self): + c = C() + c.value = 3.0 + d = C() + d.value = 2.0 + self.assertEqual(c.x, 10) + self.assertEqual(C.x, 10) + self.assertEqual(C.x, 10) + self.assertEqual((c * d).value, 6.0) + self.assertEqual((c + d).value, 5.0) + self.assertEqual(int(c), 3) + self.assertEqual(int(d), 2) + self.assertEqual(c(), 10) + self.assertEqual(d(), 10) + self.assertEqual(c(3.0), 13.0) + self.assertEqual(d(6.0), 16.0) + self.assertEqual(str(c), "C") + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/policiesUT.py b/libs/python/pyste/tests/policiesUT.py new file mode 100644 index 000000000..7255baeb4 --- /dev/null +++ b/libs/python/pyste/tests/policiesUT.py @@ -0,0 +1,67 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 sys +import unittest +from Pyste.policies import * + + +#================================================================================ +# PolicicesTest +#================================================================================ +class PoliciesTest(unittest.TestCase): + + def testReturnInternal(self): + 'tests the code from a simple internal_reference' + + x = return_internal_reference(1) + self.assertEqual(x.Code(), 'return_internal_reference< 1 >') + x = return_internal_reference(3) + self.assertEqual(x.Code(), 'return_internal_reference< 3 >') + + + def testCustodian(self): + 'tests the code from a simple custodian_and_ward' + + x = with_custodian_and_ward(1,2) + self.assertEqual(x.Code(), 'with_custodian_and_ward< 1, 2 >') + x = with_custodian_and_ward(3,4) + self.assertEqual(x.Code(), 'with_custodian_and_ward< 3, 4 >') + + + def testReturnPolicies(self): + 'tests all the return_value_policies' + + ret = 'return_value_policy< %s >' + x = return_value_policy(reference_existing_object) + self.assertEqual(x.Code(), ret % 'reference_existing_object') + x = return_value_policy(copy_const_reference) + self.assertEqual(x.Code(), ret % 'copy_const_reference') + x = return_value_policy(copy_non_const_reference) + self.assertEqual(x.Code(), ret % 'copy_non_const_reference') + x = return_value_policy(manage_new_object) + self.assertEqual(x.Code(), ret % 'manage_new_object') + x = return_value_policy(return_opaque_pointer) + self.assertEqual(x.Code(), ret % 'return_opaque_pointer') + + def testReturnWithCustodiam(self): + 'test the mix of return_internal with custodian' + + x = return_internal_reference(1, with_custodian_and_ward(3,2)) + self.assertEqual( + x.Code(), + 'return_internal_reference< 1, with_custodian_and_ward< 3, 2 > >') + + + def testReturnPoliciesWithInternal(self): + 'test the mix of return_internal with return_policy' + + x = return_internal_reference(1, return_value_policy(manage_new_object)) + self.assertEqual( + x.Code(), + 'return_internal_reference< 1, return_value_policy< manage_new_object > >') + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/runtests.py b/libs/python/pyste/tests/runtests.py new file mode 100644 index 000000000..4bf83b345 --- /dev/null +++ b/libs/python/pyste/tests/runtests.py @@ -0,0 +1,21 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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) +#!/usr/bin/python + +import sys +sys.path.append('../src/Pyste') +import unittest +import os.path +from glob import glob + +if __name__ == '__main__': + loader = unittest.defaultTestLoader + tests = [] + for name in glob('*UT.py'): + module = __import__(os.path.splitext(name)[0]) + tests.append(loader.loadTestsFromModule(module)) + runner = unittest.TextTestRunner() + result = runner.run(unittest.TestSuite(tests)) + sys.exit(not result.wasSuccessful()) diff --git a/libs/python/pyste/tests/smart_ptr.h b/libs/python/pyste/tests/smart_ptr.h new file mode 100644 index 000000000..b230b9179 --- /dev/null +++ b/libs/python/pyste/tests/smart_ptr.h @@ -0,0 +1,50 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 SMART_PTR_H +#define SMART_PTR_H + + +#include <memory> +#include <boost/shared_ptr.hpp> + +namespace smart_ptr { + +struct C +{ + int value; +}; + +inline boost::shared_ptr<C> NewC() { return boost::shared_ptr<C>( new C() ); } + +struct D +{ + boost::shared_ptr<C> Get() { return ptr; } + void Set( boost::shared_ptr<C> c ) { ptr = c; } +private: + boost::shared_ptr<C> ptr; +}; + +inline std::auto_ptr<D> NewD() { return std::auto_ptr<D>( new D() ); } + + +// test an abstract class +struct A +{ + virtual int f() = 0; +}; + +struct B: A +{ + virtual int f(){ return 1; } +}; + +inline boost::shared_ptr<A> NewA() { return boost::shared_ptr<A>(new B()); } +inline int GetA(boost::shared_ptr<A> a) { return a->f(); } + +} + +#endif diff --git a/libs/python/pyste/tests/smart_ptr.pyste b/libs/python/pyste/tests/smart_ptr.pyste new file mode 100644 index 000000000..cfbdd81ae --- /dev/null +++ b/libs/python/pyste/tests/smart_ptr.pyste @@ -0,0 +1,13 @@ +C = Class('smart_ptr::C', 'smart_ptr.h') +use_shared_ptr(C) + +D = Class('smart_ptr::D', 'smart_ptr.h') +use_auto_ptr(D) + +A = Class('smart_ptr::A', 'smart_ptr.h') +use_shared_ptr(A) + +Function('smart_ptr::NewC', 'smart_ptr.h') +Function('smart_ptr::NewD', 'smart_ptr.h') +Function('smart_ptr::NewA', 'smart_ptr.h') +Function('smart_ptr::GetA', 'smart_ptr.h') diff --git a/libs/python/pyste/tests/smart_ptrUT.py b/libs/python/pyste/tests/smart_ptrUT.py new file mode 100644 index 000000000..9d81f08dd --- /dev/null +++ b/libs/python/pyste/tests/smart_ptrUT.py @@ -0,0 +1,22 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _smart_ptr import * + +class BasicExampleTest(unittest.TestCase): + + def testIt(self): + c = NewC() + d = NewD() + c.value = 3 + d.Set(c) + c1 = d.Get() + c1.value = 6 + self.assertEqual(c.value, 6) + a = NewA() + self.assertEqual(GetA(a), 1) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/templates.h b/libs/python/pyste/tests/templates.h new file mode 100644 index 000000000..7258e91c7 --- /dev/null +++ b/libs/python/pyste/tests/templates.h @@ -0,0 +1,15 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace templates { + +template <class T> +struct Point +{ + T x; + T y; +}; + +} diff --git a/libs/python/pyste/tests/templates.pyste b/libs/python/pyste/tests/templates.pyste new file mode 100644 index 000000000..77eaceaa3 --- /dev/null +++ b/libs/python/pyste/tests/templates.pyste @@ -0,0 +1,8 @@ +Point = Template('templates::Point', 'templates.h') +rename(Point.x, 'i') +rename(Point.y, 'j') +IPoint = Point('int') +FPoint = Point('double', 'FPoint') +rename(IPoint, 'IPoint') +rename(IPoint.x, 'x') +rename(IPoint.y, 'y') diff --git a/libs/python/pyste/tests/templatesUT.py b/libs/python/pyste/tests/templatesUT.py new file mode 100644 index 000000000..0c4b08b50 --- /dev/null +++ b/libs/python/pyste/tests/templatesUT.py @@ -0,0 +1,30 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _templates import * + +class TemplatesTest(unittest.TestCase): + + def testIt(self): + fp = FPoint() + fp.i = 3.0 + fp.j = 4.0 + ip = IPoint() + ip.x = 10 + ip.y = 3 + + self.assertEqual(fp.i, 3.0) + self.assertEqual(fp.j, 4.0) + self.assertEqual(ip.x, 10) + self.assertEqual(ip.y, 3) + self.assertEqual(type(fp.i), float) + self.assertEqual(type(fp.j), float) + self.assertEqual(type(ip.x), int) + self.assertEqual(type(ip.y), int) + + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/test_all.py b/libs/python/pyste/tests/test_all.py new file mode 100644 index 000000000..ba3c54dee --- /dev/null +++ b/libs/python/pyste/tests/test_all.py @@ -0,0 +1,140 @@ +#!/usr/bin/python +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 glob +import shutil +import sys +import time + +#============================================================================= +# win32 configuration +#============================================================================= +if sys.platform == 'win32': + + includes = '-ID:/programming/libraries/boost-cvs/boost -ID:/Bin/Python/include' + build_pyste_cmd = 'python ../src/Pyste/pyste.py --pyste-ns=pyste --cache-dir=cache %s ' % includes + compile_single_cmd = 'cl /nologo /GR /GX -c %s -I. ' % includes + link_single_cmd = 'link /nologo /DLL '\ + '/libpath:D:/programming/libraries/boost-cvs/lib /libpath:D:/Bin/Python/libs '\ + 'boost_python.lib python24.lib /out:_%s.dll ' + obj_ext = 'obj' + +#============================================================================= +# linux configuration +#============================================================================= +elif sys.platform == 'linux2': + + build_pyste_cmd = 'python ../src/Pyste/pyste.py -I. ' + compile_single_cmd = 'g++ -shared -c -I. -I/usr/include/python2.4 ' + link_single_cmd = 'g++ -shared -o _%s.so -lboost_python ' + obj_ext = 'o' + + + +def build_pyste(multiple, module): + rest = '%s --module=_%s %s.pyste' % (multiple, module, module) + execute(build_pyste_cmd + rest) + + +def compile_single(module): + module_obj = '' + if os.path.isfile(module+'.cpp'): + execute(compile_single_cmd + module+'.cpp') + module_obj = module + '.' + obj_ext + execute(compile_single_cmd + ('_%s.cpp' % module)) + link = link_single_cmd % module + execute(link + ('_%s.%s ' % (module, obj_ext)) + module_obj) + + +def compile_multiple(module): + module_obj = '' + if os.path.isfile(module+'.cpp'): + execute(compile_single_cmd + module+'.cpp') + module_obj = module + '.' + obj_ext + files = glob.glob('_%s/*.cpp' % module) + for f in files: + execute(compile_single_cmd + f) + def basename(name): + return os.path.basename(os.path.splitext(name)[0]) + objs = [basename(x) + '.' + obj_ext for x in files] + objs.append(module_obj) + execute((link_single_cmd % module) + ' '.join(objs)) + + +def execute(cmd): + os.system(cmd) + + +def run_tests(): + if os.system('python runtests.py') != 0: + raise RuntimeError, 'tests failed' + + +def cleanup(): + modules = get_modules() + extensions = '*.dll *.pyc *.obj *.exp *.lib *.o *.so' + files = [] + for module in modules: + files.append('_' + module + '.cpp') + for ext in extensions.split(): + files += glob.glob(ext) + files.append('build.log') + for file in files: + try: + os.remove(file) + except OSError: pass + + for module in modules: + try: + shutil.rmtree('_' + module) + except OSError: pass + + +def main(multiple, module=None): + if module is None: + modules = get_modules() + else: + modules = [module] + + start = time.clock() + for module in modules: + build_pyste(multiple, module) + print '-'*50 + print 'Building pyste files: %0.2f seconds' % (time.clock()-start) + print + + start = time.clock() + for module in modules: + if multiple: + compile_multiple(module) + else: + compile_single(module) + print '-'*50 + print 'Compiling files: %0.2f seconds' % (time.clock()-start) + print + if len(modules) == 1: + os.system('python %sUT.py' % modules[0]) + else: + run_tests() + #cleanup() + + +def get_modules(): + def getname(file): + return os.path.splitext(os.path.basename(file))[0] + return [getname(x) for x in glob.glob('*.pyste')] + +if __name__ == '__main__': + if len(sys.argv) > 1: + module = sys.argv[1] + else: + module = None + try: +# main('--multiple', module) + main('', module) + except RuntimeError, e: + print e diff --git a/libs/python/pyste/tests/vars.cpp b/libs/python/pyste/tests/vars.cpp new file mode 100644 index 000000000..e2abcd332 --- /dev/null +++ b/libs/python/pyste/tests/vars.cpp @@ -0,0 +1,12 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +#include "vars.h" + +const Color black = Color(0, 0, 0); +const Color red = Color(255, 0, 0); +const Color green = Color(0, 255, 0); +const Color blue = Color(0, 0, 255); +Color in_use = black; diff --git a/libs/python/pyste/tests/vars.h b/libs/python/pyste/tests/vars.h new file mode 100644 index 000000000..24e87d802 --- /dev/null +++ b/libs/python/pyste/tests/vars.h @@ -0,0 +1,24 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ + +struct Color +{ + Color(int r_ = 0, int g_ = 0, int b_ = 0): + r(r_), g(g_), b(b_) + {} + Color( const Color &c): + r(c.r), g(c.g), b(c.b) + {} + int r; + int g; + int b; +}; + +extern const Color black; +extern const Color red; +extern const Color green; +extern const Color blue; +extern Color in_use; diff --git a/libs/python/pyste/tests/vars.pyste b/libs/python/pyste/tests/vars.pyste new file mode 100644 index 000000000..3fd9d689d --- /dev/null +++ b/libs/python/pyste/tests/vars.pyste @@ -0,0 +1 @@ +AllFromHeader('vars.h') diff --git a/libs/python/pyste/tests/varsUT.py b/libs/python/pyste/tests/varsUT.py new file mode 100644 index 000000000..4c32cbb2f --- /dev/null +++ b/libs/python/pyste/tests/varsUT.py @@ -0,0 +1,22 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +import _vars + + +class VarsTest(unittest.TestCase): + + def testIt(self): + def testColor(c, r, g, b): + self.assertEqual(c.r, r) + self.assertEqual(c.g, g) + self.assertEqual(c.b, b) + testColor(_vars.black, 0, 0, 0) + testColor(_vars.red, 255, 0, 0) + testColor(_vars.green, 0, 255, 0) + testColor(_vars.blue, 0, 0, 255) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/virtual.cpp b/libs/python/pyste/tests/virtual.cpp new file mode 100644 index 000000000..070d9d346 --- /dev/null +++ b/libs/python/pyste/tests/virtual.cpp @@ -0,0 +1,75 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ + +// Includes ==================================================================== +#include <boost/python.hpp> +#include <virtual.h> + +// Using ======================================================================= +using namespace boost::python; + +// Declarations ================================================================ + + +namespace { + + +struct virtual_C_Wrapper: virtual_::C +{ + virtual_C_Wrapper(PyObject* self_, const virtual_::C & p0): + virtual_::C(p0), self(self_) {} + + virtual_C_Wrapper(PyObject* self_): + virtual_::C(), self(self_) {} + + int f() { + return call_method< int >(self, "f"); + } + + int default_f() { + return virtual_::C::f(); + } + + void bar(int p0) { + call_method< void >(self, "bar", p0); + } + + void default_bar(int p0) { + virtual_::C::bar(p0); + } + + void bar(char * p0) { + call_method< void >(self, "bar", p0); + } + + void default_bar(char * p0) { + virtual_::C::bar(p0); + } + + int f_abs() { + return call_method< int >(self, "f_abs"); + } + + PyObject* self; +}; + + + +}// namespace + + +// Module ====================================================================== +BOOST_PYTHON_MODULE(virtual) +{ + class_< virtual_::C, boost::noncopyable, virtual_C_Wrapper >("C", init< >()) + .def("get_name", &virtual_::C::get_name) + .def("f", &virtual_::C::f, &virtual_C_Wrapper::default_f) + .def("bar", (void (virtual_::C::*)(int) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(int))&virtual_C_Wrapper::default_bar) + .def("bar", (void (virtual_::C::*)(char *) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(char *))&virtual_C_Wrapper::default_bar) + ; + + def("call_f", &virtual_::call_f); +} diff --git a/libs/python/pyste/tests/virtual.h b/libs/python/pyste/tests/virtual.h new file mode 100644 index 000000000..d0bb194a1 --- /dev/null +++ b/libs/python/pyste/tests/virtual.h @@ -0,0 +1,41 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ +namespace virtual_ { + +struct C +{ +public: + virtual int f() + { + return f_abs(); + } + + virtual void bar(int) {} + virtual void bar(char*) {} + + const char* get_name() + { + return name(); + } + virtual int dummy() { return 0; } + +protected: + virtual int f_abs() = 0; + +private: + virtual const char* name() { return "C"; } +}; + +struct D +{ + virtual int dummy() { return 0; } +}; + +inline int call_f(C& c) { return c.f(); } +inline int call_dummy(C* c) { return c->dummy(); } +inline int call_dummy(D* d) { return d->dummy(); } + +} diff --git a/libs/python/pyste/tests/virtual.pyste b/libs/python/pyste/tests/virtual.pyste new file mode 100644 index 000000000..ef9664124 --- /dev/null +++ b/libs/python/pyste/tests/virtual.pyste @@ -0,0 +1,6 @@ +C = Class('virtual_::C', 'virtual.h') +final(C.dummy) +D = Class('virtual_::D', 'virtual.h') +final(D.dummy) +Function('virtual_::call_f', 'virtual.h') +Function('virtual_::call_dummy', 'virtual.h') diff --git a/libs/python/pyste/tests/virtual2.h b/libs/python/pyste/tests/virtual2.h new file mode 100644 index 000000000..a6677ad16 --- /dev/null +++ b/libs/python/pyste/tests/virtual2.h @@ -0,0 +1,34 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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) + */ + +namespace virtual2 { + +struct A +{ + virtual int f() { return 0; } + virtual int f1() { return 10; } + virtual A* make_new() { return new A; } +}; + +struct B: A +{ + virtual int f() { return 1; } + virtual int f2() { return 20; } + virtual A* make_new() { return new B; } +}; + +inline int call_fs(A*a) +{ + int r = a->f1(); + B* b = dynamic_cast<B*>(a); + return r + b->f2(); +} + +inline int call_f(A* a) +{ + return a->f(); +} +} diff --git a/libs/python/pyste/tests/virtual2.pyste b/libs/python/pyste/tests/virtual2.pyste new file mode 100644 index 000000000..785b819c8 --- /dev/null +++ b/libs/python/pyste/tests/virtual2.pyste @@ -0,0 +1,6 @@ +A = Class('virtual2::A', 'virtual2.h') +set_policy(A.make_new, return_value_policy(manage_new_object)) +B = Class('virtual2::B', 'virtual2.h') +set_policy(B.make_new, return_value_policy(manage_new_object)) +Function('virtual2::call_fs', 'virtual2.h') +Function('virtual2::call_f', 'virtual2.h') diff --git a/libs/python/pyste/tests/virtual2UT.py b/libs/python/pyste/tests/virtual2UT.py new file mode 100644 index 000000000..312277d26 --- /dev/null +++ b/libs/python/pyste/tests/virtual2UT.py @@ -0,0 +1,40 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _virtual2 import * + +class Virtual2Test(unittest.TestCase): + + def testIt(self): + a = A() + self.assertEqual(a.f1(), 10) + b = B() + self.assertEqual(b.f1(), 10) + self.assertEqual(b.f2(), 20) + self.assertEqual(call_fs(b), 30) + self.assertEqual(call_f(a), 0) + self.assertEqual(call_f(b), 1) + nb = b.make_new() + na = a.make_new() + self.assertEqual(na.f1(), 10) + self.assertEqual(nb.f1(), 10) + self.assertEqual(nb.f2(), 20) + self.assertEqual(call_fs(nb), 30) + self.assertEqual(call_f(na), 0) + self.assertEqual(call_f(nb), 1) + class C(B): + def f1(self): return 1 + def f2(self): return 2 + def f(self): return 100 + + c = C() + self.assertEqual(call_fs(c), 3) + self.assertEqual(call_fs(c), 3) + self.assertEqual(call_f(c), 100) + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/virtualUT.py b/libs/python/pyste/tests/virtualUT.py new file mode 100644 index 000000000..deff68189 --- /dev/null +++ b/libs/python/pyste/tests/virtualUT.py @@ -0,0 +1,55 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _virtual import * + +class VirtualTest(unittest.TestCase): + + def testIt(self): + + class E(C): + def f_abs(self): + return 3 + def dummy(self): + # override should not work + return 100 + + class F(C): + def f(self): + return 10 + def name(self): + return 'F' + + class G(D): + def dummy(self): + # override should not work + return 100 + + e = E() + f = F() + + self.assertEqual(e.f(), 3) + self.assertEqual(call_f(e), 3) + self.assertEqual(f.f(), 10) + self.assertEqual(call_f(f), 10) + self.assertEqual(e.get_name(), 'C') + #self.assertEqual(e.get_name(), 'E') check this later + + c = C() + c.bar(1) # ok + c.bar('a') # ok + self.assertRaises(TypeError, c.bar, 1.0) + + # test no_overrides + d = G() + self.assertEqual(e.dummy(), 100) + self.assertEqual(call_dummy(e), 0) + self.assertEqual(d.dummy(), 100) + self.assertEqual(call_dummy(d), 0) + + + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/wrappertest.h b/libs/python/pyste/tests/wrappertest.h new file mode 100644 index 000000000..2304fd843 --- /dev/null +++ b/libs/python/pyste/tests/wrappertest.h @@ -0,0 +1,51 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 WRAPPER_TEST +#define WRAPPER_TEST + + +#include <vector> + +namespace wrappertest { + +inline std::vector<int> Range(int count) +{ + std::vector<int> v; + v.reserve(count); + for (int i = 0; i < count; ++i){ + v.push_back(i); + } + return v; +} + + +struct C +{ + C() {} + + std::vector<int> Mul(int value) + { + std::vector<int> res; + res.reserve(value); + std::vector<int>::const_iterator it; + std::vector<int> v(Range(value)); + for (it = v.begin(); it != v.end(); ++it){ + res.push_back(*it * value); + } + return res; + } +}; + + +struct A +{ + virtual int f() { return 1; }; +}; + +inline int call_foo(A* a){ return a->f(); } +} +#endif + diff --git a/libs/python/pyste/tests/wrappertest.pyste b/libs/python/pyste/tests/wrappertest.pyste new file mode 100644 index 000000000..12ba47b6b --- /dev/null +++ b/libs/python/pyste/tests/wrappertest.pyste @@ -0,0 +1,21 @@ +Include('wrappertest_wrappers.h') + +f = Function('wrappertest::Range', 'wrappertest.h') +set_wrapper(f, 'RangeWrapper') + +mul = Wrapper('MulWrapper', +''' +list MulWrapper(wrappertest::C& c, int value){ + return VectorToList(c.Mul(value)); +} +''' +) + +C = Class('wrappertest::C', 'wrappertest.h') +set_wrapper(C.Mul, mul) + + +A = Class('wrappertest::A', 'wrappertest.h') +set_wrapper(A.f, 'f_wrapper') + +Function('wrappertest::call_foo', 'wrappertest.h') diff --git a/libs/python/pyste/tests/wrappertestUT.py b/libs/python/pyste/tests/wrappertestUT.py new file mode 100644 index 000000000..d770408b7 --- /dev/null +++ b/libs/python/pyste/tests/wrappertestUT.py @@ -0,0 +1,24 @@ +# Copyright Bruno da Silva de Oliveira 2003. Use, modification and +# distribution is subject to 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 unittest +from _wrappertest import * + +class WrapperTest(unittest.TestCase): + + def testIt(self): + self.assertEqual(Range(10), range(10)) + self.assertEqual(C().Mul(10), [x*10 for x in range(10)]) + + a = A() + self.assertEqual(a.f(), 10) + self.assertEqual(call_foo(a), 10) + class D(A): + def f(self): return 2 + d = D() + self.assertEqual(d.f(), 2) + self.assertEqual(call_foo(d), 2) + +if __name__ == '__main__': + unittest.main() diff --git a/libs/python/pyste/tests/wrappertest_wrappers.h b/libs/python/pyste/tests/wrappertest_wrappers.h new file mode 100644 index 000000000..31570a051 --- /dev/null +++ b/libs/python/pyste/tests/wrappertest_wrappers.h @@ -0,0 +1,33 @@ +/* Copyright Bruno da Silva de Oliveira 2003. Use, modification and + distribution is subject to 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 WRAPPER_TEST_WRAPPERS +#define WRAPPER_TEST_WRAPPERS + +#include <vector> +#include <boost/python.hpp> +#include "wrappertest.h" + +using namespace boost::python; + +template <class T> +list VectorToList(const std::vector<T> & v) +{ + list res; + typename std::vector<T>::const_iterator it; + for(it = v.begin(); it != v.end(); ++it){ + res.append(*it); + } + Py_XINCREF(res.ptr()); + return res; +} + +inline list RangeWrapper(int count){ + return VectorToList(wrappertest::Range(count)); +} + +inline int f_wrapper(wrappertest::A*) { return 10; } + +#endif |