Snippets

Michael Bethencourt http://michaelb.org/unit-tests-for-your-godot-scripts/

Created by Michael Bethencourt last modified
extends "res://bin/unittest.gd"

const Board = preload('res://scripts/board.gd')
var board

func tests():

    case("check_placement method checks boundaries")
    board = Board.new()
    board.set_dimensions(13, 13)
    assert_true(board.check_placement(0, 0, 0, 0), 'top left corner')
    assert_true(board.check_placement(0, 0, 12, 12), 'bottom right corner')
    assert_false(board.check_placement(0, 0, 13, 12), 'beyond right')
    assert_false(board.check_placement(0, 0, 12, 13), 'beyond bottom')
    assert_false(board.check_placement(0, 0, -1, 0), 'beyond left')
    assert_false(board.check_placement(0, 0, 0, -1), 'beyond top')
    endcase()

    case("check_placement checks boundaries for different types")
    board = Board.new()
    board.set_dimensions(13, 13)
    assert_true(board.check_placement(2, 0, 0, 0), 'top left corner')
    assert_false(board.check_placement(2, 0, 11, 12), 'bottom right corner')
    assert_false(board.check_placement(2, 0, 12, 11), 'bottom right corner')
    endcase()

    case("board can place pieces and check for collisions")
    board = Board.new()
    board.set_dimensions(13, 13)
    board.add_block(0, 0, 1, 1)
    assert_false(board.check_placement(0, 0, 1, 1), 'near top left corner')
    assert_false(board.check_placement(2, 0, 0, 0), 'top left corner')
    endcase()

    case("board handles triangles when checking for collisions")
    board = Board.new()
    board.set_dimensions(13, 13)
    assert_true(board.check_placement(1, 0, 1, 0), '')
    board.add_block(1, 0, 1, 0)
    assert_false(board.check_placement(1, 0, 1, 0), '')
    assert_false(board.check_placement(0, 0, 1, 0), '')
    assert_false(board.check_placement(2, 0, 0, 0), '')
    assert_true(board.check_placement(3, 0, 0, 0), 'complimenting piece')
    board.add_block(1, 0, 0, 1)
    assert_false(board.check_placement(3, 0, 0, 0), 'not complimenting piece')
    endcase()

    case("board determines sawdust (points) for basic placements")
    board = Board.new()
    board.set_dimensions(13, 13)
    assert_eq(board.get_points_for_placement(3, 0, 0, 0), 5, 'empty')
    board.add_block(1, 0, 1, 0)
    # Single complementing one:
    assert_eq(board.get_points_for_placement(3, 0, 0, 0), 6, 'single complimenting triangle')

    board = Board.new()
    board.set_dimensions(5, 5)
    board.add_block(0, 0, 2, 1)
    board.add_block(0, 0, 1, 2)
    assert_eq(board.get_points_for_placement(0, 0, 1, 1), 7, 'two adjacent')
    board.add_block(0, 0, 0, 0)
    assert_eq(board.get_points_for_placement(0, 0, 1, 1), 7, 'ignores catty-corner')
    board.add_block(0, 0, 0, 1)
    assert_eq(board.get_points_for_placement(0, 0, 1, 1), 8, 'three adjacent')
    endcase()

    case("board determines sawdust (points) for bonus placements")
    board = Board.new()
    board.set_dimensions(5, 5)
    board.add_block(0, 0, 2, 1)
    board.add_block(0, 0, 1, 2)
    board.add_block(0, 0, 0, 1)
    board.add_block(0, 0, 1, 0)
    board.add_block(0, 0, 1, 0)
    # Adjacent to 4 or more, gets 1 + 3 bonus
    assert_eq(board.get_points_for_placement(0, 0, 1, 1), 12, 'bonus')

    board = Board.new()
    board.set_dimensions(10, 10)
    board.add_block(0, 0, 0, 1)
    board.add_block(0, 0, 1, 0)
    assert_eq(board.get_points_for_placement(3, 0, 1, 1), 7, 'two adjacent')
    board.add_block(0, 0, 2, 0)
    assert_eq(board.get_points_for_placement(3, 0, 1, 1), 7, 'three')
    board.add_block(0, 0, 2, 2)
    assert_eq(board.get_points_for_placement(3, 0, 1, 1), 8, 'three')
    board.add_block(1, 2, 1, 2)
    assert_eq(board.get_points_for_placement(3, 0, 1, 1), 12, 'four')
    board.add_block(1, 0, 2, 1)
    assert_true(board.check_placement(3, 0, 1, 1), 'ensure placement')
    assert_eq(board.get_points_for_placement(3, 0, 1, 1), 15, 'five')
    endcase()
extends "res://bin/unittest.gd"

const pieces_consts = preload('res://scripts/pieces_consts.gd')
var original_piece
var piece
var expected

func tests():
    case("rotation works as expected for idempotent pieces")
    assert_dict_equal(pieces_consts.rotate_once(pieces_consts.PIECE_0), pieces_consts.PIECE_0, 'small square')
    assert_dict_equal(pieces_consts.rotate_once(pieces_consts.PIECE_2), pieces_consts.PIECE_2, 'large square')
    endcase()

    case("rotation works for a few pieces")
    expected = {Vector2(0, 0): pieces_consts.TOP_RIGHT, Vector2(1, 0): pieces_consts.TOP_LEFT}
    assert_dict_equal(pieces_consts.rotate_once(pieces_consts.PIECE_6), expected, 'triangle thingie')
    endcase()

    case("2x rotation is idempotent for 7 & 8")
    for i in [7, 8]:
        original_piece = pieces_consts.PIECES[i]
        piece = pieces_consts.rotate(original_piece, 2)
        assert_dict_equal(piece, original_piece, '2x rotation piece ' + str(i))
    endcase()

    case("4x rotation is idempotent for all pieces")
    for i in range(10):
        original_piece = pieces_consts.PIECES[i]
        piece = pieces_consts.rotate(original_piece, 4)
        assert_dict_equal(piece, original_piece, '4x rotation piece ' + str(i))
    endcase()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.