From 4fb25d04638e27ae40a734bc8515e8c78ed925c3 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 15 Nov 2025 16:32:42 +0100 Subject: [PATCH] Add a complex test for csv parser --- processor/luahelper-test.lua | 84 +++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/processor/luahelper-test.lua b/processor/luahelper-test.lua index eb38523..9bb12cf 100644 --- a/processor/luahelper-test.lua +++ b/processor/luahelper-test.lua @@ -436,28 +436,12 @@ francis_reid_daly 20 0.1 130 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 sh -- Test that numeric indices still work assert(rows[1][1] == "john_hawkwood_boss", "First row first field by index should work") assert(rows[1][2] == "20", "First row second field by index should work") - - -- Debug: Print first row keys to verify headers are assigned - print("DEBUG: First row keys:") - for k, v in pairs(rows[1]) do - if type(k) == "string" then print(" " .. k .. " = " .. tostring(v)) end - end end) test("fromCSV debug header assignment", function() local csv = "Id Name Value\n1 Test 100\n2 Test2 200" local rows, err = fromCSV(csv, { delimiter = "\t", hasheader = true }) if err then error("fromCSV error: " .. err) end - print("DEBUG: Number of rows: " .. #rows) - if #rows > 0 then - print("DEBUG: First row Id = " .. tostring(rows[1].Id)) - print("DEBUG: First row Name = " .. tostring(rows[1].Name)) - print("DEBUG: First row Value = " .. tostring(rows[1].Value)) - print("DEBUG: First row keys:") - for k, v in pairs(rows[1]) do - print(" " .. tostring(k) .. " (" .. type(k) .. ") = " .. tostring(v)) - end - end assert(rows[1].Id == "1", "Id should be '1'") assert(rows[1].Name == "Test", "Name should be 'Test'") assert(rows[1].Value == "100", "Value should be '100'") @@ -471,21 +455,69 @@ francis_reid_daly 20 0.1 130 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 sh ]] local rows, err = fromCSV(csv, { delimiter = "\t", hasheader = true, hascomments = true }) if err then error("fromCSV error: " .. err) end - print("DEBUG: Number of rows: " .. #rows) assert(#rows == 2, "Should have 2 data rows") - if #rows > 0 then - print("DEBUG: First row Id = " .. tostring(rows[1].Id)) - print("DEBUG: First row ModifyStartCost = " .. tostring(rows[1].ModifyStartCost)) - print("DEBUG: First row all string keys:") - for k, v in pairs(rows[1]) do - if type(k) == "string" then print(" " .. tostring(k) .. " = " .. tostring(v)) end - end - end - assert(rows[1].Id == "john_hawkwood_boss", "First row Id should be 'john_hawkwood_boss'") assert(rows[1].ModifyStartCost == "20", "First row ModifyStartCost should be '20'") assert(rows[2].Id == "francis_reid_daly", "Second row Id should be 'francis_reid_daly'") end) +test("full CSV parser complex", function() + local original = [[ +#mercenary_profiles +Id ModifyStartCost ModifyStep ModifyLevelLimit Health ResistSheet WoundSlots MeleeDamage MeleeAccuracy RangeAccuracy ReceiveAmputationChance ReceiveWoundChanceMult AttackWoundChanceMult Dodge Los StarvationLimit PainThresholdLimit PainThresholdRegen TalentPerkId ActorId SkinIndex HairType HairColorHex VoiceBank Immunity CreatureClass +john_hawkwood_boss 20 0.1 140 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 8 16 crit 1.60 critchance 0.05 0.5 0.5 0.03 0.5 1.2 0.3 8 2200 16 2 talent_the_man_who_sold_the_world human_male 0 hair1 #633D08 player Human +francis_reid_daly 20 0.1 130 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 7 14 crit 1.70 critchance 0.05 0.5 0.4 0.04 0.9 1 0.3 8 2000 10 1 talent_weapon_durability human_male 0 player Human +victoria_boudicca 20 0.1 90 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 5 10 crit 1.70 critchance 0.1 0.4 0.45 0.05 1 1.2 0.3 8 1800 8 1 talent_weapon_distance human_female 0 hair1 #633D08 player Human +persival_fawcett 20 0.1 150 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 6 12 crit 1.70 critchance 0.05 0.5 0.35 0.05 0.6 1 0.25 8 2100 16 1 talent_all_resists human_male 1 hair1 #633D08 player Human +Isabella_capet 20 0.1 100 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 7 14 crit 1.70 critchance 0.15 0.55 0.3 0.03 0.8 1.4 0.35 7 1700 14 2 talent_ignore_infection human_female 1 hair3 #FF3100 player Human +maximilian_rohr 20 0.1 120 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 8 16 crit 1.75 critchance 0.05 0.45 0.45 0.06 0.9 1 0.2 8 2000 14 1 talent_ignore_pain human_male 0 hair2 #FFC400 player Human +priya_marlon 20 0.1 110 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 5 10 crit 1.70 critchance 0.15 0.45 0.35 0.05 1 1.1 0.3 7 2200 12 1 talent_all_consumables_stack human_female 0 hair2 #FFC400 player Human +jacques_kennet 20 0.1 120 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 5 10 crit 1.70 critchance 0.05 0.45 0.35 0.04 0.9 1.2 0.3 8 2300 10 1 talent_reload_time human_male 0 hair1 #908E87 player Human +mirza_aishatu 20 0.1 110 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 7 14 crit 1.70 critchance 0.05 0.55 0.45 0.03 1 1.1 0.25 9 2000 10 1 talent_starving_slower human_female 1 hair2 #633D08 player Human +kenzie_yukio 20 0.1 100 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 5 10 crit 1.70 critchance 0.1 0.6 0.4 0.04 1 1 0.4 7 1600 12 1 talent_weight_dodge_affect human_male 0 hair2 #633D08 player Human +marika_wulfnod 20 0.1 100 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 6 12 crit 1.60 critchance 0.05 0.5 0.5 0.04 1 1 0.3 9 1900 12 1 talent_belt_slots human_female 0 hair1 #FFC400 player Human +auberon_lukas 20 0.1 120 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 4 8 crit 1.60 critchance 0.15 0.45 0.45 0.05 0.8 1 0.2 9 1900 8 2 talent_weapon_slot human_male 0 hair2 #633D08 player Human +niko_medich 20 0.1 120 blunt 0 pierce 0 lacer 0 fire 0 cold 0 poison 0 shock 0 beam 0 HumanHead HumanShoulder HumanArm HumanThigh HumanFeet HumanChest HumanBody HumanStomach HumanKnee blunt 5 10 crit 1.70 critchance 0.05 0.4 0.45 0.04 1 1.3 0.25 8 2000 10 1 talent_pistol_acc human_male 0 hair1 #908E87 player Human +#end + +#mercenary_classes +Id ModifyStartCost ModifyStep PerkIds +scouts_of_hades 30 0.1 cqc_specialist_basic military_training_basic gear_maintenance_basic blind_fury_basic fire_transfer_basic assault_reflex_basic +ecclipse_blades 30 0.1 berserkgang_basic athletics_basic reaction_training_basic cold_weapon_wielding_basic cannibalism_basic carnage_basic +tifton_elite 30 0.1 heavy_weaponary_basic grenadier_basic selfhealing_basic stationary_defense_basic spray_and_pray_basic shock_awe_basic +tunnel_rats 30 0.1 cautious_basic handmade_shotgun_ammo_basic marauder_basic dirty_shot_basic vicious_symbiosis_basic covermaster_basic +phoenix_brigade 30 0.1 shielding_basic battle_physicist_basic reinforced_battery_basic revealing_flame_basic cauterize_basic scholar_basic +]] + + -- Parse with headers and comments + local rows, err = fromCSV(original, { delimiter = "\t", hasheader = true, hascomments = true }) + if err then error("fromCSV error: " .. err) end + assert(#rows > 0, "Should have parsed rows") + + -- Convert back to CSV with headers + local csv, err = toCSV(rows, { delimiter = "\t", hasheader = true }) + if err then error("toCSV error: " .. err) end + + -- Parse again + local rows2, err = fromCSV(csv, { delimiter = "\t", hasheader = true, hascomments = false }) + if err then error("fromCSV error: " .. err) end + + -- Verify identical - same number of rows + assert(#rows2 == #rows, "Round trip should have same number of rows") + + -- Verify first row data is identical + assert(rows2[1].Id == rows[1].Id, "Round trip first row Id should match") + assert( + rows2[1].ModifyStartCost == rows[1].ModifyStartCost, + "Round trip first row ModifyStartCost should match" + ) + assert(rows2[1].Health == rows[1].Health, "Round trip first row Health should match") + + -- Verify headers are preserved + assert(rows2[1].Headers ~= nil, "Round trip rows should have Headers field") + assert(#rows2[1].Headers == #rows[1].Headers, "Headers should have same number of elements") + assert(rows2[1].Headers[1] == rows[1].Headers[1], "First header should match") +end) + print("\nAll tests completed!")