testreport
1.需求分析
自主使用Junit完成对Jumper类的测试
2. 具体测试细节
2.1 Jumper类相关成员函数的介绍
由于截图无法被渲染所以能被代码替代的部分都选择了代码。
2.1.1 构造函数
//constructor
public Jumper() {
setColor(Color.BLUE);
}
public Jumper(Color jumperColor) {
setColor(jumperColor);
}
2.1.2 act()函数
功能:选择Jumper进行move、jump、turn且jump优先级高于move
//act
public void act() {
if(canJump())
{
Jump();
}
else if (canMove())
{
move();
}
else
{
turn();
}
}
**2.1.3 turn()函数 **
功能:当Jumper无法进行jump或者move时候进行turn,转换角度
///turn
public void turn() {
setDirection(getDirection() + Location.HALF_RIGHT);
}
2.1.4 move()函数与canMove()函数
功能:
1. move()函数:Jumper往前移动一步
2. canMove()函数:判断Jumper是否可以move
// move
public void move() {
Grid<Actor> gr = getGrid();
if (gr == null) {
return;
}
//new position
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
{
moveTo(next);
}
//remove
else
{
removeSelfFromGrid();
}
Flower flower = new Flower(getColor());
flower.putSelfInGrid(gr,loc);
}
// judge if bug can move
public boolean canMove() {
//new a things
Grid<Actor> gr = getGrid();
if (gr == null) {
return false;
}
Location loc = getLocation();
//new position
Location next = loc.getAdjacentLocation(getDirection());
if (!gr.isValid(next))
{
return false;
}
//next position
Actor neighbor = gr.get(next);
return (neighbor == null) || (neighbor instanceof Flower);
}
2.1.5 jump()函数与canJump()函数
功能:
1. jump()函数:Jumper往前移动两步
2. canJump()函数:判断Jumper是否可以jump
//jump
public void Jump()
{
Grid<Actor> gr = getGrid();
if (gr == null)
{
return ;
}
//new position
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
if(gr.isValid(next))
{
Location next2 = next.getAdjacentLocation(getDirection());
if(gr.isValid(next2))
{
moveTo(next2);
}
// remove
else
{
removeSelfFromGrid();
}
}
}
//judge if bug can jump
public boolean canJump()
{
Grid<Actor> gr = getGrid();
if (gr == null) {
return false;
}
//new seat
Location loc = getLocation();
Location next = loc.getAdjacentLocation(getDirection());
Location next2 = next.getAdjacentLocation(getDirection());
//if bug couldn't find do other things
if(!gr.isValid(next2))
{
return false ;
}
Actor neighbor = gr.get(next2);
return (neighbor == null || neighbor instanceof Flower);
}
2.2 函数测试
这一部分我选择了4个函数进行测试,分别是:
- act()函数
- canMove()函数
- move()函数
- Jump()函数
- turn()函数
2.2.1 act()函数测试
2.2.2 canMove()函数测试
// test if bug can judge move
@Test
public void canMoveTest() {
final int x = 5, y = 5;
Jumper jumper = new Jumper();
ActorWorld world = new ActorWorld();
world.add(new Location(x, y), jumper);
assertTrue("Failure - can move", jumper.canMove());
Rock rock = new Rock();
world.add(new Location(x-1, y), rock);
assertFalse("Failure - can not move", jumper.canMove());
Jumper jumper2 = new Jumper();
world.add(new Location(0, 0), jumper2);
assertFalse("Failure - can not move due to the edge", jumper2.canMove());
}
2.2.3 move()函数测试
// test if bug can move
@Test
public void moveTest() {
final int x = 5, y = 5;
Jumper jumper = new Jumper();
ActorWorld world = new ActorWorld();
world.add(new Location(x, y), jumper);
jumper.move();
assertEquals("Failure - jumper should move forward with a distance of 1", x-1, jumper.getLocation().getRow());
assertEquals("Failure - jumper should move forward with a distance of 1", y, jumper.getLocation().getCol());
}
2.2.4 Jump()函数测试
// test if bug can jump
@Test
public void jumpTest() {
final int x = 5, y = 5;
Jumper jumper = new Jumper();
ActorWorld world = new ActorWorld();
world.add(new Location(x, y), jumper);
jumper.Jump();
assertEquals("Failure - jumper should move forward with a distance of 2", x-2, jumper.getLocation().getRow());
assertEquals("Failure - jumper should move forward with a distance of 2", y, jumper.getLocation().getCol());
}
2.2.5 turn()函数测试
// test if bug can turn
@Test
public void turnTest() {
final int x = 5, y = 5;
Jumper jumper = new Jumper();
ActorWorld world = new ActorWorld();
world.add(new Location(x, y), jumper);
jumper.turn();
assertEquals("Failure - jumper should turn 45 degrees to the right", Location.NORTHEAST, jumper.getDirection());
}
2.3 测试结果及详解
2.3.1 actTest
- 首先把jumper放置在点(5,5)上,随后jumper进行act,由于地图上并没有障碍物且jump之后并不会出界,所以此时jumper进行jump
- 完成jump之后,jumper的X坐标减2,此时jumper.getXposition() = 3 , 而 x - 2 = 3 ,由于x = jumper.getXposition()相等所以返回true;
- 放置一个rock到点(1,4)上
- jumper继续运动,由于此时jumper再继续向前跳跃时候,会碰到rock,所以此时jumper选择进行move,向前move一步
- 此时jumper执行turn,方向变为东北方向
- jumper完成move之后,jumper的X坐标减1,此时jumper.getXposition() = 2 ,而此时x - 3 = 2 ,所以 x - 3 = jumper.getXposition() ,并且NORTHEAST,=jumper.getDirection(),所以最终返回true;
2.3.2 canMoveTest
- 首先把jumper放置在点(5,5)上
- 进行canMove()测试,由于jumper可以移动,所以返回true
2.3.3 moveTest
- 首先把jumper放置在点(5,5)上
- jumper进行move之后,jumper的x坐标减小1,此时jumper.getXposition() = 4
- 由于原本的x = x - 1 = 4 ,此时x与jumper.getXposition()相等,于是就**返回true
**
2.3.3 jumpTest
- 首先把jumper放置在点(5,5)上
- jumper进行Jump之后,jumper的x坐标减小2,此时jumper.getXposition() = 3
- 由于原本的x = x - 2 = 3 ,此时x与jumper.getXposition()相等,于是就返回true
2.3.5 turnTest
- 在part2部分中,已经定义了NORTHEAST是北偏东45°,并且定义jumper面朝正北,且自身旋转顺序为顺时针;
- 这里jumper进行turn之后,是顺时针旋转45°,此时也就是相对于原来未知的北偏东45°方向,所以返回true