Coverage Report - org.truth0.Expect
 
Classes in this File Line Coverage Branch Coverage Complexity
Expect
100%
11/11
100%
2/2
1.833
Expect$1
100%
10/10
100%
4/4
1.833
Expect$ExpectationGatherer
100%
4/4
N/A
1.833
 
 1  
 /*
 2  
  * Copyright (c) 2011 David Saff
 3  
  * Copyright (c) 2011 Christian Gruber
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.truth0;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.junit.rules.MethodRule;
 23  
 import org.junit.runners.model.FrameworkMethod;
 24  
 import org.junit.runners.model.Statement;
 25  
 
 26  
 import com.google.common.annotations.GwtIncompatible;
 27  
 
 28  
 @GwtIncompatible("JUnit4")
 29  
 @SuppressWarnings("deprecation")
 30  111
 public class Expect extends TestVerb implements MethodRule {
 31  35
   protected static class ExpectationGatherer implements FailureStrategy {
 32  35
     List<String> messages = new ArrayList<String>();
 33  
 
 34  
     @Override public void fail(String message) {
 35  7
       messages.add(message);
 36  7
     }
 37  
   }
 38  
 
 39  
   private final ExpectationGatherer gatherer;
 40  35
   private boolean inRuleContext = false;
 41  
 
 42  
   public static Expect create() {
 43  31
     return new Expect(new ExpectationGatherer());
 44  
   }
 45  
 
 46  
   Expect(ExpectationGatherer gatherer) {
 47  35
     super(gatherer);
 48  35
     this.gatherer = gatherer;
 49  35
   }
 50  
 
 51  
   @Override
 52  
   protected FailureStrategy getFailureStrategy() {
 53  17
           if (!inRuleContext) {
 54  1
                   String message = "assertion made on Expect instance, but it's not enabled as a @Rule.";
 55  1
                         throw new IllegalStateException(message);
 56  
           }
 57  16
           return super.getFailureStrategy();
 58  
   }
 59  
 
 60  
   // TODO(cgruber): Make this override TestRule when 4.9 is released.
 61  
   @Override public Statement apply(final Statement base,
 62  
       FrameworkMethod method, Object target) {
 63  32
     return new Statement() {
 64  
       @Override public void evaluate() throws Throwable {
 65  32
         inRuleContext = true;
 66  32
         base.evaluate();
 67  30
         inRuleContext = false;
 68  30
         if (!gatherer.messages.isEmpty()) {
 69  5
           StringBuilder message = new StringBuilder("All failed expectations:\n");
 70  12
           for (int i = 0; i < gatherer.messages.size(); i++) {
 71  7
             message.append("  ").append(i + 1).append(". ")
 72  
                    .append(gatherer.messages.get(i)).append("\n");
 73  
           }
 74  5
           throw new AssertionError(message.toString());
 75  
         }
 76  25
       }
 77  
     };
 78  
   }
 79  
 }