@@ -41,3 +41,85 @@ py_class!(class Particle |py| {
41
41
Ok ( py. None ( ) )
42
42
}
43
43
} ) ;
44
+
45
+ #[ cfg( test) ]
46
+ mod tests {
47
+ mod rust {
48
+ use cpython:: Python ;
49
+ use super :: super :: Particle ;
50
+
51
+ #[ test]
52
+ fn name ( ) {
53
+ let gil = Python :: acquire_gil ( ) ;
54
+ let py = gil. python ( ) ;
55
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
56
+ assert ! ( particle. name( py) . unwrap( ) == "He" ) ;
57
+ particle. set_name ( py, "Kr" ) . unwrap ( ) ;
58
+ assert ! ( particle. name( py) . unwrap( ) == "Kr" ) ;
59
+ }
60
+
61
+ #[ test]
62
+ fn mass ( ) {
63
+ let gil = Python :: acquire_gil ( ) ;
64
+ let py = gil. python ( ) ;
65
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
66
+ assert ! ( particle. mass( py) . unwrap( ) == 4.0026021003723145 ) ;
67
+ particle. set_mass ( py, 42.0 ) . unwrap ( ) ;
68
+ assert ! ( particle. mass( py) . unwrap( ) == 42.0 ) ;
69
+ }
70
+
71
+ #[ test]
72
+ fn charge ( ) {
73
+ let gil = Python :: acquire_gil ( ) ;
74
+ let py = gil. python ( ) ;
75
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
76
+ assert ! ( particle. charge( py) . unwrap( ) == 0.0 ) ;
77
+ particle. set_charge ( py, 2.0 ) . unwrap ( ) ;
78
+ assert ! ( particle. charge( py) . unwrap( ) == 2.0 ) ;
79
+ }
80
+ }
81
+
82
+ mod python {
83
+ use cpython:: Python ;
84
+ use super :: super :: Particle ;
85
+
86
+ #[ test]
87
+ fn name ( ) {
88
+ let gil = Python :: acquire_gil ( ) ;
89
+ let py = gil. python ( ) ;
90
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
91
+
92
+ py_run_with ! ( py, particle;
93
+ "assert particle.name() == 'He'" ,
94
+ "particle.set_name('Kr')" ,
95
+ "assert particle.name() == 'Kr'"
96
+ ) ;
97
+ }
98
+
99
+ #[ test]
100
+ fn mass ( ) {
101
+ let gil = Python :: acquire_gil ( ) ;
102
+ let py = gil. python ( ) ;
103
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
104
+
105
+ py_run_with ! ( py, particle;
106
+ "assert particle.mass() == 4.0026021003723145" ,
107
+ "particle.set_mass(33)" ,
108
+ "assert particle.mass() == 33"
109
+ ) ;
110
+ }
111
+
112
+ #[ test]
113
+ fn charge ( ) {
114
+ let gil = Python :: acquire_gil ( ) ;
115
+ let py = gil. python ( ) ;
116
+ let particle = create_instance ! ( py, Particle , ( "He" , ) ) ;
117
+
118
+ py_run_with ! ( py, particle;
119
+ "assert particle.charge() == 0.0" ,
120
+ "particle.set_charge(2)" ,
121
+ "assert particle.charge() == 2"
122
+ ) ;
123
+ }
124
+ }
125
+ }
0 commit comments