kareltherobot
Class KJRTest

java.lang.Object
  extended byjunit.framework.Assert
      extended byjunit.framework.TestCase
          extended bykareltherobot.KJRTest
All Implemented Interfaces:
Directions, junit.framework.Test

public class KJRTest
extends junit.framework.TestCase
implements Directions

Title: KJRTest

Description: Test harness for JUnit testing of Karel J. Robot programs.

Copyright: Copyright (c) 2003, Joseph Bergin

To use this harness, create a new subclass of KJRTest. The class should be in package kareltherobot AND should import junit.framework.* (if you want to use the other methods from JUnit). The class needs a constructor with a String parameter, just as this class has. Declare any variables that you wish to use in the test as fields of this class, but do not initialize them in the declarations. For example, declare some UrRobot variables.

Write a setUp method with no parameters returning void, in which you give values to all of the variables that you want to use elsewhere in your tests. If you have declared some robots as fields of your new class, then initialize these fields with new UrRobot(...) or whatever.

Then write a set of public void methods whose names begin with "test", such as "testMoving", or "testTurning". Beginning with the word "test" is important to the infrastructure. Within these methods you can send messages to your robots and can call inherited methods of this class. Assert what you believe should be true of the state of your robots at any point. You can also call methods inherited here from the JUnit framework. Note that when an assertion "succeeds" nothing happens, but when it fails, an error is indicated to the test framework.

Finally, give your class a main with one statement:

      junit.swingui.TestRunner.run(UnitTest.class);
where you replace "UnitTest" with the name of your class. If you don't have access to the swing libraries (Mac OS 9) you can say:
    junit.awtui.TestRunner.run(UnitTest.class); 
instead. See UnitTest.java for a sample of such a class.

This new class should be the "main" class in your project. When you run the project, you will get a GUI with a run button. Pushing the run button will execute all of the "test" methods in your class, independently of the others but with setUp executed just prior to the execution of each. If none of the assertions fail then you will see a green bar. If any fail, you will see a red bar and be given some information about the failure, including which assertion failed and its line number.

NOTE that once the gui is running, you can change your code and rebuild it without terminating the gui. If the "Reload classes for every run" box is checked, the new code will be exercised when you hit the run button, so you don't need to "run" the project again. You DO need to build it each time however.

For more advanced uses of this framework, read the documentation for TestSuite in the JUnit docs. You can build up a single main test from lots of smaller test classes to test parts of your application separately from others, but maintain the ability to run all the tests from all the classes at once.

Version:
1.0
Author:
Joseph Bergin

Nested Class Summary
 
Nested classes inherited from class kareltherobot.Directions
Directions.Direction
 
Field Summary
 
Fields inherited from interface kareltherobot.Directions
East, EastVal, infinity, North, NorthVal, South, SouthVal, West, WestVal
 
Constructor Summary
KJRTest(java.lang.String name)
          Create a new Karel J Robot test case with the given name.
 
