Search in sources :

Example 56 with EqualsTester

use of com.google.common.testing.EqualsTester in project auto by google.

the class AutoValueTest method testGenericProperties.

@Test
public void testGenericProperties() throws Exception {
    GenericProperties instance1 = GenericProperties.create(ImmutableMap.of("twenty-three", 23), ImmutableMap.of("very", (Map<String, Integer>) ImmutableMap.of("hairy", 17)));
    GenericProperties instance2 = GenericProperties.create(ImmutableMap.of("seventeen", 17), ImmutableMap.of("very", (Map<String, Integer>) ImmutableMap.of("hairy", 23)));
    new EqualsTester().addEqualityGroup(instance1).addEqualityGroup(instance2).testEquals();
    assertEquals(ImmutableMap.of("very", (Map<String, Integer>) ImmutableMap.of("hairy", 23)), instance2.hairyMap());
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 57 with EqualsTester

use of com.google.common.testing.EqualsTester in project auto by google.

the class AutoValueTest method testSimple.

@Test
public void testSimple() throws Exception {
    Simple instance1a = Simple.create("example", 23, ImmutableMap.of("twenty-three", 23L));
    Simple instance1b = Simple.create("example", 23, ImmutableMap.of("twenty-three", 23L));
    Simple instance2 = Simple.create("", 0, ImmutableMap.<String, Long>of());
    assertEquals("example", instance1a.publicString());
    assertEquals(23, instance1a.protectedInt());
    assertEquals(ImmutableMap.of("twenty-three", 23L), instance1a.packageMap());
    MoreObjects.ToStringHelper toStringHelper = MoreObjects.toStringHelper(Simple.class);
    toStringHelper.add("publicString", "example");
    toStringHelper.add("protectedInt", 23);
    toStringHelper.add("packageMap", ImmutableMap.of("twenty-three", 23L));
    assertEquals(toStringHelper.toString(), instance1a.toString());
    new EqualsTester().addEqualityGroup(instance1a, instance1b).addEqualityGroup(instance2).testEquals();
}
Also used : MoreObjects(com.google.common.base.MoreObjects) EqualsTester(com.google.common.testing.EqualsTester) Test(org.junit.Test)

Example 58 with EqualsTester

use of com.google.common.testing.EqualsTester in project auto by google.

the class AutoAnnotationDefaultsTest method testDefaults.

@Test
public void testDefaults() throws Exception {
    @EverythingWithDefaults
    class Annotated {
    }
    EverythingWithDefaults expected = Annotated.class.getAnnotation(EverythingWithDefaults.class);
    EverythingWithDefaults actual = newEverythingWithDefaults();
    // Iterate over the annotation members to see if any differ. We could just compare expected and
    // actual but if the comparison failed it could be hard to see exactly what differed.
    StringBuilder differencesBuilder = new StringBuilder();
    for (Method member : EverythingWithDefaults.class.getDeclaredMethods()) {
        String name = member.getName();
        Object expectedValue = member.invoke(expected);
        Object actualValue = member.invoke(actual);
        if (!equal(expectedValue, actualValue)) {
            differencesBuilder.append("For ").append(name).append(" expected <").append(string(expectedValue)).append("> but was <").append(string(actualValue)).append(">\n");
        }
    }
    String differences = differencesBuilder.toString();
    assertTrue(differences, differences.isEmpty());
    // All the members were the same. Check that the equals and hashCode results say so too.
    new EqualsTester().addEqualityGroup(expected, actual).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 59 with EqualsTester

use of com.google.common.testing.EqualsTester in project guava by hceylan.

the class PredicatesTest method testIn_equality.

public void testIn_equality() {
    Collection<Integer> nums = ImmutableSet.of(1, 5);
    Collection<Integer> sameOrder = ImmutableSet.of(1, 5);
    Collection<Integer> differentOrder = ImmutableSet.of(5, 1);
    Collection<Integer> differentNums = ImmutableSet.of(1, 3, 5);
    new EqualsTester().addEqualityGroup(Predicates.in(nums), Predicates.in(nums), Predicates.in(sameOrder), Predicates.in(differentOrder)).addEqualityGroup(Predicates.in(differentNums)).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester)

Example 60 with EqualsTester

use of com.google.common.testing.EqualsTester in project guava by hceylan.

the class FunctionsTest method testForMapWithoutDefault.

public void testForMapWithoutDefault() {
    Map<String, Integer> map = Maps.newHashMap();
    map.put("One", 1);
    map.put("Three", 3);
    map.put("Null", null);
    Function<String, Integer> function = Functions.forMap(map);
    assertEquals(1, function.apply("One").intValue());
    assertEquals(3, function.apply("Three").intValue());
    assertNull(function.apply("Null"));
    try {
        function.apply("Two");
        fail();
    } catch (IllegalArgumentException expected) {
    }
    new EqualsTester().addEqualityGroup(function, Functions.forMap(map)).addEqualityGroup(Functions.forMap(map, 42)).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester)

Aggregations

EqualsTester (com.google.common.testing.EqualsTester)177 Test (org.junit.Test)83 GwtIncompatible (com.google.common.annotations.GwtIncompatible)10 ParameterizedType (java.lang.reflect.ParameterizedType)10 WildcardType (java.lang.reflect.WildcardType)8 Method (java.lang.reflect.Method)7 Entry (java.util.Map.Entry)7 GenericArrayType (java.lang.reflect.GenericArrayType)6 Type (java.lang.reflect.Type)6 ImmutableList (com.google.common.collect.ImmutableList)5 Path (com.google.devtools.build.lib.vfs.Path)5 List (java.util.List)5 Set (java.util.Set)5 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)4 HashMap (java.util.HashMap)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 Label (com.google.devtools.build.lib.cmdline.Label)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3