• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.matchers;
6 
7 import org.junit.Test;
8 
9 import java.util.ArrayList;
10 import java.util.List;
11 
12 import static org.assertj.core.api.Assertions.assertThat;
13 
14 public class InstanceOfTest {
15 
16     @Test
should_describe_the_matcher()17     public void should_describe_the_matcher() {
18         assertThat(new InstanceOf(Object.class).toString()).contains("isA")
19                                                            .contains("Object");
20         assertThat(new InstanceOf(Object[].class).toString()).contains("isA")
21                                                            .contains("Object[]");
22         assertThat(new InstanceOf(Object.class, "matches something").toString()).isEqualTo("matches something");
23     }
24 
25     @Test
should_check_instance_type()26     public void should_check_instance_type() {
27         assertThat(new InstanceOf(Object.class).matches(new Object())).isTrue();
28         assertThat(new InstanceOf(Object.class).matches(new ArrayList())).isTrue();
29         assertThat(new InstanceOf(List.class).matches(new ArrayList())).isTrue();
30         assertThat(new InstanceOf(List.class).matches(new Object())).isFalse();
31     }
32 
33     @Test
should_check_for_primitive_wrapper_types()34     public void should_check_for_primitive_wrapper_types() {
35         assertThat(new InstanceOf(int.class).matches(1000)).isTrue();
36         assertThat(new InstanceOf(Integer.class).matches(1000)).isTrue();
37         assertThat(new InstanceOf(int.class).matches(new Integer(1000))).isTrue();
38         assertThat(new InstanceOf(Integer.class).matches(new Integer(1000))).isTrue();
39 
40         assertThat(new InstanceOf(double.class).matches(1000.1)).isTrue();
41         assertThat(new InstanceOf(Double.class).matches(1000.1)).isTrue();
42         assertThat(new InstanceOf(double.class).matches(new Double(1000.1))).isTrue();
43         assertThat(new InstanceOf(Double.class).matches(new Double(1000.1))).isTrue();
44 
45         assertThat(new InstanceOf(int.class).matches(1000L)).isFalse();
46         assertThat(new InstanceOf(Integer.class).matches(1000L)).isFalse();
47         assertThat(new InstanceOf(int.class).matches(new Long(1000))).isFalse();
48         assertThat(new InstanceOf(Integer.class).matches(new Long(1000))).isFalse();
49 
50         assertThat(new InstanceOf(long.class).matches(1000L)).isTrue();
51         assertThat(new InstanceOf(Long.class).matches(1000L)).isTrue();
52         assertThat(new InstanceOf(long.class).matches(new Long(1000))).isTrue();
53         assertThat(new InstanceOf(Long.class).matches(new Long(1000))).isTrue();
54 
55         assertThat(new InstanceOf(long.class).matches(1000)).isFalse();
56         assertThat(new InstanceOf(Long.class).matches(1000)).isFalse();
57         assertThat(new InstanceOf(long.class).matches(new Integer(1000))).isFalse();
58         assertThat(new InstanceOf(Long.class).matches(new Integer(1000))).isFalse();
59     }
60 
61     @Test
can_be_vararg_aware()62     public void can_be_vararg_aware() {
63         assertThat(new InstanceOf.VarArgAware(Number[].class)).isInstanceOf(VarargMatcher.class);
64         assertThat(new InstanceOf.VarArgAware(Number[].class).matches(new Integer[0])).isTrue();
65         assertThat(new InstanceOf.VarArgAware(Number[].class).matches(new Number[0])).isTrue();
66         assertThat(new InstanceOf.VarArgAware(Number[].class).matches(new Object[0])).isFalse();
67     }
68 }
69