|
Submit on D2L:
utils.php,
utils-test.php,
silly.php.
(You can also provide a css file, if separate.)
The file silly.php
should also be viewable on the web, as discussed below.
(5pts) Write a function test : ANY, ANY → void which determines if the first value (an actual test-result) is equal to the second value (the desired test-result). If the two are not equal, it should print a message saying that a test failed. If the two are equal, it should merely print “.”. (Note that this is the only functions we’ll write all semester, whose purpose is to print something rather than return a value.)
We’ll use this function to make our testing more convenient. For example, your tests from hw01 could be replaced with lines like “test( pluralize(4,"cow"), "4 cows" );”. This way, only tests that fail get printed, and we don’t need to visually scan through lots of output about successful tests — if you see “......” you know you’re good. (And if any test failed, you’ll get an error message blaring at you, that you can’t miss.)
Optional (for slight extra credit): These further features make test even more useful:
Note that rectifying whitespace is particularly helpful when you are comparing test-cases-written-on-a-Windows-system (where a newline uses two bytes), and then running the code on itec-php01.radford.edu (a UNIX machine, where any newlines are one byte). (Alternately, you can also run dos2unix on files that were originally created in Windows.)
Put this function into utils.php -- it is a utility function-definition. It doesn’t go in utils-test.php. because it is not itself testing anything (it’s a function that your unit-tests will call to help them do their job!)
Note: We are not creating HTML for an entire table — merely one row. (Though, we might make a bunch of calls to this function, and splice the results between the string “<table>” and “</table>” … and it might even be a another function with its own loop, that is making the calls to asRow.)
(2pts extra credit) Add an optional second-parameter “firstItemIsAHeader”; if false then the resulting tr won’t contain a leading th. (The parameter’s default value should be true, for compatibility with the above when no second argument is provided. You don’t need to use that exact parameter-name.)
For example, given an array with "id" associated with "main-point" and "class" associated with "unimportant", asAttrs would return3 "id='main-point' class='unimportant'".
We mentioned in lecture how PHP arrays are hash tables. We didn’t mention, though, how to loop over keys&values together. See the 2nd half of the video-in-lecture notes “arrays in php” (actual loop starts at 10m30s).
For all assignments in this class (and all your classes, as appropriate):
You do not need to check for invalid-inputs to your functions. E.g.: if I ask for a function taking in (say) a number and a string, and you repeat in your function-comment that your function handles a number and a string, then you don’t need to check for being given incorrect inputs — that’s the caller’s problem, not yours. Similarly for more refined types like "non-empty-string" or "integer in the range [1,12]": if it’s in the comments as a pre-condition, that’ll suffice.4
(We’ll talk about validating input received from the (external, untrusted) user later.)
function foo( $someNum ) { // optional error-check: if !(is_numeric($someNum)) { throw new InvalidArgumentException( 'foo: expected number, given ' . $someNum ); } // ...now put your real code, uncluttered by further type-checks. } |
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |