Skip to content

Commit c7d16fb

Browse files
committed
Pearu's test
[SVN r14057]
1 parent 033a3dd commit c7d16fb

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ rule bpl-test ( name ? : files * )
5252
}
5353

5454
bpl-test minimal ;
55+
bpl-test pearu1 : test_cltree.py cltree.cpp ;
5556
bpl-test try : newtest.py m1.cpp m2.cpp ;
5657
bpl-test builtin_converters : test_builtin_converters.py test_builtin_converters.cpp ;
5758
bpl-test test_pointer_adoption ;

test/cltree.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <boost/python/module.hpp>
2+
#include <boost/python/class.hpp>
3+
#include <boost/utility.hpp>
4+
5+
/* Non-modifiable definitions */
6+
7+
class basic {
8+
public:
9+
basic() { name = "cltree.basic"; }
10+
std::string repr() { return name+"()"; }
11+
protected:
12+
std::string name;
13+
};
14+
15+
class constant: public basic {
16+
public:
17+
constant() { name = "cltree.constant"; }
18+
};
19+
20+
class symbol: public basic {
21+
public:
22+
symbol() { name = "cltree.symbol"; }
23+
};
24+
25+
class variable: public basic {
26+
public:
27+
variable() { name = "cltree.variable"; }
28+
};
29+
30+
/* EOF: Non-modifiable definitions */
31+
32+
class symbol_wrapper: public symbol {
33+
public:
34+
symbol_wrapper(PyObject* self): symbol() {
35+
name = "cltree.wrapped_symbol";
36+
}
37+
};
38+
39+
class variable_wrapper: public variable {
40+
public:
41+
variable_wrapper(PyObject* self): variable() {
42+
name = "cltree.wrapped_variable";
43+
}
44+
45+
// This constructor is introduced only because cannot use
46+
// boost::noncopyable, see below.
47+
variable_wrapper(PyObject* self,variable v): variable(v) {}
48+
49+
};
50+
51+
BOOST_PYTHON_MODULE_INIT(cltree) {
52+
53+
boost::python::module m("cltree");
54+
m
55+
.add(
56+
boost::python::class_<basic>("basic")
57+
.def_init(boost::python::args<>())
58+
.def("__repr__",&basic::repr)
59+
)
60+
;
61+
62+
m
63+
.add(boost::python::class_<constant
64+
,boost::python::bases<basic>
65+
,boost::noncopyable
66+
>("constant")
67+
.def_init(boost::python::args<>())
68+
)
69+
70+
.add(boost::python::class_<symbol
71+
,symbol_wrapper
72+
,boost::noncopyable
73+
>("symbol")
74+
.def_init(boost::python::args<>())
75+
)
76+
77+
.add(boost::python::class_<variable
78+
,boost::python::bases<basic>
79+
,variable_wrapper
80+
//,boost::noncopyable // leads to compiler failure?!
81+
>("variable")
82+
.def_init(boost::python::args<>())
83+
)
84+
;
85+
}

test/test_cltree.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
3+
from cltree import basic,symbol,constant,variable
4+
5+
b = basic()
6+
c = constant()
7+
s = symbol()
8+
v = variable()
9+
10+
assert isinstance(b,basic)
11+
assert not isinstance(b,symbol)
12+
assert not isinstance(b,constant)
13+
assert not isinstance(b,variable)
14+
15+
assert isinstance(c,basic)
16+
assert isinstance(c,constant)
17+
assert not isinstance(c,symbol)
18+
assert not isinstance(c,variable)
19+
20+
assert not isinstance(s,basic)
21+
assert isinstance(s,symbol)
22+
assert not isinstance(s,constant)
23+
assert not isinstance(s,variable)
24+
25+
assert isinstance(v,basic)
26+
assert not isinstance(v,symbol)
27+
assert not isinstance(v,constant)
28+
assert isinstance(v,variable)
29+
30+
print 'b=',b
31+
assert repr(b)=='cltree.basic()'
32+
print 's=',s
33+
assert repr(s)!='cltree.wrapped_symbol()' # because not isinstance(s,basic)
34+
print 'c=',c
35+
assert repr(c)=='cltree.constant()'
36+
print 'v=',v
37+
assert repr(v)=='cltree.wrapped_variable()'
38+
39+
40+
print 'ok'

0 commit comments

Comments
 (0)