[WIP] Swift unit tests

Unit tests for #1382, plus some fixes to bugs that the tests exposed. :wink: