Improve some deriving code and add a test The `.stdout` test is particularly useful. r? `@petrochenkov`