package yokohama.unit.ast_junit;

import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Value;
import static org.apache.commons.lang3.StringEscapeUtils.escapeJava;
import yokohama.unit.util.SBuilder;

@Value
@EqualsAndHashCode(callSuper=false)
public class SuchThatMatcherExpr extends MatcherExpr {
    private List<Statement> statements;
    private String description;
    private Var argVar;

    @Override
    public void getExpr(SBuilder sb, String varName, ExpressionStrategy expressionStrategy, MockStrategy mockStrategy) {
        sb.appendln(varName, " = new org.hamcrest.BaseMatcher() {");
        sb.shift();
            sb.appendln("@Override");
            sb.appendln("public boolean matches(Object ", argVar.getName(), ") {");
            sb.shift();
                sb.appendln("try {");
                sb.shift();
                for (Statement statement : statements) {
                    statement.toString(sb, expressionStrategy, mockStrategy);
                }
                sb.unshift();
                sb.appendln("} catch (Exception $e) {"); // TODO: var name should be gensym'ed
                sb.shift();
                    sb.appendln("throw new RuntimeException($e);");
                sb.unshift();
                sb.appendln("}");
            sb.unshift();
            sb.appendln("}");
            sb.appendln("@Override");
            sb.appendln("public void describeTo(org.hamcrest.Description description) {");
            sb.shift();
                sb.appendln("description.appendText(\"",  escapeJava(description), "\");");
            sb.unshift();
            sb.appendln("}");
        sb.unshift();
        sb.appendln("};");
    }
}

@@ -179,16 +179,49 @@
 eJava;%0D%0A
+import yokohama.unit.util.Pair;%0D%0A
 import y
@@ -822,32 +822,346 @@
    sb.shift();%0D%0A
+                for (Pair%3CType, String%3E pair : VarDeclVisitor.sortedSet(new VarDeclVisitor().visitStatements(statements))) %7B%0D%0A                    Type type = pair.getFirst();%0D%0A                    String name = pair.getSecond();%0D%0A                    sb.appendln(type.getText(), %22 %22, name, %22;%22);%0D%0A                %7D%0D%0A
                 

Generate var declaration java code