We're long overdue for a "D is awesome" post

H. S. Teoh hsteoh at qfbox.info
Wed May 31 00:05:22 UTC 2023


On Tue, May 30, 2023 at 04:27:05PM -0700, Walter Bright via Digitalmars-d wrote:
> Excellent! The technique you're exhibiting is called "mocking". It
> works especially well when a function's inputs and outputs are
> properly parameterized.

Yes, it's an age-old technique since at least the days of Java.

What makes it stand out in this particular case, though, is D's ability
to let you mock references to global (module-level) symbols with just a
1-line change.  To mock the top-level `System` in Java, for example, you
have to do some arcane black magic with the help of an external package;
it's not something for mere mortals or the faint of heart to do by hand.
In D, you add a single template parameter to your function, and you're
all set to go.  Now *that's* power.


> When I fixed the D lexer.d to not access any globals, I was able to
> simplify the unittests for it by mocking up the inputs and outputs.
> For example, instead of having to use the global "gagging" switch to
> suppress error messages, I used a mock error handler that just did
> nothing.

Good to hear.  Error gagging is the source of some of the most
irritating D compiler bugs in the past; the less it gets used, the
better IMNSHO.


T

-- 
Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard


More information about the Digitalmars-d mailing list