简体   繁体   中英

How Will I Test A MineSweepers Grid Paint Method?

I'm trying to learn TDD by solving the problem of text based MineSweeper game. The last part in it was writing fillGrid method which paints the Grid

I ended up in:

public Grid fillGrid(Grid grid) {
        String[][] gridCells = grid.getGrid();

        fillBomb(grid);
        for(int i=0;i<gridCells.length;i++){
            for(int j=0;j<gridCells[0].length;j++){

                if(!constraints.checkBombsAndNumbersPresentAsRequrired(grid.getSurroundingValues(i,j)))
                {
                    if(gridCells[i][j] != "*") {
                        if(getBombCount(grid.getSurroundingValues(i, j)) != 0)
                            grid.setCells(i, j, "" + getBombCount(grid.getSurroundingValues(i, j)));
                    }
                }
            }
        }

        grid.prettyPrint( grid.getGrid() );

        return  null;
    }

In the above code I have tested both fillBomb and checkBombsAndNumbersPresentAsRequried separately. Now I'm sort of struck with how will I test the fillGrid in itself!

As I was trying to follow the TDD pattern I wrote fillGrid fail test cases like:

  1. It should have only 4 bombs (sort of easy mode) -- made it fail
  2. Check constraints whether the return Grid satisfies MineSweeper itself

The 2nd point where I'm struck. If I going to write a test cases for the 2nd point, it so likely that, I'm going to check the same logic in my test case too!

One thing I thought that I'm using checkBombsAndNumbersPresentAsRequrired which is clearly checks the formed data, but with this can we skip testing fillGrid in TDD, even though it does some crucial logic?

Am I miss something here?

If you are trying to learn TDD, the first thing to learn is to write a test before you write the code. This will eliminate a lot of the problems with "how can I test this code" because you will already have the test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM