Coverage Report - org.truth0.subjects.ListSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
ListSubject
0%
0/34
0%
0/12
2
ListSubject$1
0%
0/4
0%
0/2
2
ListSubject$2
0%
0/4
0%
0/2
2
ListSubject$3
0%
0/4
0%
0/2
2
ListSubject$4
0%
0/4
0%
0/2
2
ListSubject$PairwiseChecker
N/A
N/A
2
 
 1  
 /*
 2  
  * Copyright (c) 2011 David Beaumont
 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.Comparator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.truth0.FailureStrategy;
 22  
 
 23  
 import com.google.common.annotations.GwtCompatible;
 24  
 
 25  
 @GwtCompatible
 26  
 public class ListSubject<S extends ListSubject<S, T, C>, T, C extends List<T>>
 27  
     extends CollectionSubject<S, T, C> {
 28  
 
 29  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 30  
   public static <T, C extends List<T>> ListSubject<? extends ListSubject<?, T, C>, T, C> create(
 31  
       FailureStrategy failureStrategy, List<T> list) {
 32  0
     return new ListSubject(failureStrategy, list);
 33  
   }
 34  
 
 35  
   protected ListSubject(FailureStrategy failureStrategy, C list) {
 36  0
     super(failureStrategy, list);
 37  0
   }
 38  
 
 39  
   /**
 40  
    * Attests that a List contains the specified sequence.
 41  
    */
 42  
   public void containsSequence(List<?> sequence) {
 43  0
     if (sequence.isEmpty()) {
 44  0
       return;
 45  
     }
 46  0
     List<?> list = getSubject();
 47  
     while (true) {
 48  0
       int first = list.indexOf(sequence.get(0));
 49  0
       if (first < 0) {
 50  0
         break;    // Not found
 51  
       }
 52  0
       int last = first + sequence.size();
 53  0
       if (last > list.size()) {
 54  0
         break;    // Not enough room left
 55  
       }
 56  0
       if (sequence.equals(list.subList(first, last))) {
 57  0
         return;
 58  
       }
 59  0
       list = list.subList(first + 1, list.size());
 60  0
     }
 61  0
     fail("contains sequence", sequence);
 62  0
   }
 63  
 
 64  
   /**
 65  
    * Attests that a List is strictly ordered according to the natural ordering of its elements.
 66  
    * Null elements are not permitted.
 67  
    *
 68  
    * @throws ClassCastException if any pair of elements is not mutually Comparable.
 69  
    * @throws NullPointerException if any element is null.
 70  
    */
 71  
   public void isOrdered() {
 72  0
     pairwiseCheck(new PairwiseChecker<T>() {
 73  
       @SuppressWarnings("unchecked")
 74  
       @Override public void check(T prev, T next) {
 75  0
         if (((Comparable<T>) prev).compareTo(next) >= 0) {
 76  0
           fail("is strictly ordered", prev, next);
 77  
         }
 78  0
       }
 79  
     });
 80  0
   }
 81  
 
 82  
   /**
 83  
    * Attests that a List is partially ordered according to the natural ordering of its elements.
 84  
    * Null elements are not permitted.
 85  
    *
 86  
    * @throws ClassCastException if any pair of elements is not mutually Comparable.
 87  
    * @throws NullPointerException if any element is null.
 88  
    */
 89  
   public void isPartiallyOrdered() {
 90  0
     pairwiseCheck(new PairwiseChecker<T>() {
 91  
       @SuppressWarnings("unchecked")
 92  
       @Override public void check(T prev, T next) {
 93  0
         if (((Comparable<T>) prev).compareTo(next) > 0) {
 94  0
           fail("is partially ordered", prev, next);
 95  
         }
 96  0
       }
 97  
     });
 98  0
   }
 99  
 
 100  
   /**
 101  
    * Attests that a List is strictly ordered according to the given comparator.
 102  
    * Null elements are not permitted.
 103  
    *
 104  
    * @throws ClassCastException if any pair of elements is not mutually Comparable.
 105  
    * @throws NullPointerException if any element is null.
 106  
    */
 107  
   public void isOrdered(final Comparator<T> comparator) {
 108  0
     pairwiseCheck(new PairwiseChecker<T>() {
 109  
       @Override public void check(T prev, T next) {
 110  0
         if (comparator.compare(prev, next) >= 0) {
 111  0
           fail("is strictly ordered", prev, next);
 112  
         }
 113  0
       }
 114  
     });
 115  0
   }
 116  
 
 117  
   /**
 118  
    * Attests that a List is partially ordered according to the given comparator.
 119  
    * Null elements are not permitted.
 120  
    *
 121  
    * @throws ClassCastException if any pair of elements is not mutually Comparable.
 122  
    * @throws NullPointerException if any element is null.
 123  
    */
 124  
   public void isPartiallyOrdered(final Comparator<T> comparator) {
 125  0
     pairwiseCheck(new PairwiseChecker<T>() {
 126  
       @Override public void check(T prev, T next) {
 127  0
         if (comparator.compare(prev, next) > 0) {
 128  0
           fail("is partially ordered", prev, next);
 129  
         }
 130  0
       }
 131  
     });
 132  0
   }
 133  
 
 134  
   private void pairwiseCheck(PairwiseChecker<T> checker) {
 135  0
     List<T> list = getSubject();
 136  0
     if (list.size() > 1) {
 137  0
       T prev = list.get(0);
 138  0
       for (int n = 1; n < list.size(); n++) {
 139  0
         T next = list.get(n);
 140  0
         checker.check(prev, next);
 141  0
         prev = next;
 142  
       }
 143  
     }
 144  0
   }
 145  
 
 146  
   private interface PairwiseChecker<T> {
 147  
     void check(T prev, T next);
 148  
   }
 149  
 }