test-env-replace: assert results instead of printing them

Also code cleanup and split into functions in preparation for more tests
to come.