Jumper 測試報告

星 宇�發表於2020-10-18

Jumper 測試報告

目的

測試我們所寫的Jumper函式是否符合預期,以及是否會出現一些意外錯誤。

思路

  1. 首先測試 Jumper能否正常的跳兩格
  2. 測試Jumper在第二格是牆的時候是否只跳一格
  3. 然後測試 Jumper 在面對一格有岩石時能否跳過去
  4. 測試Jumper無路可走時是否會轉彎
  5. 測試Jumper面對兩格有岩石時是否跳一格
  6. 再測試 Jumper 面對兩格有花時是否只跳一格
  7. 最後測試兩個 Jumper 互相面對時是否會轉彎。

程式碼

import org.junit.Test;
import static org.junit.Assert.assertEquals;

import info.gridworld.actor.*;
import info.gridworld.grid.Location;

import java.awt.Color;


public class JumperTest{
	@Test 
	public void testJump1() { //測試 Jumper能否正常的跳兩格 
		ActorWorld world = new ActorWorld();
		//new a jumper 
		Jumper a = new Jumper(Color.RED); 
		world.add(new Location(2,0), a);
		//test initial location 
		assertEquals(2, a.getLocation().getRow()); 
		assertEquals(0, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection()); 
		a.act(); 
		//test result location 
		assertEquals(0, a.getLocation().getRow()); 
		assertEquals(0, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection());
	}

	@Test
	public void testJump2() {//測試Jumper在第二格是牆的時候是否只跳一格
		ActorWorld world = new ActorWorld();
		//new a jumper 
		Jumper a = new Jumper(Color.RED); 
		world.add(new Location(1,0), a);
		//test initial location 
		assertEquals(1, a.getLocation().getRow()); 
		assertEquals(0, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection()); 
		a.act(); 
		//test result location 
		assertEquals(0, a.getLocation().getRow()); 
		assertEquals(0, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection());
	}

	@Test
	public void testJump3() {//然後測試 Jumper 在面對一格有岩石時能否跳過去
		ActorWorld world = new ActorWorld();

		//new a jumper 
		Jumper a = new Jumper(Color.RED);
		Rock r = new Rock(); 
		world.add(new Location(4,4), a);
		world.add(new Location(3,4), r);
		//test initial location 
		assertEquals(4, a.getLocation().getRow()); 
		assertEquals(4, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection()); 
		a.act(); 
		//test result location 
		assertEquals(2, a.getLocation().getRow()); 
		assertEquals(4, a.getLocation().getCol()); 
		assertEquals(Location.NORTH, a.getDirection());
	}

	@Test
	public void testMove() {//測試Jumper無路可走時是否會轉彎

	      Jumper ab = new Jumper(Color.RED);

	      ActorWorld world = new ActorWorld();
	      world.add(new Location(0,0), ab);
	      assertEquals(0, ab.getLocation().getRow());
	      assertEquals(0, ab.getLocation().getCol());
	      assertEquals(Location.NORTH, ab.getDirection());
	      ab.act();
	      assertEquals(0, ab.getLocation().getRow());
	      assertEquals(0, ab.getLocation().getCol());
	      assertEquals(Location.NORTHWEST, ab.getDirection());

	}

	@Test
	public void testRock()//測試Jumper面對兩格有岩石時是否跳一格
	{
	    ActorWorld world = new ActorWorld();
	    Jumper a = new Jumper(Color.RED);
	    Rock r=new Rock(Color.GREEN);
	    world.add(new Location(3,3), a);
	    world.add(new Location(2,3), r);

	    a.act();
	    assertEquals(1, a.getLocation().getRow());
	    assertEquals(3, a.getLocation().getCol());
	    assertEquals(Location.NORTH, a.getDirection());
	}

	@Test
	public void testFlower()//再測試 Jumper 面對兩格有花時是否只跳一格
	{
	    ActorWorld world = new ActorWorld();
	    Jumper a = new Jumper(Color.RED);
	    Flower r=new Flower(Color.GREEN);
	    world.add(new Location(3,3), a);
	    world.add(new Location(1,3), r);

	    a.act();
	    assertEquals(2, a.getLocation().getRow());
	    assertEquals(3, a.getLocation().getCol());
	    assertEquals(Location.NORTH, a.getDirection());
	}

	@Test
	public void jumperface()//最後測試兩個 Jumper 互相面對時是否會轉彎
	{
	    ActorWorld world = new ActorWorld();
	    Jumper a = new Jumper(Color.RED);
	    Jumper b = new Jumper(Color.ORANGE);
	    a.setDirection(180);
	      world.add(new Location(1,3), a);
	      world.add(new Location(3,3), b);

	      a.act();
	    assertEquals(1, a.getLocation().getRow());
	    assertEquals(3, a.getLocation().getCol());
	    assertEquals(Location.SOUTHEAST, a.getDirection());

	    b.act();
	    assertEquals(3, b.getLocation().getRow());
	    assertEquals(3, b.getLocation().getCol());
	    assertEquals(Location.NORTHWEST, b.getDirection());
	}
}

結果

在這裡插入圖片描述
全部通過測試

相關文章