|
17 | 17 | import com.github.nylle.javafixture.testobjects.TestObjectGeneric; |
18 | 18 | import com.github.nylle.javafixture.testobjects.TestPrimitive; |
19 | 19 | import com.github.nylle.javafixture.testobjects.example.IContract; |
| 20 | +import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceTUWithGenericImplementationT; |
| 21 | +import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceTUWithGenericImplementationTU; |
| 22 | +import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceTUWithGenericImplementationU; |
| 23 | +import com.github.nylle.javafixture.testobjects.interfaces.GenericInterfaceWithImplementation; |
| 24 | +import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithGenericImplementation; |
| 25 | +import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithImplementation; |
| 26 | +import com.github.nylle.javafixture.testobjects.interfaces.InterfaceWithoutImplementation; |
20 | 27 | import org.junit.jupiter.api.DisplayName; |
| 28 | +import org.junit.jupiter.api.Nested; |
21 | 29 | import org.junit.jupiter.api.Test; |
22 | 30 |
|
23 | 31 | import java.io.File; |
@@ -65,23 +73,100 @@ void build(Class<?> value, Class<?> expected) { |
65 | 73 | void buildGeneric() { |
66 | 74 | var sut = new SpecimenFactory(new Context(new Configuration())); |
67 | 75 |
|
68 | | - assertThat(sut.build(new SpecimenType<List<String>>(){})).isExactlyInstanceOf(CollectionSpecimen.class); |
69 | | - assertThat(sut.build(new SpecimenType<Map<String, Integer>>(){})).isExactlyInstanceOf(MapSpecimen.class); |
70 | | - assertThat(sut.build(new SpecimenType<Class<String>>(){})).isExactlyInstanceOf(GenericSpecimen.class); |
71 | | - assertThat(sut.build(new SpecimenType<TestObjectGeneric<String, List<Integer>>>(){})).isExactlyInstanceOf(GenericSpecimen.class); |
| 76 | + assertThat(sut.build(new SpecimenType<List<String>>() {})).isExactlyInstanceOf(CollectionSpecimen.class); |
| 77 | + assertThat(sut.build(new SpecimenType<Map<String, Integer>>() {})).isExactlyInstanceOf(MapSpecimen.class); |
| 78 | + assertThat(sut.build(new SpecimenType<Class<String>>() {})).isExactlyInstanceOf(GenericSpecimen.class); |
| 79 | + assertThat(sut.build(new SpecimenType<TestObjectGeneric<String, List<Integer>>>() {})).isExactlyInstanceOf(GenericSpecimen.class); |
72 | 80 | } |
73 | 81 |
|
74 | 82 | @Test |
75 | | - @DisplayName( "when cache contains a predefined value, return this" ) |
76 | | - void buildReturnsCacnedValue() { |
77 | | - var context = new Context( new Configuration() ); |
| 83 | + @DisplayName("when cache contains a predefined value, return this") |
| 84 | + void buildReturnsCachedValue() { |
| 85 | + var context = new Context(new Configuration()); |
78 | 86 | var cachedValue = new TestPrimitive(); |
79 | | - var type = SpecimenType.fromClass( TestPrimitive.class ); |
80 | | - context.overwrite( type, cachedValue ); |
81 | | - var sut = new SpecimenFactory( context ); |
| 87 | + var type = SpecimenType.fromClass(TestPrimitive.class); |
| 88 | + context.overwrite(type, cachedValue); |
| 89 | + var sut = new SpecimenFactory(context); |
82 | 90 |
|
83 | | - assertThat( sut.build( type ) ).isExactlyInstanceOf( PredefinedSpecimen.class ); |
| 91 | + assertThat(sut.build(type)).isExactlyInstanceOf(PredefinedSpecimen.class); |
84 | 92 | } |
85 | 93 |
|
| 94 | + @Nested |
| 95 | + class Interfaces { |
86 | 96 |
|
| 97 | + Context context = new Context(Configuration.configure().experimentalInterfaces(true)); |
| 98 | + |
| 99 | + @TestWithCases |
| 100 | + @TestCase(bool1 = true, class2 = ObjectSpecimen.class) |
| 101 | + @TestCase(bool1 = false, class2 = InterfaceSpecimen.class) |
| 102 | + void interfaceImplementationsAreOnlySupportedIfExperimentalInterfacesAreEnabled(boolean experimental, Class<?> expected) { |
| 103 | + var context = new Context(Configuration.configure().experimentalInterfaces(experimental)); |
| 104 | + |
| 105 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(InterfaceWithImplementation.class))).isExactlyInstanceOf(expected); |
| 106 | + } |
| 107 | + |
| 108 | + @Nested |
| 109 | + @DisplayName("creates InterfaceSpecimen if") |
| 110 | + class CreatesInterfaceSpecimen { |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("no implementations found") |
| 114 | + void createsInterfaceSpecimenIfInterfaceHasNoImplementations() { |
| 115 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(InterfaceWithoutImplementation.class))) |
| 116 | + .isExactlyInstanceOf(InterfaceSpecimen.class); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @DisplayName("implementation is generic and interface is not") |
| 121 | + void createsInterfaceSpecimenIfImplementationIsGenericAndInterfaceIsNot() { |
| 122 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(InterfaceWithGenericImplementation.class))) |
| 123 | + .isExactlyInstanceOf(InterfaceSpecimen.class); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Nested |
| 128 | + @DisplayName("creates ObjectSpecimen if") |
| 129 | + class CreatesObjectSpecimen { |
| 130 | + |
| 131 | + @Test |
| 132 | + @DisplayName("implementation is not generic and interface is not generic") |
| 133 | + void createsObjectSpecimenIfBothImplementationAndInterfaceAreNotGeneric() { |
| 134 | + assertThat(new SpecimenFactory(context).build(SpecimenType.fromClass(InterfaceWithImplementation.class))) |
| 135 | + .isExactlyInstanceOf(ObjectSpecimen.class); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @DisplayName("implementation is not generic and interface is generic") |
| 140 | + void createsObjectSpecimenIfImplementationIsNotGenericAndInterfaceIs() { |
| 141 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericInterfaceWithImplementation<Integer, String>>() {})) |
| 142 | + .isExactlyInstanceOf(ObjectSpecimen.class); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Nested |
| 147 | + @DisplayName("creates GenericSpecimen if") |
| 148 | + class CreatesGenericSpecimen { |
| 149 | + |
| 150 | + @Test |
| 151 | + @DisplayName("implementation is generic and interface is generic") |
| 152 | + void createsGenericSpecimenIfImplementationAndInterfaceAreGeneric() { |
| 153 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericInterfaceTUWithGenericImplementationTU<String, Integer>>() {})) |
| 154 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + @DisplayName("generic implementation only uses first type-argument of generic interface") |
| 159 | + void createsGenericSpecimenIfGenericImplementationOnlyUsesFirstTypeArgumentOfGenericInterface() { |
| 160 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericInterfaceTUWithGenericImplementationT<String, Integer>>() {})) |
| 161 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + @DisplayName("generic implementation only uses second type-argument of generic interface") |
| 166 | + void createsGenericSpecimenIfGenericImplementationOnlyUsesSecondTypeArgumentOfGenericInterface() { |
| 167 | + assertThat(new SpecimenFactory(context).build(new SpecimenType<GenericInterfaceTUWithGenericImplementationU<String, Integer>>() {})) |
| 168 | + .isExactlyInstanceOf(GenericSpecimen.class); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
87 | 172 | } |
0 commit comments