Coverage Report - org.truth0.subjects.IterableSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
IterableSubject
95%
19/20
87%
14/16
2.6
 
 1  
 /*
 2  
  * Copyright (C) 2011 Google, Inc.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.truth0.subjects;
 17  
 
 18  
 import java.util.Arrays;
 19  
 import java.util.Iterator;
 20  
 
 21  
 import org.truth0.FailureStrategy;
 22  
 
 23  
 import com.google.common.annotations.GwtCompatible;
 24  
 
 25  
 /**
 26  
  * @author Kevin Bourrillion
 27  
  */
 28  
 @GwtCompatible
 29  
 public class IterableSubject<S extends IterableSubject<S, T, C>, T, C extends Iterable<T>> extends Subject<S, C> {
 30  
 
 31  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 32  
   public static <T, C extends Iterable<T>> IterableSubject<? extends IterableSubject<?, T, C>, T, C> create(
 33  
       FailureStrategy failureStrategy, Iterable<T> list) {
 34  9
     return new IterableSubject(failureStrategy, list);
 35  
   }
 36  
 
 37  
   // TODO: Arguably this should even be package private
 38  
   protected IterableSubject(FailureStrategy failureStrategy, C list) {
 39  62
     super(failureStrategy, list);
 40  62
   }
 41  
 
 42  
   /**
 43  
    * Attests that the subject holds no more objects, or fails.
 44  
    */
 45  
   public void isEmpty() {
 46  2
     if (getSubject().iterator().hasNext()) {
 47  1
       fail("is empty");
 48  
     }
 49  1
   }
 50  
 
 51  
   /**
 52  
    * Attests that the subject holds one or more objects, or fails
 53  
    */
 54  
   public void isNotEmpty() {
 55  2
     if (!getSubject().iterator().hasNext()) {
 56  1
       fail("is not empty");
 57  
     }
 58  1
   }
 59  
 
 60  
   /**
 61  
    * Asserts that the items are supplied in the order given by the iterable. For
 62  
    * Collections and other things which contain items but may not have guaranteed
 63  
    * iteration order, this method should be overridden.
 64  
    */
 65  
   public void iteratesOverSequence(Object... expectedItems) {
 66  5
     Iterator<T> actualItems = getSubject().iterator();
 67  13
     for (Object expected : expectedItems) {
 68  10
       if (!actualItems.hasNext()) {
 69  1
         fail("iterates through", Arrays.asList(expectedItems));
 70  
       } else {
 71  9
         Object actual = actualItems.next();
 72  9
         if (actual == expected || actual != null && actual.equals(expected)) {
 73  0
           continue;
 74  
         } else {
 75  1
           fail("iterates through", Arrays.asList(expectedItems));
 76  
         }
 77  
       }
 78  
     }
 79  3
     if (actualItems.hasNext()) {
 80  1
       fail("iterates through", Arrays.asList(expectedItems));
 81  
     }
 82  2
   }
 83  
 }