Unittest Absurdity
jfondren
julian.fondren at gmail.com
Fri Aug 5 02:06:05 UTC 2022
On Friday, 5 August 2022 at 01:53:42 UTC, Ruby The Roobster wrote:
>>>> On Friday, 5 August 2022 at 01:38:48 UTC, jfondren wrote:
>>>>
>>>> Here's a complete example that passes tests:
>>>>
>>>> ```d
>>>> struct S {
>>>> int n;
>>>> void opOpAssign(string op)(S rhs) if (op == "/") {
>>>> n++;
>>>> }
>>>> }
>
> Nevermind. I have to remove the trailing equals sign.
ah, so you had `(op == "/=")` instead?
I think this is a problem actually, maybe arguably not a bug, but
not desirable. Consider:
```d
struct S {
int n;
void opOpAssign(string op)(S rhs) if (op == "/=") {
n++;
}
void opOpAssign(string op)(S rhs) if (op == "/") {
// do nothing
}
}
unittest {
auto a = S(1), b = S(2);
a /= b;
b /= a;
assert(a.n == 2);
assert(b.n == 3);
}
```
The test fails because the second definition is used (preventing
a "none of the overlords are callable" error) and the first
definition never matches because it's wrong. I say this arguably
isn't a bug because it can still be used:
```d
a.opOpAssign!"/="(b);
b.opOpAssign!"/="(a);
```
but what's actually wanted is operator overloading and a mistake
at the definition is silently allowed.
More information about the Digitalmars-d-learn
mailing list