#ifndef ASSIGNMENTSPECIFICATIONTESTS_H_ #define ASSIGNMENTSPECIFICATIONTESTS_H_ #include #include "Student.h" #include "Gradebook.h" // ------------------------------------------------------------------------- /** * This test class shows how one might specify required behavior in an * assignment using a series of test cases. * * @author Stephen Edwards * @version Feb 22, 2005 */ class AssignmentSpecificationTests : public CxxTest::TestSuite { public: //~ Methods ............................................................... // ---------------------------------------------------------- /** * Sets up the test fixture. * Called automatically before every test case method. */ void setUp() { fred = new Student( "Fred" ); wilma = new Student( "Wilma" ); gradebook = new Gradebook(); } // ---------------------------------------------------------- /** * Tears down the test fixture. * Called automatically after every test case method. */ void tearDown() { delete fred; delete wilma; delete gradebook; } // ---------------------------------------------------------- /** * Test the {@link Student#name()} accessor. */ void testName1() { TS_ASSERT_EQUALS( "Fred", fred->name() ); TS_ASSERT_EQUALS( "Wilma", wilma->name() ); } // ---------------------------------------------------------- /** * Test the {@link Student#setName(String)} setter method. */ void testSetName() { fred->setName( "XYZ" ); TS_ASSERT_EQUALS( "XYZ", fred->name() ); } // ---------------------------------------------------------- /** * Test computing the assignment average when there are no assignments. */ void testNoAssignmentAverage() { TS_ASSERT_EQUALS( 0, fred->assignmentAverage() ); } // ---------------------------------------------------------- /** * Test computing the assignment average when there are some assignments. */ void testAssignmentAverage() { fred->addAssignment( 70 ); TS_ASSERT_EQUALS( 70, fred->assignmentAverage() ); fred->addAssignment( 80 ); TS_ASSERT_EQUALS( 75, fred->assignmentAverage() ); fred->addAssignment( 60 ); TS_ASSERT_EQUALS( 70, fred->assignmentAverage() ); wilma->addAssignment( 60 ); TS_ASSERT_EQUALS( 60, wilma->assignmentAverage() ); wilma->addAssignment( 65 ); TS_ASSERT_EQUALS( 62, wilma->assignmentAverage() ); } // ---------------------------------------------------------- /** * Test the {@link Student#toString()} method. */ void testToString() { fred->addAssignment( 70 ); TS_ASSERT_EQUALS( "Fred (70)", fred->toString() ); } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on an empty gradebook. */ void testSearchOfEmptyBook() { try { gradebook->findStudent( "" ); TS_FAIL( "exception not thrown" ); } catch ( StudentNotFound& ) { // succeeded } catch ( ... ) { TS_FAIL( "incorrect exception thrown" ); } } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on a gradebook containing * a single student. */ void testFindFred() { gradebook->addStudent( *fred ); TS_ASSERT_EQUALS( *fred, gradebook->findStudent( "Fred" ) ); TS_ASSERT_EQUALS( *fred, gradebook->findStudent( fred->name() ) ); } // ---------------------------------------------------------- /** * Try {@link Gradebook#findStudent(String)} on a gradebook containing * multiple students. */ void testFindFredAndWilma() { gradebook->addStudent( *fred ); gradebook->addStudent( *wilma ); TS_ASSERT_EQUALS( *fred, gradebook->findStudent( "Fred" ) ); TS_ASSERT_EQUALS( *wilma, gradebook->findStudent( "Wilma" ) ); try { gradebook->findStudent( "Frank" ); TS_FAIL( "exception not thrown" ); } catch ( StudentNotFound& ) { // succeeded } catch ( ... ) { TS_FAIL( "incorrect exception thrown" ); } } private: //~ Instance/static variables ............................................. Student* fred; Student* wilma; Gradebook* gradebook; }; #endif /*ASSIGNMENTSPECIFICATIONTESTS_H_*/