Coverage Report - org.truth0.subjects.IntegerSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegerSubject
89%
34/38
96%
27/28
2.154
IntegerSubject$1
100%
2/2
N/A
2.154
 
 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.subjects;
 18  
 
 19  
 
 20  
 import org.truth0.FailureStrategy;
 21  
 
 22  
 import com.google.common.annotations.GwtCompatible;
 23  
 
 24  
 /**
 25  
  * Propositions for Integral numeric subjects
 26  
  *
 27  
  * @author David Saff
 28  
  * @author Christian Gruber (cgruber@israfil.net)
 29  
  */
 30  
 @GwtCompatible
 31  
 public class IntegerSubject extends Subject<IntegerSubject, Long> {
 32  
 
 33  
   public IntegerSubject(FailureStrategy failureStrategy, Long i) {
 34  11
     super(failureStrategy, i);
 35  11
   }
 36  
 
 37  
   public IntegerSubject(FailureStrategy failureStrategy, Integer i) {
 38  33
     super(failureStrategy, i == null ? null : Long.valueOf(i.longValue()));
 39  33
   }
 40  
 
 41  
   /**
 42  
    * Attests that a Subject<Integer> is inclusively within the {@code lower} and
 43  
    * {@code upper} bounds provided or fails.
 44  
    *
 45  
    * @throws IllegalArgumentException
 46  
    *           if the lower bound is greater than the upper.
 47  
    */
 48  
   public void isInclusivelyInRange(long lower, long upper) {
 49  6
     ensureOrderedBoundaries(lower, upper);
 50  5
     if (!(lower <= getSubject() && getSubject() <= upper)) {
 51  2
       fail("is inclusively in range", lower, upper);
 52  
     }
 53  3
   }
 54  
 
 55  
   /**
 56  
    * Attests that a Subject<Integer> is exclusively within the {@code lower} and
 57  
    * {@code upper} bounds provided or fails.
 58  
    *
 59  
    * @throws IllegalArgumentException
 60  
    *           if the lower bound is greater than the upper.
 61  
    */
 62  
   public void isBetween(long lower, long upper) {
 63  5
     ensureOrderedBoundaries(lower, upper);
 64  4
     if (!(lower < getSubject() && getSubject() < upper)) {
 65  1
       fail("is in between", lower, upper);
 66  
     }
 67  3
   }
 68  
 
 69  
   /**
 70  
    * Guards against inverted lower/upper boundaries, and throws if
 71  
    * they are so inverted.
 72  
    */
 73  
   private static void ensureOrderedBoundaries(long lower, long upper) {
 74  11
     if (lower > upper) {
 75  2
       throw new IllegalArgumentException(
 76  
           "Range inclusion parameter lower (" + lower + ") "
 77  
               + " should not be greater than upper (" + upper + ")");
 78  
     }
 79  9
   }
 80  
 
 81  
   public void isEqualTo(Integer other) {
 82  11
     isEqualTo((other == null) ? null : Long.valueOf(other.longValue()));
 83  6
   }
 84  
 
 85  
   public void isEqualTo(Long other) {
 86  12
     if (getSubject() == null) {
 87  2
       if(other != null) {
 88  1
         fail("is equal to", other);
 89  
       }
 90  
     } else {
 91  10
       if (!getSubject().equals(other)) {
 92  4
         fail("is equal to", other);
 93  
       }
 94  
     }
 95  7
   }
 96  
 
 97  
   public void isNotEqualTo(Integer other) {
 98  15
     isNotEqualTo((other == null) ? null : Long.valueOf(other.longValue()));
 99  11
   }
 100  
 
 101  
   public void isNotEqualTo(Long other) {
 102  16
     if (getSubject() == null) {
 103  2
       if(other == null) {
 104  1
         fail("is not equal to", (Object)null);
 105  
       }
 106  
     } else {
 107  14
       if (getSubject().equals(other)) {
 108  4
         fail("is not equal to", other);
 109  
       }
 110  
     }
 111  12
   }
 112  
 
 113  
   public void is(int other) {
 114  2
     super.is((long)other);
 115  2
   }
 116  
 
 117  
   public void is(short other) {
 118  0
     super.is((long)other);
 119  0
   }
 120  
 
 121  
   public void is(byte other) {
 122  0
     super.is((long)other);
 123  0
   }
 124  
 
 125  
 
 126  1
   public static final SubjectFactory<IntegerSubject, Long> INTEGER =
 127  7
       new SubjectFactory<IntegerSubject, Long>() {
 128  
         @Override public IntegerSubject getSubject(FailureStrategy fs, Long target) {
 129  6
           return new IntegerSubject(fs, target);
 130  
         }
 131  
       };
 132  
 }