Add tests for nil/NULL handling
More documentation of behavior and upstream spec

Also refactor the private method '.escape' so that we
can better describe its function in terms of the spec.
This commit should have no effect on behavior.