RU beehive logo ITEC dept promo banner
ITEC 325
2020fall
ibarland

intro functions and unit tests
in php

Due Sep.01 (Tue) 23:59
Submit on D2L.
For this homework, you'll submit three files: utils.php, utils-test.php, and silly.php. The files silly.php does not need to be viewable on the web.

Notes:

  1. (10pts) Write a php function pluralize which takes in any number, and a singular noun, and returns a user-friendly version of that number-and-noun1. For example, pluralize( 7, "t-shirt" ) === "7 t-shirts". We might use this function on future web-pages, when (say) confirming somebody's order, or generating a message about the current tempurature: the number might well come from the database, and we want to make sure we create a message with correct-grammar, for any possible number.
    1. Be sure to include adequate test cases, each covering a different situation.
      The provided string will always be a singular-noun (in particular, it will never be the empty string), so you don't need to check for that. But what other corner-cases should you handle? Hint: I think there are at least five plausible situations to consider.
    2. You can presume that a plural is always formed by just adding “s” to any word.
      (A small amount of extra credit: incorporate a few more-correct rules about how plurals are formed in English.
      You can also be thankful that we're not writing this function for Finnish, or Welsh, or Arabic, or Turkish, or ….)
    3. Note that you can include test-cases that don't pass — e.g. the expected output for pluralize(4,"goose") must be "4 geese"; don't brain-damage tests just so they appear to pass! (Admittedly, better term than “expected output” would be “desired output”.)
      But you don't need write a comprehensive set of tests for this problem, fortunately.
    4. You do not need to check for being given a non-number for the first argument (or, not a singular-noun for the second); that's what a type system would check/guarantee (and in a big project, you might add type-annotations and run-time type-checking, if you valued a consistent approach to correctness).
  2. (10pts) Write a function string hyperlink( string $url, /*string-or-boolean*/ $linkTxt ): string which takes in a string $url and a string-or-false $linkTxt, and returns a string of html for a hyperlink to $url. If the provided $linkTxt is false (instead of a string), then use the $url as the link-text as well.
    hyperlink( "http://www.gutenberg.org", "free books!" ) === "<a href='http://www.gutenberg.org'>free books!</a>"
    hyperlink( "myLocalFile.html", false ) === "<a href='myLocalFile.html'>myLocalFile.html</a>"

    Note that this is just a regular ol' function that takes in strings and returns strings!
    The fact that the strings happen to have angle-brackets in them means that the result might be useful to people generating html, but the function itself doesn't really care.

    (You don't need to come up with additional test cases, but you do need to actually include these tests or their equivalent. You can assume that neither string is ever empty.)

    Note how the input strings don't contain any quote-characters, but the returned result does (since HTML attributes must be quoted). I recommend that your generated html uses single quote marks around attributes, but you'll get full credit for either single- or double-quote marks, as long as it is valid HTML. Make sure your test case(s) express exactly what the ideal, desired result is!

  3. (10pts) Thumbnails:
    1. What is the html element for an image with source http://imgur.com/Qv2wl56.png that is styled to display 300px wide? (Use an inline style attribute.)
      Include your answer in a comment, next to your test-case (from part (c) below).
    2. What is the html for such an image which is also a link — where clicking the image takes you to that image-file (not an html page — just that image-file).
      Include your answer in a comment, next to your test-case (from part (c) below).
    3. Turn your previous answer into a (runnable) test-case for thumbnail("http://imgur.com/Qv2wl56.png",300); the expected-output should be a string that happened to be your answer.
      You don't need further test-cases for this problem.

      Do, however, pay close attention to the exact spacing you want to have. I recommend using single-quotes (') around the attribute-values. (We'll discuss this more in week2b's lecture.)

    4. Finally, implement the function thumbnail that takes in a string (a URL) and a number (the width in pixels), and returns a string that is an html link containing an image2.

      For full credit, your function should call hyperlink as already written!

  4. (5pts) Move your test-cases for these functions to a file named “utils-test.php”, leaving the definitions in “utils.php”. (You'll of course use require_once as in lecture, so that utils-test.php is able to get the definitions in utils.php.)
  5. (5pts) Make a file silly.php which: Prints a web-page. A silly, exceedingly short web-page.
    1. It must call pluralize, hyperlink, and thumbnail somewhere.
    2. It should contain “<?phpexactly once at the very top, end with “?>”, and be filled with echo statements in between.
    3. It should print proper (x)html(5), as one of the standard-requirements for this class. Mostly, that means making sure every tag closed (including br and p tags), and any attributes should have a value which is enclosed in quotes).
    4. Yes, this is exactly like what we did in lecture, with sample.php (and our function blend which we put inside utils.php next to utils-test.php).
    5. No, this program is not going to be invoked/run via the web. It's simply a regular program that happens to print (just like a hello-world), except that what it prints happens to have some angle-brackets in it.
      If you like: You can run your program, and to make sure what it printed is valid xhtml, you can redirect (or, paste) your output into a file, and then open that local-file via a browser's File » Open…. But observe that in this process, we are not actually accessing/using the web at all.

1 Since the noun may or may not be actually get pluralized, depending on the provided number, some students don't like the name “pluralize”. In that case, you are free to name it “numberAndNounWhereTheNounAgreesWithTheNumberWhetherPluralOrSingularOrWhatever” instead. In general though, it's not uncommon to say that the general act of correctly-pluralizing includes specific “trivial” cases, in the same way “paying taxes” can plausibly be interepreted to include the case where one pays zero taxes (and hence has completed the task trivially), and if the police tell all the partiers to “go home”, the host might be in compliance trivially even though they didn't go anywhere.

Let me know if you have a better name for the function; an ibuck awarded if I accept it.

     
2 Since the width can be as big as you like, sometimes students don't think of a (say) 300px-wide image as a thumbnail. In that case, you are free to name it “imageOfGivenWidthWhichWhenClickedOnGivesYouTheOriginalFullSizeImage” instead. In general though, it's not unreasonable to name a task after its most common use-case, and also realize that it can be used in mildly-counterintuitive ways as well.

Let me know if you have a better name for the function; an ibuck awarded if I accept it.

     

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.