Coverage Report - org.truth0.subjects.CollectionSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionSubject
0%
0/7
0%
0/2
1.654
CollectionSubject$1
0%
0/36
0%
0/18
1.654
CollectionSubject$Has
N/A
N/A
1.654
CollectionSubject$InOrder
0%
0/16
0%
0/12
1.654
 
 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.countDuplicates;
 22  
 
 23  
 import com.google.common.annotations.GwtCompatible;
 24  
 
 25  
 import org.truth0.FailureStrategy;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collection;
 29  
 import java.util.Iterator;
 30  
 
 31  
 @GwtCompatible
 32  
 public class CollectionSubject<S extends CollectionSubject<S, T, C>, T, C extends Collection<T>> extends IterableSubject<S, T, C> {
 33  
 
 34  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 35  
   public static <T, C extends Collection<T>> CollectionSubject<? extends CollectionSubject<?, T, C>, T, C> create(
 36  
       FailureStrategy failureStrategy, Collection<T> list) {
 37  0
     return new CollectionSubject(failureStrategy, list);
 38  
   }
 39  
 
 40  
   // TODO: Arguably this should even be package private
 41  
   protected CollectionSubject(FailureStrategy failureStrategy, C list) {
 42  0
     super(failureStrategy, list);
 43  0
   }
 44  
 
 45  
   /**
 46  
    * Attests that a Collection is empty or fails.
 47  
    */
 48  
   @Override public void isEmpty() {
 49  0
     if (!getSubject().isEmpty()) {
 50  0
       fail("is empty");
 51  
     }
 52  0
   }
 53  
 
 54  
   public Has<T, C> has() {
 55  0
     return new Has<T, C>() {
 56  
       @Override public void item(T item) {
 57  0
         if (!getSubject().contains(item)) {
 58  0
           fail("has item", item);
 59  
         }
 60  0
       }
 61  
 
 62  
       @Override public void anyOf(T first) {
 63  0
         anyFrom(accumulate(first));
 64  0
       }
 65  
       @SafeVarargs @Override public final void anyOf(T first, T second, T ... rest) {
 66  0
         anyFrom(accumulate(first, second, rest));
 67  0
       }
 68  
       @Override public void anyFrom(Collection<T> col) {
 69  0
         for (Object item : col) {
 70  0
           if (getSubject().contains(item)) {
 71  0
             return;
 72  
           }
 73  0
         }
 74  0
         fail("contains", col);
 75  0
       }
 76  
 
 77  
       @Override public Ordered allOf(T first) {
 78  0
         return allFrom(accumulate(first));
 79  
       }
 80  
       @SafeVarargs @Override public final Ordered allOf(T first, T second, T ... rest) {
 81  0
         return allFrom(accumulate(first, second, rest));
 82  
       }
 83  
       @Override public Ordered allFrom(final Collection<T> required) {
 84  0
         Collection<T> toRemove = new ArrayList<T>(required);
 85  
         // remove each item in the subject, as many times as it occurs in the subject.
 86  0
         for (Object item : getSubject()) {
 87  0
           toRemove.remove(item);
 88  0
         }
 89  0
         if (!toRemove.isEmpty()) {
 90  0
           failWithBadResults("has all of", required, "is missing", countDuplicates(toRemove));
 91  
         }
 92  0
         return new InOrder("has all in order", required);
 93  
       }
 94  
 
 95  
       @Override public Ordered exactly(T first) {
 96  0
         return exactlyAs(accumulate(first));
 97  
       }
 98  
       @SafeVarargs @Override public final Ordered exactly(T first, T second, T ... rest) {
 99  0
         return exactlyAs(accumulate(first, second, rest));
 100  
       }
 101  
       @Override public Ordered exactlyAs(Collection<T> required) {
 102  0
         Collection<T> toRemove = new ArrayList<T>(required);
 103  0
         Collection<Object> extra = new ArrayList<Object>();
 104  
         // remove each item in the subject, as many times as it occurs in the subject.
 105  0
         for (Object item : getSubject()) {
 106  0
           if (!toRemove.remove(item)) {
 107  0
             extra.add(item);
 108  
           }
 109  0
         }
 110  0
         if (!toRemove.isEmpty()) {
 111  0
           failWithBadResults("has exactly", required, "is missing", countDuplicates(toRemove));
 112  
         }
 113  0
         if (!extra.isEmpty()) {
 114  0
           failWithBadResults("has exactly", required, "has unexpected items", countDuplicates(extra));
 115  
         }
 116  0
         return new InOrder("has exactly in order", required);
 117  
       }
 118  
     };
 119  
   }
 120  
 
 121  
   private class InOrder implements Ordered {
 122  
     private final String check;
 123  
     private final Collection<T> required;
 124  
 
 125  0
     InOrder(String check, Collection<T> required) {
 126  0
       this.check = check;
 127  0
       this.required = required;
 128  0
     }
 129  
 
 130  
     @Override public void inOrder() {
 131  0
       Iterator<T> actualItems = getSubject().iterator();
 132  0
       for (Object expected : required) {
 133  0
         if (!actualItems.hasNext()) {
 134  0
           fail(check, required);
 135  
         } else {
 136  0
           Object actual = actualItems.next();
 137  0
           if (actual == expected || actual != null && actual.equals(expected)) {
 138  0
             continue;
 139  
           } else {
 140  0
             fail(check, required);
 141  
           }
 142  
         }
 143  0
       }
 144  0
       if (actualItems.hasNext()) {
 145  0
         fail(check, required);
 146  
       }
 147  0
     }
 148  
   }
 149  
 
 150  
   public interface Has<E, C extends Collection<E>> {
 151  
     /**
 152  
      * Attests that a Collection contains at least the item
 153  
      */
 154  
     void item(E item);
 155  
 
 156  
     /**
 157  
      * Attests that a Collection contains at least one of the provided objects
 158  
      * or fails.
 159  
      */
 160  
     void anyOf(E first);
 161  
 
 162  
     /**
 163  
      * Attests that a Collection contains at least one of the provided objects
 164  
      * or fails.
 165  
      */
 166  
     void anyOf(E first, E second, E... rest);
 167  
 
 168  
     /**
 169  
      * Attests that a Collection contains at least one of the objects contained
 170  
      * in the provided collection or fails.
 171  
      */
 172  
     void anyFrom(Collection<E> expected);
 173  
 
 174  
     /**
 175  
      * Attests that a Collection contains at least all of the provided objects
 176  
      * or fails, coping with duplicates in both the Collection and the
 177  
      * parameters.
 178  
      */
 179  
     Ordered allOf(E first);
 180  
 
 181  
     /**
 182  
      * Attests that a Collection contains at least all of the provided objects
 183  
      * or fails, coping with duplicates in both the Collection and the
 184  
      * parameters.
 185  
      */
 186  
     Ordered allOf(E first, E second, E... rest);
 187  
 
 188  
     /**
 189  
      * Attests that a Collection contains at least all of the objects contained
 190  
      * in the provided collection or fails, coping with duplicates in both
 191  
      * the Collection and the parameters.
 192  
      */
 193  
     Ordered allFrom(Collection<E> expected);
 194  
 
 195  
     /**
 196  
      * Attests that a Collection contains at all of the provided objects and
 197  
      * only these objects or fails. This copes with duplicates in both the
 198  
      * Collection and the parameters. It makes no attestation about order
 199  
      * unless {@code inOrder()} is explicitly called.
 200  
      */
 201  
     Ordered exactly(E first);
 202  
 
 203  
     /**
 204  
      * Attests that a Collection contains at all of the provided objects and
 205  
      * only these objects or fails. This copes with duplicates in both the
 206  
      * Collection and the parameters. It makes no attestation about order
 207  
      * unless {@code inOrder()} is explicitly called.
 208  
      */
 209  
     Ordered exactly(E first, E second, E... rest);
 210  
 
 211  
     /**
 212  
      * Attests that a Collection contains at all of the objects contained in the
 213  
      * provided collection and only these objects or fails. This copes with
 214  
      * duplicates in both the Collection and the parameters. It makes no
 215  
      * attestation about order unless {@code inOrder()} is explicitly called.
 216  
      */
 217  
     Ordered exactlyAs(Collection<E> expected);
 218  
   }
 219  
 }