Need for tests
I done refactoring my pet project I used to learn Rust. I started by writing an ugly bed sheet-sized main()
. I debugged it and learned a lot about the graphic/window library I use (piston
). I fixed memory leaks and made performance half-decent. (30 fps for full-screen 2560x1440 repaint is good enough, given my 8 years old Core 2 Quad).
I spend few months (given the sparse free time I got) to refactor it into smaller pieces. The process was super hard and it was the moment to learn Rust for real. There were traits, types, generics, iterators , everything which is the core part of the language. After I finish, I got a reasonably good understanding of most aspects of the language. I shrunk my main
to 17 lines, and ‘drawing’ code into about 20. The rest (to handle window resizes, update requests, multi-threading, etc) is hidden away in ‘lib.rs
’. During that time my ‘drawing’ was just a pseudo-random noise on the screen (which look cool in 30 fps, actually).
Then I decided to go back to my original goal: to render graphs for relations (like sin(x)-y=0
, or sin(1/sin(y) ) + y*sin(x) = sin(x*y)
).
And the first thing I got was a bug. It wasn’t a trivial typo, and it was missed by compiler. It was a true bug. May it was even not in my code, but in the algorithm I invented.
I use my pet project to learn Rust, so, instead of fixing my code and continue, I’m going to write some tests. I think I learned enough Rust to be able to deal with quirks and tricks of test setups. At my initial Rust learning I just glimpsed over testing chapter, now it’s time to do some real-world testing.
Even before staring, I’m sure, that there going to be trivial things (AssertEq!(foo(2), 4)
or something like that), crazy things (when I need to test some function with heavy side effects without causing those effects) and, I suspect, some things which are impossible in Rust. I know that even in Python, with almost infinite ability to patch and mock, not every method in the class is easy to test. I suspect this problem (mock and patch) is much harder for Rust.
(there is no punchline in this post except for this line).