Coverage Report - org.truth0.subjects.CollectionSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionSubject
100%
7/7
100%
2/2
1.727
CollectionSubject$1
75%
25/33
100%
14/14
1.727
CollectionSubject$1$1
84%
11/13
83%
10/12
1.727
CollectionSubject$Has
N/A
N/A
1.727
 
 1  
 /*
 2  
  * Copyright (c) 2011 David Saff
 3  
  * Copyright (c) 2011 Christian Gruber
 4  
  * Copyright (c) 2012 Google, Inc.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.truth0.subjects;
 19  
 
 20  
 import static org.truth0.subjects.SubjectUtils.accumulate;
 21  
 import static org.truth0.subjects.SubjectUtils.countOf;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.Collection;
 26  
 import java.util.HashSet;
 27  
 import java.util.Iterator;
 28  
 import java.util.Set;
 29  
 
 30  
 import org.truth0.FailureStrategy;
 31  
 
 32  
 import com.google.common.annotations.GwtCompatible;
 33  
 
 34  
 @GwtCompatible
 35  
 public class CollectionSubject<S extends CollectionSubject<S, T, C>, T, C extends Collection<T>> extends IterableSubject<S, T, C> {
 36  
 
 37  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 38  
   public static <T, C extends Collection<T>> CollectionSubject<? extends CollectionSubject<?, T, C>, T, C> create(
 39  
       FailureStrategy failureStrategy, Collection<T> list) {
 40  23
     return new CollectionSubject(failureStrategy, list);
 41  
   }
 42  
 
 43  
   // TODO: Arguably this should even be package private
 44  
   protected CollectionSubject(FailureStrategy failureStrategy, C list) {
 45  53
     super(failureStrategy, list);
 46  53
   }
 47  
 
 48  
   /**
 49  
    * Attests that a Collection is empty or fails.
 50  
    */
 51  
   @Override public void isEmpty() {
 52  2
     if (!getSubject().isEmpty()) {
 53  1
       fail("is empty");
 54  
     }
 55  1
   }
 56  
 
 57  
   public Has<T, C> has() {
 58  26
     return new Has<T, C>() {
 59  
       @Override public void item(T item) {
 60  4
         if (!getSubject().contains(item)) {
 61  1
           fail("has item", item);
 62  
         }
 63  3
       }
 64  
 
 65  
       @Override public void anyOf(T first) {
 66  0
         anyFrom(accumulate(first));
 67  0
       }
 68  
       @Override public void anyOf(T first, T second, T ... rest) {
 69  6
         anyFrom(accumulate(first, second, rest));
 70  5
       }
 71  
       @Override public void anyFrom(Collection<T> col) {
 72  6
         for (Object item : col) {
 73  11
           if (getSubject().contains(item)) {
 74  4
             return;
 75  
           }
 76  
         }
 77  2
         fail("contains", col);
 78  1
       }
 79  
 
 80  
       @Override public Ordered allOf(T first) {
 81  0
         return allFrom(accumulate(first));
 82  
       }
 83  
       @Override public Ordered allOf(T first, T second, T ... rest) {
 84  16
         return allFrom(accumulate(first, second, rest));
 85  
       }
 86  
       @Override public Ordered allFrom(final Collection<T> required) {
 87  16
         Collection<T> toRemove = new ArrayList<T>(required);
 88  
         // remove each item in the subject, as many times as it occurs in the subject.
 89  16
         for (Object item : getSubject()) {
 90  49
           toRemove.remove(item);
 91  
         }
 92  16
         if (!toRemove.isEmpty()) {
 93  
           // Try and make a useful message when dealing with duplicates.
 94  5
           Set<T> missing = new HashSet<T>(toRemove);
 95  5
           Object[] params = new Object[missing.size()];
 96  5
           int n = 0;
 97  5
           for (T item : missing) {
 98  6
             int count = countOf(item, toRemove);
 99  6
             params[n++] = (count > 1) ? count + " copies of " + item : item;
 100  6
           }
 101  5
           failWithBadResults("has all of", required, "is missing", Arrays.asList(params));
 102  
         }
 103  
 
 104  12
         return new Ordered() {
 105  
           @Override public void inOrder() {
 106  6
             Iterator<T> actualItems = getSubject().iterator();
 107  6
             for (Object expected : required) {
 108  13
               if (!actualItems.hasNext()) {
 109  1
                 fail("has all in order", required);
 110  
               } else {
 111  12
                 Object actual = actualItems.next();
 112  12
                 if (actual == expected || actual != null && actual.equals(expected)) {
 113  0
                   continue;
 114  
                 } else {
 115  2
                   fail("has all in order", required);
 116  
                 }
 117  0
               }
 118  
             }
 119  3
             if (actualItems.hasNext()) {
 120  1
               fail("has all in order", required);
 121  
             }
 122  2
           }
 123  
         };
 124  
       }
 125  
 
 126  
       /*@Override*/ public void exactly(T first) {
 127  0
         exactlyAs(accumulate(first));
 128  0
       }
 129  
       /*@Override*/ public void exactly(T first, T second, T ... rest) {
 130  0
         exactlyAs(accumulate(first, second, rest));
 131  0
       }
 132  
       /*@Override*/ public void exactlyAs(Collection<T> col) {
 133  0
         throw new UnsupportedOperationException("Not yet implemented.");
 134  
       }
 135  
     };
 136  
   }
 137  
 
 138  
   public interface Has<E, C extends Collection<E>> {
 139  
     /**
 140  
      * Attests that a Collection contains at least the item
 141  
      */
 142  
     void item(E item);
 143  
 
 144  
     /**
 145  
      * Attests that a Collection contains at least one of the provided objects
 146  
      * or fails.
 147  
      */
 148  
     void anyOf(E first);
 149  
 
 150  
     /**
 151  
      * Attests that a Collection contains at least one of the provided objects
 152  
      * or fails.
 153  
      */
 154  
     void anyOf(E first, E second, E... rest);
 155  
 
 156  
     /**
 157  
      * Attests that a Collection contains at least one of the objects contained
 158  
      * in the provided collection or fails.
 159  
      */
 160  
     void anyFrom(Collection<E> expected);
 161  
 
 162  
     /**
 163  
      * Attests that a Collection contains at least all of the provided objects
 164  
      * or fails, coping with duplicates in both the Collection and the
 165  
      * parameters.
 166  
      */
 167  
     Ordered allOf(E first);
 168  
 
 169  
     /**
 170  
      * Attests that a Collection contains at least all of the provided objects
 171  
      * or fails, coping with duplicates in both the Collection and the
 172  
      * parameters.
 173  
      */
 174  
     Ordered allOf(E first, E second, E... rest);
 175  
 
 176  
     /**
 177  
      * Attests that a Collection contains at least all of the objects contained
 178  
      * in the provided collection or fails, coping with duplicates in both
 179  
      * the Collection and the parameters.
 180  
      */
 181  
     Ordered allFrom(Collection<E> expected);
 182  
 
 183  
     /**
 184  
      * Attests that a Collection contains at all of the provided objects and
 185  
      * only these objects or fails. This copes with duplicates in both the
 186  
      * Collection and the parameters.
 187  
      */
 188  
     //void exactly(E first);
 189  
 
 190  
     /**
 191  
      * Attests that a Collection contains at all of the provided objects and
 192  
      * only these objects or fails. This copes with duplicates in both the
 193  
      * Collection and the parameters.
 194  
      */
 195  
     //void exactly(E first, E second, E... rest);
 196  
 
 197  
     /**
 198  
      * Attests that a Collection contains at all of the objects contained in the
 199  
      * provided collection and only these objects or fails. This copes with
 200  
      * duplicates in both the Collection and the parameters.
 201  
      */
 202  
     //void exactlyAs(Collection<E> expected);
 203  
   }
 204  
 }