Method Summary
 void assertAt(UrRobot robot, int street, int avenue)
          Fail if the robot is not at a given intersection.
 void assertBeepersAt(int street, int avenue)
          Assert that a given corner has at least one beeper.
 void assertBeepersAt(int street, int avenue, int n)
          Assert that a given corner has exactly n beepers
 void assertBeepersInBeeperBag(UrRobot robot)
          Fail if the beeper bag of the robot is empty.
 void assertBeepersInWorld()
          Assert that there is at least one beeper in the world.
 void assertBeepersInWorld(int n)
          Fail unless there are exactly a certain number of beepers in the world
 void assertFacingEast(UrRobot robot)
          Fail if the robot is not facing East.
 void assertFacingNorth(UrRobot robot)
          Fail if the robot is not facing North.
 void assertFacingSouth(UrRobot robot)
          Fail if the robot is not facing South.
 void assertFacingWest(UrRobot robot)
          Fail if the robot is not facing West.
 void assertFrontIsBlocked(UrRobot robot)
          Fail if there is no wall in front of the robot.
 void assertFrontIsClear(UrRobot robot)
          Fail if there is a wall in front of the robot.
 void assertHasNeighbor(UrRobot robot)
          Fail if the robot has no neighbors.
 void assertHasNoNeighbor(UrRobot robot)
          Fail if the robot has any neighbors.
 void assertNextToABeeper(UrRobot robot)
          Fail if the robot is on a corner with no beepers.
 void assertNoBeepersInBeeperBag(UrRobot robot)
          Fail if the robot has any beepers in its beeper bag.
 void assertNotAt(UrRobot robot, int street, int avenue)
          Fail if the robot is at a given intersection.
 void assertNotFacingEast(UrRobot robot)
          Fail if the robot is facing East.
 void assertNotFacingNorth(UrRobot robot)
          Fail if the robot is facing North.
 void assertNotFacingSouth(UrRobot robot)
          Fail if the robot is facing South.
 void assertNotFacingWest(UrRobot robot)
          Fail if the robot is facing West.
 void assertNotNextToABeeper(UrRobot robot)
          Fail if the robot is on a corner with one or more beepers.
 void assertNotOnAvenue(UrRobot robot, int avenue)
          Fail if the robot is on a given avenue.
 void assertNotOnStreet(UrRobot robot, int street)
          Fail if the robot is on a given street.
 void assertNotRunning(UrRobot robot)
          Fail if the robot not has turned off.
 void assertOnAvenue(UrRobot robot, int avenue)
          Fail if the robot is not on a given avenue.
 void assertOnStreet(UrRobot robot, int street)
          Fail if the robot is not on a given street.
 void assertRobotsAt(int street, int avenue)
          Assert that a given corner has at least one robot.
 void assertRobotsAt(int street, int avenue, int n)
          Assert that a given corner has exactly n robots.
 void assertRobotsInWorld()
          Assert that there is at least one robot in the world.
 void assertRobotsInWorld(int n)
          Fail unless there are exactly a certain number of robots in the world
 void assertRunning(UrRobot robot)
          Fail if the robot has turned off or experienced an error shutoff.
 
Methods inherited from class junit.framework.TestCase
countTestCases, getName, run, run, runBare, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

KJRTest

public KJRTest(java.lang.String name)
Create a new Karel J Robot test case with the given name. This is called by the test infrastructure, not by the programmer. It is required.

Parameters:
name - the name for this test.
Method Detail

assertFacingNorth

public final void assertFacingNorth(UrRobot robot)
Fail if the robot is not facing North. No-op if the robot IS facing North.

Parameters:
robot - the robot of interest.

assertNotFacingNorth

public final void assertNotFacingNorth(UrRobot robot)
Fail if the robot is facing North. No-op if the robot IS NOT facing North.

Parameters:
robot - the robot of interest.

assertFacingEast

public final void assertFacingEast(UrRobot robot)
Fail if the robot is not facing East. No-op if the robot IS facing East.

Parameters:
robot - the robot of interest.

assertNotFacingEast

public final void assertNotFacingEast(UrRobot robot)
Fail if the robot is facing East. No-op if the robot IS NOT facing East.

Parameters:
robot - the robot of interest.

assertFacingSouth

public final void assertFacingSouth(UrRobot robot)
Fail if the robot is not facing South. No-op if the robot IS facing South.

Parameters:
robot - the robot of interest.

assertNotFacingSouth

public final void assertNotFacingSouth(UrRobot robot)
Fail if the robot is facing South. No-op if the robot IS NOT facing South.

Parameters:
robot - the robot of interest.

assertFacingWest

public final void assertFacingWest(UrRobot robot)
Fail if the robot is not facing West. No-op if the robot IS facing West.

Parameters:
robot - the robot of interest.

assertNotFacingWest

public final void assertNotFacingWest(UrRobot robot)
Fail if the robot is facing West. No-op if the robot IS NOT facing West.

Parameters:
robot - the robot of interest.

assertAt

public final void assertAt(UrRobot robot,
                           int street,
                           int avenue)
Fail if the robot is not at a given intersection. No-op otherwise.

Parameters:
robot - the robot of interest
street - the street you assert the robot is on.
avenue - the avenue you assert the robot is on.

assertNotAt

public final void assertNotAt(UrRobot robot,
                              int street,
                              int avenue)
Fail if the robot is at a given intersection. No-op otherwise.

Parameters:
robot - the robot of interest
street - the street of the corner you assert the robot is on.
avenue - the avenue of the corner you assert the robot is on.

assertOnStreet

public final void assertOnStreet(UrRobot robot,
                                 int street)
Fail if the robot is not on a given street. No-op otherwise.

