UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ sample2

1. Previous JS Quiz

1.1. A scary Halloween story

Young Brendan decides to help Uncle Escher organize the family candy store for the busy Halloween season. The storeroom is full of different sized boxes, each of which contains packages of candy, other storage boxes, or sometimes nothing at all. Brendan decides to write some JavaScript to help track the inventory. Given an example like Figure 1, Brendan wants to be able to calculate that there is a total of 525g of Smarties™ in the box.

./boxes.pdf

1.2. Questions

1.2.1. Candy class

(2 Marks) Make a class Candy that has properties brand and grams describing the brand name and size of the package respectively. Your class should pass the following tests.

let Candy=require("../candy.js").Candy;
describe("candy",
         function() {
             let treat = new Candy("M&M", 25);
             it("brand", () => expect(treat.brand).toBe("M&M"));
             it("grams", () => expect(treat.grams).toBe(25));
         });

1.2.2. Boxes class

(7 Marks) Write a class Box representing candy storage boxes. The Box class has a constructor that accepts any number of arguments, each of which is either a Box object or a Candy object. Box objects support a total method that calculates the total weight of candy of a given brand contained in the box. Your class should pass the following tests, but also work for general Box input. Hint: You might find instanceof useful to tell the difference between Box objects and Candy objects.

let Box=require("../box.js").Box;
describe(
  "candy in boxes in boxes",
  function () {
    let inner = new Box (new Box (new Candy("Smarties", 500)),
                         new Box (new Box(new Candy("M&M", 100))),
                         new Candy("Smarties", 25));
    let box = new Box (new Box(new Candy("O Henry", 80)), inner);

    it("single match",
       () => expect(box.total("M&M")).toEqual(100));

    it("multi match",
       () => expect(box.total("Smarties")).toEqual(525));

    it("recursive non matching is 0",
       () => expect(box.total("Wasabi Peas")).toEqual(0));
  });

1.2.3. Testing

(2 Marks)

  • As usual, you should make sure you have complete test coverage using nyc jasmine; depending on your solution you may need to add extra tests.
  • Add two jasmine tests (at least one for the Box class) that test something not tested by the given tests. Explain what you are testing.

2. Previous Final Exam JS question

(7 marks) As we all know, the most important metric of success in life is how many ``friends’’ we have on social media. In this question you will write a function to help people keep track of this important number. SurveillanceCaptitalism Inc.\ has a handy API that returns a list of all of the ``friend’’ relationships on all of their platforms, including FriendFace, InstaGrief, and TackyTicky. Unfortunately this data is a bit unwieldy for the status conscious user that wants to quickly know how many friends they have. Your task is write a JavaScript class Friends that takes JavaScript objects with the two properties ``person’’ and ``hasFriend’’, and allows queries with a method count to count the total number of friends one user has. Your class should

  • pass the following tests, and
  • have complete test coverage (add tests if needed).

You may assume that no duplicate objects (i.e. objects with corresponding properties equal) are passed to the Friends constructor.

let Friends=require("../friends.js").Friends;

describe("friends",
    function (){
        let friends=new Friends({"person": "Alice", "hasFriend": "Santa"},
                                {"person": "Mallory", "hasFriend": "Bob"},
                                {"person": "Alice", "hasFriend": "Mallory"});
        let alice_friends = friends["Alice"];
        alice_friends.sort();

        it("not null", () => expect(friends).not.toEqual(null));
        it("property non-existent",
           () => expect(friends["Gollum"]).toEqual(undefined));
        it("property value",
           () => expect(alice_friends).toEqual(["Mallory","Santa"]));
        it("friend count, non-existent",
           () => expect(friends.count("Gollum")).toBe(0));
        it("friend count, 1",
           () => expect(friends.count("Mallory")).toBe(1));
        it("friend count, 2",
           () => expect(friends.count("Alice")).toBe(2));
    });