• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Guava Authors
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  * diOBJECTibuted under the License is diOBJECTibuted 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 
17 package com.google.common.base;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.testing.EqualsTester;
23 import com.google.common.testing.EquivalenceTester;
24 import com.google.common.testing.SerializableTester;
25 
26 import junit.framework.TestCase;
27 
28 /**
29  * Unit test for {@link Equivalence}.
30  *
31  * @author Jige Yu
32  */
33 @GwtCompatible(emulated = true)
34 public class EquivalenceTest extends TestCase {
35 
36   @SuppressWarnings("unchecked") // Iterable<String>...
testPairwiseEquivalent()37   public void testPairwiseEquivalent() {
38     EquivalenceTester.of(Equivalences.equals().<String>pairwise())
39         .addEquivalenceGroup(ImmutableList.<String>of())
40         .addEquivalenceGroup(ImmutableList.of("a"))
41         .addEquivalenceGroup(ImmutableList.of("b"))
42         .addEquivalenceGroup(ImmutableList.of("a", "b"), ImmutableList.of("a", "b"))
43         .test();
44   }
45 
testPairwiseEquivalent_equals()46   public void testPairwiseEquivalent_equals() {
47     new EqualsTester()
48         .addEqualityGroup(Equivalences.equals().pairwise(), Equivalences.equals().pairwise())
49         .addEqualityGroup(Equivalences.identity().pairwise())
50         .testEquals();
51   }
52 
53   private enum LengthFunction implements Function<String, Integer> {
54     INSTANCE;
55 
apply(String input)56     @Override public Integer apply(String input) {
57       return input.length();
58     }
59   }
60 
61   private static final Equivalence<String> LENGTH_EQUIVALENCE = Equivalences.equals()
62       .onResultOf(LengthFunction.INSTANCE);
63 
testWrap()64   public void testWrap() {
65     new EqualsTester()
66         .addEqualityGroup(
67             LENGTH_EQUIVALENCE.wrap("hello"),
68             LENGTH_EQUIVALENCE.wrap("hello"),
69             LENGTH_EQUIVALENCE.wrap("world"))
70         .addEqualityGroup(
71             LENGTH_EQUIVALENCE.wrap("hi"),
72             LENGTH_EQUIVALENCE.wrap("yo"))
73         .addEqualityGroup(
74             LENGTH_EQUIVALENCE.wrap(null),
75             LENGTH_EQUIVALENCE.wrap(null))
76         .addEqualityGroup(Equivalences.equals().wrap("hello"))
77         .addEqualityGroup(Equivalences.equals().wrap(null))
78         .testEquals();
79   }
80 
81   @GwtIncompatible("SerializableTester")
testWrapSerialization()82   public void testWrapSerialization() {
83     SerializableTester.reserializeAndAssert(LENGTH_EQUIVALENCE.wrap("hello"));
84   }
85 
86   private static class IntValue {
87     private final int value;
88 
IntValue(int value)89     IntValue(int value) {
90       this.value = value;
91     }
92 
toString()93     @Override public String toString() {
94       return "value = " + value;
95     }
96   }
97 
testOnResultOf()98   public void testOnResultOf() {
99     EquivalenceTester.of(Equivalences.equals().onResultOf(Functions.toStringFunction()))
100         .addEquivalenceGroup(new IntValue(1), new IntValue(1))
101         .addEquivalenceGroup(new IntValue(2))
102         .test();
103   }
104 
testOnResultOf_equals()105   public void testOnResultOf_equals() {
106     new EqualsTester()
107         .addEqualityGroup(
108             Equivalences.identity().onResultOf(Functions.toStringFunction()),
109             Equivalences.identity().onResultOf(Functions.toStringFunction()))
110         .addEqualityGroup(Equivalences.equals().onResultOf(Functions.toStringFunction()))
111         .addEqualityGroup(Equivalences.identity().onResultOf(Functions.identity()))
112         .testEquals();
113   }
114 
testEquivalentTo()115   public void testEquivalentTo() {
116     Predicate<Object> equalTo1 = Equivalences.equals().equivalentTo("1");
117     assertTrue(equalTo1.apply("1"));
118     assertFalse(equalTo1.apply("2"));
119     assertFalse(equalTo1.apply(null));
120     Predicate<Object> isNull = Equivalences.equals().equivalentTo(null);
121     assertFalse(isNull.apply("1"));
122     assertFalse(isNull.apply("2"));
123     assertTrue(isNull.apply(null));
124 
125     new EqualsTester()
126         .addEqualityGroup(equalTo1, Equivalences.equals().equivalentTo("1"))
127         .addEqualityGroup(isNull)
128         .addEqualityGroup(Equivalences.identity().equivalentTo("1"))
129         .testEquals();
130   }
131 }
132