extends SceneTree ## The shared action menus (Actions): what the card, thread, group and spread ## menus offer in Play and Build, the ☰ menu, running an action, and placing ## a new card where the spread was right-clicked. ## Run: godot --headless --path . -s res://tests/test_actions.gd -- --sample var _failures := 0 var main: Main func _init() -> void: if not OS.get_cmdline_user_args().has("--sample"): print("Run with -- --sample so the test can't touch saved data.") quit(2) return main = load("res://ui/main.tscn").instantiate() root.add_child(main) await _frames(5) await _test_lists() await _test_popup_and_run() print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) quit(1 if _failures else 0) func _texts(list: Array) -> Array[String]: var out: Array[String] = [] for a: Dictionary in list: if a.has("text"): out.append(str(a["text"])) return out func _test_lists() -> void: var t := main.data.get_table("loot") var play := _texts(Actions.card(main, t)) _check(play.has("Roll") and not play.has("Delete table"), "Play: a card can be rolled, not deleted: %s" % str(play)) var bg := _texts(Actions.background(main, Vector2.ZERO)) _check(bg.has("Search… Ctrl+F") and bg.has("Shuffle all decks") and not bg.has("New card"), "Play spread menu: %s" % str(bg)) main.set_mode(Main.Mode.BUILD) await _frames() var build := _texts(Actions.card(main, t)) _check(build.has("Table settings…") and build.has("Colour") and build.has("Delete table"), "Build card menu: %s" % str(build)) bg = _texts(Actions.background(main, Vector2.ZERO)) _check(bg.has("New card") and bg.has("Group selected cards Ctrl+G"), "Build spread menu: %s" % str(bg)) var key := "" for e in t.entries: if not e.pipes.is_empty(): key = Roller.pipe_key(t.id, e.id, e.pipes[0]) break var th := _texts(Actions.thread(main, key)) _check(th.has("Go to the end") and th.has("Delete thread") and th.has("Colour"), "thread menu: %s" % str(th)) _check(Actions.thread(main, "nope:x>y").is_empty(), "a missing thread has no menu") func _test_popup_and_run() -> void: var at := Vector2(-900, -700) var pm := Actions.popup_at(main, Actions.background(main, at), Vector2(50, 50)) await _frames() var sub: PopupMenu = null for c in pm.get_children(): if c is PopupMenu: sub = c _check(sub != null and sub.item_count == MainEditing.ADD_KINDS.size(), "New card ▸ lists every kind") var before := main.data.tables.size() _check(Actions.press(sub, "Roll table"), "pressing an item runs it") await _frames() var added: RollTable = main.data.tables[main.data.tables.size() - 1] _check(main.data.tables.size() == before + 1 and added.position.distance_to(at) < 2.0, "the new card goes where the spread was clicked: %s" % str(added.position)) pm.hide() var t := main.data.get_table("loot") Actions.set_card_colour(main, t, "#2f8a4f") _check(t.color == "#2f8a4f", "Colour ▸ sets the card's colour") main.undo_redo.undo() _check(main.data.get_table("loot").color == "", "and it's undoable") var bar_pm := main.top_bar.actions_menu.get_popup() bar_pm.about_to_popup.emit() _check(bar_pm.item_count > 4, "the ☰ menu fills with the spread's actions") func _frames(n: int = 2) -> void: for i in n: await process_frame func _check(ok: bool, what: String) -> void: if not ok: _failures += 1 print("FAIL ", what)