[TDD] How to test output

Hi.

At the moment, I’m writing a new View layer implementation, which will incorporate all features I expect a View to have (e.g. template-parsing, partials, layouts, slots, helpers, etc.), but I’m having an issue. I’m writing my code using TDD and although that has always helped my in the past, it’s getting an annoying job on this particular set of objects.

When you’re testing whether or not something rendered correctly, you’ll simply compare two strings: the one you expect and the one that is actually rendered. The problem though, lies in the fact that this is extremely error-prone: it may have rendered correctly, but if there is an extra space, the tests will fail, reporting that my code doesn’t work, and righteously so.

So, I guess what I’m asking is, does someone know of a less delicate and error-prone way of testing multiple lines of output are the ones you’d expect?

Normalise both strings then compare? Or, define a ‘tolerance’ for the variance ?

Hi. While writing this very post, I actually thought of that. If you can strip newlines and additional spaces, that would be of an incredible help. I’ll start hacking away at such a method, thanks for the input!

Liar, you’re just ‘nicking’ my incredible idea. :stuck_out_tongue:

Quite possibly. Guess we’ll never know huh? I’ll put you in the header of the file, stating that it’s your idea, okay? :slight_smile:

Hi…

SimpleTest’s WebTestCase::assertText() normalises the text before comparison. All whitespace is reduced to a single space, HTML entities are decoded, etc. This is partly because the assertion text is human written, but it’s mainly to solve the problem you’ve just outlined.

yours, Marcus

Hi Marcus,

I’ve failed to mention I’m using PHPUnit instead of SimpleTest. You’d think PHPUnit has a method which does something similar as well, but I haven’t found it up until now.

Cheers,

Berry

Hi…

I figured. Feel free to nick the code though. It’s not too correct with Unicode and character sets, but as you fully control the test environment that shouldn’t be an issue.

yours, Marcus

I agree. I’ll see what I can use and let you all know once I’ve made some progress. Thanks.

Cheers,

Berry