|
| 1 | +config.target = 'combine' |
| 2 | + |
| 3 | +local combine = reqscript('combine') |
| 4 | +local p = combine.unit_test_hooks |
| 5 | + |
| 6 | +local POWDER_MISC = df.item_type.POWDER_MISC |
| 7 | +local DRINK = df.item_type.DRINK |
| 8 | + |
| 9 | +local function mock_item(item_type, fields) |
| 10 | + local item = { |
| 11 | + getType=function() return item_type end, |
| 12 | + isCrafted=function() return false end, |
| 13 | + } |
| 14 | + for k,v in pairs(fields) do item[k] = v end |
| 15 | + return item |
| 16 | +end |
| 17 | + |
| 18 | +local function mock_dye(color_index, materials) |
| 19 | + return mock_item(POWDER_MISC, { |
| 20 | + mat_type=0, mat_index=0, |
| 21 | + dye_profile={ |
| 22 | + color_index=color_index, |
| 23 | + dye_material=materials, |
| 24 | + dye_matg={}, |
| 25 | + degree={}, |
| 26 | + target_index={}, |
| 27 | + }, |
| 28 | + }) |
| 29 | +end |
| 30 | + |
| 31 | +function test.dye_same_profile_same_key() |
| 32 | + local stack_type = {type_id=POWDER_MISC} |
| 33 | + local dye_a = mock_dye(5, {10}) |
| 34 | + local dye_b = mock_dye(5, {10}) |
| 35 | + expect.eq(p.make_comp_key(stack_type, dye_a), |
| 36 | + p.make_comp_key(stack_type, dye_b)) |
| 37 | +end |
| 38 | + |
| 39 | +function test.dye_different_color_different_key() |
| 40 | + local stack_type = {type_id=POWDER_MISC} |
| 41 | + local dye_a = mock_dye(5, {10}) |
| 42 | + local dye_b = mock_dye(7, {10}) |
| 43 | + expect.ne(p.make_comp_key(stack_type, dye_a), |
| 44 | + p.make_comp_key(stack_type, dye_b)) |
| 45 | +end |
| 46 | + |
| 47 | +function test.dye_mix_different_key_than_component() |
| 48 | + -- a mixed dye shares mat_type/mat_index with its components but has a |
| 49 | + -- different dye_profile; it must not be merged into a component stack |
| 50 | + local stack_type = {type_id=POWDER_MISC} |
| 51 | + local dye_a = mock_dye(5, {10}) |
| 52 | + local dye_mix = mock_dye(5, {10, 20}) |
| 53 | + expect.ne(p.make_comp_key(stack_type, dye_a), |
| 54 | + p.make_comp_key(stack_type, dye_mix)) |
| 55 | +end |
| 56 | + |
| 57 | +function test.powder_without_profile() |
| 58 | + -- non-dye powders have an empty/unset profile and still merge as before |
| 59 | + local stack_type = {type_id=POWDER_MISC} |
| 60 | + local p1 = mock_item(POWDER_MISC, {mat_type=1, mat_index=2}) |
| 61 | + local p2 = mock_item(POWDER_MISC, {mat_type=1, mat_index=2}) |
| 62 | + expect.eq(p.make_comp_key(stack_type, p1), |
| 63 | + p.make_comp_key(stack_type, p2)) |
| 64 | +end |
| 65 | + |
| 66 | +local function find_dye_mat() |
| 67 | + for _, plant in ipairs(df.global.world.raws.plants.all) do |
| 68 | + local mtype = plant.material_defs.type.mill |
| 69 | + local midx = plant.material_defs.idx.mill |
| 70 | + if mtype ~= -1 then |
| 71 | + local matinfo = dfhack.matinfo.decode(mtype, midx) |
| 72 | + if matinfo and matinfo.material.flags.IS_DYE then |
| 73 | + return mtype, midx |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +function test.unprofiled_dye_never_combines() |
| 80 | + -- merchant dyes can lose their dye_profile to a vanilla bug; such items |
| 81 | + -- must not be merged, not even with each other. on real items the |
| 82 | + -- embedded dye_profile is never absent but has color_index -1 |
| 83 | + local mtype, midx = find_dye_mat() |
| 84 | + if not mtype then return end |
| 85 | + local empty_profile = {color_index=-1, dye_material={}, dye_matg={}, |
| 86 | + degree={}, target_index={}} |
| 87 | + local stack_type = {type_id=POWDER_MISC} |
| 88 | + local d1 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=101, |
| 89 | + dye_profile=empty_profile}) |
| 90 | + local d2 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=102, |
| 91 | + dye_profile=empty_profile}) |
| 92 | + local d3 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=103}) |
| 93 | + local d4 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=104, |
| 94 | + dye_profile={color_index=1, dye_material={1}, dye_matg={}, degree={}, |
| 95 | + target_index={}}}) |
| 96 | + expect.ne(p.make_comp_key(stack_type, d1), |
| 97 | + p.make_comp_key(stack_type, d2)) |
| 98 | + expect.ne(p.make_comp_key(stack_type, d1), |
| 99 | + p.make_comp_key(stack_type, d3)) |
| 100 | + expect.ne(p.make_comp_key(stack_type, d1), |
| 101 | + p.make_comp_key(stack_type, d4)) |
| 102 | +end |
| 103 | + |
| 104 | +function test.dye_key_ignores_other_types() |
| 105 | + local stack_type = {type_id=DRINK} |
| 106 | + local d1 = mock_item(DRINK, {mat_type=1, mat_index=2}) |
| 107 | + local d2 = mock_item(DRINK, {mat_type=1, mat_index=2, |
| 108 | + dye_profile={color_index=9, dye_material={1}, |
| 109 | + dye_matg={}, degree={}, |
| 110 | + target_index={}}}) |
| 111 | + expect.eq(p.make_comp_key(stack_type, d1), |
| 112 | + p.make_comp_key(stack_type, d2)) |
| 113 | +end |
0 commit comments