Watch out for testing against multiline strings

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the testing category.

Last Updated: 2024-04-25

I had frustrating test breakages after reformatting some HTML emails.

Previously they were supposed to match on this text:

"is GBP 39.95, but we applied a discount of GBP 28.54".

Here is relevant part of the render

+  "  The regular price of Biology Notes is GBP 39.95,\r\n" +
   +  "  but we applied a discount of GBP 28.54 to secure you a sale.\r\n" +

I first thought of using tr("\n", "") to get rid of the newlines and make it easier to match.

Unfortunately this led to white space difficulties. So I got more fancy, and turned any sequence of white space or new lines into a single white space. This worked.

gsub(/[\n\r\s]+/, " ")

This is only a half-way good solution. It would have been wiser to test against a much small substring and avoid this complexity.

Lesson:

Watch out for unexpected multi-line strings (e.g. in email testing) and how they introduce newline characters.

Match against smaller stings to make life easier.