/
IngredientFilterTest.java
188 lines (144 loc) · 7.63 KB
/
IngredientFilterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package mezz.jei.test;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.minecraft.util.NonNullList;
import mezz.jei.api.helpers.IModIdHelper;
import mezz.jei.config.HideModeConfig;
import mezz.jei.config.IHideModeConfig;
import mezz.jei.config.IngredientBlacklistType;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.ingredients.IngredientBlacklistInternal;
import mezz.jei.ingredients.IngredientFilter;
import mezz.jei.ingredients.IngredientListElementFactory;
import mezz.jei.ingredients.IngredientManager;
import mezz.jei.ingredients.ModIngredientRegistration;
import mezz.jei.ingredients.SubtypeManager;
import mezz.jei.load.registration.SubtypeRegistration;
import mezz.jei.test.lib.TestIngredient;
import mezz.jei.test.lib.TestIngredientFilterConfig;
import mezz.jei.test.lib.TestIngredientHelper;
import mezz.jei.test.lib.TestModIdHelper;
import mezz.jei.test.lib.TestPlugin;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class IngredientFilterTest {
private static final int EXTRA_INGREDIENT_COUNT = 5;
@Nullable
private IngredientManager ingredientManager;
@Nullable
private IngredientFilter ingredientFilter;
@Nullable
private NonNullList<IIngredientListElement<?>> baseList;
@Nullable
private IHideModeConfig hideModeConfig;
@Nullable
private IModIdHelper modIdHelper;
@BeforeEach
public void setup() {
TestPlugin testPlugin = new TestPlugin();
SubtypeRegistration subtypeRegistration = new SubtypeRegistration();
testPlugin.registerItemSubtypes(subtypeRegistration);
SubtypeManager subtypeManager = new SubtypeManager(subtypeRegistration);
ModIngredientRegistration modIngredientRegistration = new ModIngredientRegistration(subtypeManager);
testPlugin.registerIngredients(modIngredientRegistration);
IngredientBlacklistInternal blacklist = new IngredientBlacklistInternal();
this.modIdHelper = new TestModIdHelper();
this.ingredientManager = modIngredientRegistration.createIngredientManager(modIdHelper, blacklist, true);
this.baseList = IngredientListElementFactory.createBaseList(ingredientManager);
this.hideModeConfig = new HideModeConfig(null);
TestIngredientFilterConfig ingredientFilterConfig = new TestIngredientFilterConfig();
this.ingredientFilter = new IngredientFilter(blacklist, ingredientFilterConfig, hideModeConfig, ingredientManager, modIdHelper);
}
@Test
public void testSetup() {
Assertions.assertNotNull(ingredientFilter);
Assertions.assertNotNull(baseList);
Assertions.assertNotNull(ingredientManager);
Assertions.assertNotNull(modIdHelper);
ingredientFilter.addIngredients(baseList, ingredientManager, modIdHelper);
List<IIngredientListElement<?>> ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT, ingredientList.size());
}
@Test
public void testAddingAndRemovingIngredients() {
Assertions.assertNotNull(ingredientFilter);
Assertions.assertNotNull(baseList);
Assertions.assertNotNull(ingredientManager);
Assertions.assertNotNull(modIdHelper);
ingredientFilter.addIngredients(baseList, ingredientManager, modIdHelper);
addIngredients(ingredientFilter);
removeIngredients(ingredientFilter);
}
@Test
public void testRebuilding() {
Assertions.assertNotNull(ingredientFilter);
Assertions.assertNotNull(baseList);
Assertions.assertNotNull(ingredientManager);
Assertions.assertNotNull(modIdHelper);
ingredientFilter.addIngredients(baseList, ingredientManager, modIdHelper);
ingredientFilter.modesChanged();
List<IIngredientListElement<?>> ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT, ingredientList.size());
addIngredients(ingredientFilter);
ingredientFilter.modesChanged();
ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT + EXTRA_INGREDIENT_COUNT, ingredientList.size());
removeIngredients(ingredientFilter);
ingredientFilter.modesChanged();
ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT, ingredientList.size());
}
@Test
public void testConfigBlacklist() {
Assertions.assertNotNull(ingredientFilter);
Assertions.assertNotNull(ingredientManager);
Assertions.assertNotNull(baseList);
Assertions.assertNotNull(ingredientManager);
Assertions.assertNotNull(hideModeConfig);
Assertions.assertNotNull(modIdHelper);
ingredientFilter.addIngredients(baseList, ingredientManager, modIdHelper);
TestIngredient blacklistedIngredient = (TestIngredient) baseList.get(0).getIngredient();
TestIngredientHelper testIngredientHelper = new TestIngredientHelper();
hideModeConfig.addIngredientToConfigBlacklist(ingredientFilter, ingredientManager, blacklistedIngredient, IngredientBlacklistType.ITEM, testIngredientHelper);
ingredientFilter.updateHidden();
List<IIngredientListElement<?>> ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT - 1, ingredientList.size());
// test after reloading the ingredient filter
ingredientFilter.modesChanged();
ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT - 1, ingredientList.size());
}
private void addIngredients(IngredientFilter ingredientFilter) {
Assertions.assertNotNull(ingredientManager);
List<TestIngredient> ingredientsToAdd = new ArrayList<>();
for (int i = TestPlugin.BASE_INGREDIENT_COUNT; i < TestPlugin.BASE_INGREDIENT_COUNT + EXTRA_INGREDIENT_COUNT; i++) {
ingredientsToAdd.add(new TestIngredient(i));
}
Assertions.assertEquals(EXTRA_INGREDIENT_COUNT, ingredientsToAdd.size());
List<IIngredientListElement<TestIngredient>> listToAdd = IngredientListElementFactory.createList(ingredientManager, TestIngredient.TYPE, ingredientsToAdd);
Assertions.assertEquals(EXTRA_INGREDIENT_COUNT, listToAdd.size());
ingredientManager.addIngredientsAtRuntime(TestIngredient.TYPE, ingredientsToAdd, ingredientFilter);
Collection<TestIngredient> testIngredients = ingredientManager.getAllIngredients(TestIngredient.TYPE);
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT + EXTRA_INGREDIENT_COUNT, testIngredients.size());
List<IIngredientListElement<?>> ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT + EXTRA_INGREDIENT_COUNT, ingredientList.size());
}
private void removeIngredients(IngredientFilter ingredientFilter) {
Assertions.assertNotNull(ingredientManager);
List<TestIngredient> ingredientsToRemove = new ArrayList<>();
for (int i = TestPlugin.BASE_INGREDIENT_COUNT; i < TestPlugin.BASE_INGREDIENT_COUNT + EXTRA_INGREDIENT_COUNT; i++) {
ingredientsToRemove.add(new TestIngredient(i));
}
Assertions.assertEquals(EXTRA_INGREDIENT_COUNT, ingredientsToRemove.size());
List<IIngredientListElement<TestIngredient>> listToRemove = IngredientListElementFactory.createList(ingredientManager, TestIngredient.TYPE, ingredientsToRemove);
Assertions.assertEquals(EXTRA_INGREDIENT_COUNT, listToRemove.size());
ingredientManager.removeIngredientsAtRuntime(TestIngredient.TYPE, ingredientsToRemove, ingredientFilter);
List<IIngredientListElement<?>> ingredientList = ingredientFilter.getIngredientList("");
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT, ingredientList.size());
Collection<TestIngredient> testIngredients = ingredientManager.getAllIngredients(TestIngredient.TYPE);
Assertions.assertEquals(TestPlugin.BASE_INGREDIENT_COUNT, testIngredients.size());
}
}