Coverage Report - org.truth0.subjects.MapSubject
 
Classes in this File Line Coverage Branch Coverage Complexity
MapSubject
0%
0/21
0%
0/12
2
MapSubject$1
0%
0/5
0%
0/8
2
MapSubject$WithValue
N/A
N/A
2
 
 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  
 import java.util.Arrays;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.truth0.FailureStrategy;
 23  
 
 24  
 import com.google.common.annotations.GwtCompatible;
 25  
 
 26  
 /**
 27  
  * @author Christian Gruber (cgruber@israfil.net)
 28  
  */
 29  
 @GwtCompatible
 30  
 public class MapSubject<S extends MapSubject<S, K, V, M>, K, V, M extends Map<K, V>> extends Subject<S, M> {
 31  
 
 32  
   public MapSubject(FailureStrategy failureStrategy, M map) {
 33  0
     super(failureStrategy, map);
 34  0
   }
 35  
 
 36  
   /**
 37  
    * Attests that the subject holds no objects, or fails.
 38  
    */
 39  
   public void isEmpty() {
 40  0
     if (!getSubject().isEmpty()) {
 41  0
       fail("is empty");
 42  
     }
 43  0
   }
 44  
 
 45  
   /**
 46  
    * Attests that the subject holds one or more objects, or fails
 47  
    */
 48  
   public void isNotEmpty() {
 49  0
     if (getSubject().isEmpty()) {
 50  0
       fail("is not empty");
 51  
     }
 52  0
   }
 53  
 
 54  
   /**
 55  
    * Attests that the subject contains the provided key or fails.
 56  
    */
 57  
   public WithValue<V> hasKey(final K key) {
 58  0
     if (!getSubject().containsKey(key)) {
 59  0
       fail("has key", key);
 60  
     }
 61  0
     return new WithValue<V>() {
 62  
       @Override public void withValue(V expected) {
 63  0
         V actual = getSubject().get(key);
 64  0
         if ((actual == null && expected != null) ||
 65  
             !(actual == expected || actual.equals(expected))) {
 66  0
           fail("has key/value pair", Arrays.asList(key, expected),
 67  
               "actually has key/value pair", Arrays.asList(key, actual));
 68  
         }
 69  0
       }
 70  
     };
 71  
   }
 72  
 
 73  
   public void lacksKey(K key) {
 74  0
     if (getSubject().containsKey(key)) {
 75  0
       fail("lacks key", key);
 76  
     }
 77  0
   }
 78  
 
 79  
   public void hasValue(V key) {
 80  0
     if (!getSubject().containsValue(key)) {
 81  0
       fail("has value", key);
 82  
     }
 83  0
   }
 84  
 
 85  
   public void lacksValue(V key) {
 86  0
     if (getSubject().containsValue(key)) {
 87  0
       fail("lacks value", key);
 88  
     }
 89  0
   }
 90  
 
 91  
   public interface WithValue<V> {
 92  
     void withValue(V value);
 93  
   }
 94  
 
 95  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 96  
   public static <K, V, M extends Map<K, V>> MapSubject<? extends MapSubject<?, K, V, M>, K, V, M> create(
 97  
       FailureStrategy failureStrategy, Map<K, V> map) {
 98  0
     return new MapSubject(failureStrategy, map);
 99  
   }
 100  
 
 101  
 }