Skip to content

Commit d482d57

Browse files
committed
added properties tests
[SVN r19532]
1 parent edf6516 commit d482d57

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

test/properties.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <boost/python.hpp>
2+
3+
using namespace boost::python;
4+
5+
struct X
6+
{
7+
X( int value ) : m_value( value )
8+
{ ++s_count; }
9+
10+
X( const X &other ) : m_value( other.m_value )
11+
{ ++s_count; }
12+
13+
~X()
14+
{ --s_count; }
15+
16+
int get_value() const
17+
{ return m_value; }
18+
19+
void set_value(int new_value)
20+
{ m_value = new_value; }
21+
22+
static int get_instance_count()
23+
{ return s_count; }
24+
25+
int m_value;
26+
27+
static int s_count;
28+
};
29+
30+
int X::s_count = 0;
31+
32+
int get_X_instance_count()
33+
{ return X::get_instance_count(); }
34+
35+
36+
37+
BOOST_PYTHON_MODULE(properties_ext)
38+
{
39+
typedef return_value_policy<return_by_value> return_by_value_t;
40+
typedef return_internal_reference<> return_by_internal_reference_t;
41+
class_<X>("X", init<int>() )
42+
//defining read only property
43+
.add_property( "value_r", &X::get_value )
44+
//defining read \ write property
45+
.add_property( "value_rw", &X::get_value, &X::set_value )
46+
//defining read \ write property using make_getter and make_setter
47+
.add_property( "value_direct",
48+
make_getter( &X::m_value, return_by_value_t() ),
49+
make_setter( &X::m_value, return_by_internal_reference_t() ) )
50+
//defining read only property for static member
51+
.add_static_property( "instance_count", &X::get_instance_count )
52+
//defining read \ write property for static member using make_getter and make_setter
53+
.add_static_property( "instance_count_direct",
54+
make_getter( &X::s_count, return_by_value_t() ),
55+
make_setter( &X::s_count, return_by_internal_reference_t() ) )
56+
//defining class property using a global function
57+
.add_static_property( "instance_count_injected", &get_X_instance_count );
58+
}
59+
60+
#include "module_tail.cpp"

test/properties.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
This is test module for properies.
3+
4+
>>> X = properties.X
5+
6+
>>> x1 = X(1)
7+
8+
value read only
9+
>>> x1.value_r
10+
1
11+
12+
value read - write
13+
>>> x1.value_rw
14+
1
15+
16+
value direct access
17+
>>> x1.value_direct
18+
1
19+
20+
class instance count read - only
21+
>>> X.instance_count
22+
1
23+
24+
class instance count direct
25+
>>> X.instance_count_direct
26+
1
27+
28+
class instance count injected
29+
>>> X.instance_count_injected
30+
1
31+
32+
class instance count from object
33+
>>> x1.instance_count
34+
1
35+
36+
class instance count from object
37+
>>> x1.instance_count_direct
38+
1
39+
40+
class instance count from object:
41+
>>> x1.instance_count_injected
42+
1
43+
44+
as expected you can't assign new value to read only property
45+
>>> x1.value_r = 2
46+
Traceback (most recent call last):
47+
File "properties.py", line 49, in ?
48+
x1.value_r = 2
49+
AttributeError: can't set attribute
50+
51+
setting value_rw to 2. value_direct:
52+
>>> x1.value_rw = 2
53+
>>> x1.value_rw
54+
2
55+
56+
setting value_direct to 3. value_direct:
57+
>>> x1.value_direct = 3
58+
>>> x1.value_direct
59+
3
60+
61+
>>> assert x1.value_r == 3
62+
63+
>>> x2 = X(2)
64+
65+
after creating second intstance of X instances count is 2
66+
>>> x2.instance_count
67+
2
68+
69+
>>> del x2
70+
>>> assert x1.instance_count == 1
71+
"""
72+
73+
#import sys; sys.path.append(r'P:\Actimize4.0\smart_const\py_smart_const___Win32_Debug')
74+
import properties_ext as properties
75+
76+
77+
def run(args = None):
78+
import sys
79+
import doctest
80+
81+
if args is not None:
82+
sys.argv = args
83+
return doctest.testmod(sys.modules.get(__name__))
84+
85+
if __name__ == '__main__':
86+
print "running..."
87+
import sys
88+
sys.exit(run()[0])

0 commit comments

Comments
 (0)