Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 10 Hide and Seek test (HouseTests) #39

Open
DarwinsCorn opened this issue Feb 27, 2022 · 5 comments
Open

Chapter 10 Hide and Seek test (HouseTests) #39

DarwinsCorn opened this issue Feb 27, 2022 · 5 comments

Comments

@DarwinsCorn
Copy link

The code below for the test for the House class of the project have the following asserts, for which the the first three seem to be written correctly (they use House.Entry). The ones for kitchen and the rest of the rooms of the house seem to be incorrect as they are defined in the House as "var" within a static constructor and are not field members of the class and thus would not be accessible directly outside it as described in the solution.

Unless I'm missing something let me know.

incorrect (from below):

     var kitchen = hallway.GetExit(Direction.Northwest);
     Assert.AreEqual("Kitchen", kitchen.Name);

correct (what it should be):

     var kitchen = House.Entry.Exits[Direction.NorthWest];
     Assert.AreEqual("Kitchen",kitchen.Name);

(Excerpt from the solution) as written in the addendum project solution online:

    [TestMethod]
    public void TestLayout()
    {
        Assert.AreEqual("Entry", House.Entry.Name);

        var garage = House.Entry.GetExit(Direction.Out);
        Assert.AreEqual("Garage", garage.Name);

        var hallway = House.Entry.GetExit(Direction.East);
        Assert.AreEqual("Hallway", hallway.Name);

        var kitchen = hallway.GetExit(Direction.Northwest);
        Assert.AreEqual("Kitchen", kitchen.Name);

        var bathroom = hallway.GetExit(Direction.North);
        Assert.AreEqual("Bathroom", bathroom.Name);

     ....
    }
@andrewstellman
Copy link
Collaborator

Hi! Thanks so much for reporting this. I'm trying to understand the issue you're raising—I definitely want to fix it if it's a problem, but I'm not sure I get it yet.

You're looking at the code from page 11 of Chapter_10_project.pdf, right? That code definitely works—it's copied and pasted from HouseTests.cs, and I just downloaded and tested that solution, it compiles and all the tests pass. I just checked, and the code is identical. So there doesn't seem to be a syntax error in it. But that doesn't mean there isn't an issue! It might just be that I need to add some notes to the code to explain it a little better, because I could definitely see that it might not be 100% clear at first glance.

The idea with the way that test is structured is that first it's trying to test the layout of the house. It starts with the entry, using House.Entry, and makes sure it's correct by checking its Name. Then it uses the GetExit that Entry inherits because it extends Location—that's how it gets a reference to the hallway location, and it checks its Name property to make sure it has the right location. Then it uses the hallway object's GetExit to get to the kitchen and the bathroom, and tests their names to make sure they're the right object.

The reason it uses House.Entry to get the reference to the first location but uses GetExit to get to the rest of the locations is because it's navigating throughout the layout of the house, the same way the player will later in the project.

Does that help explain what's going on? If that helps, I'll add that to the PDF to help other readers.

If it's still confusing, I'd love to hear your thoughts! I really want to try to make this project as effective as possible. And the nice thing about PDF downloads is that I can update them immediately, so I definitely want to help out here if I can.

@DarwinsCorn
Copy link
Author

DarwinsCorn commented Feb 27, 2022 via email

@andrewstellman
Copy link
Collaborator

I think maybe you're just overthinking it a bit! Here's how this all works.

It all starts with Entry, a readonly field in the static House class:

public static readonly Location Entry;

That field is set in the House constructor:

Entry = new Location("Entry");

And now Entry points to a Location object. The constructor then creates two new Locations objects:

var hallway = new Location("Hallway"); var livingRoom = new Location("Living Room");

And later it uses Entry.AddExit to add them to the Entry location's exit collection:

Entry.AddExit(Direction.East, hallway); Entry.AddExit(Direction.Out, garage);

It's fine to use var for those declarations, because the static House object doesn't need to keep track of them. It just needs to keep track of the one Location reference, Entry, and then it can use its Exits collection to get to them.

The rest of the constructor just uses var to define variables to reference new locations, because House doesn't need to keep track of any of the other Location objects, either. Any time it needs a Location for a new place in the house, it will look it up in another Location object's Exits collection.

Does that help make sense of it?

@DarwinsCorn
Copy link
Author

DarwinsCorn commented Feb 27, 2022 via email

@andrewstellman
Copy link
Collaborator

I'm glad to help And thanks for explaining what went wrong—that's really useful to me. I'll think about how I might be able to help other readers prevent this in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants