four new director tests
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4449 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
3f5815cf03
commit
08e16bed8e
9 changed files with 299 additions and 0 deletions
|
|
@ -97,6 +97,10 @@ CPP_TEST_CASES += \
|
||||||
default_constructor \
|
default_constructor \
|
||||||
default_ns \
|
default_ns \
|
||||||
default_ref \
|
default_ref \
|
||||||
|
director_basic \
|
||||||
|
director_exception \
|
||||||
|
director_finalizer \
|
||||||
|
director_unroll \
|
||||||
dynamic_cast \
|
dynamic_cast \
|
||||||
enum_plus \
|
enum_plus \
|
||||||
enum_scope \
|
enum_scope \
|
||||||
|
|
|
||||||
26
Examples/test-suite/director_basic.i
Normal file
26
Examples/test-suite/director_basic.i
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
%module(directors="1") director_basic
|
||||||
|
%{
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "typemaps.i"
|
||||||
|
%include "exception.i"
|
||||||
|
%include "std_string.i"
|
||||||
|
|
||||||
|
%feature("director") Foo;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
54
Examples/test-suite/director_exception.i
Normal file
54
Examples/test-suite/director_exception.i
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
%module(directors="1") director_exception
|
||||||
|
%{
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
Foo *launder(Foo *f) {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// define dummy director exception classes to prevent spurious errors
|
||||||
|
// in target languages that do not support directors.
|
||||||
|
|
||||||
|
#ifndef SWIG_DIRECTORS
|
||||||
|
class SWIG_DIRECTOR_EXCEPTION {};
|
||||||
|
class SWIG_DIRECTOR_METHOD_EXCEPTION: public SWIG_DIRECTOR_EXCEPTION {};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "typemaps.i"
|
||||||
|
%include "exception.i"
|
||||||
|
%include "std_string.i"
|
||||||
|
|
||||||
|
|
||||||
|
%feature("director:except") {
|
||||||
|
if ($error != NULL) {
|
||||||
|
throw SWIG_DIRECTOR_METHOD_EXCEPTION();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
%exception {
|
||||||
|
try { $action }
|
||||||
|
catch (SWIG_DIRECTOR_EXCEPTION &e) { SWIG_fail; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
%feature("director") Foo;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
Foo *launder(Foo *f);
|
||||||
|
|
||||||
42
Examples/test-suite/director_finalizer.i
Normal file
42
Examples/test-suite/director_finalizer.i
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
%module(directors="1") director_finalizer
|
||||||
|
%{
|
||||||
|
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() { orStatus(1); }
|
||||||
|
virtual void orStatus(int x) { status |= x; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void deleteFoo(Foo *f) {
|
||||||
|
delete f;
|
||||||
|
}
|
||||||
|
|
||||||
|
Foo *launder(Foo *f) {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetStatus() {
|
||||||
|
status = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%feature("director") Foo;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo();
|
||||||
|
virtual void orStatus(int x);
|
||||||
|
};
|
||||||
|
|
||||||
|
void deleteFoo(Foo *f);
|
||||||
|
int getStatus();
|
||||||
|
Foo *launder(Foo *f);
|
||||||
|
void resetStatus();
|
||||||
|
|
||||||
42
Examples/test-suite/director_unroll.i
Normal file
42
Examples/test-suite/director_unroll.i
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
%module(directors="1") director_unroll
|
||||||
|
%{
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
private:
|
||||||
|
Foo *foo;
|
||||||
|
public:
|
||||||
|
void set(Foo *x) { foo = x; }
|
||||||
|
Foo *get() { return foo; }
|
||||||
|
};
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "typemaps.i"
|
||||||
|
%include "exception.i"
|
||||||
|
%include "std_string.i"
|
||||||
|
|
||||||
|
%feature("director") Foo;
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
public:
|
||||||
|
virtual ~Foo() {}
|
||||||
|
virtual std::string ping() { return "Foo::ping()"; }
|
||||||
|
virtual std::string pong() { return "Foo::pong();" + ping(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
private:
|
||||||
|
Foo *foo;
|
||||||
|
public:
|
||||||
|
void set(Foo *x) { foo = x; }
|
||||||
|
Foo *get() { return foo; }
|
||||||
|
};
|
||||||
|
|
||||||
24
Examples/test-suite/python/director_basic_runme.py
Normal file
24
Examples/test-suite/python/director_basic_runme.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import director_basic
|
||||||
|
|
||||||
|
class MyFoo(director_basic.Foo):
|
||||||
|
def ping(self):
|
||||||
|
return "MyFoo::ping()"
|
||||||
|
|
||||||
|
|
||||||
|
a = MyFoo()
|
||||||
|
|
||||||
|
if a.ping() != "MyFoo::ping()":
|
||||||
|
raise RuntimeError, a.ping()
|
||||||
|
|
||||||
|
if a.pong() != "Foo::pong();MyFoo::ping()":
|
||||||
|
raise RuntimeError, a.pong()
|
||||||
|
|
||||||
|
b = director_basic.Foo()
|
||||||
|
|
||||||
|
if b.ping() != "Foo::ping()":
|
||||||
|
raise RuntimeError, b.ping()
|
||||||
|
|
||||||
|
if b.pong() != "Foo::pong();Foo::ping()":
|
||||||
|
raise RuntimeError, b.pong()
|
||||||
|
|
||||||
|
|
||||||
43
Examples/test-suite/python/director_exception_runme.py
Normal file
43
Examples/test-suite/python/director_exception_runme.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
from director_exception import *
|
||||||
|
from exceptions import *
|
||||||
|
|
||||||
|
class MyFoo(Foo):
|
||||||
|
def ping(self):
|
||||||
|
raise NotImplementedError, "MyFoo::ping() EXCEPTION"
|
||||||
|
|
||||||
|
class MyFoo2(Foo):
|
||||||
|
def ping(self):
|
||||||
|
pass # error: should return a string
|
||||||
|
|
||||||
|
ok = 0
|
||||||
|
|
||||||
|
a = MyFoo()
|
||||||
|
b = launder(a)
|
||||||
|
|
||||||
|
try:
|
||||||
|
b.pong()
|
||||||
|
except NotImplementedError, e:
|
||||||
|
ok = 1
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not ok:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
ok = 0
|
||||||
|
|
||||||
|
a = MyFoo2()
|
||||||
|
b = launder(a)
|
||||||
|
|
||||||
|
try:
|
||||||
|
b.pong()
|
||||||
|
except TypeError, e:
|
||||||
|
ok = 1
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not ok:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
47
Examples/test-suite/python/director_finalizer_runme.py
Normal file
47
Examples/test-suite/python/director_finalizer_runme.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
from director_finalizer import *
|
||||||
|
|
||||||
|
class MyFoo(Foo):
|
||||||
|
def __del__(self):
|
||||||
|
self.orStatus(2)
|
||||||
|
Foo.__del__(self)
|
||||||
|
|
||||||
|
|
||||||
|
resetStatus()
|
||||||
|
|
||||||
|
a = MyFoo()
|
||||||
|
del a
|
||||||
|
|
||||||
|
if getStatus() != 3:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
resetStatus()
|
||||||
|
|
||||||
|
a = MyFoo()
|
||||||
|
launder(a)
|
||||||
|
|
||||||
|
if getStatus() != 0:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
del a
|
||||||
|
|
||||||
|
if getStatus() != 3:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
resetStatus()
|
||||||
|
|
||||||
|
a = MyFoo().__disown__()
|
||||||
|
deleteFoo(a)
|
||||||
|
|
||||||
|
if getStatus() != 3:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
resetStatus()
|
||||||
|
|
||||||
|
a = MyFoo().__disown__()
|
||||||
|
deleteFoo(launder(a))
|
||||||
|
|
||||||
|
if getStatus() != 3:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
resetStatus()
|
||||||
|
|
||||||
17
Examples/test-suite/python/director_unroll_runme.py
Normal file
17
Examples/test-suite/python/director_unroll_runme.py
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import director_unroll
|
||||||
|
|
||||||
|
class MyFoo(director_unroll.Foo):
|
||||||
|
def ping(self):
|
||||||
|
return "MyFoo::ping()"
|
||||||
|
|
||||||
|
|
||||||
|
a = MyFoo()
|
||||||
|
|
||||||
|
b = director_unroll.Bar()
|
||||||
|
|
||||||
|
b.set(a)
|
||||||
|
c = b.get()
|
||||||
|
|
||||||
|
if not (a is c):
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue