reorganize json compare tests

This commit is contained in:
Relentless 2022-12-05 21:55:59 +01:00
parent df9fc1acd0
commit 5c6916b436
No known key found for this signature in database
GPG key ID: 759D97B8C6F25265
2 changed files with 233 additions and 221 deletions

View file

@ -5,10 +5,7 @@ import com.almostreliable.unified.config.Defaults;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.RecipeTransformer;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.TagMapTests;
import com.almostreliable.unified.utils.UnifyTag;
import com.almostreliable.unified.utils.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.core.Registry;
@ -22,36 +19,59 @@ import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
public class TestUtils {
public final class TestUtils {
public static final String TEST_MOD_1 = "test_mod_1";
public static final String TEST_MOD_2 = "test_mod_2";
public static final String TEST_MOD_3 = "test_mod_3";
public static final String TEST_MOD_4 = "test_mod_4";
public static final String TEST_MOD_5 = "test_mod_5";
public static final List<String> TEST_MOD_PRIORITIES = List.of(TEST_MOD_1,
public static final List<String> TEST_MOD_PRIORITIES = List.of(
TEST_MOD_1,
TEST_MOD_2,
TEST_MOD_3,
TEST_MOD_4,
TEST_MOD_5);
public static UnifyConfig DEFAULT_UNIFY_CONFIG = new UnifyConfig(Defaults.STONE_STRATA,
TEST_MOD_5
);
public static final UnifyConfig DEFAULT_UNIFY_CONFIG = new UnifyConfig(
Defaults.STONE_STRATA,
Defaults.MATERIALS,
Defaults.getTags(Platform.FORGE),
TestUtils.TEST_MOD_PRIORITIES,
TEST_MOD_PRIORITIES,
new HashMap<>(),
new HashSet<>(),
new HashSet<>(),
new HashSet<>(),
new HashSet<>(),
true);
true
);
public static final JsonCompare.CompareSettings DEFAULT_COMPARE_SETTINGS = getDefaultCompareSettings();
public static final JsonCompare.CompareSettings DEFAULT_SHAPED_COMPARE_SETTINGS = getDefaultShapedCompareSettings();
private TestUtils() {}
public static JsonCompare.CompareSettings getDefaultCompareSettings() {
return Defaults.getDefaultDuplicateRules(Platform.FORGE);
}
public static JsonCompare.CompareSettings getDefaultShapedCompareSettings() {
return Defaults.getDefaultDuplicateOverrides(Platform.FORGE).get(new ResourceLocation("crafting_shaped"));
}
public static final ResourceKey<Registry<Item>> FAKE_ITEM_REGISTRY = FakeResourceKeyRegistry.create("item");
public static final UnifyTag<Item> BRONZE_ORES_TAG = tag("forge:ores/bronze");
public static final UnifyTag<Item> INVAR_ORES_TAG = tag("forge:ores/invar");
public static final UnifyTag<Item> TIN_ORES_TAG = tag("forge:ores/tin");
public static final UnifyTag<Item> SILVER_ORES_TAG = tag("forge:ores/silver");
public static final List<UnifyTag<Item>> TEST_ALLOWED_TAGS = List.of(BRONZE_ORES_TAG,
public static final List<UnifyTag<Item>> TEST_ALLOWED_TAGS = List.of(
BRONZE_ORES_TAG,
INVAR_ORES_TAG,
TIN_ORES_TAG,
SILVER_ORES_TAG);
SILVER_ORES_TAG
);
/**
* ResourceKey is null because otherwise tests can't run because Minecraft is not bootstrapped ...
@ -106,4 +126,152 @@ public class TestUtils {
consumer.accept(obj);
return obj;
}
public static final class Recipes {
public static final String SMELTING = """
{
"type": "minecraft:smelting",
"group": "coal",
"ingredient": {
"item": "minecraft:coal_ore"
},
"result": "minecraft:coal",
"experience": 0.1,
"cookingtime": 200
}
""";
public static final String SHAPED_NO_MATCH_1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"ici",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"item": "minecraft:carrot"
}
},
"result": "minecraft:iron_ingot"
}
""";
public static final String SHAPED_NO_MATCH_2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"ici",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"item": "minecraft:pumpkin"
}
},
"result": "minecraft:iron_ingot"
}
""";
public static final String SHAPED_SPECIAL_MATCH_1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
public static final String SHAPED_SPECIAL_MATCH_2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iki",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
public static final String SHAPED_SANITIZE_1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
public static final String SHAPED_SANITIZE_2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": {
"item": "minecraft:iron_ingot",
"count": 1
}
}
""";
public static final String CRUSHING_NESTED_SANITIZE_1 = """
{
"type": "create:crushing",
"ingredients": [{ "tag": "forge:raw_materials/lead" }],
"processingTime": 400,
"results": [
{ "item": "emendatusenigmatica:crushed_lead_ore" },
{ "chance": 0.75, "item": "create:experience_nugget" }
]
}
""";
public static final String CRUSHING_NESTED_SANITIZE_2 = """
{
"type": "create:crushing",
"ingredients": [{ "tag": "forge:raw_materials/lead" }],
"processingTime": 400,
"results": [
{ "count": 1, "item": "emendatusenigmatica:crushed_lead_ore" },
{ "chance": 0.75, "count": 1, "item": "create:experience_nugget" }
]
}
""";
private Recipes() {}
}
}

View file

@ -1,99 +1,77 @@
package com.almostreliable.unified.util;
import com.almostreliable.unified.Platform;
import com.almostreliable.unified.TestUtils;
import com.almostreliable.unified.config.Defaults;
import com.almostreliable.unified.utils.JsonCompare;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
public class JsonCompareTest {
private static final JsonCompare.CompareSettings DEFAULT_COMPARE_SETTINGS = getDefaultCompareSettings();
private static final JsonCompare.CompareSettings DEFAULT_SHAPED_COMPARE_SETTINGS = getDefaultShapedCompareSettings();
private static final String RECIPE = """
{
"type": "minecraft:smelting",
"group": "coal",
"ingredient": {
"item": "minecraft:coal_ore"
},
"result": "minecraft:coal",
"experience": 0.1,
"cookingtime": 200
}
""";
private static JsonCompare.CompareSettings getDefaultCompareSettings() {
return Defaults.getDefaultDuplicateRules(Platform.FORGE);
}
private static JsonCompare.CompareSettings getDefaultShapedCompareSettings() {
return Defaults.getDefaultDuplicateOverrides(Platform.FORGE).get(new ResourceLocation("crafting_shaped"));
}
private static final String EXPERIENCE = "experience";
private static final String COOKING_TIME = "cookingtime";
@Test
public void simpleCompareFirst() {
JsonObject first = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.2));
JsonObject second = TestUtils.json(RECIPE); // 0.1 experience
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.2));
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING); // 0.1 experience
LinkedHashMap<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put("experience", new JsonCompare.LowerRule());
Map<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put(EXPERIENCE, new JsonCompare.LowerRule());
JsonObject result = JsonCompare.compare(rules, first, second);
assertEquals(second, result);
}
@Test
public void simpleCompareSecond() {
JsonObject first = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.05));
JsonObject second = TestUtils.json(RECIPE); // 0.1 experience
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.05));
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING); // 0.1 experience
LinkedHashMap<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put("experience", new JsonCompare.LowerRule());
Map<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put(EXPERIENCE, new JsonCompare.LowerRule());
JsonObject result = JsonCompare.compare(rules, first, second);
assertEquals(first, result);
}
@Test
public void compareHigherWins() {
JsonObject first = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.05));
JsonObject second = TestUtils.json(RECIPE); // 0.1 experience // 0.1 experience
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.05));
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING); // 0.1 experience
LinkedHashMap<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put("experience", new JsonCompare.HigherRule());
Map<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put(EXPERIENCE, new JsonCompare.HigherRule());
JsonObject result = JsonCompare.compare(rules, first, second);
assertEquals(second, result);
}
@Test
public void compareMulti() {
JsonObject a = TestUtils.json(RECIPE, j -> {
j.addProperty("experience", 0.1);
j.addProperty("cookingtime", 100);
JsonObject a = TestUtils.json(TestUtils.Recipes.SMELTING, j -> {
j.addProperty(EXPERIENCE, 0.1);
j.addProperty(COOKING_TIME, 100);
});
JsonObject b = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.1));
JsonObject c = TestUtils.json(RECIPE, j -> {
j.addProperty("experience", 0.1);
j.addProperty("cookingtime", 50);
JsonObject b = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.1));
JsonObject c = TestUtils.json(TestUtils.Recipes.SMELTING, j -> {
j.addProperty(EXPERIENCE, 0.1);
j.addProperty(COOKING_TIME, 50);
});
JsonObject d = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.2));
JsonObject e = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.2));
JsonObject f = TestUtils.json(RECIPE, j -> j.addProperty("experience", 0.1));
JsonObject g = TestUtils.json(RECIPE, j -> {
j.addProperty("experience", 0.2);
j.addProperty("cookingtime", 100);
JsonObject d = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.2));
JsonObject e = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.2));
JsonObject f = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 0.1));
JsonObject g = TestUtils.json(TestUtils.Recipes.SMELTING, j -> {
j.addProperty(EXPERIENCE, 0.2);
j.addProperty(COOKING_TIME, 100);
});
LinkedHashMap<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put("experience", new JsonCompare.HigherRule());
rules.put("cookingtime", new JsonCompare.LowerRule());
Map<String, JsonCompare.Rule> rules = new LinkedHashMap<>();
rules.put(EXPERIENCE, new JsonCompare.HigherRule());
rules.put(COOKING_TIME, new JsonCompare.LowerRule());
List<JsonObject> list = Arrays.asList(a, b, c, d, e, f, g);
list.sort((first, second) -> JsonCompare.compare(first, second, rules));
@ -105,162 +83,51 @@ public class JsonCompareTest {
@Test
public void simpleMatch() {
JsonObject first = TestUtils.json(RECIPE);
JsonObject second = TestUtils.json(RECIPE);
boolean matches = JsonCompare.matches(first, second, DEFAULT_COMPARE_SETTINGS);
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING);
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING);
boolean matches = JsonCompare.matches(first, second, TestUtils.DEFAULT_COMPARE_SETTINGS);
assertTrue(matches);
}
@Test
public void noMatch() {
JsonObject first = TestUtils.json(RECIPE, j -> j.addProperty("experience", 100));
JsonObject second = TestUtils.json(RECIPE);
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 100));
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING);
boolean matches = JsonCompare.matches(first, second, new JsonCompare.CompareSettings());
assertFalse(matches);
}
@Test
public void matchBecauseIgnore() {
JsonObject first = TestUtils.json(RECIPE, j -> j.addProperty("experience", 100));
JsonObject second = TestUtils.json(RECIPE);
var compareSettings = getDefaultCompareSettings();
compareSettings.ignoreField("experience");
JsonObject first = TestUtils.json(TestUtils.Recipes.SMELTING, j -> j.addProperty(EXPERIENCE, 100));
JsonObject second = TestUtils.json(TestUtils.Recipes.SMELTING);
var compareSettings = TestUtils.getDefaultCompareSettings();
compareSettings.ignoreField(EXPERIENCE);
boolean matches = JsonCompare.matches(first, second, compareSettings);
assertTrue(matches);
}
@Test
public void shapedNoMatch() {
String recipe1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"ici",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"item": "minecraft:carrot"
}
},
"result": "minecraft:iron_ingot"
}
""";
String recipe2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"ici",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"item": "minecraft:pumpkin"
}
},
"result": "minecraft:iron_ingot"
}
""";
JsonObject first = TestUtils.json(recipe1);
JsonObject second = TestUtils.json(recipe2);
JsonObject result = JsonCompare.compareShaped(first, second, DEFAULT_SHAPED_COMPARE_SETTINGS);
JsonObject first = TestUtils.json(TestUtils.Recipes.SHAPED_NO_MATCH_1);
JsonObject second = TestUtils.json(TestUtils.Recipes.SHAPED_NO_MATCH_2);
JsonObject result = JsonCompare.compareShaped(first, second, TestUtils.DEFAULT_SHAPED_COMPARE_SETTINGS);
assertNull(result);
}
@Test
public void shapedMatch() {
String recipe1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
String recipe2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iki",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
},
"k": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
JsonObject first = TestUtils.json(recipe1);
JsonObject second = TestUtils.json(recipe2);
JsonObject result = JsonCompare.compareShaped(first, second, DEFAULT_SHAPED_COMPARE_SETTINGS);
public void shapedSpecialMatch() {
JsonObject first = TestUtils.json(TestUtils.Recipes.SHAPED_SPECIAL_MATCH_1);
JsonObject second = TestUtils.json(TestUtils.Recipes.SHAPED_SPECIAL_MATCH_2);
JsonObject result = JsonCompare.compareShaped(first, second, TestUtils.DEFAULT_SHAPED_COMPARE_SETTINGS);
assertEquals(first, result);
}
@Test
public void sanitizeImplicitCount() {
String recipe1 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": "minecraft:iron_ingot"
}
""";
String recipe2 = """
{
"type": "minecraft:crafting_shaped",
"pattern": [
"iii",
"iii",
"iii"
],
"key": {
"i": {
"tag": "forge:raw_materials/iron"
}
},
"result": {
"item": "minecraft:iron_ingot",
"count": 1
}
}
""";
JsonObject first = TestUtils.json(recipe1);
JsonObject second = TestUtils.json(recipe2);
var compareSettings = getDefaultShapedCompareSettings();
JsonObject first = TestUtils.json(TestUtils.Recipes.SHAPED_SANITIZE_1);
JsonObject second = TestUtils.json(TestUtils.Recipes.SHAPED_SANITIZE_2);
var compareSettings = TestUtils.getDefaultShapedCompareSettings();
compareSettings.setShouldSanitize(true);
JsonObject result = JsonCompare.compareShaped(first, second, compareSettings);
assertEquals(first, result);
@ -268,32 +135,9 @@ public class JsonCompareTest {
@Test
public void sanitizeImplicitCountNested() {
String recipe1 = """
{
"type": "create:crushing",
"ingredients": [{ "tag": "forge:raw_materials/lead" }],
"processingTime": 400,
"results": [
{ "item": "emendatusenigmatica:crushed_lead_ore" },
{ "chance": 0.75, "item": "create:experience_nugget" }
]
}
""";
String recipe2 = """
{
"type": "create:crushing",
"ingredients": [{ "tag": "forge:raw_materials/lead" }],
"processingTime": 400,
"results": [
{ "count": 1, "item": "emendatusenigmatica:crushed_lead_ore" },
{ "chance": 0.75, "count": 1, "item": "create:experience_nugget" }
]
}
""";
JsonObject first = TestUtils.json(recipe1);
JsonObject second = TestUtils.json(recipe2);
var compareSettings = getDefaultCompareSettings();
JsonObject first = TestUtils.json(TestUtils.Recipes.CRUSHING_NESTED_SANITIZE_1);
JsonObject second = TestUtils.json(TestUtils.Recipes.CRUSHING_NESTED_SANITIZE_2);
var compareSettings = TestUtils.getDefaultCompareSettings();
compareSettings.setShouldSanitize(true);
boolean result = JsonCompare.matches(first, second, compareSettings);
assertTrue(result);