package com.redhat.ceylon.compiler.util;

import com.redhat.ceylon.compiler.tree.Node;
import com.redhat.ceylon.compiler.tree.Tree;
import com.redhat.ceylon.compiler.tree.Visitor;

public class AssertionVisitor extends Visitor {
    
    boolean broken = false;
    boolean foundError = false;

    @Override
    public void visit(Tree.TypedDeclaration that) {
        for (Tree.CompilerAnnotation c: that.getCompilerAnnotations()) {
            if (c.getIdentifier().getText().equals("type")) {
                String expectedType = c.getStringLiteral().getText();
                String actualType = that.getTypeOrSubtype().getTypeModel().getProducedTypeName();
                assert actualType.equals(expectedType.substring(1,expectedType.length()-1)) :
                    c.getIdentifier().getText()  + " is not of type " + expectedType + 
                    " at "+ that.getAntlrTreeNode().getLine() + ":" +
                    that.getAntlrTreeNode().getCharPositionInLine();;
            }
        }
        super.visit(that);
    }
    
    @Override
    public void visit(Tree.Statement that) {
        boolean b = broken;
        boolean f = foundError;
        broken = false;
        foundError = false;
        for (Tree.CompilerAnnotation c: that.getCompilerAnnotations()) {
            if (c.getIdentifier().getText().equals("broken")) {
                broken = true;
            }
        }
        super.visit(that);
        assert broken==foundError : "broken at " + 
            that.getAntlrTreeNode().getLine() + ":" +
            that.getAntlrTreeNode().getCharPositionInLine();
        broken = b;
        foundError = f;
    }
    
    @Override
    public void visitAny(Node that) {
        if ( !that.getErrors().isEmpty() )
            foundError = true;
        super.visitAny(that);
    }

    
}

@@ -239,22 +239,30 @@
 boolean 
-broken
+expectingError
  = false
@@ -681,23 +681,22 @@
         
-assert 
+if ( !
 actualTy
@@ -760,10 +760,54 @@
 1)) 
-:%0A
+)%0A                    System.err.println(%0A    
     
@@ -853,16 +853,41 @@
 xt()  + 
+%0A                        
 %22 is not
@@ -911,24 +911,28 @@
 ctedType + %0A
+    
             
@@ -1001,32 +1001,36 @@
                 
+    
 that.getAntlrTre
@@ -1060,17 +1060,17 @@
 InLine()
-;
+)
 ;%0A      
@@ -1204,22 +1204,30 @@
 ean b = 
-broken
+expectingError
 ;%0A      
@@ -1260,22 +1260,30 @@
         
-broken
+expectingError
  = false
@@ -1437,22 +1437,21 @@
 equals(%22
-broken
+error
 %22)) %7B%0A  
@@ -1464,22 +1464,30 @@
         
-broken
+expectingError
  = true;
@@ -1550,43 +1550,138 @@
     
-assert broken==foundError : %22broken
+if (expectingError!=foundError)%0A            System.err.println(%0A                (expectingError ? %22no %22 : %22%22) + %22error encountered
  at 
@@ -1681,24 +1681,28 @@
 red at %22 + %0A
+    
             
@@ -1747,32 +1747,36 @@
 %22 +%0A            
+    
 that.getAntlrTre
@@ -1806,16 +1806,17 @@
 InLine()
+)
 ;%0A      
@@ -1821,14 +1821,22 @@
     
-broken
+expectingError
  = b

log all the assertion failures