Factorize reference checking into a single class

And add more tests, and more tests.

Signed-off-by: Francis Galiegue <fgaliegue@gmail.com>