Parameters:
robot - the robot of interest.
street - the street you assert the robot is on.

assertNotOnStreet

public final void assertNotOnStreet(UrRobot robot,
                                    int street)
Fail if the robot is on a given street. No-op otherwise.

Parameters:
robot - the robot of interest.
street - the street you assert the robot is not on.

assertOnAvenue

public final void assertOnAvenue(UrRobot robot,
                                 int avenue)
Fail if the robot is not on a given avenue. No-op otherwise.

Parameters:
robot - the robot of interest.
avenue - the avenue you assert the robot is on.

assertNotOnAvenue

public final void assertNotOnAvenue(UrRobot robot,
                                    int avenue)
Fail if the robot is on a given avenue. No-op otherwise.

Parameters:
robot - the robot of interest.
avenue - the avenue you assert the robot is not on.

assertHasNeighbor

public final void assertHasNeighbor(UrRobot robot)
Fail if the robot has no neighbors. No-op otherwise.

Parameters:
robot - the robot of interest.

assertHasNoNeighbor

public final void assertHasNoNeighbor(UrRobot robot)
Fail if the robot has any neighbors. No-op otherwise.

Parameters:
robot - the robot of interest.

assertNextToABeeper

public final void assertNextToABeeper(UrRobot robot)
Fail if the robot is on a corner with no beepers. No-op otherwise.

Parameters:
robot - the robot of interest.

assertNotNextToABeeper

public final void assertNotNextToABeeper(UrRobot robot)
Fail if the robot is on a corner with one or more beepers. No-op otherwise.

Parameters:
robot - the robot of interest.

assertBeepersInBeeperBag

public final void assertBeepersInBeeperBag(UrRobot robot)
Fail if the beeper bag of the robot is empty. No-op otherwise.

Parameters:
robot - the robot of interest.

assertNoBeepersInBeeperBag

public final void assertNoBeepersInBeeperBag(UrRobot robot)
Fail if the robot has any beepers in its beeper bag. No-op otherwise.

Parameters:
robot - the robot of interest.

assertFrontIsClear

public final void assertFrontIsClear(UrRobot robot)
Fail if there is a wall in front of the robot. No-op otherwise.

Parameters:
robot - the robot of interest.

assertFrontIsBlocked

public final void assertFrontIsBlocked(UrRobot robot)
Fail if there is no wall in front of the robot. No-op otherwise.

Parameters:
robot - the robot of interest.

assertRunning

public final void assertRunning(UrRobot robot)
Fail if the robot has turned off or experienced an error shutoff. No-op otherwise.

Parameters:
robot - the robot of interest.

assertNotRunning

public final void assertNotRunning(UrRobot robot)
Fail if the robot not has turned off. No-op otherwise. Note that an error shutoff is considered a turn off.

Parameters:
robot - the robot of interest.

assertBeepersInWorld

public final void assertBeepersInWorld(int n)
Fail unless there are exactly a certain number of beepers in the world

Parameters:
n - the asserted number of beepers in the world

assertBeepersInWorld

public final void assertBeepersInWorld()
Assert that there is at least one beeper in the world.


assertRobotsInWorld

public final void assertRobotsInWorld(int n)
Fail unless there are exactly a certain number of robots in the world

Parameters:
n - the asserted number of robots in the world

assertRobotsInWorld

public final void assertRobotsInWorld()
Assert that there is at least one robot in the world.


assertBeepersAt

public final void assertBeepersAt(int street,
                                  int avenue,
                                  int n)
Assert that a given corner has exactly n beepers

Parameters:
street - the street in question
avenue - the avenue of the corner in question
n - the asserted numbrer of beepers

assertBeepersAt

public final void assertBeepersAt(int street,
                                  int avenue)
Assert that a given corner has at least one beeper.

Parameters:
street - the street in question
avenue - the avenue of the corner in question

assertRobotsAt

public final void assertRobotsAt(int street,
                                 int avenue,
                                 int n)
Assert that a given corner has exactly n robots.

Parameters:
street - the street in question
avenue - the avenue of the corner in question
n - the asserted number of robots on this corner

assertRobotsAt

public final void assertRobotsAt(int street,
                                 int avenue)
Assert that a given corner has at least one robot.

Parameters:
street - the street in question
avenue - the avenue of the corner in question