Added unit tests for #2029, #2028

This pull request include @hitbra's change + some unit tests for it.