Skip to content

Commit a82089b

Browse files
author
Guillaume Fraux
committed
Add tests for the Particle class
1 parent d777f4e commit a82089b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
extern crate cpython;
33
extern crate lumol;
44

5+
#[macro_use]
6+
mod testing;
57
mod systems;
68

79
py_module_initializer!(lumol, initlumol, PyInit_lumol, |py, m| {

src/systems/particle.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,85 @@ py_class!(class Particle |py| {
4141
Ok(py.None())
4242
}
4343
});
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+
}

src/testing.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
macro_rules! create_instance {
2+
($py: ident, $Type: ty, $args: expr) => ({
3+
let class = $py.get_type::<$Type>();
4+
let instance: $Type = class.call($py, $args, None).unwrap().extract($py).unwrap();
5+
instance
6+
});
7+
}
8+
9+
macro_rules! py_run_with {
10+
($py: ident, $obj: ident; $($code: expr),+) => ({
11+
use cpython::PyDict;
12+
let dict = PyDict::new($py);
13+
dict.set_item($py, stringify!($obj), $obj).unwrap();
14+
py_run!($py, dict, $($code),+);
15+
});
16+
}
17+
18+
macro_rules! py_run {
19+
($py: ident, $dict: ident, $code: expr) => ({
20+
$py.run($code, None, Some(&$dict)).expect($code);
21+
});
22+
($py: ident, $dict: ident, $code: expr, $($tail: expr),+) => ({
23+
$py.run($code, None, Some(&$dict)).expect($code);
24+
py_run!($py, $dict, $($tail),+);
25+
});
26+
}

0 commit comments

Comments
 (0)