Refactor to get assertTrue
As we are also programmers we will soon recognize that there is code duplication in our test script and will factor out the whole printing of pass/fail part to a function that we call assertTrue(). It should receive a true or false value and it will print "pass" or "fail" accordingly.
examples/php/intro/t04.php
<?php require_once(dirname(__FILE__) . '/../includes/mylib.php'); assertTrue(add(1,1) == 2); assertTrue(add(2,2) == 4); assertTrue(add(1,1,1) == 3); function assertTrue($condition) { print ($condition ? 'pass' : 'fail') . '<br>'; } ?>
Result:
pass pass fail
As in every refactoring the functionality and the output should remain the same, just the implementation improves. (Hopefully)