Simplify test cases where both __*__ and __r*__ are defined
This commit is contained in:
parent
3e958bf607
commit
8cbfdd52b8
|
@ -280,124 +280,16 @@ fn rhs_arithmetic() {
|
|||
}
|
||||
|
||||
#[pyclass]
|
||||
struct LhsOverridesRhs {}
|
||||
struct LhsAndRhs {}
|
||||
|
||||
#[pyproto]
|
||||
impl PyNumberProtocol for LhsOverridesRhs {
|
||||
fn __add__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} + {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __sub__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} - {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __mul__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} * {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __lshift__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} << {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __rshift__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} >> {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __and__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} & {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __xor__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} ^ {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __or__(lhs: &PyAny, rhs: &PyAny) -> String {
|
||||
format!("{:?} | {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __pow__(lhs: &PyAny, rhs: &PyAny, _mod: Option<&PyAny>) -> String {
|
||||
format!("{:?} ** {:?}", lhs, rhs)
|
||||
}
|
||||
|
||||
fn __radd__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} + RA", other)
|
||||
}
|
||||
|
||||
fn __rsub__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} - RA", other)
|
||||
}
|
||||
|
||||
fn __rmul__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} * RA", other)
|
||||
}
|
||||
|
||||
fn __rlshift__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} << RA", other)
|
||||
}
|
||||
|
||||
fn __rrshift__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} >> RA", other)
|
||||
}
|
||||
|
||||
fn __rand__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} & RA", other)
|
||||
}
|
||||
|
||||
fn __rxor__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} ^ RA", other)
|
||||
}
|
||||
|
||||
fn __ror__(&self, other: &PyAny) -> String {
|
||||
format!("{:?} | RA", other)
|
||||
}
|
||||
|
||||
fn __rpow__(&self, other: &PyAny, _mod: Option<&'p PyAny>) -> String {
|
||||
format!("{:?} ** RA", other)
|
||||
impl std::fmt::Debug for LhsAndRhs {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "LR")
|
||||
}
|
||||
}
|
||||
|
||||
#[pyproto]
|
||||
impl PyObjectProtocol for LhsOverridesRhs {
|
||||
fn __repr__(&self) -> &'static str {
|
||||
"BA"
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lhs_overrides_rhs() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let c = PyCell::new(py, LhsOverridesRhs {}).unwrap();
|
||||
// Not overrided
|
||||
py_run!(py, c, "assert c.__radd__(1) == '1 + RA'");
|
||||
py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'");
|
||||
py_run!(py, c, "assert c.__rmul__(1) == '1 * RA'");
|
||||
py_run!(py, c, "assert c.__rlshift__(1) == '1 << RA'");
|
||||
py_run!(py, c, "assert c.__rrshift__(1) == '1 >> RA'");
|
||||
py_run!(py, c, "assert c.__rand__(1) == '1 & RA'");
|
||||
py_run!(py, c, "assert c.__rxor__(1) == '1 ^ RA'");
|
||||
py_run!(py, c, "assert c.__ror__(1) == '1 | RA'");
|
||||
py_run!(py, c, "assert c.__rpow__(1) == '1 ** RA'");
|
||||
// Overrided
|
||||
py_run!(py, c, "assert 1 + c == '1 + BA'");
|
||||
py_run!(py, c, "assert 1 - c == '1 - BA'");
|
||||
py_run!(py, c, "assert 1 * c == '1 * BA'");
|
||||
py_run!(py, c, "assert 1 << c == '1 << BA'");
|
||||
py_run!(py, c, "assert 1 >> c == '1 >> BA'");
|
||||
py_run!(py, c, "assert 1 & c == '1 & BA'");
|
||||
py_run!(py, c, "assert 1 ^ c == '1 ^ BA'");
|
||||
py_run!(py, c, "assert 1 | c == '1 | BA'");
|
||||
py_run!(py, c, "assert 1 ** c == '1 ** BA'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[derive(Debug)]
|
||||
struct LhsFellbackToRhs {}
|
||||
|
||||
#[pyproto]
|
||||
impl PyNumberProtocol for LhsFellbackToRhs {
|
||||
impl PyNumberProtocol for LhsAndRhs {
|
||||
fn __add__(lhs: PyRef<Self>, rhs: &PyAny) -> String {
|
||||
format!("{:?} + {:?}", lhs, rhs)
|
||||
}
|
||||
|
@ -480,7 +372,7 @@ impl PyNumberProtocol for LhsFellbackToRhs {
|
|||
}
|
||||
|
||||
#[pyproto]
|
||||
impl PyObjectProtocol for LhsFellbackToRhs {
|
||||
impl PyObjectProtocol for LhsAndRhs {
|
||||
fn __repr__(&self) -> &'static str {
|
||||
"BA"
|
||||
}
|
||||
|
@ -491,8 +383,19 @@ fn lhs_fellback_to_rhs() {
|
|||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
let c = PyCell::new(py, LhsFellbackToRhs {}).unwrap();
|
||||
// Fallbacked to RHS because of type mismatching
|
||||
let c = PyCell::new(py, LhsAndRhs {}).unwrap();
|
||||
// If the light hand value is `LhsAndRhs`, LHS is used.
|
||||
py_run!(py, c, "assert c + 1 == 'LR + 1'");
|
||||
py_run!(py, c, "assert c - 1 == 'LR - 1'");
|
||||
py_run!(py, c, "assert c * 1 == 'LR * 1'");
|
||||
py_run!(py, c, "assert c << 1 == 'LR << 1'");
|
||||
py_run!(py, c, "assert c >> 1 == 'LR >> 1'");
|
||||
py_run!(py, c, "assert c & 1 == 'LR & 1'");
|
||||
py_run!(py, c, "assert c ^ 1 == 'LR ^ 1'");
|
||||
py_run!(py, c, "assert c | 1 == 'LR | 1'");
|
||||
py_run!(py, c, "assert c ** 1 == 'LR ** 1'");
|
||||
py_run!(py, c, "assert c @ 1 == 'LR @ 1'");
|
||||
// Fellback to RHS because of type mismatching
|
||||
py_run!(py, c, "assert 1 + c == '1 + RA'");
|
||||
py_run!(py, c, "assert 1 - c == '1 - RA'");
|
||||
py_run!(py, c, "assert 1 * c == '1 * RA'");
|
||||
|
@ -502,6 +405,7 @@ fn lhs_fellback_to_rhs() {
|
|||
py_run!(py, c, "assert 1 ^ c == '1 ^ RA'");
|
||||
py_run!(py, c, "assert 1 | c == '1 | RA'");
|
||||
py_run!(py, c, "assert 1 ** c == '1 ** RA'");
|
||||
py_run!(py, c, "assert 1 @ c == '1 @ RA'");
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
|
|
Loading…
Reference in a new issue