Показать сообщение отдельно
  #1 (permalink)  
Старый 02.06.2021, 13:10
Аспирант
Отправить личное сообщение для prototip Посмотреть профиль Найти все сообщения от prototip
 
Регистрация: 15.05.2021
Сообщений: 35

unit тесты как писать?
ребята, покажите пожалуйста, как написать любые юнит тесты с помощью jest или mocha на примере этого кода. Это код бинарного дерева.
class TreeNode {
    public left: TreeNode = null;
    public right: TreeNode = null;

    constructor(
        public value: number
    ) { }
}

class BinaryTree {
    public root: TreeNode = null;

    public add(value: number): void {
        const node: TreeNode = new TreeNode(value);

        if (this.isEmpty()) {
            this.root = node;
        } else {
            let currentNode: TreeNode = this.root;

            while (currentNode) {
                if (value > currentNode.value) {
                    if (currentNode.right === null) {
                        currentNode.right = node;
                        return;
                    }

                    currentNode = currentNode.right;
                } else {
                    if (currentNode.left === null) {
                        currentNode.left = node;
                        return;
                    }

                    currentNode = currentNode.left;
                }
            }
        }
    }

    public search(value: number): number {
        let currentNode: TreeNode = this.root;

        while (currentNode) {
            if (value === currentNode.value) {
                return value;
            } else if (value > currentNode.value) {
                currentNode = currentNode.right;
            } else {
                currentNode = currentNode.left;
            }
        }

        return null;
    }

    private getSuccessor(node: TreeNode): TreeNode {
        let currentNode: TreeNode = node;

        while (currentNode) {
            if (currentNode.right === null) {
                break;
            }

            currentNode = currentNode.right;
        }

        return currentNode;
    }

    public isEmpty(): boolean {
        return this.root === null;
    }
}
Ответить с цитированием