Files
fs25-adsmartpickup/tests/test_adGuide.lua
T

114 lines
4.9 KiB
Lua

-- Kør: luajit tests/test_adGuide.lua (fra repo-roden)
dofile("FS25_ADSmartPickup/adGuide.lua")
local G = ADGuide
local failures = 0
local function check(name, actual, expected)
if actual == expected then print("OK " .. name) else
failures = failures + 1
print(string.format("FAIL %s: forventede %s, fik %s", name, tostring(expected), tostring(actual)))
end
end
-- trin a..e + summary; b vises kun når draft.showB, d aldrig
local def = {
order = {"a", "b", "c", "d", "e", "summary"},
isVisible = function(step, guide)
if step == "b" then return guide.draft.showB == true end
if step == "d" then return false end
return true
end,
}
local guide = G.new(def, {showB = false, value = 1}, {x = 1})
check("G1 start på første synlige", guide.step, "a")
local index, total = G.position(guide)
check("G1 position", index, 1)
check("G1 total over synlige", total, 4)
check("G1 synlige", table.concat(G.visibleSteps(guide), ","), "a,c,e,summary")
-- næste springer skjulte over
local g2, event = G.next(guide)
check("G2 næste springer b over", g2.step, "c")
check("G2 intet event", event, nil)
check("G2 gammel guide urørt", guide.step, "a")
local g3 = G.next(g2)
check("G3 springer d over", g3.step, "e")
check("G3 position", (G.position(g3)), 3)
local g4 = G.next(g3)
check("G4 summary er sidst", G.isLast(g4), true)
local _, done = G.next(g4)
check("G4 næste på sidste = done", done, "done")
-- tilbage
local back = G.back(g3)
check("G5 tilbage til c", back.step, "c")
local _, cancel = G.back(guide)
check("G5 tilbage på første = cancel", cancel, "cancel")
-- valg der gør et trin synligt ændrer tælling og næste
local shown = G.withDraft(guide, {showB = true, value = 1})
check("G6 withDraft ny guide", guide.draft.showB, false)
check("G6 b synlig", G.next(shown).step, "b")
check("G6 total", select(2, G.position(shown)), 5)
-- aktuelt trin bliver usynligt: næste går til næste synlige i rækkefølgen, position nil
local onB = G.new(def, {showB = true}, {}, {step = "b"})
local hidden = G.withDraft(onB, {showB = false})
check("G7 position nil for skjult trin", (G.position(hidden)), nil)
check("G7 næste efter skjult", G.next(hidden).step, "c")
check("G7 tilbage fra skjult", G.back(hidden).step, "a")
-- spring (Ret) fra oversigt-trinnet: næste -> tilbage til summary, ikke single
local fromSummary = G.jump(g4, "c", "summary")
check("G8 spring trin", fromSummary.step, "c")
check("G8 single", fromSummary.single, true)
local returned, ev = G.next(fromSummary)
check("G8 retur til summary", returned.step, "summary")
check("G8 ikke single mere", returned.single, false)
check("G8 intet event", ev, nil)
-- tilbage i spring gendanner kladden fra før springet
local changed = G.withDraft(fromSummary, {showB = false, value = 99})
local undone = G.back(changed)
check("G9 tilbage gendanner kladde", undone.draft.value, 1)
check("G9 tilbage til summary", undone.step, "summary")
-- spring fra fanens oversigt (close): næste = done, tilbage = cancel
local single = G.new(def, {value = 5}, {}, {step = "e", single = true, returnTo = "close"})
local _, singleDone = G.next(single)
check("G10 single close næste = done", singleDone, "done")
local _, singleCancel = G.back(single)
check("G10 single close tilbage = cancel", singleCancel, "cancel")
check("G10 single isLast", G.isLast(single), true)
-- withContext
local ctxGuide = G.withContext(guide, {x = 2})
check("G11 ny context", ctxGuide.context.x, 2)
check("G11 gammel context", guide.context.x, 1)
-- deepCopy / toggleItem
local original = {list = {1, 2}, nested = {a = {b = 1}}}
local copy = G.deepCopy(original)
copy.list[1] = 9
copy.nested.a.b = 7
check("G12 deepCopy liste", original.list[1], 1)
check("G12 deepCopy indlejret", original.nested.a.b, 1)
local list = {"x", "y"}
check("G13 toggle til", table.concat(G.toggleItem(list, "z", true), ","), "x,y,z")
check("G13 ingen dublet", table.concat(G.toggleItem(list, "x", true), ","), "x,y")
check("G13 toggle fra", table.concat(G.toggleItem(list, "x", false), ","), "y")
check("G13 original urørt", #list, 2)
-- tekster
local texts = {greet = "Hej %s", n = "%d stk", title = "Afgrøde", plain = "Ingen"}
local function T(key) return texts[key] or key end
check("G14 label args", G.labelText({key = "greet", args = {"Bo"}}, T), "Hej Bo")
check("G14 label tekst", G.labelText({text = "rå"}, T), "rå")
check("G14 manglende args fejler ikke", G.labelText({key = "n"}, T), "%d stk")
check("G14 prefix", G.labelText({key = "plain", prefix = "Mælk: "}, T), "Mælk: Ingen")
check("G14 nil label", G.labelText(nil, T), "")
check("G15 linje", G.lineText({step = "crop", title = {key = "title"}, parts = {{key = "plain"}, {key = "n", args = {3}}}}, T), "Afgrøde: Ingen, 3 stk")
check("G15 linje uden dele", G.lineText({step = "crop", title = {key = "title"}, parts = {}}, T), "Afgrøde: -")
print(failures == 0 and "\nALLE TESTS OK" or ("\n" .. failures .. " FEJL"))
os.exit(failures == 0 and 0 or 1)