aboutsummaryrefslogtreecommitdiffstats
path: root/test/eu/equalparts/cardbase/sorting/CardSortingTest.java
blob: 2e4f0e6d3ab9304e991bfd97135b829a73516d00 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package eu.equalparts.cardbase.sorting;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.fasterxml.jackson.core.type.TypeReference;

import eu.equalparts.cardbase.card.Card;
import eu.equalparts.cardbase.json.JSON;

/**
 * Tests the sorting functionality.
 * 
 * @author Eduardo Pedroni
 *
 */
public class CardSortingTest {
	private static List<Card> testCards;
	
	@Rule
	public final ExpectedException exception = ExpectedException.none();

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		testCards = JSON.mapper.readValue(CardSortingTest.class.getResourceAsStream("/testcards.json"), new TypeReference<List<Card>>() {});
	}

	@Test
	public void sortByName() throws Exception {
		CardSorting.sortByField(testCards, "name");
		int i = 0;
		String[] names = {
				"Callow Jushi",
				"Coerced Confession",
				"Disrupting Shoal",
				"Khalni Hydra",
				"Nightmare",
				"Shivan Dragon",
				"Sorin Markov",
				"Ugin's Construct",
		};
		for (Card card : testCards) {
			assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i]));
			i++;
		}
	}

	@Test
	public void sortByLayout() throws Exception {
		CardSorting.sortByField(testCards, "layout");
		int i = 0;
		String[] layouts = {
				"flip",
				"normal",
				"normal",
				"normal",
				"normal",
				"normal",
				"normal",
				"normal",
		};
		for (Card card : testCards) {
			assertTrue(card.layout.get() + " should have been " + layouts[i] + ", i = " + i, card.layout.get().equals(layouts[i]));
			i++;
		}
	}

	@Test
	public void sortByManaCost() throws Exception {
		CardSorting.sortByField(testCards, "manaCost");
		int i = 0;
		String[] costs = {
				"{1}{U}{U}",
				"{3}{B}{B}{B}",
				"{4}",
				"{4}{R}{R}",
				"{4}{U/B}",
				"{5}{B}",
				"{G}{G}{G}{G}{G}{G}{G}{G}",
				"{X}{U}{U}",
		};
		for (Card card : testCards) {
			assertTrue(card.manaCost.get() + " should have been " + costs[i] + ", i = " + i, card.manaCost.get().equals(costs[i]));
			i++;
		}
	}

	@Test
	public void sortByCMC() throws Exception {
		CardSorting.sortByField(testCards, "cmc");
		int i = 0;
		Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8};
		for (Card card : testCards) {
			assertTrue(card.cmc.get() + " should have been " + cmcs[i] + ", i = " + i, card.cmc.get().equals(cmcs[i]));
			i++;
		}
	}

	@Test
	public void sortByType() throws Exception {
		CardSorting.sortByField(testCards, "type");
		int i = 0;
		String[] types = {
				"Artifact Creature — Construct",
				"Creature — Dragon",
				"Creature — Human Wizard",
				"Creature — Hydra",
				"Creature — Nightmare Horse",
				"Instant — Arcane",
				"Planeswalker — Sorin",
				"Sorcery",
		};
		for (Card card : testCards) {
			assertTrue(card.type.get() + " should have been " + types[i] + ", i = " + i, card.type.get().equals(types[i]));
			i++;
		}
	}

	@Test
	public void sortByRarity() throws Exception {
		CardSorting.sortByField(testCards, "rarity");
		int i = 0;
		String[] rarities = {
				"Uncommon",
				"Uncommon",
				"Uncommon",
				"Rare",
				"Rare",
				"Rare",
				"Mythic Rare",
				"Mythic Rare",
		};
		for (Card card : testCards) {
			assertTrue(card.rarity.get() + " should have been " + rarities[i] + ", i = " + i, card.rarity.get().equals(rarities[i]));
			i++;
		}
	}

	@Test
	public void sortByText() throws Exception {
		CardSorting.sortByField(testCards, "text");
		int i = 0;
		String[] texts = {
				"+2: Sorin Markov deals 2 damage to target creature or player and you gain 2 life.\n−3: Target opponent's life total becomes 10.\n−7: You control target player during that player's next turn.",
				"Flying (This creature can't be blocked except by creatures with flying or reach.)\nNightmare's power and toughness are each equal to the number of Swamps you control.",
				"Flying (This creature can't be blocked except by creatures with flying or reach.)\n{R}: Shivan Dragon gets +1/+0 until end of turn.",
				"Khalni Hydra costs {G} less to cast for each green creature you control.\nTrample",
				"Target player puts the top four cards of his or her library into his or her graveyard. You draw a card for each creature card put into that graveyard this way.",
				"When Ugin's Construct enters the battlefield, sacrifice a permanent that's one or more colors.",
				"Whenever you cast a Spirit or Arcane spell, you may put a ki counter on Callow Jushi.\nAt the beginning of the end step, if there are two or more ki counters on Callow Jushi, you may flip it.",
				"You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.\nCounter target spell if its converted mana cost is X.",
		};
		for (Card card : testCards) {
			assertTrue(card.text.get() + " should have been " + texts[i] + ", i = " + i, card.text.get().equals(texts[i]));
			i++;
		}
	}
	
	@Test
	public void sortByFlavor() throws Exception {
		CardSorting.sortByField(testCards, "flavor");
		int i = 0;
		String[] flavors = {
				"",
				"",
				"",
				"\"Ask the right questions in the right way and truth is inevitable.\"\n—Lazav",
				"\"In ages past, bargains were struck and promises were made. Now we must collect on our debt. Begin the hymns.\"\n—Moruul, Khalni druid",
				"The thunder of its hooves beats dreams into despair.",
				"The undisputed master of the mountains of Shiv.",
				"While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.",
		};
		for (Card card : testCards) {
			String flavor = card.flavor.get() != null ? card.flavor.get() : "";
			assertTrue(flavor + " should have been " + flavors[i] + ", i = " + i, flavor.equals(flavors[i]));
			i++;
		}
	}
	
	@Test
	public void sortByArtist() throws Exception {
		CardSorting.sortByField(testCards, "artist");
		int i = 0;
		String[] artists = {
				"Donato Giancola",
				"Mathias Kollros",
				"Michael Komarck",
				"Peter Mohrbacher",
				"Scott M. Fischer",
				"Todd Lockwood",
				"Tsutomu Kawade",
				"Vance Kovacs",
		};
		for (Card card : testCards) {
			assertTrue(card.artist.get() + " should have been " + artists[i] + ", i = " + i, card.artist.get().equals(artists[i]));
			i++;
		}
	}
	
	@Test
	public void sortByNumber() throws Exception {
		CardSorting.sortByField(testCards, "number");
		int i = 0;
		String[] numbers = {
				"31a",
				"33",
				"109",
				"164",
				"192",
				"217",
				"276",
				"281",
		};
		for (Card card : testCards) {
			assertTrue(card.number.get() + " should have been " + numbers[i] + ", i = " + i, card.number.get().equals(numbers[i]));
			i++;
		}
	}
	
	@Test
	public void sortByPower() throws Exception {
		CardSorting.sortByField(testCards, "power");
		int i = 0;
		String[] powers = {
				"",
				"",
				"",
				"*",
				"2",
				"4",
				"5",
				"8",
		};
		for (Card card : testCards) {
			String power = card.power.get() != null ? card.power.get() : "";
			assertTrue(power + " should have been " + powers[i] + ", i = " + i, power.equals(powers[i]));
			i++;
		}
	}
	
	@Test
	public void sortByToughness() throws Exception {
		CardSorting.sortByField(testCards, "power");
		int i = 0;
		String[] toughnesses = {
				"",
				"",
				"",
				"*",
				"2",
				"5",
				"5",
				"8",
		};
		for (Card card : testCards) {
			String toughness = card.toughness.get() != null ? card.toughness.get() : "";
			assertTrue(toughness + " should have been " + toughnesses[i] + ", i = " + i, toughness.equals(toughnesses[i]));
			i++;
		}
	}
	
	@Test
	public void sortByLoyalty() throws Exception {
		CardSorting.sortByField(testCards, "loyalty");
		int i = 0;
		Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4};
		for (Card card : testCards) {
			Integer loyalty = card.loyalty.get() != null ? card.loyalty.get() : 0;
			assertTrue(loyalty + " should have been " + loyalties[i] + ", i = " + i, loyalty.equals(loyalties[i]));
			i++;
		}
	}
	
	@Test
	public void sortByMultiverseID() throws Exception {
		CardSorting.sortByField(testCards, "multiverseid");
		int i = 0;
		Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 };
		for (Card card : testCards) {
			Integer id = card.multiverseid.get() != null ? card.multiverseid.get() : 0;
			assertTrue(id + " should have been " + ids[i] + ", i = " + i, id.equals(ids[i]));
			i++;
		}
	}
	
	@Test
	public void sortByImageName() throws Exception {
		CardSorting.sortByField(testCards, "imageName");
		int i = 0;
		String[] names = {
				"callow jushi",
				"coerced confession",
				"disrupting shoal",
				"khalni hydra",
				"nightmare",
				"shivan dragon",
				"sorin markov",
				"ugin's construct",
		};
		for (Card card : testCards) {
			assertTrue(card.imageName.get() + " should have been " + names[i] + ", i = " + i, card.imageName.get().equals(names[i]));
			i++;
		}
	}
	
	@Test
	public void sortByWatermark() throws Exception {
		CardSorting.sortByField(testCards, "watermark");
		int i = 0;
		String[] watermarks = {
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"Dimir",
		};
		for (Card card : testCards) {
			String watermark = card.watermark.get() != null ? card.watermark.get() : "";
			assertTrue(watermark + " should have been " + watermarks[i] + ", i = " + i, watermark.equals(watermarks[i]));
			i++;
		}
	}
	
	@Test
	public void sortBySetCode() throws Exception {
		CardSorting.sortByField(testCards, "setCode");
		int i = 0;
		String[] sets = {
				"BOK",
				"BOK",
				"FRF",
				"GTC",
				"M12",
				"M15",
				"M15",
				"ROE",
		};
		for (Card card : testCards) {
			assertTrue(card.setCode.get() + " should have been " + sets[i] + ", i = " + i, card.setCode.get().equals(sets[i]));
			i++;
		}
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void sortFieldDoesNotExist() throws Exception {
		exception.expect(NoSuchFieldException.class);
		CardSorting.sortByField(testCards, "not a field name");
	}
}