package uk.co.benjiweber.expressions;

import org.junit.Test;
import uk.co.benjiweber.expressions.caseclass.Case2;

import static org.junit.Assert.assertEquals;
import static uk.co.benjiweber.expressions.CaseTreeTest.Leaf.leaf;
import static uk.co.benjiweber.expressions.CaseTreeTest.Node.node;

public class CaseTreeTest {

    interface Tree extends Case2<Leaf, Node> {}
    interface Leaf extends Tree {
        static Leaf leaf = new Leaf() {};
    }
    interface Node<T> extends Tree {
        Tree left();
        Tree right();
        T value();
        static <T> Node<T> node(T value, Tree left, Tree right) {
            return new Node<T>() {
                public T value() { return value; }
                public Tree left() { return left; }
                public Tree right() { return right; }
            };
        }
    }

    @Test
    public void counting_nodes_example() {
         Tree tree =
             node(5,
                node(1, leaf, leaf),
                node(3, leaf,
                    node(4, leaf, leaf)
                )
             );

        assertEquals(4, countNodes(tree));
    }

    int countNodes(Tree tree) {
        return tree.match()
            .when(Leaf.class, n -> 0)
            .when(Node.class, n -> 1 + countNodes(n.left()) + countNodes(n.right()));
    }
}

@@ -197,29 +197,16 @@
 essions.
-CaseTreeTest.
 Leaf.lea
@@ -255,21 +255,8 @@
 ons.
-CaseTreeTest.
 Node
@@ -290,16 +290,505 @@
 eeTest %7B
+%0A%0A    @Test%0A    public void counting_nodes_example() %7B%0A        Tree tree =%0A            node(5,%0A                node(1, leaf, leaf),%0A                node(3, leaf,%0A                        node(4, leaf, leaf)%0A                )%0A            );%0A%0A        assertEquals(4, countNodes(tree));%0A    %7D%0A%0A    int countNodes(Tree tree) %7B%0A        return tree.match()%0A                .when(Leaf.class, n -%3E 0)%0A                .when(Node.class, n -%3E 1 + countNodes(n.left()) + countNodes(n.right()));%0A    %7D%0A%7D
 %0A%0A    in
@@ -1304,483 +1304,4 @@
  %7D%0A%0A
-    @Test%0A    public void counting_nodes_example() %7B%0A         Tree tree =%0A             node(5,%0A                node(1, leaf, leaf),%0A                node(3, leaf,%0A                    node(4, leaf, leaf)%0A                )%0A             );%0A%0A        assertEquals(4, countNodes(tree));%0A    %7D%0A%0A    int countNodes(Tree tree) %7B%0A        return tree.match()%0A            .when(Leaf.class, n -%3E 0)%0A            .when(Node.class, n -%3E 1 + countNodes(n.left()) + countNodes(n.right()));%0A    %7D%0A%7D%0A

Move test to top