// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// public interface Expr {
// 
//     static Expr Nat(int i) {
//         return new Nat(i);
//     }
// 
//     static Expr Add(Expr l, Expr r) {
//         return new Add(l, r);
//     }
// 
//     static Expr Add3(Expr l, Expr m, Expr r) {
//         return new Add3(l, m, r);
//     }
// 
//     class Nat implements Expr {
//         public int val;
// 
//         public Nat(int val) {
//             this.val = val;
//         }
//     }
// 
//     class Add implements Expr {
//         public Expr left, right;
// 
//         public Add(Expr left, Expr right) {
//             this.left = left;
//             this.right = right;
//         }
//     }
// 
//     class Add3 implements Expr {
//         public Expr left, middle, right;
// 
//         public Add3(Expr left, Expr middle, Expr right) {
//             this.left = left;
//             this.middle = middle;
//             this.right = right;
//         }
//     }
// }
// 
// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// class Add implements Expr {
//     public Expr left, right;
// 
//     public Add(Expr left, Expr right) {
//         this.left = left;
//         this.right = right;
//     }
// }
// 
// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// class Nat implements Expr {
//     public int val;
// 
//     public Nat(int val) {
//         this.val = val;
//     }
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/core/Cases.java
// static <T> Var.WithoutInnerCapture<T, T> Var() {
//     return Var(Any());
// }
// 
// Path: src/main/java/org/smallibs/suitcase/utils/Functions.java
// public static <C1, C2, R> Function<Pair<C1, C2>, R> function(Function2<C1, C2, R> function) {
//     return params -> function.apply(params._1, params._2);
// }


import org.junit.Test;
import org.smallibs.suitcase.cases.ExprTest.Expr;
import org.smallibs.suitcase.cases.ExprTest.Expr.Add;
import org.smallibs.suitcase.cases.ExprTest.Expr.Nat;
import static junit.framework.Assert.assertEquals;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Add;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Add3;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Nat;
import static org.smallibs.suitcase.cases.core.Cases.Var;
import static org.smallibs.suitcase.utils.Functions.function;
/*
 * Copyright (C)2015 D. Plaindoux.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.smallibs.suitcase.match;



public class ExprMatcherTest {

    @Test
    public void shouldEvalExpression() {
        Matcher<Expr, Integer> adder = Matcher.create();

        adder.caseOf(Var(Nat.class)).then(n -> n.val);
        adder.caseOf(Var(Add.class)).then(n -> adder.match(n.left) + adder.match(n.right));

        assertEquals((int) adder.match(Expr.Nat(1)), 1);
        assertEquals((int) adder.match(Expr.Add(Expr.Nat(1), Expr.Nat(3))), 4);
    }

    @Test
    public void shouldEvalCapturedExpression() {
        Matcher<Expr, Integer> adder = Matcher.create();

        adder.caseOf(Nat.$(Var())).then(n -> n);
// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// public interface Expr {
// 
//     static Expr Nat(int i) {
//         return new Nat(i);
//     }
// 
//     static Expr Add(Expr l, Expr r) {
//         return new Add(l, r);
//     }
// 
//     static Expr Add3(Expr l, Expr m, Expr r) {
//         return new Add3(l, m, r);
//     }
// 
//     class Nat implements Expr {
//         public int val;
// 
//         public Nat(int val) {
//             this.val = val;
//         }
//     }
// 
//     class Add implements Expr {
//         public Expr left, right;
// 
//         public Add(Expr left, Expr right) {
//             this.left = left;
//             this.right = right;
//         }
//     }
// 
//     class Add3 implements Expr {
//         public Expr left, middle, right;
// 
//         public Add3(Expr left, Expr middle, Expr right) {
//             this.left = left;
//             this.middle = middle;
//             this.right = right;
//         }
//     }
// }
// 
// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// class Add implements Expr {
//     public Expr left, right;
// 
//     public Add(Expr left, Expr right) {
//         this.left = left;
//         this.right = right;
//     }
// }
// 
// Path: src/test/java/org/smallibs/suitcase/cases/ExprTest.java
// class Nat implements Expr {
//     public int val;
// 
//     public Nat(int val) {
//         this.val = val;
//     }
// }
// 
// Path: src/main/java/org/smallibs/suitcase/cases/core/Cases.java
// static <T> Var.WithoutInnerCapture<T, T> Var() {
//     return Var(Any());
// }
// 
// Path: src/main/java/org/smallibs/suitcase/utils/Functions.java
// public static <C1, C2, R> Function<Pair<C1, C2>, R> function(Function2<C1, C2, R> function) {
//     return params -> function.apply(params._1, params._2);
// }


// Path: src/test/java/org/smallibs/suitcase/match/ExprMatcherTest.java
import org.junit.Test;
import org.smallibs.suitcase.cases.ExprTest.Expr;
import org.smallibs.suitcase.cases.ExprTest.Expr.Add;
import org.smallibs.suitcase.cases.ExprTest.Expr.Nat;
import static junit.framework.Assert.assertEquals;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Add;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Add3;
import static org.smallibs.suitcase.cases.ExprTest.Patterns.Nat;
import static org.smallibs.suitcase.cases.core.Cases.Var;
import static org.smallibs.suitcase.utils.Functions.function;

/*
 * Copyright (C)2015 D. Plaindoux.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.smallibs.suitcase.match;



public class ExprMatcherTest {

    @Test
    public void shouldEvalExpression() {
        Matcher<Expr, Integer> adder = Matcher.create();

        adder.caseOf(Var(Nat.class)).then(n -> n.val);
        adder.caseOf(Var(Add.class)).then(n -> adder.match(n.left) + adder.match(n.right));

        assertEquals((int) adder.match(Expr.Nat(1)), 1);
        assertEquals((int) adder.match(Expr.Add(Expr.Nat(1), Expr.Nat(3))), 4);
    }

    @Test
    public void shouldEvalCapturedExpression() {
        Matcher<Expr, Integer> adder = Matcher.create();

        adder.caseOf(Nat.$(Var())).then(n -> n);
        adder.caseOf(Add.$(Var(), Var())).then(function((p1, p2) -> adder.match(p1) + adder.match(p2)));