| 1265 | |
| 1266 | #[test] |
| 1267 | fn properties() { |
| 1268 | let gil = Python::acquire_gil(); |
| 1269 | let py = gil.python(); |
| 1270 | |
| 1271 | let c = Properties::create_instance( |
| 1272 | py, |
| 1273 | Cell::new(0), |
| 1274 | RefCell::new(String::new()), |
| 1275 | RefCell::new(String::new()), |
| 1276 | ) |
| 1277 | .unwrap(); |
| 1278 | |
| 1279 | py_run!( |
| 1280 | py, |
| 1281 | c, |
| 1282 | "assert 'docs for match' in c.__class__.match.__doc__" |
| 1283 | ); |
| 1284 | |
| 1285 | py_run!(py, c, "assert c.prop == 0"); |
| 1286 | py_run!(py, c, "assert not c.match"); |
| 1287 | py_run!(py, c, "c.prop = 42"); |
| 1288 | assert_eq!(c.value(py).get(), 42); |
| 1289 | py_run!(py, c, "assert c.match"); |
| 1290 | assert!(c.r#match(py).unwrap()); |
| 1291 | |
| 1292 | // Instead of really deleting, our setter sets back to 0 |
| 1293 | py_run!(py, c, "delattr(c, 'prop')"); |
| 1294 | py_run!(py, c, "assert c.prop == 0"); |
| 1295 | |
| 1296 | py_run!(py, c, "c.prop_by_ref = 'testing'"); |
| 1297 | py_run!(py, c, "assert c.prop_by_ref == 'testing'"); |
| 1298 | |
| 1299 | py_run!(py, c, "c.prop_by_opt_ref = 'something'"); |
| 1300 | assert_eq!(*c.value_by_opt_ref(py).borrow(), "something"); |
| 1301 | py_run!(py, c, "c.prop_by_opt_ref = None"); |
| 1302 | py_run!(py, c, "repr(c) == 'P(42, \"testing\" \"NO VALUE\")'"); |
| 1303 | |
| 1304 | py_run!(py, c, "del c.prop_by_opt_ref"); |
| 1305 | py_run!(py, c, "repr(c) == 'P(42, \"testing\" \"DELETED\")'"); |
| 1306 | } |
| 1307 | |
| 1308 | py_class!(pub(crate) class ClassWithVisibility |py| { |
| 1309 | pub(crate) def __new__(_cls) -> PyResult<Self> { |