|
| 1 | +from collections import defaultdict |
| 2 | +from contextlib import ExitStack |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import pytest |
| 7 | +from hexdoc.core.loader import ModResourceLoader |
| 8 | +from hexdoc.core.properties import LangProps, Properties |
| 9 | +from hexdoc.core.resource import ResourceLocation |
| 10 | +from hexdoc.core.resource_dir import PathResourceDir |
| 11 | +from hexdoc.minecraft.assets.textures import TextureContext |
| 12 | +from hexdoc.minecraft.i18n import I18n |
| 13 | +from hexdoc.minecraft.recipe.recipes import CraftingShapelessRecipe, Recipe |
| 14 | +from hexdoc.plugin.manager import PluginManager |
| 15 | +from hexdoc.utils.context import ContextSource |
| 16 | +from pydantic import TypeAdapter |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def context(): |
| 21 | + props = Properties.model_construct() |
| 22 | + |
| 23 | + pm = PluginManager("branch", props=props) |
| 24 | + |
| 25 | + loader = ModResourceLoader( |
| 26 | + props=props, |
| 27 | + export_dir=None, |
| 28 | + resource_dirs=[], |
| 29 | + _stack=ExitStack(), |
| 30 | + ) |
| 31 | + |
| 32 | + i18n = I18n( |
| 33 | + lookup=I18n.parse_lookup( |
| 34 | + { |
| 35 | + "item.minecraft.stick": "Stick", |
| 36 | + "item.minecraft.diamond": "Diamond", |
| 37 | + } |
| 38 | + ), |
| 39 | + lang="en_us", |
| 40 | + default_i18n=None, |
| 41 | + enabled=True, |
| 42 | + lang_props=LangProps(), |
| 43 | + ) |
| 44 | + |
| 45 | + texture_ctx = TextureContext( |
| 46 | + textures=defaultdict(dict), |
| 47 | + allowed_missing_textures={ |
| 48 | + ResourceLocation("minecraft", "*"), |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + context: ContextSource = {} |
| 53 | + for ctx in [ |
| 54 | + props, |
| 55 | + pm, |
| 56 | + loader, |
| 57 | + i18n, |
| 58 | + texture_ctx, |
| 59 | + ]: |
| 60 | + ctx.add_to_context(context) |
| 61 | + |
| 62 | + return context |
| 63 | + |
| 64 | + |
| 65 | +def test_shapeless(context: dict[str, Any]): |
| 66 | + data = { |
| 67 | + "type": "minecraft:crafting_shapeless", |
| 68 | + "ingredients": [{"item": "minecraft:stick"}], |
| 69 | + "result": {"item": "minecraft:diamond"}, |
| 70 | + # for hexdoc |
| 71 | + "id": ResourceLocation("a", "b"), |
| 72 | + "resource_dir": PathResourceDir.model_construct(path=Path()), |
| 73 | + } |
| 74 | + |
| 75 | + recipe = TypeAdapter(Recipe).validate_python(data, context=context) |
| 76 | + |
| 77 | + assert isinstance(recipe, CraftingShapelessRecipe) |
| 78 | + |
| 79 | + |
| 80 | +def test_fabric_load_conditions(context: dict[str, Any]): |
| 81 | + data = { |
| 82 | + "type": "minecraft:crafting_shapeless", |
| 83 | + "ingredients": [{"item": "minecraft:stick"}], |
| 84 | + "result": {"item": "minecraft:diamond"}, |
| 85 | + "fabric:load_conditions": [ |
| 86 | + { |
| 87 | + "condition": "fabric:all_mods_loaded", |
| 88 | + "values": ["fabric-resource-conditions-api-v1"], |
| 89 | + } |
| 90 | + ], |
| 91 | + # for hexdoc |
| 92 | + "id": ResourceLocation("a", "b"), |
| 93 | + "resource_dir": PathResourceDir.model_construct(path=Path()), |
| 94 | + } |
| 95 | + |
| 96 | + recipe = TypeAdapter(Recipe).validate_python(data, context=context) |
| 97 | + |
| 98 | + assert isinstance(recipe, CraftingShapelessRecipe) |
0 commit comments