Adds tests for tokens.
Adds equality, copy and replace tests; and fixed various related bugs.