TableTower/tests/test_actions.gd

99 lines
4.1 KiB
GDScript3
Raw Normal View History

extends TestBase
## 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
func _init() -> void:
if not await start_main():
return
await _test_lists()
await _test_popup_and_run()
await _test_delete_key()
finish()
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 the selection 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
# The first submenu is New card ▸ (Sound ▸ comes later).
for c in pm.get_children():
if c is PopupMenu and sub == null:
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")
## The Delete key: asks first (by default), deletes the selection as one undo
## step, and skips the question when "Ask before deleting" is off.
func _test_delete_key() -> void:
main.set_mode(Main.Mode.BUILD)
await _frames()
main.clear_selection()
main.editing.toggle_selected("loot")
main.editing.toggle_selected("names")
var found := DeleteKey.target(main)
_eq(str(found.get("what", "")), "2 cards", "Delete names what it would delete")
DeleteKey.press(main)
await _frames()
var asked := main.get_children().filter(func(n: Node) -> bool: return n is ModalCard)
_check(not asked.is_empty() and main.data.get_table("loot") != null, "by default it asks first and deletes nothing yet")
for dialog: ModalCard in asked:
dialog.close()
DeleteKey.set_asks(main, false)
DeleteKey.press(main)
await _frames()
_check(main.data.get_table("loot") == null and main.data.get_table("names") == null, "with asking off it deletes both cards")
main.undo_redo.undo()
await _frames()
_check(main.data.get_table("loot") != null and main.data.get_table("names") != null, "one undo brings both back")
DeleteKey.set_asks(main, true)
main.clear_selection()
main.set_mode(Main.Mode.PLAY)