After writing the code to support the unit test in part II, beginning in this part we’ll look at the code behind the test to verify that our test are sound and correct.
Where we left off we had just implemented the unit test for the Item class. However, before we can even run the test we have to first write out the methods. So create a new C++ file, title it “database.cpp” and add it to your project’s directory. Now go ahead and type in the following.
For the first method of the Item class we’ll define the method for the Item’s constructor. The Item constructor will just take a single argument for each of its attributes as we defined in the “database.h” file. Here is the Item class definition in the header file again as a refresher.
As you’ll notice there are five attributes which have getter and setter methods while the sixth attribute of the Item class “mAvailiable” will not require getter and setter methods. Thus the constructor should allow a new Item to be created given some initial values for each of the five attributes we mentioned.
Next we’ll overload the assignment operator for the Item class. For this operator overload we just want to the copy one Item’s attributes to another Item’s.
Afterwards we turn our attention to the Item class’ default constructor. The default constructor for the Item should just create a blank Item that will be completely useless but still available for pickup. In addition since the item will not have a name, we can always check for the condition when a Item was accidently created in our game by seeing if the Item’s name is the empty string.
The remaining methods are the getters and setters for the attributes power, vitality, health, name and type. Getters and setters in general for the C++ language are a pair of methods that allow you to write a value to an attribute or read the current value from an attribute. Anyways here they are below.
Lastly we’ll run the test for our Item class; to begin create a new C++ file and title it “main.cpp”. Add the the code below to “main.cpp”. Remember that to use our Test object we have to call the “get_instance()” method since we created Test as a singleton.
Ok, that’s it for today.