Live data from Hacker News

What if writing tests was a joyful experience? (2023)

blog.janestreet.com

1–10 of 47 posts

Re: What if writing tests was a joyful experience? (2023)

#4
> You start writing assert fibonacci(15) == ... and already you’re forced to think. What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t?

Um …duh? Get out a calculator. Consult a reference, etc. Otherwise compute the result, and ensure you've done that correctly, ideally as independent of the code under test as possible. A lot of even mathematical stuff has "test vectors"; e.g., the SHA algorithms.

> Here’s how you’d do it with an expect test:

  printf "%d" (fibonacci 15);
  [%expect {||}]
> The %expect block starts out blank precisely because you don’t know what to expect. You let the computer figure it out for you. In our setup, you don’t just get a build failure telling you that you want 610 instead of a blank string. You get a diff showing you the exact change you’d need to make to your file to make this test pass; and with a keybinding you can “accept” that diff. The Emacs buffer you’re in will literally be overwritten in place with the new contents:

…you're kidding me. This is "fix the current state of the function — whether correct or not — as the expected output."

Yeah… no kidding that's easier.

We gloss over errors — "some things just looked incorrect" — well, but how do you know that any differently than fib(10)?

Re: What if writing tests was a joyful experience? (2023)

#7

> You start writing assert fibonacci(15) == ... and already you’re forced to think. What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t? Um …duh? Get out a calculator. Consult a reference, etc. Otherwise compute the result, and ensure you've done that correctly , ideally as independent of the code under test as possible. A lot of even mathematical stuff has "test vec…

A lot of tests are designed as regression prevention. You know the system is working as designed, but what if somebody comes along and changes the Fibonacci function to compute much more efficiently (and, in the process, makes some arithmetic errors?).

Re: What if writing tests was a joyful experience? (2023)

#8

> You start writing assert fibonacci(15) == ... and already you’re forced to think. What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t? Um …duh? Get out a calculator. Consult a reference, etc. Otherwise compute the result, and ensure you've done that correctly , ideally as independent of the code under test as possible. A lot of even mathematical stuff has "test vec…

It is called snapshot testing, very valid technique. Maybe not best suited to a mathematical function like they have here, but I have found it useful for stuff like compilers asserting on the AST, where it would be a pain to write out and assert on the output and may also change shape.

Re: What if writing tests was a joyful experience? (2023)

#10
Amazing to see Jane Street uses Emacs. And property-based testing too.

> you don’t just get a build failure telling you that you want 610 instead of a blank string

So I had to scratch my head a bit because I was thinking: "Wait, the whole point is that you don't know whether what you're testing is correct or not, so how can you rely on that as input to your tests!?".

But even though I didn't understand everything they do yet I do see at least a big case where it makes lots of sense. And it happens to be a case where a lot of people see the benefits of test: before refactoring.

> What does fibonacci(15) equal? If you already know, terrific—but what are you meant to do if you don’t?

Yeah a common one is reuse a function in the same language which you believe is correct (you probably haven't proven it to be correct). Another typical one is you reuse a similar function from another language (once again, it's probably not been proven it is correct). But if two implementation differ, you know you have an issue.

> let d = create_marketdata_processor () in > ( Do some preprocessing to define the symbol with id=1 as "APPL" )

Typo. It's AAPL, not APPL. It's correctly used as AAPL later on.

FWIW writing tests better become a joyful experience for we're going to need a lot* of these with all our AI generated code.

Post reply on HN