RFC: patch statement
Dejan Lekic via Digitalmars-d
digitalmars-d at puremagic.com
Mon Apr 3 04:16:57 PDT 2017
I know people her do not like to see proposals that change (add
stuff to) the language. However, I strongly feel that for the
testing purposes D should provide means to patch any object (no
matter whether it is final or not!). Therefore I wonder what
people think of adding a `patch(obj) {}` or perhaps change the
semantics of the `with(obj) {}` so unittest writers can modify
the object and set values.
The patch keyword would work ONLY inside unittest {} blocks AND
inside functions annotated with @test annotation.
Imagine we have:
int myFun(Person person) { /* some logic here */ }
unittest {
auto p = new Person() /* does not really matter which
constructor we use */
patch(p) {
// here we can modify ANY attribute, no matter whether it is
private or public
p.fname = "Nikola"
p.sname = "Tesla"
}
auto res = myFun(p)
// do some assertions here
}
Similarly:
@test
void test_myFun() {
// same code as in the unittest above.
}
I do not even know if patch() {} statement is even possible, that
is the whole point of writing this, so people can enlighten me...
:)
As I said in the introduction paragraph, for this purpose the
semantics of the with statement could be changed, but I prefer a
different keyword (patch) to be honest.
More information about the Digitalmars-d
mailing list